diff --git a/wordwrap/wordwrap.go b/wordwrap/wordwrap.go index f1b4418..aa7200e 100644 --- a/wordwrap/wordwrap.go +++ b/wordwrap/wordwrap.go @@ -215,9 +215,6 @@ func (w *WordWrap) Write(b []byte) (int, error) { // Close will finish the word-wrap operation. Always call it before trying to // retrieve the final result. func (w *WordWrap) Close() error { - if w.HardWrap && w.word.PrintableRuneWidth()+w.lineLen > w.Limit { - w.addNewLine() - } if w.PreserveSpaces { w.addSpace() } diff --git a/wordwrap/wordwrap_test.go b/wordwrap/wordwrap_test.go index df72550..c6c523f 100644 --- a/wordwrap/wordwrap_test.go +++ b/wordwrap/wordwrap_test.go @@ -223,6 +223,7 @@ func TestHardWrap(t *testing.T) { false, "", }, + // If the text fits -> passing through { "test", "test", @@ -231,6 +232,7 @@ func TestHardWrap(t *testing.T) { true, "", }, + // If requested preserve spaces, no matter how much. { " ", " \n \n \n \n \n \n \n \n \n ", @@ -239,6 +241,15 @@ func TestHardWrap(t *testing.T) { true, "", }, + // If requested preserve spaces, no matter how much and wrap them accordingly + { + " ", + " \n \n \n \n \n \n \n \n \n \n ", + 4, + true, + true, + "", + }, } for i, tc := range tt { f := NewWriter(tc.Limit) @@ -257,3 +268,18 @@ func TestHardWrap(t *testing.T) { } } } + +func TestHarWrapShort(t *testing.T) { + testCase := "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust an\nother test\x1B[38;2;249;38;114m)\x1B[0m" + expected := `(ju +st +an +oth +er +tes +t)` + out := HardWrap(testCase, 3, " ") + if out != expected { + t.Errorf("From input expected:\n\n`%s`\n\nActual Output:\n\n`%s`", expected, out) + } +}