summaryrefslogtreecommitdiffstats
path: root/src/search_buffer.rs
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2016-09-25 14:53:31 -0400
committerGitHub <noreply@github.com>2016-09-25 14:53:31 -0400
commit95edcd4d3a7c98d5679c890ed9875a17e54ca001 (patch)
tree42df5a5777609321dfa947156845c31a7d2cc589 /src/search_buffer.rs
parentd97f4049701a483570cf0ebeb2bc00e9b9a1f214 (diff)
parenta8f3d9e87e7e9192a9c1815da7c07d4522ccc1b2 (diff)
Merge pull request #42 from andschwa/files-with-matches
Files with matches
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));