diff --git a/src/cmdargs.rs b/src/cmdargs.rs new file mode 100644 index 0000000..25b77db --- /dev/null +++ b/src/cmdargs.rs @@ -0,0 +1,65 @@ +use std::path::PathBuf; +use std::process::exit; + +use clap::Parser; +use num_cpus; + +use crate::output as op; + +#[derive(Parser)] +#[command(name = "fcnt")] +#[command(version = "0.2.1")] +#[command(about = "Count the total number of files in given directories.")] +pub struct CmdArgParser { + /// the directories (default: ./) + pub directories: Vec, + + /// count all regular and hidden files. + #[arg(short = 'a')] + pub all_files: bool, + + /// count the total size of files. + #[arg(short = 's')] + pub with_size: bool, + + /// non-recursive mode (files in sub-directories will be ignored). + #[arg(short = 'R')] + pub non_recursive: bool, + + /// the number of threads for traversal (invalid in `non_recursive` mode). + #[arg(short = 't')] + pub n_thread: Option, +} + +impl CmdArgParser { + pub fn get_threads_num(&self) -> usize { + match self.n_thread { + Some(num) => return num, + None => { + let n_cpu = num_cpus::get(); + return if n_cpu >= 4 { n_cpu } else { 4 }; + } + } + } + + pub fn get_directories(&self) -> Vec { + let mut directories: Vec = vec![]; + if self.directories.is_empty() { + directories.push(PathBuf::from("./")); + } else { + for dir in self.directories.iter().map(|p| PathBuf::from(p)) { + if dir.is_dir() { + directories.push(dir); + } else { + let msg = format!("{:?} is not a directory.", dir); + println!("{}", op::warn(&msg)); + } + } + if directories.is_empty() { + println!("{}", op::err(&"fcnt: no directory found.")); + exit(1); + } + } + return directories; + } +} diff --git a/src/main.rs b/src/main.rs index e139c74..ecef579 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,14 @@ +mod cmdargs; mod output; mod walker; -use std::path::PathBuf; -use std::process::exit; - use clap::Parser; -use num_cpus::get as cpu_count; - +use cmdargs::CmdArgParser; use walker::Counter; fn main() { // parse cmd-line args and get directories - let args = CmdLineArgs::parse(); + let args = CmdArgParser::parse(); // walk all files let directories = args.get_directories(); @@ -35,61 +32,3 @@ fn main() { Counter::output(&counters, args.with_size); } - -#[derive(Parser)] -#[command(name = "fcnt")] -#[command(version = "0.2.1")] -#[command(about = "Count the total number of files in given directories.")] -struct CmdLineArgs { - /// the directories (default: ./) - directories: Vec, - - /// count all regular and hidden files. - #[arg(short = 'a')] - all_files: bool, - - /// count the total size of files. - #[arg(short = 's')] - with_size: bool, - - /// non-recursive mode (files in sub-directories will be ignored). - #[arg(short = 'R')] - non_recursive: bool, - - /// the number of threads for traversal (invalid in `non_recursive` mode). - #[arg(short = 't')] - n_thread: Option, -} - -impl CmdLineArgs { - pub fn get_threads_num(&self) -> usize { - match self.n_thread { - Some(num) => num, - None => { - let n_cpu = cpu_count(); - return if n_cpu >= 4 { n_cpu } else { 4 }; - } - } - } - - pub fn get_directories(&self) -> Vec { - let mut directories: Vec = vec![]; - if self.directories.is_empty() { - directories.push(PathBuf::from("./")); - } else { - for dir in self.directories.iter().map(|p| PathBuf::from(p)) { - if dir.is_dir() { - directories.push(dir); - } else { - let msg = format!("{:?} is not a directory.", dir); - println!("{}", output::warn(&msg)); - } - } - if directories.is_empty() { - println!("{}", output::err(&"fcnt: no directory found.")); - exit(1); - } - } - return directories; - } -}