Skip to content

Commit

Permalink
Add CutLongWords option to Wrapper (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrks committed Jan 2, 2021
1 parent e0dddf3 commit 11ff934
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
26 changes: 19 additions & 7 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ type Wrapper struct {
TrimInputSuffix string

// StripTrailingNewline can be set to true if you want the trailing
// newline to be removed from the return vailue.
// newline to be removed from the return value.
// Default: false
StripTrailingNewline bool

// CutLongWords will cause a hard-wrap in the middle of a word if the word's length exceeds the given limit.
CutLongWords bool
}

// NewWrapper returns a new instance of a Wrapper initialised with defaults.
Expand Down Expand Up @@ -98,15 +101,24 @@ func (w Wrapper) line(s string, limit int) string {
// Find the index of the last breakpoint within the limit.
i := strings.LastIndexAny(s[:limit+1], w.Breakpoints)

// Can't wrap within the limit, wrap at the next breakpoint instead.
breakpointWidth := 1

// Can't wrap within the limit
if i < 0 {
i = strings.IndexAny(s, w.Breakpoints)
// Nothing left to do!
if i < 0 {
return w.OutputLinePrefix + s + w.OutputLineSuffix
if w.CutLongWords {
// wrap at the limit
i = limit
breakpointWidth = 0
} else {
// wrap at the next breakpoint instead
i = strings.IndexAny(s, w.Breakpoints)
// Nothing left to do!
if i < 0 {
return w.OutputLinePrefix + s + w.OutputLineSuffix
}
}
}

// Recurse until we have nothing left to do.
return w.OutputLinePrefix + s[:i] + w.OutputLineSuffix + w.Newline + w.line(s[i+1:], limit)
return w.OutputLinePrefix + s[:i] + w.OutputLineSuffix + w.Newline + w.line(s[i+breakpointWidth:], limit)
}
42 changes: 42 additions & 0 deletions wrapper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wrap_test

import (
"strconv"
"strings"
"testing"
"unicode/utf8"
Expand Down Expand Up @@ -63,3 +64,44 @@ func TestWrapper_Wrap(t *testing.T) {

}
}

func TestWrapper_Wrap_CutLongWords(t *testing.T) {
const limit = 8

tests := []struct {
input string
expected string
}{
{
input: "A short woooord",
expected: `A short
woooord`,
},
{
input: "A perfect wooooord",
expected: `A
perfect
wooooord`,
},
{
input: "A long wooooooooooooord",
expected: `A long
wooooooo
oooooord`,
},
}

for i, test := range tests {
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) {
w := wrap.NewWrapper()
w.CutLongWords = true
w.StripTrailingNewline = true

actual := w.Wrap(test.input, limit)

if actual != test.expected {
t.Errorf("expected %q but got %q", test.expected, actual)
}
})
}
}

0 comments on commit 11ff934

Please sign in to comment.