Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
breakpoints are now treated like single character words
so that the can be HardWrapped. According Test are added too. diff --git a/wordwrap/wordwrap.go b/wordwrap/wordwrap.go index 90a32b2..bbf1054 100644 --- a/wordwrap/wordwrap.go +++ b/wordwrap/wordwrap.go @@ -231,7 +231,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 limit -> begin new word _, _ = w.word.WriteRune(c) diff --git a/wordwrap/wordwrap_test.go b/wordwrap/wordwrap_test.go index 22df6bf..8cf0dad 100644 --- a/wordwrap/wordwrap_test.go +++ b/wordwrap/wordwrap_test.go @@ -250,7 +250,7 @@ func TestHardWrap(t *testing.T) { true, "", }, - // check if befor zero gets reset and after gets remembered/repeated + // check if before zero gets reset and after gets remembered/repeated { "\x1b[34mblue\x1b[33;0;31mred\x1b[0m", "\x1b[34mblue\x1b[33;0m\x1b[31mre\x1b[0m\n\x1b[31md\x1b[0m", @@ -268,6 +268,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)
- Loading branch information