summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2023-03-04 00:45:33 +0400
committerJoursoir <chat@joursoir.net>2023-03-04 01:04:19 +0400
commit7b612f0f1854eb164b658547ac57be6ab9dad338 (patch)
treedfa28d0eed1eb023e8f83d9c79f25055dc6b6a5c
parentc9894d2a0a20064bcf844899c10275498ea6a923 (diff)
downloadtrgrep-7b612f0f1854eb164b658547ac57be6ab9dad338.tar.gz
trgrep-7b612f0f1854eb164b658547ac57be6ab9dad338.tar.bz2
trgrep-7b612f0f1854eb164b658547ac57be6ab9dad338.zip
add option to prefix lines with the line number
-rw-r--r--src/main.rs12
1 files changed, 10 insertions, 2 deletions
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<dyn Error>> {
&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)
};