fix(ansi): do not end coloring on identical colors

resolves #3366
This commit is contained in:
Jan De Dobbeleer 2023-01-14 10:54:27 +01:00 committed by Jan De Dobbeleer
parent cc3a64fad2
commit 8452b5b1e4
2 changed files with 12 additions and 0 deletions

View file

@ -409,6 +409,12 @@ func (w *Writer) writeColorOverrides(match map[string]string, background string,
if match[ANCHOR] == colorStyle.AnchorEnd {
// make sure to reset the colors if needed
position += len([]rune(colorStyle.AnchorEnd)) - 1
// do not reset when colors are identical
if w.currentBackground == w.background && w.currentForeground == w.foreground {
return
}
// do not restore colors at the end of the string, we print it anyways
if position == len(w.runes)-1 {
return

View file

@ -17,6 +17,12 @@ func TestWriteANSIColors(t *testing.T) {
Parent *Colors
TerminalBackground string
}{
{
Case: "Inline override identical",
Input: "\ue0a0saturnay <red>↑</>1",
Expected: "\x1b[31m\ue0a0saturnay ↑1\x1b[0m",
Colors: &Colors{Foreground: "red", Background: Transparent},
},
{
Case: "Bold",
Input: "<b>test</b>",