fix(ansi): restore previous override correctly

This commit is contained in:
Jan De Dobbeleer 2023-09-23 22:24:57 +02:00 committed by Jan De Dobbeleer
parent 2c7e6b26a3
commit bd3cd316c2
2 changed files with 9 additions and 12 deletions

View file

@ -488,12 +488,15 @@ func (w *Writer) endColorOverride(position int) int {
// make sure to reset the colors if needed
position += len([]rune(resetStyle.AnchorEnd)) - 1
// reset colors to previous when we have > 2 in stack
// - first is always the starting colors used in Write()
// - second is the first override
// as soon as we have more than 2, we can pop the last one
// do not restore colors at the end of the string, we print it anyways
if position == len(w.runes)-1 {
return position
}
// reset colors to previous when we have >= 2 in stack
// as soon as we have more than 1, we can pop the last one
// and print the previous override as it wasn't ended yet
if w.current.Len() >= 3 {
if w.current.Len() >= 2 {
fg := w.current.Foreground()
bg := w.current.Background()
@ -515,11 +518,6 @@ func (w *Writer) endColorOverride(position int) int {
return position
}
// do not restore colors at the end of the string, we print it anyways
if position == len(w.runes)-1 {
return position
}
if w.transparent {
w.writeEscapedAnsiString(transparentEnd)
}

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[49m\x1b[30m\x1b[22m\x1b[0m",
Expected: "\x1b[1m\x1b[30m\x1b[38;2;255;255;255mtest\x1b[30m\x1b[22m\x1b[0m",
Colors: &Colors{Foreground: "black", Background: ParentBackground},
},
{
@ -41,7 +41,6 @@ func TestWriteANSIColors(t *testing.T) {
Expected: "\x1b[38;2;255;255;255m\x1b[1mtest\x1b[22m\x1b[0m",
Colors: &Colors{Foreground: "black", Background: ParentBackground},
},
{
Case: "Double override",
Input: "<#ffffff>jan</>@<#ffffff>Jans-MBP</>",