Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ansi/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ func wordwrap(m Method, s string, limit int, breakpoints string) string {
addWord()
space.WriteRune(r)
} else if bytes.ContainsAny(cluster, breakpoints) {
// Treat the current word + breakpoint as one unit:
// if appending it would overflow the line, break first
// so the word doesn't trail past the limit.
if curWidth+space.Len()+wordLen+1 > limit && wordLen+1 <= limit {
addNewline()
}
addSpace()
addWord()
buf.Write(cluster)
Expand Down Expand Up @@ -235,6 +241,9 @@ func wordwrap(m Method, s string, limit int, breakpoints string) string {
case r == '-':
fallthrough
case runeContainsAny(r, breakpoints):
if curWidth+space.Len()+wordLen+1 > limit && wordLen+1 <= limit {
addNewline()
}
addSpace()
addWord()
buf.WriteByte(b[i])
Expand Down
1 change: 1 addition & 0 deletions ansi/wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var wwCases = []struct {
{"emoji_breakpoint", "foo😃 foobar", 4, "😃", "foo😃\nfoobar"},
{"wide_emoji_breakpoint", "foo🫧 foobar", 4, "🫧", "foo🫧\nfoobar"},
{"space_breakpoint", "foo --bar", 9, "-", "foo --bar"},
{"breakpoint_overflow", "foo bar, fo", 7, ",", "foo\nbar, fo"},
{"simple", "foo bars foobars", 4, "", "foo\nbars\nfoobars"},
{"limit", "foo bar", 5, "", "foo\nbar"},
{"remove white spaces", "foo \nb ar ", 4, "", "foo\nb\nar"},
Expand Down