Skip to content

Commit

Permalink
breakpoints are now treated like single character words
Browse files Browse the repository at this point in the history
so that the can be HardWrapped and are not ignored.
According Test are added too.
  • Loading branch information
treilik committed Sep 22, 2021
1 parent ea525b1 commit f68fdaa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion wordwrap/wordwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,15 @@ func (w *WordWrap) Write(b []byte) (int, error) {
// valid breakpoint
w.addSpace()
w.addWord()
w.buf.WriteRune(c)
_, _ = w.word.WriteRune(c)

// Wrap line if the breakpoint would exceed the Limit
if w.HardWrap && w.lineLen+w.space.Len()+runewidth.RuneWidth(c) > w.Limit {
w.addNewLine()
}

// treat breakpoint as single character length words
w.addWord()
} else if w.HardWrap && w.lineLen+w.word.PrintableRuneWidth()+runewidth.RuneWidth(c)+w.space.Len() == w.Limit {
// Word is at the limite -> begin new word
w.word.WriteRune(c)
Expand Down
9 changes: 9 additions & 0 deletions wordwrap/wordwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ func TestHardWrap(t *testing.T) {
true,
"",
},
// hyphens have also to be hardwrapped
{
"------------------------------------",
"----\n----\n----\n----\n----\n----\n----\n----\n----",
4,
true,
true,
"",
},
}
for i, tc := range tt {
f := NewWriter(tc.Limit)
Expand Down

0 comments on commit f68fdaa

Please sign in to comment.