mirror of
https://github.com/sreedevk/deduplicator.git
synced 2026-08-31 12:35:34 +00:00
refactor: output.rs
This commit is contained in:
@@ -1,35 +1,40 @@
|
||||
use crate::database::File;
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
use anyhow::Result;
|
||||
use chrono::offset::Utc;
|
||||
use chrono::DateTime;
|
||||
use colored::Colorize;
|
||||
use humansize::{format_size, DECIMAL};
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
use crate::database::File;
|
||||
use crate::params::Params;
|
||||
|
||||
fn format_path(path: &String, opts: &Params) -> String {
|
||||
let display_path = path.replace(&opts.get_directory().unwrap(), "");
|
||||
fn format_path(path: &str, opts: &Params) -> Result<String> {
|
||||
let display_path = path.replace(&opts.get_directory()?, "");
|
||||
let text_vec = display_path.chars().collect::<Vec<_>>();
|
||||
|
||||
let display_range = if text_vec.len() > 32 {
|
||||
text_vec[(display_path.len() - 32)..].into_iter().collect::<String>()
|
||||
text_vec[(display_path.len() - 32)..]
|
||||
.iter()
|
||||
.collect::<String>()
|
||||
} else {
|
||||
display_path
|
||||
};
|
||||
|
||||
format!("...{}", display_range)
|
||||
|
||||
Ok(format!("...{}", display_range))
|
||||
}
|
||||
|
||||
fn file_size(path: &String) -> String {
|
||||
let mdata = fs::metadata(path).unwrap();
|
||||
fn file_size(path: &String) -> Result<String> {
|
||||
let mdata = fs::metadata(path)?;
|
||||
let formatted_size = format_size(mdata.len(), DECIMAL);
|
||||
format!("{}", formatted_size)
|
||||
Ok(formatted_size)
|
||||
}
|
||||
|
||||
fn modified_time(path: &String) -> String {
|
||||
let mdata = fs::metadata(path).unwrap();
|
||||
let modified_time: DateTime<Utc> = mdata.modified().unwrap().into();
|
||||
fn modified_time(path: &String) -> Result<String> {
|
||||
let mdata = fs::metadata(path)?;
|
||||
let modified_time: DateTime<Utc> = mdata.modified()?.into();
|
||||
|
||||
modified_time.format("%Y-%m-%d %H:%M:%S").to_string()
|
||||
Ok(modified_time.format("%Y-%m-%d %H:%M:%S").to_string())
|
||||
}
|
||||
|
||||
fn print_divider() {
|
||||
@@ -50,17 +55,26 @@ pub fn print(duplicates: Vec<File>, opts: &Params) {
|
||||
dup_index
|
||||
.entry(file.hash.clone())
|
||||
.and_modify(|value| value.push(file.clone()))
|
||||
.or_insert(vec![file]);
|
||||
.or_insert_with(|| vec![file]);
|
||||
});
|
||||
|
||||
dup_index.into_iter().for_each(|(_, group)| {
|
||||
group.into_iter().for_each(|file| {
|
||||
let Ok(path) = format_path(&file.path, opts) else {
|
||||
return;
|
||||
};
|
||||
let Ok(file_size) = file_size(&file.path) else {
|
||||
return;
|
||||
};
|
||||
let Ok(modified_time) = modified_time(&file.path) else {
|
||||
return;
|
||||
};
|
||||
println!(
|
||||
"| {0: <16} | {1: <35} | {2: <16} | {3: <32} |",
|
||||
file.hash.red(),
|
||||
format_path(&file.path, opts).yellow(),
|
||||
file_size(&file.path).blue(),
|
||||
modified_time(&file.path).blue()
|
||||
path.yellow(),
|
||||
file_size.blue(),
|
||||
modified_time.blue()
|
||||
);
|
||||
});
|
||||
print_divider();
|
||||
|
||||
Reference in New Issue
Block a user