From df3bea3d4f73b7e22907994351705fa95f86939e Mon Sep 17 00:00:00 2001 From: Valentin Bersier Date: Wed, 18 Jan 2023 07:59:51 +0100 Subject: [PATCH 1/5] feat: display results in ascending size order --- src/output.rs | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/output.rs b/src/output.rs index 4767d6f..2f9e1b6 100644 --- a/src/output.rs +++ b/src/output.rs @@ -145,18 +145,31 @@ pub fn print(duplicates: DashMap>, opts: &Params) { let mut output_table = Table::new(); output_table.set_titles(row!["hash", "duplicates"]); - duplicates.into_iter().for_each(|(hash, group)| { - let mut inner_table = Table::new(); - inner_table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR); - group.iter().for_each(|file| { - 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() - ]); + duplicates + .into_iter() + .map(|f| { + // we extract the file size of the first file in the group + let size = + f.1.first() + .and_then(|ff| Some(&ff.path)) + .and_then(|p| fs::metadata(p).ok()) + .and_then(|m| Some(m.len())); + (f.0, f.1, size) + }) + .sorted_unstable_by_key(|f| f.2) // sort by ascending size + .for_each(|(hash, group, size)| { + let mut inner_table = Table::new(); + inner_table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR); + group.iter().for_each(|file| { + inner_table.add_row(row![ + format_path(&file.path, opts).unwrap_or_default().blue(), + // since all files should logically have the same size, + // we can print the pre-computed size of the first element in the group + format!("{:>12}", format_size(size.unwrap_or_default(), DECIMAL)).red(), + modified_time(&file.path).unwrap_or_default().yellow() + ]); + }); + output_table.add_row(row![hash.green(), inner_table]); }); - output_table.add_row(row![hash.green(), inner_table]); - }); - output_table.printstd(); } From 8c76c6a3c851ed6fe1cf245a8d0e3af5c088f048 Mon Sep 17 00:00:00 2001 From: Valentin Bersier Date: Wed, 18 Jan 2023 08:00:53 +0100 Subject: [PATCH 2/5] refactor: simplify iterator to get size --- src/output.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/output.rs b/src/output.rs index 2f9e1b6..2d635e8 100644 --- a/src/output.rs +++ b/src/output.rs @@ -151,8 +151,7 @@ pub fn print(duplicates: DashMap>, opts: &Params) { // we extract the file size of the first file in the group let size = f.1.first() - .and_then(|ff| Some(&ff.path)) - .and_then(|p| fs::metadata(p).ok()) + .and_then(|ff| fs::metadata(&ff.path).ok()) .and_then(|m| Some(m.len())); (f.0, f.1, size) }) From e61d1f1475d00525bd743e48a9f5ba0b6b698522 Mon Sep 17 00:00:00 2001 From: Valentin Bersier Date: Wed, 18 Jan 2023 08:07:51 +0100 Subject: [PATCH 3/5] perf: unwrap the size once --- src/output.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/output.rs b/src/output.rs index 2d635e8..3bd0e8a 100644 --- a/src/output.rs +++ b/src/output.rs @@ -152,7 +152,8 @@ pub fn print(duplicates: DashMap>, opts: &Params) { let size = f.1.first() .and_then(|ff| fs::metadata(&ff.path).ok()) - .and_then(|m| Some(m.len())); + .and_then(|m| Some(m.len())) + .unwrap_or_default(); (f.0, f.1, size) }) .sorted_unstable_by_key(|f| f.2) // sort by ascending size @@ -164,7 +165,7 @@ pub fn print(duplicates: DashMap>, opts: &Params) { format_path(&file.path, opts).unwrap_or_default().blue(), // since all files should logically have the same size, // we can print the pre-computed size of the first element in the group - format!("{:>12}", format_size(size.unwrap_or_default(), DECIMAL)).red(), + format!("{:>12}", format_size(size, DECIMAL)).red(), modified_time(&file.path).unwrap_or_default().yellow() ]); }); From dfb73ceb5c92b994d7374c427c62bd24925a2455 Mon Sep 17 00:00:00 2001 From: Valentin Bersier Date: Wed, 18 Jan 2023 15:33:23 +0100 Subject: [PATCH 4/5] feat: sort interactive results by descending size --- src/output.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/output.rs b/src/output.rs index 3ed023f..0b33787 100644 --- a/src/output.rs +++ b/src/output.rs @@ -128,6 +128,9 @@ pub fn interactive(duplicates: DashMap>, opts: &Params) { duplicates .clone() .into_iter() + .sorted_unstable_by_key(|f| { + -(f.1.first().and_then(|ff| ff.size).unwrap_or_default() as i64) + }) .enumerate() .for_each(|(gindex, (_, group))| { let mut itable = Table::new(); From d162ca98ef09313f1c5d884c3481b5ac20e23759 Mon Sep 17 00:00:00 2001 From: Valentin Bersier Date: Wed, 18 Jan 2023 15:34:58 +0100 Subject: [PATCH 5/5] docs: comment --- src/output.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/output.rs b/src/output.rs index 0b33787..188a51e 100644 --- a/src/output.rs +++ b/src/output.rs @@ -130,7 +130,7 @@ pub fn interactive(duplicates: DashMap>, opts: &Params) { .into_iter() .sorted_unstable_by_key(|f| { -(f.1.first().and_then(|ff| ff.size).unwrap_or_default() as i64) - }) + }) // sort by descending file size in interactive mode .enumerate() .for_each(|(gindex, (_, group))| { let mut itable = Table::new();