| skipped 7 lines |
8 | 8 | | |
9 | 9 | | use crate::database::File; |
10 | 10 | | use crate::params::Params; |
11 | | - | use prettytable::{row, Cell, Row, Table}; |
| 11 | + | use prettytable::{row, Cell, Row, format, Table}; |
12 | 12 | | |
13 | 13 | | fn format_path(path: &str, opts: &Params) -> Result<String> { |
14 | 14 | | let display_path = path.replace(&opts.get_directory()?, ""); |
| skipped 7 lines |
22 | 22 | | display_path |
23 | 23 | | }; |
24 | 24 | | |
25 | | - | Ok(format!("...{}", display_range)) |
| 25 | + | Ok(format!("...{:<32}", display_range)) |
26 | 26 | | } |
27 | 27 | | |
28 | 28 | | fn file_size(path: &String) -> Result<String> { |
29 | 29 | | let mdata = fs::metadata(path)?; |
30 | | - | let formatted_size = format_size(mdata.len(), DECIMAL); |
| 30 | + | let formatted_size = format!("{:>12}", format_size(mdata.len(), DECIMAL)); |
31 | 31 | | Ok(formatted_size) |
32 | 32 | | } |
33 | 33 | | |
| skipped 4 lines |
38 | 38 | | Ok(modified_time.format("%Y-%m-%d %H:%M:%S").to_string()) |
39 | 39 | | } |
40 | 40 | | |
41 | | - | pub fn print(duplicates: Vec<File>, opts: &Params) { |
42 | | - | let mut output_table = Table::new(); |
43 | | - | let mut dup_index: HashMap<String, Vec<File>> = HashMap::new(); |
44 | | - | output_table.add_row(row!["hash", "duplicates"]); |
| 41 | + | fn group_duplicates(duplicates: Vec<File>) -> HashMap<String, Vec<File>> { |
| 42 | + | let mut duplicate_mapper: HashMap<String, Vec<File>> = HashMap::new(); |
45 | 43 | | duplicates.into_iter().for_each(|file| { |
46 | | - | dup_index |
| 44 | + | duplicate_mapper |
47 | 45 | | .entry(file.hash.clone()) |
48 | 46 | | .and_modify(|value| value.push(file.clone())) |
49 | 47 | | .or_insert_with(|| vec![file]); |
50 | 48 | | }); |
51 | 49 | | |
52 | | - | dup_index.iter().for_each(|(hash, group)| { |
| 50 | + | duplicate_mapper |
| 51 | + | } |
| 52 | + | |
| 53 | + | pub fn print(duplicates: Vec<File>, opts: &Params) { |
| 54 | + | let mut output_table = Table::new(); |
| 55 | + | let grouped_duplicates: HashMap<String, Vec<File>> = group_duplicates(duplicates); |
| 56 | + | |
| 57 | + | output_table.set_titles(row!["hash", "duplicates"]); |
| 58 | + | grouped_duplicates.iter().for_each(|(hash, group)| { |
53 | 59 | | let mut inner_table = Table::new(); |
54 | | - | inner_table.add_row(row!["filename", "size", "updated_at"]); |
| 60 | + | // inner_table.set_format(inner_table_format); |
| 61 | + | inner_table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR); |
| 62 | + | //inner_table.set_titles(row!["filename", "size", "updated_at"]); |
55 | 63 | | group.iter().for_each(|file| { |
56 | 64 | | inner_table.add_row(row![ |
57 | 65 | | format_path(&file.path, opts).unwrap_or_default().blue(), |
| skipped 1 lines |
59 | 67 | | modified_time(&file.path).unwrap_or_default().yellow() |
60 | 68 | | ]); |
61 | 69 | | }); |
62 | | - | output_table.add_row(row![hash, inner_table]); |
| 70 | + | output_table.add_row(row![hash.green(), inner_table]); |
63 | 71 | | }); |
64 | 72 | | |
65 | 73 | | output_table.printstd(); |
| skipped 2 lines |