From 0e999482cb13c194427d47687a49473e1e240a20 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 25 Dec 2023 17:05:14 +0900 Subject: [PATCH] Fix handling of empty ANSI color sequence Fix #3320 --- src/ansi.go | 9 +++++++++ src/ansi_test.go | 3 +++ 2 files changed, 12 insertions(+) diff --git a/src/ansi.go b/src/ansi.go index 05e0861..e917a4f 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 f352c99..b5481e5 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")