-
Notifications
You must be signed in to change notification settings - Fork 0
/
brimtext.go
263 lines (246 loc) · 7.5 KB
/
brimtext.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Package brimtext contains tools for working with text. Probably the most
// complex of these tools is Align, which allows for formatting "pretty
// tables".
package brimtext
import (
"bytes"
"fmt"
"math"
"strconv"
"strings"
"unicode/utf8"
)
// OrdinalSuffix returns "st", "nd", "rd", etc. for the number given (1st, 2nd,
// 3rd, etc.).
func OrdinalSuffix(number int) string {
if (number/10)%10 == 1 || number%10 > 3 {
return "th"
} else if number%10 == 1 {
return "st"
} else if number%10 == 2 {
return "nd"
} else if number%10 == 3 {
return "rd"
}
return "th"
}
// ThousandsSep returns the number formatted using the separator at each
// thousands position, such as ThousandsSep(1234567, ",") giving 1,234,567.
func ThousandsSep(v int64, sep string) string {
s := strconv.FormatInt(v, 10)
for i := len(s) - 3; i > 0; i -= 3 {
s = s[:i] + "," + s[i:]
}
return s
}
// ThousandsSepU returns the number formatted using the separator at each
// thousands position, such as ThousandsSepU(1234567, ",") giving 1,234,567.
func ThousandsSepU(v uint64, sep string) string {
s := strconv.FormatUint(v, 10)
for i := len(s) - 3; i > 0; i -= 3 {
s = s[:i] + "," + s[i:]
}
return s
}
// HumanSize returns a more readable size format. Quick, "standard"
// implementations are HumanSize1000 and HumanSize1024, but this more generic
// function is provided so you can tweak the output a bit more.
//
// Here are the implementations of the two standard functions, to get an idea
// of how you might wish to use HumanSize directly.
//
// // HumanSize1000 returns a more readable size format, such as
// // HumanSize1000(1234567) giving "1.23m".
// // These are 1,000 unit based: 1k = 1000, 1m = 1000000, etc.
// func HumanSize1000(v float64) string {
// return HumanSize(v, 1000, []string{"", "k", "m", "g", "t", "p", "e", "z", "y"})
// }
//
// // HumanSize1024 returns a more readable size format, such as
// // HumanSize1024(1234567) giving "1.18M".
// // These are 1,024 unit based: 1K = 1024, 1M = 1048576, etc.
// func HumanSize1024(v float64) string {
// return HumanSize(v, 1024, []string{"", "K", "M", "G", "T", "P", "E", "Z", "Y"})
// }
func HumanSize(v float64, u float64, s []string) string {
n := v
i := 0
for ; i < len(s); i++ {
if math.Ceil(n) < 1000 {
break
}
n = n / u
}
if i >= len(s) {
return fmt.Sprintf("%.0f%s", n*u, s[len(s)-1])
}
if i == 0 {
return fmt.Sprintf("%.4g", n)
}
if n < 1 {
return fmt.Sprintf("%.2g%s", n, s[i])
}
return fmt.Sprintf("%.3g%s", n, s[i])
}
// HumanSize1000 returns a more readable size format, such as
// HumanSize1000(1234567) giving "1.23m".
// These are 1,000 unit based: 1k = 1000, 1m = 1000000, etc.
func HumanSize1000(v float64) string {
return HumanSize(v, 1000, []string{"", "k", "m", "g", "t", "p", "e", "z", "y"})
}
// HumanSize1024 returns a more readable size format, such as
// HumanSize1024(1234567) giving "1.18M".
// These are 1,024 unit based: 1K = 1024, 1M = 1048576, etc.
func HumanSize1024(v float64) string {
return HumanSize(v, 1024, []string{"", "K", "M", "G", "T", "P", "E", "Z", "Y"})
}
// Sentence converts the value into a sentence, uppercasing the first character
// and ensuring the string ends with a period. Useful to output better looking
// error.Error() messages, which are all lower case with no trailing period by
// convention.
func Sentence(value string) string {
if value != "" {
if value[len(value)-1] != '.' {
value = strings.ToUpper(value[:1]) + value[1:] + "."
} else {
value = strings.ToUpper(value[:1]) + value[1:]
}
}
return value
}
// StringSliceToLowerSort provides a sort.Interface that will sort a []string
// by their strings.ToLower values. This isn't exactly a case insensitive sort
// due to Unicode situations, but is usually good enough.
type StringSliceToLowerSort []string
func (s StringSliceToLowerSort) Len() int {
return len(s)
}
func (s StringSliceToLowerSort) Swap(x int, y int) {
s[x], s[y] = s[y], s[x]
}
func (s StringSliceToLowerSort) Less(x int, y int) bool {
return strings.ToLower(s[x]) < strings.ToLower(s[y])
}
// Wrap wraps text for more readable output.
//
// The width can be a positive int for a specific width, 0 for the default
// width (attempted to get from terminal, 79 otherwise), or a negative number
// for a width relative to the default.
//
// The indent1 is the prefix for the first line.
//
// The indent2 is the prefix for any second or subsequent lines.
func Wrap(text string, width int, indent1 string, indent2 string) string {
if width < 1 {
width = GetTTYWidth() - 1 + width
}
bs := []byte(text)
bs = wrap(bs, width, []byte(indent1), []byte(indent2))
return string(bytes.Trim(bs, "\n"))
}
func wrap(text []byte, width int, indent1 []byte, indent2 []byte) []byte {
if utf8.RuneCount(text) == 0 {
return text
}
text = bytes.Replace(text, []byte{'\r', '\n'}, []byte{'\n'}, -1)
var out bytes.Buffer
for _, par := range bytes.Split([]byte(text), []byte{'\n', '\n'}) {
par = bytes.Replace(par, []byte{'\n'}, []byte{' '}, -1)
lineLen := 0
start := true
for _, word := range bytes.Split(par, []byte{' '}) {
wordLen := utf8.RuneCount(word)
if wordLen == 0 {
continue
}
scan := word
for len(scan) > 1 {
i := bytes.IndexByte(scan, '\x1b')
if i == -1 {
break
}
j := bytes.IndexByte(scan[i+1:], 'm')
if j != -1 {
j += 2
wordLen -= j
scan = scan[i+j:]
}
}
if start {
out.Write(indent1)
lineLen += utf8.RuneCount(indent1)
out.Write(word)
lineLen += wordLen
start = false
} else if lineLen+1+wordLen > width {
out.WriteByte('\n')
out.Write(indent2)
out.Write(word)
lineLen = utf8.RuneCount(indent2) + wordLen
} else {
out.WriteByte(' ')
out.Write(word)
lineLen += 1 + wordLen
}
}
out.WriteByte('\n')
out.WriteByte('\n')
}
return out.Bytes()
}
// AllEqual returns true if all the values are equal strings; no strings,
// AllEqual() or AllEqual([]string{}...), are considered AllEqual.
func AllEqual(values ...string) bool {
if len(values) < 2 {
return true
}
compare := values[0]
for _, v := range values[1:] {
if v != compare {
return false
}
}
return true
}
// TrueString returns true if the string contains a recognized true value, such
// as "true", "True", "TRUE", "yes", "on", etc. Yes, there is already
// strconv.ParseBool, but this function is often easier to work with since it
// just returns true or false instead of (bool, error) like ParseBool does. If
// you need to differentiate between true, false, and unknown, ParseBool should
// be your choice. Although I suppose you could use TrueString(s),
// FalseString(s), and !TrueString(s) && !FalseString(s).
func TrueString(value string) bool {
v := strings.ToLower(value)
switch v {
case "true":
return true
case "yes":
return true
case "on":
return true
case "1":
return true
}
return false
}
// FalseString returns true if the string contains a recognized false value,
// such as "false", "False", "FALSE", "no", "off", etc. Yes, there is already
// strconv.ParseBool, but this function is often easier to work with since it
// just returns true or false instead of (bool, error) like ParseBool does. If
// you need to differentiate between true, false, and unknown, ParseBool should
// be your choice. Although I suppose you could use TrueString(s),
// FalseString(s), and !TrueString(s) && !FalseString(s).
func FalseString(value string) bool {
v := strings.ToLower(value)
switch v {
case "false":
return true
case "no":
return true
case "off":
return true
case "0":
return true
}
return false
}