mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-01-19 05:49:14 +02:00
printer: fix colors on empty matches
This fixes a bug where a "match" color escape was erroneously emitted after the new line character. This is because `^` is actually allowed to match after the end of a trailing new line, which means `^$` matches both before and after the trailing new line when multiline mode is enabled. The trailing match was causing the phantom escape sequence to appear, which we don't want. Incidentally, this is the root cause of #441 as well, although this commit doesn't fix that issue, since the line itself is printed before we detect the phantom match. Fixes #599
This commit is contained in:
parent
f0028a66ec
commit
2a14bf2249
@ -361,8 +361,13 @@ impl<W: WriteColor> Printer<W> {
|
||||
let mut last_written = 0;
|
||||
for o in offsets {
|
||||
self.write(&buf[last_written..o.start]);
|
||||
self.write_colored(
|
||||
&buf[o.start..o.end], |colors| colors.matched());
|
||||
// This conditional checks if the match is both empty *and*
|
||||
// past the end of the line. In this case, we never want to
|
||||
// emit an additional color escape.
|
||||
if o.start != o.end || o.end != buf.len() {
|
||||
self.write_colored(
|
||||
&buf[o.start..o.end], |colors| colors.matched());
|
||||
}
|
||||
last_written = o.end;
|
||||
}
|
||||
self.write(&buf[last_written..]);
|
||||
|
@ -1146,6 +1146,29 @@ clean!(regression_493, " 're ", "input.txt", |wd: WorkDir, mut cmd: Command| {
|
||||
assert_eq!(lines, " 're \n");
|
||||
});
|
||||
|
||||
// See: https://github.com/BurntSushi/ripgrep/issues/599
|
||||
clean!(regression_599, "^$", "input.txt", |wd: WorkDir, mut cmd: Command| {
|
||||
wd.create("input.txt", "\n\ntest\n");
|
||||
cmd.args(&[
|
||||
"--color", "ansi",
|
||||
"--colors", "path:none",
|
||||
"--colors", "line:none",
|
||||
"--colors", "match:fg:red",
|
||||
"--colors", "match:style:nobold",
|
||||
"--line-number",
|
||||
]);
|
||||
|
||||
let lines: String = wd.stdout(&mut cmd);
|
||||
// Technically, the expected output should only be two lines, but:
|
||||
// https://github.com/BurntSushi/ripgrep/issues/441
|
||||
let expected = "\
|
||||
[m1[m:[m[31m[m
|
||||
[m2[m:[m[31m[m
|
||||
[m4[m:
|
||||
";
|
||||
assert_eq!(expected, lines);
|
||||
});
|
||||
|
||||
// See: https://github.com/BurntSushi/ripgrep/issues/1
|
||||
clean!(feature_1_sjis, "Шерлок Холмс", ".", |wd: WorkDir, mut cmd: Command| {
|
||||
let sherlock =
|
||||
|
Loading…
x
Reference in New Issue
Block a user