summaryrefslogtreecommitdiffstats
path: root/src/search_buffer.rs
diff options
context:
space:
mode:
authorAndrew Schwartzmeyer <andrew@schwartzmeyer.com>2016-09-23 19:06:34 -0700
committerAndrew Schwartzmeyer <andrew@schwartzmeyer.com>2016-09-24 21:40:17 -0700
commita8f3d9e87e7e9192a9c1815da7c07d4522ccc1b2 (patch)
tree27dc814fa92f1f56359dfe75b281cc42fff2f376 /src/search_buffer.rs
parent1595f0faf594be5d303b1783857d23a0fda74230 (diff)
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.
Diffstat (limited to 'src/search_buffer.rs')
-rw-r--r--src/search_buffer.rs24
1 files changed, 23 insertions, 1 deletions
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);
@@ -238,6 +252,14 @@ and exhibited clearly, with a label attached.\
}
#[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(
"Sherlock", SHERLOCK, |s| s.invert_match(true));