Skip to content

Commit 7541e7e

Browse files
author
treilik
committed
breakpoints are now treated like single character words
so that the can be HardWrapped. According Test are added too.
1 parent 372009e commit 7541e7e

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

wordwrap/wordwrap.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,15 @@ func (w *WordWrap) Write(b []byte) (int, error) {
231231
// valid breakpoint
232232
w.addSpace()
233233
w.addWord()
234-
_, _ = w.buf.WriteRune(c)
234+
_, _ = w.word.WriteRune(c)
235+
236+
// Wrap line if the breakpoint would exceed the Limit
237+
if w.HardWrap && w.lineLen+w.space.Len()+runewidth.RuneWidth(c) > w.Limit {
238+
w.addNewLine()
239+
}
240+
241+
// treat breakpoint as single character length words
242+
w.addWord()
235243
} else if w.HardWrap && w.lineLen+w.word.PrintableRuneWidth()+runewidth.RuneWidth(c)+w.space.Len() == w.Limit {
236244
// Word is at the limit -> begin new word
237245
_, _ = w.word.WriteRune(c)

wordwrap/wordwrap_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func TestHardWrap(t *testing.T) {
250250
true,
251251
"",
252252
},
253-
// check if befor zero gets reset and after gets remembered/repeated
253+
// check if before zero gets reset and after gets remembered/repeated
254254
{
255255
"\x1b[34mblue\x1b[33;0;31mred\x1b[0m",
256256
"\x1b[34mblue\x1b[33;0m\x1b[31mre\x1b[0m\n\x1b[31md\x1b[0m",
@@ -268,6 +268,15 @@ func TestHardWrap(t *testing.T) {
268268
true,
269269
"",
270270
},
271+
// hyphens have also to be hardwrapped
272+
{
273+
"------------------------------------",
274+
"----\n----\n----\n----\n----\n----\n----\n----\n----",
275+
4,
276+
true,
277+
true,
278+
"",
279+
},
271280
}
272281
for i, tc := range tt {
273282
f := NewWriter(tc.Limit)

0 commit comments

Comments
 (0)