From 78d9a6fb94c53033516968027a65f709ae87d187 Mon Sep 17 00:00:00 2001 From: Phydon Date: Tue, 7 May 2024 22:05:30 +0200 Subject: [PATCH] added sum-flag --- src/main.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 9314532..cafa00c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,7 @@ fn main() { let chars_flag = matches.get_flag("chars"); let lines_flag = matches.get_flag("lines"); let show_errors_flag = matches.get_flag("show-errors"); + let sum_flag = matches.get_flag("sum"); let word_flag = matches.get_flag("words"); if let Some(_) = matches.subcommand_matches("log") { @@ -129,6 +130,8 @@ fn main() { count += count_chars(content); } else if bytes_flag { count += count_bytes(content); + } else if sum_flag { + count += sum(content); } else { // count words by default count += count_words(content); @@ -170,6 +173,16 @@ fn count_bytes(content: String) -> usize { content.par_bytes().count() } +fn sum(content: String) -> usize { + content + .par_split_whitespace() + .filter_map(|x| match x.parse::() { + Ok(n) => Some(n), + _ => None, + }) + .sum() +} + fn read_stdin() -> String { let mut input = io::stdin() .lock() @@ -199,11 +212,15 @@ fn countx() -> Command { "Leann Phydon ".italic().dimmed() )) // TODO update version - .version("1.2.2") + .version("1.2.3") .author("Leann Phydon ") .arg( Arg::new("arg") .help("The filepath to work with") + .long_help(format!( + "{}\n{}", + "The filepath to work with", "Reads stdin if left empty" + )) .action(ArgAction::Set) .num_args(1) .value_name("PATH"), @@ -240,6 +257,13 @@ fn countx() -> Command { .help("Show errors (ignores errors by default)") .action(ArgAction::SetTrue), ) + .arg( + Arg::new("sum") + .short('s') + .long("sum") + .help("Sum up all integers") + .action(ArgAction::SetTrue), + ) .arg( Arg::new("words") .short('w') @@ -289,6 +313,14 @@ $ echo 'Some pipe input' | cx 3 "### ); + + println!("\n{}\n----------", "Example 3".bold()); + println!( + r###" +$ echo 'This 42 is 1337 a t35t 666' | cx --sum +2045 + "### + ); } fn check_create_config_dir() -> io::Result { @@ -365,6 +397,14 @@ mod tests { assert_eq!(result, expected); } + #[test] + fn sum_test() { + let input = "This 42 is 1337 a t35t 666".to_string(); + let result = sum(input); + let expected = 2045; + assert_eq!(result, expected); + } + #[test] fn count_words_empty_test() { let input = "".to_string(); @@ -396,4 +436,12 @@ mod tests { let expected = 0; assert_eq!(result, expected); } + + #[test] + fn sum_empty_test() { + let input = "".to_string(); + let result = sum(input); + let expected = 0; + assert_eq!(result, expected); + } }