diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c4b82e0c..e0d3e6625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,8 +11,10 @@ Feature enhancements: * Added or improved file type filtering for erb, diff, Gradle, HAML, Org, Postscript, Skim, Slim, Slime, RPM Spec files, Typoscript, xml. +* [FEATURE #1370](https://github.com/BurntSushi/ripgrep/pull/1370): + Add `--include-zero` flag that shows files searched without matches. * [FEATURE #1390](https://github.com/BurntSushi/ripgrep/pull/1390): - Add new `--no-context-separator` flag that always hides context separators. + Add `--no-context-separator` flag that always hides context separators. Bug fixes: diff --git a/complete/_rg b/complete/_rg index d77ef5b61..5fa6e9c26 100644 --- a/complete/_rg +++ b/complete/_rg @@ -72,6 +72,7 @@ _rg() { + '(count)' # Counting options {-c,--count}'[only show count of matching lines for each file]' '--count-matches[only show count of individual matches for each file]' + '--include-zero[include files with zero matches in summary]' + '(encoding)' # Encoding options {-E+,--encoding=}'[specify text encoding of files to search]: :_rg_encodings' diff --git a/grep-printer/src/summary.rs b/grep-printer/src/summary.rs index a1c7785e5..d10ca17ab 100644 --- a/grep-printer/src/summary.rs +++ b/grep-printer/src/summary.rs @@ -202,6 +202,8 @@ impl SummaryBuilder { /// This completely overrides any previous color specifications. This does /// not add to any previously provided color specifications on this /// builder. + /// + /// The default color specifications provide no styling. pub fn color_specs( &mut self, specs: ColorSpecs, @@ -256,6 +258,8 @@ impl SummaryBuilder { /// If multi line search is enabled and a match spans multiple lines, then /// that match is counted exactly once for the purposes of enforcing this /// limit, regardless of how many lines it spans. + /// + /// This is disabled by default. pub fn max_matches(&mut self, limit: Option) -> &mut SummaryBuilder { self.config.max_matches = limit; self @@ -266,6 +270,8 @@ impl SummaryBuilder { /// When enabled and the mode is either `Count` or `CountMatches`, then /// results are not printed if no matches were found. Otherwise, every /// search prints a result with a possibly `0` number of matches. + /// + /// This is enabled by default. pub fn exclude_zero(&mut self, yes: bool) -> &mut SummaryBuilder { self.config.exclude_zero = yes; self @@ -292,6 +298,8 @@ impl SummaryBuilder { /// A typical use for this option is to permit cygwin users on Windows to /// set the path separator to `/` instead of using the system default of /// `\`. + /// + /// This is disabled by default. pub fn separator_path( &mut self, sep: Option, diff --git a/src/app.rs b/src/app.rs index cd4a927bc..ae26f0c68 100644 --- a/src/app.rs +++ b/src/app.rs @@ -578,6 +578,7 @@ pub fn all_args_and_flags() -> Vec { flag_ignore_case(&mut args); flag_ignore_file(&mut args); flag_ignore_file_case_insensitive(&mut args); + flag_include_zero(&mut args); flag_invert_match(&mut args); flag_json(&mut args); flag_line_buffered(&mut args); @@ -1373,6 +1374,17 @@ This flag can be disabled with the --no-ignore-file-case-insensitive flag. args.push(arg); } +fn flag_include_zero(args: &mut Vec) { + const SHORT: &str = "Include files with zero matches in summary"; + const LONG: &str = long!("\ +When used with --count or --count-matches, print the number of matches for +each file even if there were zero matches. This is disabled by default but can +be enabled to make ripgrep behave more like grep. +"); + let arg = RGArg::switch("include-zero").help(SHORT).long_help(LONG); + args.push(arg); +} + fn flag_invert_match(args: &mut Vec) { const SHORT: &str = "Invert matching."; const LONG: &str = long!("\ diff --git a/src/args.rs b/src/args.rs index 075d7d293..32ea1b110 100644 --- a/src/args.rs +++ b/src/args.rs @@ -812,6 +812,7 @@ impl ArgMatches { .stats(self.stats()) .path(self.with_filename(paths)) .max_matches(self.max_count()?) + .exclude_zero(!self.is_present("include-zero")) .separator_field(b":".to_vec()) .separator_path(self.path_separator()?) .path_terminator(self.path_terminator()); diff --git a/tests/misc.rs b/tests/misc.rs index c122f5f5c..b40267023 100644 --- a/tests/misc.rs +++ b/tests/misc.rs @@ -392,6 +392,37 @@ rgtest!(count_matches_via_only, |dir: Dir, mut cmd: TestCommand| { eqnice!(expected, cmd.stdout()); }); +rgtest!(include_zero, |dir: Dir, mut cmd: TestCommand| { + dir.create("sherlock", SHERLOCK); + cmd.args(&[ + "--count", + "--include-zero", + "nada", + ]); + cmd.assert_err(); + + let output = cmd.cmd().output().unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + let expected = "sherlock:0\n"; + + eqnice!(expected, stdout); +}); + +rgtest!(include_zero_override, |dir: Dir, mut cmd: TestCommand| { + dir.create("sherlock", SHERLOCK); + cmd.args(&[ + "--count", + "--include-zero", + "--no-include-zero", + "nada", + ]); + cmd.assert_err(); + + let output = cmd.cmd().output().unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!(stdout.is_empty()); +}); + rgtest!(files_with_matches, |dir: Dir, mut cmd: TestCommand| { dir.create("sherlock", SHERLOCK); cmd.arg("--files-with-matches").arg("Sherlock");