fix(ansi): do not print clear colors

resolves #3321
This commit is contained in:
Jan De Dobbeleer 2023-01-06 10:53:57 +01:00 committed by Jan De Dobbeleer
parent edb3842db7
commit 2b576a3cce
3 changed files with 18 additions and 6 deletions

View file

@ -403,15 +403,23 @@ func (w *Writer) writeColorOverrides(match map[string]string, background string,
if position == len(w.runes)-1 { if position == len(w.runes)-1 {
return return
} }
if w.currentBackground != w.background || w.transparent {
if w.transparent { if w.transparent {
w.writeEscapedAnsiString(transparentEnd) w.writeEscapedAnsiString(transparentEnd)
} }
if w.background.IsClear() {
w.writeEscapedAnsiString(colorStyle.End)
}
if w.currentBackground != w.background && !w.background.IsClear() {
w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.background)) w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.background))
} }
if w.currentForeground != w.foreground || w.transparent {
if (w.currentForeground != w.foreground || w.transparent) && !w.foreground.IsClear() {
w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.foreground)) w.writeEscapedAnsiString(fmt.Sprintf(colorise, w.foreground))
} }
w.transparent = false w.transparent = false
return return
} }

View file

@ -26,7 +26,7 @@ func TestWriteANSIColors(t *testing.T) {
{ {
Case: "Bold with color override", Case: "Bold with color override",
Input: "<b><#ffffff>test</></b>", Input: "<b><#ffffff>test</></b>",
Expected: "\x1b[1m\x1b[30m\x1b[38;2;255;255;255mtest\x1b[30m\x1b[22m\x1b[0m", Expected: "\x1b[1m\x1b[30m\x1b[38;2;255;255;255mtest\x1b[0m\x1b[30m\x1b[22m\x1b[0m",
Colors: &cachedColor{Foreground: "black", Background: ParentBackground}, Colors: &cachedColor{Foreground: "black", Background: ParentBackground},
}, },
{ {

View file

@ -36,6 +36,10 @@ func (c Color) IsTransparent() bool {
return c == transparentColor return c == transparentColor
} }
func (c Color) IsClear() bool {
return c == transparentColor || c == emptyColor
}
func (c Color) ToForeground() Color { func (c Color) ToForeground() Color {
colorString := string(c) colorString := string(c)
if strings.HasPrefix(colorString, "38;") { if strings.HasPrefix(colorString, "38;") {