Skip to content

Commit d180093

Browse files
authored
Merge pull request #5 from bbrks/add-prefix-and-suffix-support
Add prefix and suffix support
2 parents c8c0b2c + e105c5b commit d180093

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

wrap.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,46 @@ type Wrapper struct {
1717
// Newline defines which characters should be used to split and create new lines.
1818
// Default: "\n"
1919
Newline string
20+
21+
// OutputLinePrefix is prepended to any output lines. This can be useful
22+
// for wrapping code-comments and prefixing new lines with "// ".
23+
// Default: ""
24+
OutputLinePrefix string
25+
26+
// OutputLineSuffix is appended to any output lines.
27+
// Default: ""
28+
OutputLineSuffix string
29+
30+
// LimitIncludesPrefixSuffix can be set to false if you don't want prefixes
31+
// and suffixes to be included in the length limits.
32+
// Default: true
33+
LimitIncludesPrefixSuffix bool
34+
35+
// TrimPrefix can be set to remove a prefix on each input line.
36+
// This can be paired up with OutputPrefix to create a block of C-style
37+
// comments (/* * */ ) from a long single-line comment.
38+
// Default: ""
39+
TrimInputPrefix string
40+
41+
// TrimSuffix can be set to remove a suffix on each input line.
42+
// Default: ""
43+
TrimInputSuffix string
2044
}
2145

2246
// NewWrapper returns a new instance of a Wrapper initialised with defaults.
2347
func NewWrapper() Wrapper {
2448
return Wrapper{
25-
Breakpoints: defaultBreakpoints,
26-
Newline: defaultNewline,
49+
Breakpoints: defaultBreakpoints,
50+
Newline: defaultNewline,
51+
LimitIncludesPrefixSuffix: true,
2752
}
2853
}
2954

3055
// line will wrap a single line of text at the given length.
31-
// If limit is less than 1, the string remains unchanged.
56+
// If limit is less than 1, the string remains unwrapped.
3257
func (w Wrapper) line(s string, limit int) string {
3358
if limit < 1 || len(s) < limit {
34-
return s
59+
return w.OutputLinePrefix + s + w.OutputLineSuffix
3560
}
3661

3762
// Find the index of the last breakpoint within the limit.
@@ -42,20 +67,30 @@ func (w Wrapper) line(s string, limit int) string {
4267
i = strings.IndexAny(s, w.Breakpoints)
4368
// Nothing left to do!
4469
if i < 0 {
45-
return s
70+
return w.OutputLinePrefix + s + w.OutputLineSuffix
4671
}
4772
}
4873

4974
// Recurse until we have nothing left to do.
50-
return s[:i] + w.Newline + w.line(s[i+1:], limit)
75+
return w.OutputLinePrefix + s[:i] + w.OutputLineSuffix + w.Newline + w.line(s[i+1:], limit)
5176
}
5277

5378
// Wrap will wrap one or more lines of text at the given length.
54-
// If limit is less than 1, the string remains unchanged.
79+
// If limit is less than 1, the string remains unwrapped.
5580
func (w Wrapper) Wrap(s string, limit int) string {
81+
82+
// Subtract the length of the prefix and suffix from the limit
83+
// so we don't break length limits when using them.
84+
if w.LimitIncludesPrefixSuffix {
85+
limit -= len(w.OutputLinePrefix) + len(w.OutputLineSuffix)
86+
}
87+
5688
var ret string
5789
for _, str := range strings.Split(s, w.Newline) {
90+
str = strings.TrimPrefix(str, w.TrimInputPrefix)
91+
str = strings.TrimSuffix(str, w.TrimInputSuffix)
5892
ret += w.line(str, limit) + w.Newline
5993
}
94+
6095
return ret
6196
}

wrap_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var loremIpsums = []string{
2121
"Curabitur tellus libero, feugiat vel mauris et, consequat auctor ipsum. Praesent sed pharetra dolor, at convallis lectus. Vivamus at ullamcorper sem. Sed euismod vel massa a dignissim. Proin auctor nibh at pretium facilisis. Ut aliquam erat lacus. Integer sit amet magna urna. Maecenas bibendum pretium mauris convallis semper. Nunc arcu tortor, pulvinar quis eros ut, mattis placerat tortor. Sed et lacus magna. Proin ultrices fermentum sem et placerat. Donec eget sapien mi. Maecenas maximus justo sed vulputate pulvinar. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum accumsan, sapien sit amet suscipit dignissim, velit velit maximus elit, a cursus mi odio eu magna. Nunc nec fermentum nisi, non imperdiet purus.",
2222
"Vestibulum convallis magna arcu, sagittis porta mi luctus sit amet. Nunc tellus magna, fermentum et mi vitae, consectetur vestibulum nulla. Fusce ornare, augue vitae tempor pellentesque, orci orci fringilla tortor, porta feugiat justo purus nec sem. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nulla pellentesque sed odio in aliquam. Fusce sed molestie velit. Curabitur id quam ac felis accumsan vehicula quis in ex.",
2323
"Duis ac ornare erat. Nulla in odio eget ante tristique dignissim a non erat. Sed non nisi vitae arcu dapibus porta vitae dignissim ante. Cras et fringilla turpis. Maecenas arcu nibh, tempus euismod pretium eget, hendrerit vitae arcu. Sed vel dolor quam. Etiam consequat sed dolor ut elementum. Quisque dictum tempor pretium. Sed eu sollicitudin mi, in commodo ante.",
24+
"",
2425
}
2526

2627
func TestWrapper_Wrap(t *testing.T) {
@@ -229,3 +230,24 @@ The girl was accident-prone, good-looking, quick-thinking, carbon-neutral, bad-t
229230
// The girl was accident-prone, good-looking, quick-thinking, carbon-neutral, bad
230231
// tempered, sport-mad, fair-haired, camera-ready, and finally open-mouthed.
231232
}
233+
234+
func ExampleWrapper_Wrap_prefix() {
235+
var loremIpsum = "/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vulputate quam nibh, et faucibus enim gravida vel. Integer bibendum lectus et erat semper fermentum quis a risus. Fusce dignissim tempus metus non pretium. Nunc sagittis magna nec purus porttitor mollis. Pellentesque feugiat quam eget laoreet aliquet. Donec gravida congue massa, et sollicitudin turpis lacinia a. Fusce non tortor magna. Cras vel finibus tellus. */"
236+
237+
// Trim the single-line block comment symbols from each input line.
238+
w.TrimInputPrefix = "/* "
239+
w.TrimInputSuffix = " */"
240+
241+
// Prefix each new line with a single-line comment symbol.
242+
w.OutputLinePrefix = "// "
243+
244+
// Wrap when lines exceed 80 chars.
245+
fmt.Println(w.Wrap(loremIpsum, 80))
246+
// Output:
247+
// // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vulputate quam
248+
// // nibh, et faucibus enim gravida vel. Integer bibendum lectus et erat semper
249+
// // fermentum quis a risus. Fusce dignissim tempus metus non pretium. Nunc
250+
// // sagittis magna nec purus porttitor mollis. Pellentesque feugiat quam eget
251+
// // laoreet aliquet. Donec gravida congue massa, et sollicitudin turpis lacinia
252+
// // a. Fusce non tortor magna. Cras vel finibus tellus.
253+
}

0 commit comments

Comments
 (0)