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