summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCollin Styles <collingstyles@gmail.com>2019-10-16 19:03:00 -0700
committerAndrew Gallant <jamslam@gmail.com>2020-02-17 17:16:28 -0500
commita070722ff27c7d57d47b1766de9d253fbfe5bef9 (patch)
tree1a6370a1966bc094f22ef2d61afcb9d4c1ad6d52
parent4628d778089d0bf609462af7d5b38997fd9a014f (diff)
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
-rw-r--r--CHANGELOG.md4
-rw-r--r--complete/_rg1
-rw-r--r--grep-printer/src/summary.rs8
-rw-r--r--src/app.rs12
-rw-r--r--src/args.rs1
-rw-r--r--tests/misc.rs31
6 files changed, 56 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5c4b82e0..e0d3e662 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 d77ef5b6..5fa6e9c2 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 a1c7785e..d10ca17a 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<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>,
diff --git a/src/app.rs b/src/app.rs
index cd4a927b..ae26f0c6 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -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!("\
diff --git a/src/args.rs b/src/args.rs
index 075d7d29..32ea1b11 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 c122f5f5..b4026702 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");