summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2023-03-07 19:04:00 +0400
committerJoursoir <chat@joursoir.net>2023-03-13 01:30:17 +0400
commit96922e7d1fa6a02a4d8373828345d00326d2b799 (patch)
treed3da199b4280a350c2197994925071d02ebf0297
parent0cc3793aefa03222624b297230650d1391edd645 (diff)
downloadtrgrep-96922e7d1fa6a02a4d8373828345d00326d2b799.tar.gz
trgrep-96922e7d1fa6a02a4d8373828345d00326d2b799.tar.bz2
trgrep-96922e7d1fa6a02a4d8373828345d00326d2b799.zip
add option to count of the number of lines that match a pattern
-rw-r--r--src/main.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index b566f68..2d1de95 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -28,6 +28,10 @@ struct Config {
#[arg(short = 'v', long)]
invert_match: bool,
+ /// Displays only a count of the number of lines that match the search string
+ #[arg(short, long)]
+ count: bool,
+
/// Prefixes each matching line with the line number
#[arg(short = 'n', long)]
line_number: bool,
@@ -65,6 +69,7 @@ fn main() {
fn run(config: Config) -> Result<(), Box<dyn Error>> {
'outer: for file in config.files {
+ let mut count: usize = 0;
// On-Stack Dynamic Dispatch
let (mut stdin_read, mut file_read);
@@ -87,6 +92,9 @@ fn run(config: Config) -> Result<(), Box<dyn Error>> {
if config.files_with_matches {
println!("{file}");
continue 'outer;
+ } else if config.count {
+ count += 1;
+ continue;
}
let formatted_output = if !config.no_filename && config.line_number {
@@ -101,6 +109,10 @@ fn run(config: Config) -> Result<(), Box<dyn Error>> {
println!("{}", formatted_output);
}
+
+ if config.count {
+ println!("{}:{}", file, count);
+ }
}
Ok(())