Always prepend ANSI reset code before re-assembling tokens

This commit is contained in:
Junegunn Choi 2019-03-07 10:47:09 +09:00
parent e00e7e1e56
commit 85c1f8a9e0
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
4 changed files with 9 additions and 7 deletions

View File

@ -34,7 +34,7 @@ func (s *ansiState) equals(t *ansiState) bool {
func (s *ansiState) ToString() string {
if !s.colored() {
return "\x1b[m"
return ""
}
ret := ""

View File

@ -167,8 +167,8 @@ func TestAnsiCodeStringConversion(t *testing.T) {
strings.Replace(state.ToString(), "\x1b[", "\\x1b[", -1))
}
}
assert("\x1b[m", nil, "\x1b[m")
assert("\x1b[m", &ansiState{attr: tui.Blink}, "\x1b[m")
assert("\x1b[m", nil, "")
assert("\x1b[m", &ansiState{attr: tui.Blink}, "")
assert("\x1b[31m", nil, "\x1b[31;49m")
assert("\x1b[41m", nil, "\x1b[39;41m")

View File

@ -112,7 +112,9 @@ func Run(opts *Options, revision string) {
prevAnsiState := ansiState
_, _, ansiState = extractColor(token.text.ToString(), ansiState, nil)
if prevAnsiState != nil {
token.text.Wrap(prevAnsiState.ToString(), "\x1b[m")
token.text.Prepend("\x1b[m" + prevAnsiState.ToString())
} else {
token.text.Prepend("\x1b[m")
}
}
}

View File

@ -172,11 +172,11 @@ func (chars *Chars) CopyRunes(dest []rune) {
return
}
func (chars *Chars) Wrap(prefix string, suffix string) {
func (chars *Chars) Prepend(prefix string) {
if runes := chars.optionalRunes(); runes != nil {
runes = append(append([]rune(prefix), runes...), []rune(suffix)...)
runes = append([]rune(prefix), runes...)
chars.slice = *(*[]byte)(unsafe.Pointer(&runes))
} else {
chars.slice = append(append([]byte(prefix), chars.slice...), []byte(suffix)...)
chars.slice = append([]byte(prefix), chars.slice...)
}
}