table display improvements

This commit is contained in:
sreedev
2023-01-09 22:40:37 -05:00
parent a0083cb571
commit 6e412fc59c

View File

@@ -8,7 +8,7 @@ use humansize::{format_size, DECIMAL};
use crate::database::File;
use crate::params::Params;
use prettytable::{row, Cell, Row, Table};
use prettytable::{row, Cell, Row, format, Table};
fn format_path(path: &str, opts: &Params) -> Result<String> {
let display_path = path.replace(&opts.get_directory()?, "");
@@ -22,12 +22,12 @@ fn format_path(path: &str, opts: &Params) -> Result<String> {
display_path
};
Ok(format!("...{}", display_range))
Ok(format!("...{:<32}", display_range))
}
fn file_size(path: &String) -> Result<String> {
let mdata = fs::metadata(path)?;
let formatted_size = format_size(mdata.len(), DECIMAL);
let formatted_size = format!("{:>12}", format_size(mdata.len(), DECIMAL));
Ok(formatted_size)
}
@@ -38,20 +38,28 @@ fn modified_time(path: &String) -> Result<String> {
Ok(modified_time.format("%Y-%m-%d %H:%M:%S").to_string())
}
pub fn print(duplicates: Vec<File>, opts: &Params) {
let mut output_table = Table::new();
let mut dup_index: HashMap<String, Vec<File>> = HashMap::new();
output_table.add_row(row!["hash", "duplicates"]);
fn group_duplicates(duplicates: Vec<File>) -> HashMap<String, Vec<File>> {
let mut duplicate_mapper: HashMap<String, Vec<File>> = HashMap::new();
duplicates.into_iter().for_each(|file| {
dup_index
duplicate_mapper
.entry(file.hash.clone())
.and_modify(|value| value.push(file.clone()))
.or_insert_with(|| vec![file]);
});
dup_index.iter().for_each(|(hash, group)| {
duplicate_mapper
}
pub fn print(duplicates: Vec<File>, opts: &Params) {
let mut output_table = Table::new();
let grouped_duplicates: HashMap<String, Vec<File>> = group_duplicates(duplicates);
output_table.set_titles(row!["hash", "duplicates"]);
grouped_duplicates.iter().for_each(|(hash, group)| {
let mut inner_table = Table::new();
inner_table.add_row(row!["filename", "size", "updated_at"]);
// inner_table.set_format(inner_table_format);
inner_table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
//inner_table.set_titles(row!["filename", "size", "updated_at"]);
group.iter().for_each(|file| {
inner_table.add_row(row![
format_path(&file.path, opts).unwrap_or_default().blue(),
@@ -59,7 +67,7 @@ pub fn print(duplicates: Vec<File>, opts: &Params) {
modified_time(&file.path).unwrap_or_default().yellow()
]);
});
output_table.add_row(row![hash, inner_table]);
output_table.add_row(row![hash.green(), inner_table]);
});
output_table.printstd();