Fix handling of empty ANSI color sequence

Fix #3320
This commit is contained in:
Junegunn Choi 2023-12-25 17:05:14 +09:00
parent 65b2c06027
commit 0e999482cb
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 12 additions and 0 deletions

View File

@ -351,9 +351,11 @@ func interpretCode(ansiCode string, prevState *ansiState) ansiState {
ptr := &state.fg
var delimiter byte = 0
count := 0
for len(ansiCode) != 0 {
var num int
if num, delimiter, ansiCode = parseAnsiCode(ansiCode, delimiter); num != -1 {
count++
switch state256 {
case 0:
switch num {
@ -435,6 +437,13 @@ func interpretCode(ansiCode string, prevState *ansiState) ansiState {
}
}
// Empty sequence: reset
if count == 0 {
state.fg = -1
state.bg = -1
state.attr = 0
}
if state256 > 0 {
*ptr = -1
}

View File

@ -348,6 +348,9 @@ func TestAnsiCodeStringConversion(t *testing.T) {
}
assert("\x1b[m", nil, "")
assert("\x1b[m", &ansiState{attr: tui.Blink, lbg: -1}, "")
assert("\x1b[0m", &ansiState{fg: 4, bg: 4, lbg: -1}, "")
assert("\x1b[;m", &ansiState{fg: 4, bg: 4, lbg: -1}, "")
assert("\x1b[;;m", &ansiState{fg: 4, bg: 4, lbg: -1}, "")
assert("\x1b[31m", nil, "\x1b[31;49m")
assert("\x1b[41m", nil, "\x1b[39;41m")