Skip to content

Commit

Permalink
Trim trailing whitespaces after processing ANSI sequences
Browse files Browse the repository at this point in the history
Close #4282
  • Loading branch information
junegunn committed Feb 26, 2025
1 parent 710ebdf commit 6392538
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down
2 changes: 1 addition & 1 deletion src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions src/util/chars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6392538

Please # to comment.