@@ -46,9 +46,12 @@ type Wrapper struct {
4646 TrimInputSuffix string
4747
4848 // StripTrailingNewline can be set to true if you want the trailing
49- // newline to be removed from the return vailue .
49+ // newline to be removed from the return value .
5050 // Default: false
5151 StripTrailingNewline bool
52+
53+ // CutLongWords will cause a hard-wrap in the middle of a word if the word's length exceeds the given limit.
54+ CutLongWords bool
5255}
5356
5457// NewWrapper returns a new instance of a Wrapper initialised with defaults.
@@ -98,15 +101,24 @@ func (w Wrapper) line(s string, limit int) string {
98101 // Find the index of the last breakpoint within the limit.
99102 i := strings .LastIndexAny (s [:limit + 1 ], w .Breakpoints )
100103
101- // Can't wrap within the limit, wrap at the next breakpoint instead.
104+ breakpointWidth := 1
105+
106+ // Can't wrap within the limit
102107 if i < 0 {
103- i = strings .IndexAny (s , w .Breakpoints )
104- // Nothing left to do!
105- if i < 0 {
106- return w .OutputLinePrefix + s + w .OutputLineSuffix
108+ if w .CutLongWords {
109+ // wrap at the limit
110+ i = limit
111+ breakpointWidth = 0
112+ } else {
113+ // wrap at the next breakpoint instead
114+ i = strings .IndexAny (s , w .Breakpoints )
115+ // Nothing left to do!
116+ if i < 0 {
117+ return w .OutputLinePrefix + s + w .OutputLineSuffix
118+ }
107119 }
108120 }
109121
110122 // Recurse until we have nothing left to do.
111- return w .OutputLinePrefix + s [:i ] + w .OutputLineSuffix + w .Newline + w .line (s [i + 1 :], limit )
123+ return w .OutputLinePrefix + s [:i ] + w .OutputLineSuffix + w .Newline + w .line (s [i + breakpointWidth :], limit )
112124}
0 commit comments