Trim trailing whitespaces after processing ANSI sequences

Close #4282
This commit is contained in:
Junegunn Choi
2025-02-26 16:17:12 +09:00
parent 710ebdf9c1
commit 639253840f
3 changed files with 7 additions and 1 deletions

View File

@@ -135,6 +135,7 @@ func Run(opts *Options) (int, error) {
return false
}
item.text, item.colors = ansiProcessor(stringBytes(transformed))
item.text.TrimTrailingWhitespaces()
item.text.Index = itemIndex
item.origText = &data
itemIndex++

View File

@@ -778,7 +778,7 @@ func nthTransformer(str string) (func(Delimiter) func([]Token, int32) string, er
}
return func(Delimiter) func([]Token, int32) string {
return func(tokens []Token, index int32) string {
return strings.TrimRightFunc(JoinTokens(Transform(tokens, nth)), unicode.IsSpace)
return JoinTokens(Transform(tokens, nth))
}
}, nil
}

View File

@@ -184,6 +184,11 @@ func (chars *Chars) TrailingWhitespaces() int {
return whitespaces
}
func (chars *Chars) TrimTrailingWhitespaces() {
whitespaces := chars.TrailingWhitespaces()
chars.slice = chars.slice[0 : len(chars.slice)-whitespaces]
}
func (chars *Chars) TrimSuffix(runes []rune) {
lastIdx := len(chars.slice)
firstIdx := lastIdx - len(runes)