Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix wordwrap of hyphens at limit #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions wordwrap/wordwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ func (w *WordWrap) Write(b []byte) (int, error) {
// end of current word
w.addWord()
_, _ = w.space.WriteRune(c)
} else if inGroup(w.Breakpoints, c) {
// valid breakpoint
w.addSpace()
w.addWord()
_, _ = w.buf.WriteRune(c)
} else {
// any other character
_, _ = w.word.WriteRune(c)
Expand All @@ -143,6 +138,12 @@ func (w *WordWrap) Write(b []byte) (int, error) {
w.word.PrintableRuneWidth() < w.Limit {
w.addNewLine()
}

// if we just saw a breakpoint, it's like seeing a space; we
// immediately flush the word-in-progress to the buffer
if inGroup(w.Breakpoints, c) {
w.addWord()
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions wordwrap/wordwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ func TestWordWrap(t *testing.T) {
4,
true,
},
// A hyphen should not bypass the limit:
{
"foo foo-foobar",
"foo\nfoo-\nfoobar", // *not* "foo foo-\nfoobar"
7,
true,
},
// A run of hyphens breaks at the limit
{
"--------",
"---\n---\n--",
3,
true,
},
// hyphenated portions of words don't have inner breaks, but do break at
// the hyphen
{
"foobar-foobar-foobar",
"foobar-\nfoobar-\nfoobar",
3,
true,
},
// Space buffer needs to be emptied before breakpoints:
{
"foo --bar",
Expand Down