summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2023-03-04 00:40:14 +0400
committerJoursoir <chat@joursoir.net>2023-03-04 01:04:19 +0400
commitc9894d2a0a20064bcf844899c10275498ea6a923 (patch)
tree6c790f91afd09179ced4e943ed8e0f53a63cc3fc
parent7ed5366c0bdf376eb910fed9e566a063f2c35806 (diff)
downloadtrgrep-c9894d2a0a20064bcf844899c10275498ea6a923.tar.gz
trgrep-c9894d2a0a20064bcf844899c10275498ea6a923.tar.bz2
trgrep-c9894d2a0a20064bcf844899c10275498ea6a923.zip
print filenames if there're more than 1 files
Also add option to suppress the printing of filenames
-rw-r--r--src/main.rs22
1 files 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<dyn Error>> {
if !match_flag {
continue;
}
- println!("{line}");
+
+ let formatted_output = if !config.no_filename {
+ format!("{}:{}", file, line)
+ } else {
+ format!("{}", line)
+ };
+
+ println!("{}", formatted_output);
}
}