1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-01-29 22:01:04 +02:00

Fix printing context after an early return from a search

Print the context if there's a context left to be printed after
returning early from a search (because of --max-count).

Fixes #402.
This commit is contained in:
Antti Keränen 2017-10-01 19:53:02 +03:00 committed by Andrew Gallant
parent 353806b87a
commit e7c06b92fb

View File

@ -299,6 +299,15 @@ impl<'a, R: io::Read, W: WriteColor> Searcher<'a, R, W> {
}
}
}
if self.after_context_remaining > 0 {
if self.last_printed == self.inp.lastnl {
try!(self.fill());
}
let upto = self.inp.lastnl;
if upto > 0 {
self.print_after_context(upto);
}
}
if self.match_count > 0 {
if self.opts.count {
self.printer.path_count(self.path, self.match_count);
@ -1247,6 +1256,23 @@ fn main() {
");
}
#[test]
fn after_context_invert_one_max_count_two() {
let (count, out) = search_smallcap("Sherlock", SHERLOCK, |s| {
s.line_number(true)
.invert_match(true)
.after_context(1)
.max_count(Some(2))
});
assert_eq!(2, count);
assert_eq!(out, "\
/baz.rs:2:Holmeses, success in the province of detective work must always
/baz.rs-3-be, to a very large extent, the result of luck. Sherlock Holmes
/baz.rs:4:can extract a clew from a wisp of straw or a flake of cigar ash;
/baz.rs-5-but Doctor Watson has to have it taken out for him and dusted,
");
}
#[test]
fn after_context_two1() {
let (count, out) = search_smallcap("Sherlock", SHERLOCK, |s| {
@ -1290,6 +1316,23 @@ fn main() {
");
}
#[test]
fn after_context_two_max_count_two() {
let (count, out) = search_smallcap(
"Doctor", SHERLOCK, |s| {
s.line_number(true).after_context(2).max_count(Some(2))
});
assert_eq!(2, count);
assert_eq!(out, "\
/baz.rs:1:For the Doctor Watsons of this world, as opposed to the Sherlock
/baz.rs-2-Holmeses, success in the province of detective work must always
/baz.rs-3-be, to a very large extent, the result of luck. Sherlock Holmes
--
/baz.rs:5:but Doctor Watson has to have it taken out for him and dusted,
/baz.rs-6-and exhibited clearly, with a label attached.
");
}
#[test]
fn after_context_three1() {
let (count, out) = search_smallcap("Sherlock", SHERLOCK, |s| {