From c9894d2a0a20064bcf844899c10275498ea6a923 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Sat, 4 Mar 2023 00:40:14 +0400 Subject: print filenames if there're more than 1 files Also add option to suppress the printing of filenames --- src/main.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 72efe60..e17f151 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,13 +31,22 @@ struct Config { /// Matches only whole words #[arg(short, long)] word_regexp: bool, + + /// Suppresses the display of filenames + #[arg(short = 'h', long)] + no_filename: bool, } fn main() { let mut config = Config::parse(); - if config.files.is_empty() { - config.files.push(String::from("-")); + match config.files.len() { + 0 => { + config.files.push(String::from("-")); + config.no_filename = true; + } + 1 => config.no_filename = true, + _ => (), } if let Err(e) = run(config) { @@ -66,7 +75,14 @@ fn run(config: Config) -> Result<(), Box> { if !match_flag { continue; } - println!("{line}"); + + let formatted_output = if !config.no_filename { + format!("{}:{}", file, line) + } else { + format!("{}", line) + }; + + println!("{}", formatted_output); } } -- cgit v1.2.3-18-g5258