Skip to content

Commit

Permalink
cli: add --include-zero flag
Browse files Browse the repository at this point in the history
This flag, when used in conjunction with --count or --count-matches,
will print a result for each file searched even if there were zero
matches in that file. This is off by default but can be enabled to make
ripgrep behave more like grep.

This also clarifies some of the defaults for the
grep-printer::SummaryBuilder type.

Closes #1370, Closes #1405
  • Loading branch information
cstyles authored and BurntSushi committed Feb 17, 2020
1 parent 4628d77 commit a070722
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions complete/_rg
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 8 additions & 0 deletions grep-printer/src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<u64>) -> &mut SummaryBuilder {
self.config.max_matches = limit;
self
Expand All @@ -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
Expand All @@ -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<u8>,
Expand Down
12 changes: 12 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ pub fn all_args_and_flags() -> Vec<RGArg> {
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);
Expand Down Expand Up @@ -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<RGArg>) {
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<RGArg>) {
const SHORT: &str = "Invert matching.";
const LONG: &str = long!("\
Expand Down
1 change: 1 addition & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
31 changes: 31 additions & 0 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit a070722

Please sign in to comment.