diff options
author | Balaji Sivaraman <balaji@balajisivaraman.com> | 2018-02-20 21:03:07 +0530 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2018-03-10 10:38:25 -0500 |
commit | 27fc9f2fd341bd3ed672f79d43bf983514188b96 (patch) | |
tree | 6a0d42ebe069ac6077eace1968332246ab51cb77 /src/args.rs | |
parent | 96f73293c0b734d91b55ad1e6940da0f706eed65 (diff) |
search: add a --count-matches flag
This commit introduces a new flag, --count-matches, which will cause
ripgrep to report a total count of all matches instead of a count of
total lines matched.
Closes #566, Closes #814
Diffstat (limited to 'src/args.rs')
-rw-r--r-- | src/args.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/args.rs b/src/args.rs index b8714deb..309f5db8 100644 --- a/src/args.rs +++ b/src/args.rs @@ -41,6 +41,7 @@ pub struct Args { column: bool, context_separator: Vec<u8>, count: bool, + count_matches: bool, encoding: Option<&'static Encoding>, files_with_matches: bool, files_without_matches: bool, @@ -200,6 +201,7 @@ impl Args { pub fn file_separator(&self) -> Option<Vec<u8>> { let contextless = self.count + || self.count_matches || self.files_with_matches || self.files_without_matches; let use_heading_sep = self.heading && !contextless; @@ -262,6 +264,7 @@ impl Args { .before_context(self.before_context) .byte_offset(self.byte_offset) .count(self.count) + .count_matches(self.count_matches) .encoding(self.encoding) .files_with_matches(self.files_with_matches) .files_without_matches(self.files_without_matches) @@ -358,6 +361,7 @@ impl<'a> ArgMatches<'a> { let mmap = self.mmap(&paths)?; let with_filename = self.with_filename(&paths); let (before_context, after_context) = self.contexts()?; + let (count, count_matches) = self.counts(); let quiet = self.is_present("quiet"); let args = Args { paths: paths, @@ -368,7 +372,8 @@ impl<'a> ArgMatches<'a> { colors: self.color_specs()?, column: self.column(), context_separator: self.context_separator(), - count: self.is_present("count"), + count: count, + count_matches: count_matches, encoding: self.encoding()?, files_with_matches: self.is_present("files-with-matches"), files_without_matches: self.is_present("files-without-match"), @@ -732,6 +737,22 @@ impl<'a> ArgMatches<'a> { }) } + /// Returns whether the -c/--count or the --count-matches flags were + /// passed from the command line. + /// + /// If --count-matches and --invert-match were passed in, behave + /// as if --count and --invert-match were passed in (i.e. rg will + /// count inverted matches as per existing behavior). + fn counts(&self) -> (bool, bool) { + let count = self.is_present("count"); + let count_matches = self.is_present("count-matches"); + let invert_matches = self.is_present("invert-match"); + if count_matches && invert_matches { + return (true, false); + } + (count, count_matches) + } + /// Returns the user's color choice based on command line parameters and /// environment. fn color_choice(&self) -> termcolor::ColorChoice { |