table printing added

This commit is contained in:
sreedev
2023-01-09 22:15:44 -05:00
parent 96fe667d3f
commit a0083cb571
3 changed files with 154 additions and 21 deletions

View File

@@ -8,6 +8,7 @@ use humansize::{format_size, DECIMAL};
use crate::database::File;
use crate::params::Params;
use prettytable::{row, Cell, Row, Table};
fn format_path(path: &str, opts: &Params) -> Result<String> {
let display_path = path.replace(&opts.get_directory()?, "");
@@ -37,20 +38,10 @@ fn modified_time(path: &String) -> Result<String> {
Ok(modified_time.format("%Y-%m-%d %H:%M:%S").to_string())
}
fn print_divider() {
println!("-------------------+-------------------------------------+------------------+----------------------------------+");
}
pub fn print(duplicates: Vec<File>, opts: &Params) {
print_divider();
println!(
"| {0: <16} | {1: <35} | {2: <16} | {3: <32} |",
"hash", "filename", "size", "updated_at"
);
print_divider();
let mut output_table = Table::new();
let mut dup_index: HashMap<String, Vec<File>> = HashMap::new();
output_table.add_row(row!["hash", "duplicates"]);
duplicates.into_iter().for_each(|file| {
dup_index
.entry(file.hash.clone())
@@ -58,16 +49,18 @@ pub fn print(duplicates: Vec<File>, opts: &Params) {
.or_insert_with(|| vec![file]);
});
dup_index.iter().for_each(|(_, group)| {
dup_index.iter().for_each(|(hash, group)| {
let mut inner_table = Table::new();
inner_table.add_row(row!["filename", "size", "updated_at"]);
group.iter().for_each(|file| {
println!(
"| {0: <16} | {1: <35} | {2: <16} | {3: <32} |",
file.hash.red(),
format_path(&file.path, opts).unwrap_or_default().yellow(),
file_size(&file.path).unwrap_or_default().blue(),
modified_time(&file.path).unwrap_or_default().blue()
);
inner_table.add_row(row![
format_path(&file.path, opts).unwrap_or_default().blue(),
file_size(&file.path).unwrap_or_default().red(),
modified_time(&file.path).unwrap_or_default().yellow()
]);
});
print_divider();
output_table.add_row(row![hash, inner_table]);
});
output_table.printstd();
}