Skip to content

Commit

Permalink
fix(ansi): whitespace handling on the last line (#287)
Browse files Browse the repository at this point in the history
This commit fixes a bug where the last line of a wrapped string would
preserve spaces at the end of the line and might hard wrap the line
with spaces only. This commit changes the behavior to remove any
whitespace at the end of the line.

Fixes: #286
  • Loading branch information
aymanbagabas authored Dec 10, 2024
1 parent d2b9686 commit 7755dca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions ansi/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,17 @@ func Wrap(s string, limit int, breakpoints string) string {
i++
}

if word.Len() != 0 {
// Preserve ANSI wrapped spaces at the end of string
if wordLen == 0 {
if curWidth+space.Len() > limit {
buf.WriteByte('\n')
curWidth = 0
} else {
// preserve whitespaces
buf.Write(space.Bytes())
}
addSpace()
space.Reset()
}
buf.Write(word.Bytes())

addWord()

return buf.String()
}
Expand Down
2 changes: 1 addition & 1 deletion ansi/wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ var wrapCases = []struct {
{
name: "extra space style",
input: "\x1b[mfoo \x1b[m",
expected: "\x1b[mfoo\n \x1b[m",
expected: "\x1b[mfoo\x1b[m",
width: 3,
},
{
Expand Down

0 comments on commit 7755dca

Please sign in to comment.