-
Notifications
You must be signed in to change notification settings - Fork 1
/
wordwrap.go
124 lines (100 loc) · 2.69 KB
/
wordwrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package wordwrap
import (
"bytes"
"strings"
)
// Indent a string with the given prefix at the start of either the first, or all lines.
//
// input - The input string to indent.
// prefix - The prefix to add.
// prefixAll - If true, prefix all lines with the given prefix.
//
// Example usage:
//
// indented := wordwrap.Indent("Hello\nWorld", "-", true)
func Indent(input string, prefix string, prefixAll bool) string {
lines := strings.Split(input, "\n")
prefixLen := len(prefix)
result := make([]string, len(lines))
for i, line := range lines {
if prefixAll || i == 0 {
result[i] = prefix + line
} else {
result[i] = strings.Repeat(" ", prefixLen) + line
}
}
return strings.Join(result, "\n")
}
// WrapperFunc takes a given input string, and returns some wrapped output. The wrapping may
// be altered by currying the wrapper function.
type WrapperFunc func(string) string
// Wrapper creates a curried wrapper function (see WrapperFunc) with the given options applied to
// it. Create a WrapperFunc and store it is a variable, then re-use it elsewhere.
//
// limit - The maximum number of characters for a line.
// breakWords - Whether or not to break long words onto new lines.
//
// Example usage:
//
// wrapper := wordwrap.Wrapper(10, false)
// wrapped := wrapper("This string would be split onto several new lines")
func Wrapper(limit int, breakWords bool) WrapperFunc {
if limit < 1 {
panic("Wrapper limit cannot be less than 1.")
}
return func(input string) string {
var wrapped string
// Split string into array of words
words := strings.Fields(input)
if len(words) == 0 {
return wrapped
}
remaining := limit
if breakWords {
words = doBreakWords(words, limit)
}
for _, word := range words {
if len(word)+1 > remaining {
if len(wrapped) > 0 {
wrapped += "\n"
}
wrapped += word
remaining = limit - len(word)
} else {
if len(wrapped) > 0 {
wrapped += " "
}
wrapped += word
remaining = remaining - (len(word) + 1)
}
}
return wrapped
}
}
// Break up any words in a given array of words that exceed the given limit.
func doBreakWords(words []string, limit int) []string {
var result []string
for _, word := range words {
if len(word) > limit {
var parts []string
var partBuf bytes.Buffer
for _, char := range word {
atLimit := partBuf.Len() == limit
if atLimit {
parts = append(parts, partBuf.String())
partBuf.Reset()
}
partBuf.WriteRune(char)
}
if partBuf.Len() > 0 {
parts = append(parts, partBuf.String())
}
for _, part := range parts {
result = append(result, part)
}
} else {
result = append(result, word)
}
}
return result
}