diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2023-12-25 17:05:14 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2023-12-25 17:05:54 +0900 |
commit | 0e999482cb13c194427d47687a49473e1e240a20 (patch) | |
tree | 1a39060af647ffbd64539be3e920e9fd46a5f41e /src | |
parent | 65b2c060274c7735e6b1e0e5b924d057641f2e4a (diff) |
Fix handling of empty ANSI color sequence
Fix #3320
Diffstat (limited to 'src')
-rw-r--r-- | src/ansi.go | 9 | ||||
-rw-r--r-- | src/ansi_test.go | 3 |
2 files changed, 12 insertions, 0 deletions
diff --git a/src/ansi.go b/src/ansi.go index 05e08613..e917a4f2 100644 --- a/src/ansi.go +++ b/src/ansi.go @@ -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 } diff --git a/src/ansi_test.go b/src/ansi_test.go index f352c99e..b5481e5f 100644 --- a/src/ansi_test.go +++ b/src/ansi_test.go @@ -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") |