Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort table results by ascending file size #27

Merged
merged 6 commits into from
Jan 18, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,31 @@ pub fn print(duplicates: DashMap<String, Vec<File>>, 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| fs::metadata(&ff.path).ok())
beeb marked this conversation as resolved.
Show resolved Hide resolved
.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
.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, 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();
}