1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2024-12-12 19:18:24 +02:00

cli: add --include-zero flag

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
This commit is contained in:
Collin Styles 2019-10-16 19:03:00 -07:00 committed by Andrew Gallant
parent 4628d77808
commit a070722ff2
6 changed files with 56 additions and 1 deletions

View File

@ -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:

View File

@ -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'

View File

@ -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<u64>) -> &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<u8>,

View File

@ -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);
@ -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!("\

View File

@ -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());

View File

@ -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");