diff options
author | Andrew Gallant <jamslam@gmail.com> | 2017-10-21 20:36:51 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2017-10-21 22:40:10 -0400 |
commit | 2a14bf22491b01cabe882bfaa116a4aea048047f (patch) | |
tree | 99b921193b4b6c005f6ad3e47a865302404bd510 /src | |
parent | f0028a66ecc9cd6caf864489c93b7f60624437e4 (diff) |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/printer.rs | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/printer.rs b/src/printer.rs index 4d7966c5..3b23e9b0 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -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..]); |