Skip to content

Commit 913e6f4

Browse files
committed
Preserve hypen when breaking a word - Fixes #16
1 parent 0d85d98 commit 913e6f4

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

example_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,21 @@ func ExampleWrapper_Wrap_short() {
197197
}
198198

199199
func ExampleWrapper_Wrap_hyphens() {
200-
var loremIpsum = `
201-
In this particular example, I will spam a lot of hyphenated words, which should wrap at some point, and test the multi-breakpoint feature of this package.
202-
203-
The girl was accident-prone, good-looking, quick-thinking, carbon-neutral, bad-tempered, sport-mad, fair-haired, camera-ready, and finally open-mouthed.
204-
`
200+
var text = `My sister-in-law bought a state-of-the-art, energy-efficient, top-rated washing machine from a well-known, highly-recommended manufacturer based in the north-east. It was a last-minute, spur-of-the-moment decision, but the twenty-year guarantee and the user-friendly, self-cleaning features made it a no-brainer.`
205201

206202
w := wrap.NewWrapper()
207203

208-
fmt.Println(w.Wrap(loremIpsum, 80))
204+
fmt.Println(w.Wrap(text, 40))
209205
// Output:
210-
// In this particular example, I will spam a lot of hyphenated words, which should
211-
// wrap at some point, and test the multi-breakpoint feature of this package.
212-
//
213-
// The girl was accident-prone, good-looking, quick-thinking, carbon-neutral, bad
214-
// tempered, sport-mad, fair-haired, camera-ready, and finally open-mouthed.
206+
// My sister-in-law bought a state-of-the-
207+
// art, energy-efficient, top-rated washing
208+
// machine from a well-known, highly-
209+
// recommended manufacturer based in the
210+
// north-east. It was a last-minute, spur-
211+
// of-the-moment decision, but the twenty-
212+
// year guarantee and the user-friendly,
213+
// self-cleaning features made it a no-
214+
// brainer.
215215
}
216216

217217
func ExampleWrapper_Wrap_prefix() {

wrapper.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (w Wrapper) lineBuilder(sb *strings.Builder, s string, limit int) {
140140
i := strings.LastIndexAny(s[:limitByteIndex], w.Breakpoints)
141141

142142
breakpointWidth := 1
143+
keepBreakpoint := false
143144

144145
// Can't wrap within the limit
145146
if i < 0 {
@@ -160,9 +161,18 @@ func (w Wrapper) lineBuilder(sb *strings.Builder, s string, limit int) {
160161
}
161162
}
162163

163-
// Write this line (trim trailing breakpoints) and recurse
164+
// Non-space breakpoints (like hyphen) should stay on the line
165+
if breakpointWidth > 0 && s[i] != ' ' {
166+
keepBreakpoint = true
167+
}
168+
169+
// Write this line and recurse
164170
sb.WriteString(w.OutputLinePrefix)
165-
sb.WriteString(strings.TrimRight(s[:i], w.Breakpoints))
171+
lineContent := s[:i]
172+
if keepBreakpoint {
173+
lineContent = s[:i+1]
174+
}
175+
sb.WriteString(strings.TrimRight(lineContent, " "))
166176
sb.WriteString(w.OutputLineSuffix)
167177
sb.WriteString(w.Newline)
168178

wrapper_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ func TestWrapper_Breakpoints(t *testing.T) {
112112
expected string
113113
}{
114114
{"space only", "hello world foo bar", " ", 10, "hello\nworld foo\nbar\n"},
115-
{"hyphen only", "hello-world-foo-bar", "-", 12, "hello-world\nfoo-bar\n"},
116-
{"custom breakpoint", "hello|world|foo|bar", "|", 12, "hello|world\nfoo|bar\n"},
117-
{"multiple custom breakpoints", "hello|world,foo;bar", "|,;", 12, "hello|world\nfoo;bar\n"},
115+
{"hyphen only", "hello-world-foo-bar", "-", 12, "hello-world-\nfoo-bar\n"},
116+
{"custom breakpoint", "hello|world|foo|bar", "|", 12, "hello|world|\nfoo|bar\n"},
117+
{"multiple custom breakpoints", "hello|world,foo;bar", "|,;", 12, "hello|world,\nfoo;bar\n"},
118118
{"no breakpoints found", "helloworldfoobar", " ", 10, "helloworldfoobar\n"},
119119
}
120120

0 commit comments

Comments
 (0)