mirror of
https://github.com/sreedevk/deduplicator.git
synced 2026-09-01 13:06:43 +00:00
table printing added
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user