From f68fdaa21168a06e52cca441358cccb1909975d9 Mon Sep 17 00:00:00 2001 From: treilik Date: Fri, 5 Mar 2021 12:49:17 +0100 Subject: [PATCH] breakpoints are now treated like single character words so that the can be HardWrapped and are not ignored. According Test are added too. --- wordwrap/wordwrap.go | 10 +++++++++- wordwrap/wordwrap_test.go | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/wordwrap/wordwrap.go b/wordwrap/wordwrap.go index 0bd6e6d..6ba0008 100644 --- a/wordwrap/wordwrap.go +++ b/wordwrap/wordwrap.go @@ -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) diff --git a/wordwrap/wordwrap_test.go b/wordwrap/wordwrap_test.go index ec4acbe..5ccce2b 100644 --- a/wordwrap/wordwrap_test.go +++ b/wordwrap/wordwrap_test.go @@ -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)