Skip to content

Commit 46ea468

Browse files
author
treilik
committed
breakpoints are now treated like single character words
so that the can be HardWrapped and are not ignored. According Test are added too.
1 parent 363c7e1 commit 46ea468

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

wordwrap/wordwrap.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,15 @@ func (w *WordWrap) Write(b []byte) (int, error) {
198198
// valid breakpoint
199199
w.addSpace()
200200
w.addWord()
201-
w.buf.WriteRune(c)
201+
_, _ = w.word.WriteRune(c)
202+
203+
// Wrap line if the breakpoint would exceed the Limit
204+
if w.HardWrap && w.lineLen+w.space.Len()+runewidth.RuneWidth(c) > w.Limit {
205+
w.addNewLine()
206+
}
207+
208+
// treat breakpoint as single character length words
209+
w.addWord()
202210
} else if w.HardWrap && w.lineLen+w.word.PrintableRuneWidth()+runewidth.RuneWidth(c)+w.space.Len() == w.Limit {
203211
// Word is at the limite -> begin new word
204212
w.word.WriteRune(c)

wordwrap/wordwrap_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ func TestHardWrap(t *testing.T) {
250250
true,
251251
"",
252252
},
253+
// hyphens have also to be hardwrapped
254+
{
255+
"------------------------------------",
256+
"----\n----\n----\n----\n----\n----\n----\n----\n----",
257+
4,
258+
true,
259+
true,
260+
"",
261+
},
253262
}
254263
for i, tc := range tt {
255264
f := NewWriter(tc.Limit)

0 commit comments

Comments
 (0)