From 7b612f0f1854eb164b658547ac57be6ab9dad338 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Sat, 4 Mar 2023 00:45:33 +0400 Subject: add option to prefix lines with the line number --- src/main.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index e17f151..bc8adc1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,10 @@ struct Config { #[arg(short = 'v', long)] invert_match: bool, + /// Prefixes each matching line with the line number + #[arg(short = 'n', long)] + line_number: bool, + /// Matches only whole words #[arg(short, long)] word_regexp: bool, @@ -69,15 +73,19 @@ fn run(config: Config) -> Result<(), Box> { &mut file_read }; - for line in reader.lines().map(|l| l.unwrap()) { + for (idx, line) in reader.lines().map(|l| l.unwrap()).enumerate() { let match_flag = trgrep::contains_pattern(&line, &config.pattern, config.ignore_case, config.word_regexp); let match_flag = if config.invert_match { !match_flag } else { match_flag }; if !match_flag { continue; } - let formatted_output = if !config.no_filename { + let formatted_output = if !config.no_filename && config.line_number { + format!("{}:{}:{}", file, idx + 1, line) + } else if !config.no_filename { format!("{}:{}", file, line) + } else if config.line_number { + format!("{}:{}", idx + 1, line) } else { format!("{}", line) }; -- cgit v1.2.3-18-g5258