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.
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
treilik committed Mar 5, 2021
1 parent 372009e commit 6f6f884
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 9 additions & 1 deletion wordwrap/wordwrap.go
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion wordwrap/wordwrap_test.go
Expand Up @@ -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",
Expand All @@ -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)
Expand Down

0 comments on commit 6f6f884

Please sign in to comment.