printing sizes added

This commit is contained in:
sreedev
2022-12-27 21:05:41 -05:00
parent 6da2bd401e
commit f8242ca4ac
8 changed files with 372 additions and 22 deletions

View File

@@ -49,20 +49,3 @@ pub fn duplicate_hashes(connection: &sqlite::Connection) -> Result<Vec<File>> {
Ok(result)
}
pub fn lookup(hash: &str) -> Result<Vec<File>> {
let connection = sqlite::open(":memory:")?;
let result_set = connection
.prepare("SELECT * FROM files WHERE hash = ?")?
.into_iter()
.bind((1, hash))?
.map(|row| row.unwrap())
.map(|row| {
let path = row.read::<&str, _>("file_identifier").to_owned();
let hash = row.read::<&str, _>("hash").to_owned();
File { path, hash }
})
.collect::<Vec<File>>();
Ok(result_set)
}

View File

@@ -1,6 +1,7 @@
mod database;
mod scanner;
mod cli;
mod output;
use clap::Parser;
use anyhow::Result;
@@ -12,7 +13,7 @@ async fn main() -> Result<()> {
database::setup(&connection)?;
let duplicates = scanner::duplicates(cli::App::parse(), &connection)?;
dbg!(duplicates);
output::print(duplicates);
Ok(())
}

57
src/output.rs Normal file
View File

@@ -0,0 +1,57 @@
use crate::database::File;
use itertools::Itertools;
use colored::Colorize;
use std::fs;
use chrono::offset::Utc;
use chrono::DateTime;
use humansize::{format_size, DECIMAL};
fn format_path(path: &String) -> String {
let stringlen = path.len();
format!("...{}", String::from(&path[(stringlen - 32)..]))
}
fn file_size(path: &String) -> String {
let mdata = fs::metadata(path).unwrap();
let formatted_size = format_size(mdata.len(), DECIMAL);
format!("{}", formatted_size)
}
fn modified_time(path: &String) -> String {
let mdata = fs::metadata(path).unwrap();
let modified_time: DateTime<Utc> = mdata.modified().unwrap().into();
modified_time.format("%Y-%m-%d %H:%M:%S").to_string()
}
fn print_divider() {
println!("-------------------+-------------------------------------+------------------+----------------------------------+");
}
pub fn print(duplicates: Vec<File>) {
print_divider();
println!(
"| {0: <16} | {1: <35} | {2: <16} | {3: <32} |",
"hash", "filename", "size", "updated_at"
);
print_divider();
duplicates
.into_iter()
.group_by(|record| record.hash.clone())
.into_iter()
.for_each(|(_, group)| {
group
.into_iter()
.for_each(|file| {
println!(
"| {0: <16} | {1: <35} | {2: <16} | {3: <32} |",
&file.hash[0..16].red(),
format_path(&file.path).yellow(),
file_size(&file.path).blue(),
modified_time(&file.path).blue()
);
});
print_divider();
});
}

View File

@@ -4,7 +4,7 @@ use crate::{cli::App, database::File};
use crate::database;
use anyhow::Result;
use glob::glob;
use std::{fs, io};
use std::fs;
pub fn duplicates(app_opts: App, connection: &sqlite::Connection) -> Result<Vec<File>> {
index_files(scan(app_opts)?, connection);