fix(ansi): only clear background on inline overrides

resolves #3699
This commit is contained in:
Jan De Dobbeleer 2023-04-09 19:05:58 +02:00 committed by Jan De Dobbeleer
parent 2d0ee8c8ab
commit 3baf41e051
2 changed files with 7 additions and 6 deletions

View file

@ -24,7 +24,8 @@ var (
{AnchorStart: `<f>`, AnchorEnd: `</f>`, Start: "\x1b[5m", End: "\x1b[25m"},
{AnchorStart: `<r>`, AnchorEnd: `</r>`, Start: "\x1b[7m", End: "\x1b[27m"},
}
colorStyle = &style{AnchorStart: "COLOR", AnchorEnd: `</>`, End: "\x1b[0m"}
resetStyle = &style{AnchorStart: "RESET", AnchorEnd: `</>`, End: "\x1b[0m"}
backgroundStyle = &style{AnchorStart: "BACKGROUND", AnchorEnd: `</>`, End: "\x1b[49m"}
)
type style struct {
@ -335,7 +336,7 @@ func (w *Writer) Write(background, foreground, text string) {
w.hyperlinkState = OTHER
// reset colors
w.writeEscapedAnsiString(colorStyle.End)
w.writeEscapedAnsiString(resetStyle.End)
// reset current
w.currentBackground = ""
@ -410,9 +411,9 @@ func (w *Writer) writeSegmentColors() {
func (w *Writer) writeColorOverrides(match map[string]string, background string, i int) (position int) {
position = i
// check color reset first
if match[ANCHOR] == colorStyle.AnchorEnd {
if match[ANCHOR] == resetStyle.AnchorEnd {
// make sure to reset the colors if needed
position += len([]rune(colorStyle.AnchorEnd)) - 1
position += len([]rune(resetStyle.AnchorEnd)) - 1
// do not reset when colors are identical
if w.currentBackground == w.background && w.currentForeground == w.foreground {
@ -429,7 +430,7 @@ func (w *Writer) writeColorOverrides(match map[string]string, background string,
}
if w.background.IsClear() {
w.writeEscapedAnsiString(colorStyle.End)
w.writeEscapedAnsiString(backgroundStyle.End)
}
if w.currentBackground != w.background && !w.background.IsClear() {

View file

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