From a8f3d9e87e7e9192a9c1815da7c07d4522ccc1b2 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 23 Sep 2016 19:06:34 -0700 Subject: Add --files-with-matches flag. Closes #26. Acts like --count but emits only the paths of files with matches, suitable for piping to xargs. Both mmap and no-mmap searches terminate after the first match is found. Documentation updated and tests added. --- src/search_buffer.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/search_buffer.rs') diff --git a/src/search_buffer.rs b/src/search_buffer.rs index fb16bf0b..a3edbcb0 100644 --- a/src/search_buffer.rs +++ b/src/search_buffer.rs @@ -53,6 +53,14 @@ impl<'a, W: Send + Terminal> BufferSearcher<'a, W> { self } + /// If enabled, searching will print the path instead of each match. + /// + /// Disabled by default. + pub fn files_with_matches(mut self, yes: bool) -> Self { + self.opts.files_with_matches = yes; + self + } + /// Set the end-of-line byte used by this searcher. pub fn eol(mut self, eol: u8) -> Self { self.opts.eol = eol; @@ -96,6 +104,9 @@ impl<'a, W: Send + Terminal> BufferSearcher<'a, W> { self.print_match(m.start(), m.end()); } last_end = m.end(); + if self.opts.files_with_matches { + break; + } } if self.opts.invert_match { let upto = self.buf.len(); @@ -104,13 +115,16 @@ impl<'a, W: Send + Terminal> BufferSearcher<'a, W> { if self.opts.count && self.match_count > 0 { self.printer.path_count(self.path, self.match_count); } + if self.opts.files_with_matches && self.match_count > 0 { + self.printer.path(self.path); + } self.match_count } #[inline(always)] pub fn print_match(&mut self, start: usize, end: usize) { self.match_count += 1; - if self.opts.count { + if self.opts.skip_matches() { return; } self.count_lines(start); @@ -237,6 +251,14 @@ and exhibited clearly, with a label attached.\ assert_eq!(out, "/baz.rs:2\n"); } + #[test] + fn files_with_matches() { + let (count, out) = search( + "Sherlock", SHERLOCK, |s| s.files_with_matches(true)); + assert_eq!(1, count); + assert_eq!(out, "/baz.rs\n"); + } + #[test] fn invert_match() { let (count, out) = search( -- cgit v1.2.3