-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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::<usize>() { | ||
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 <[email protected]>".italic().dimmed() | ||
)) | ||
// TODO update version | ||
.version("1.2.2") | ||
.version("1.2.3") | ||
.author("Leann Phydon <[email protected]>") | ||
.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<PathBuf> { | ||
|
@@ -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); | ||
} | ||
} |