diff --git a/src/ansi/ansi_writer.go b/src/ansi/ansi_writer.go index 6eb921f7..d1a3207f 100644 --- a/src/ansi/ansi_writer.go +++ b/src/ansi/ansi_writer.go @@ -403,15 +403,23 @@ func (w *Writer) writeColorOverrides(match map[string]string, background string, if position == len(w.runes)-1 { return } - if w.currentBackground != w.background || w.transparent { - if w.transparent { - w.writeEscapedAnsiString(transparentEnd) - } + + if w.transparent { + 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)) } - 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.transparent = false return } diff --git a/src/ansi/ansi_writer_test.go b/src/ansi/ansi_writer_test.go index afc2a241..ce171cd8 100644 --- a/src/ansi/ansi_writer_test.go +++ b/src/ansi/ansi_writer_test.go @@ -26,7 +26,7 @@ func TestWriteANSIColors(t *testing.T) { { Case: "Bold with color override", Input: "<#ffffff>test", - 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}, }, { diff --git a/src/ansi/colors.go b/src/ansi/colors.go index 56e11b46..120b4bd7 100644 --- a/src/ansi/colors.go +++ b/src/ansi/colors.go @@ -36,6 +36,10 @@ func (c Color) IsTransparent() bool { return c == transparentColor } +func (c Color) IsClear() bool { + return c == transparentColor || c == emptyColor +} + func (c Color) ToForeground() Color { colorString := string(c) if strings.HasPrefix(colorString, "38;") {