diff --git a/smartshreds/src/main.rs b/smartshreds/src/main.rs index d5a11a1..f291439 100644 --- a/smartshreds/src/main.rs +++ b/smartshreds/src/main.rs @@ -1,7 +1,7 @@ mod shred; use std::path::PathBuf; -use shred::{dir_search, error::SmartShredsError, hashing}; +use shred::{debug::display_duplicate_files, dir_search, error::SmartShredsError, hashing}; use structopt::StructOpt; #[derive(Debug, StructOpt)] @@ -24,7 +24,7 @@ fn main() -> Result<(), SmartShredsError> { let args: CommandLine = CommandLine::from_args(); let directory_path = &args.directory_path; let dup_files = dir_search::search_files_with_similar_names_in_dir(directory_path)?; - let _ = dir_search::display_duplicate_files(&dup_files); + let _ = display_duplicate_files(&dup_files); let hashes = hashing::hash_duplicate_file(&dup_files)?; println!("{:?}", hashes); Ok(()) diff --git a/smartshreds/src/shred/debug.rs b/smartshreds/src/shred/debug.rs new file mode 100644 index 0000000..07706cb --- /dev/null +++ b/smartshreds/src/shred/debug.rs @@ -0,0 +1,54 @@ +use std::io::{StdoutLock, Write}; + +use termion::{color, input::MouseTerminal, style}; + +use super::{dir_search::DuplicateFile, error::SmartShredsError}; + +pub fn display_duplicate_files(files: &Vec) -> Result<(), SmartShredsError> { + let stdout = MouseTerminal::from(std::io::stdout()); + let mut stdout = stdout.lock(); + if files.is_empty() { + print_no_duplicates(&mut stdout)?; + } else { + print_duplicates(&mut stdout, &files)?; + } + Ok(()) +} + +fn print_no_duplicates(stdout: &mut StdoutLock) -> Result<(), SmartShredsError> { + writeln!( + stdout, + "{}{}No duplicate files found in the directory.{}", + style::Bold, + color::Fg(color::Green), + style::Reset + )?; + Ok(()) +} + +fn print_duplicates( + stdout: &mut StdoutLock, + files: &Vec, +) -> Result<(), SmartShredsError> { + writeln!( + stdout, + "{}{}Duplicate files found in the directory:{}", + style::Bold, + color::Fg(color::Yellow), + style::Reset + )?; + + for (index, file) in files.iter().enumerate() { + writeln!( + stdout, + "{}{}{} - {}{}", + style::Bold, + color::Fg(color::Red), + index + 1, + file, + style::Reset + )?; + } + + Ok(()) +} diff --git a/smartshreds/src/shred/dir_search.rs b/smartshreds/src/shred/dir_search.rs index a1af2c4..698ee16 100644 --- a/smartshreds/src/shred/dir_search.rs +++ b/smartshreds/src/shred/dir_search.rs @@ -2,10 +2,7 @@ use regex::Regex; use std::collections::HashMap; use std::ffi::OsStr; use std::fs::read_dir; -use std::io::{StdoutLock, Write}; use std::path::PathBuf; -use termion::input::MouseTerminal; -use termion::{color, style}; use super::error::SmartShredsError; @@ -78,51 +75,3 @@ pub fn search_files_with_similar_names_in_dir( Ok(duplicate_files) } -fn print_no_duplicates(stdout: &mut StdoutLock) -> Result<(), SmartShredsError> { - writeln!( - stdout, - "{}{}No duplicate files found in the directory.{}", - style::Bold, - color::Fg(color::Green), - style::Reset - )?; - Ok(()) -} - -fn print_duplicates( - stdout: &mut StdoutLock, - files: &Vec, -) -> Result<(), SmartShredsError> { - writeln!( - stdout, - "{}{}Duplicate files found in the directory:{}", - style::Bold, - color::Fg(color::Yellow), - style::Reset - )?; - - for (index, file) in files.iter().enumerate() { - writeln!( - stdout, - "{}{}{} - {}{}", - style::Bold, - color::Fg(color::Red), - index + 1, - file, - style::Reset - )?; - } - - Ok(()) -} - -pub fn display_duplicate_files(files: &Vec) -> Result<(), SmartShredsError> { - let stdout = MouseTerminal::from(std::io::stdout()); - let mut stdout = stdout.lock(); - if files.is_empty() { - print_no_duplicates(&mut stdout)?; - } else { - print_duplicates(&mut stdout, &files)?; - } - Ok(()) -} diff --git a/smartshreds/src/shred/mod.rs b/smartshreds/src/shred/mod.rs index b0c3c72..a773529 100644 --- a/smartshreds/src/shred/mod.rs +++ b/smartshreds/src/shred/mod.rs @@ -1,3 +1,4 @@ pub mod error; pub mod dir_search; -pub mod hashing; \ No newline at end of file +pub mod hashing; +pub mod debug; \ No newline at end of file