-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
381 lines (315 loc) · 7.46 KB
/
utils.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package tooey
import (
"fmt"
"math"
"reflect"
"unicode"
"github.com/davecgh/go-spew/spew"
rw "github.com/mattn/go-runewidth"
)
// InterfaceSlice takes an []interface{} represented as an interface{} and converts it
// https://stackoverflow.com/questions/12753805/type-converting-slices-of-interfaces-in-go
func InterfaceSlice(slice interface{}) []interface{} {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic(fmt.Sprintf("InterfaceSlice() given a non-slice type: %v [%v]", s.Kind(), spew.Sdump(slice)))
}
ret := make([]interface{}, s.Len())
for i := 0; i < s.Len(); i++ {
ret[i] = s.Index(i).Interface()
}
return ret
}
// TrimString trims a string to a max length and adds '…' to the end if it was trimmed.
func TrimString(s string, w int) string {
if w <= 0 {
return ""
}
if rw.StringWidth(s) > w {
return rw.Truncate(s, w, string(ELLIPSES))
}
return s
}
// ShiftRuneSliceRight takes a []rune and shifts everything right, wrapping the right
// most character back to the left most position
func ShiftRuneSliceRight(slice []rune) []rune {
if len(slice) < 1 {
return slice
}
contentLength := len(slice)
newSlice := make([]rune, 0)
newSlice = append(newSlice, slice[contentLength-1])
for i, r := range slice {
if i == contentLength-1 {
break
}
newSlice = append(newSlice, r)
}
return newSlice
}
// ShiftRuneSliceLeft takes a []rune and shifts everything left, wrapping the
// right most character back to the right most position
func ShiftRuneSliceLeft(slice []rune) []rune {
if len(slice) < 1 {
return slice
}
newSlice := append(slice[1:], slice[0])
return newSlice
}
// ShiftRuneWhitespaceToLeft takes a []rune and moves all whitespace to the left
func ShiftRuneWhitespaceToLeft(slice []rune) []rune {
if len(slice) < 1 {
return slice
}
contentLength := len(slice)
newSlice := slice
if !ContainsNonWhitespace(slice) {
return slice
}
for {
if unicode.IsSpace(newSlice[contentLength-1]) {
newSlice = ShiftRuneSliceRight(newSlice)
} else {
break
}
}
return newSlice
}
// ShiftRuneWhitespaceToRight takes a []rune and moves all whitespace to the right
func ShiftRuneWhitespaceToRight(slice []rune) []rune {
if !ContainsNonWhitespace(slice) || len(slice) < 1 {
return slice
}
newSlice := slice
for {
if unicode.IsSpace(newSlice[0]) {
newSlice = ShiftRuneSliceLeft(newSlice)
} else {
break
}
}
return newSlice
}
/*
SpreadWhitespaceAcrossSliceInterior takes a []rune
and attempts to distribute it's whitespace across the
width of the slice interior but not at the outside edges
Take the string "abc def gh ij "
"abc_def___gh_ij___" would try to make "abc___def__gh___ij"
"__abc_def___gh_ij" would try to make "abc__def__gh___ij"
*/
func SpreadWhitespaceAcrossSliceInterior(slice []rune) []rune {
wordCount := CountWordsInRuneSlice(slice)
if !ContainsNonWhitespace(slice) || len(slice) < 1 || wordCount < 2 {
return slice
}
// Shift all right whitespace to the left
newSlice := ShiftRuneWhitespaceToLeft(slice)
// Move left whitespace inwards
newSlice = NormalizeLeftWhitespace(newSlice)
//newSlice = NormalizeLeftWhitespace(newSlice)
return newSlice
}
// CheckWhichPositionHasFewest returns index of the position with the lowest
// count
func CheckWhichPositionHasFewest(positions []int) int {
lowestIndex := 0
lowestCount, _ := GetMaxIntFromSlice(positions)
if lowestCount == 0 {
// TODO: figure out what to do here, tighten this up
// I mean it works for now but will we find a condition when it doesn't
return 0
}
for i, count := range positions {
if count < lowestCount {
lowestCount = count
lowestIndex = i
}
}
return lowestIndex
}
func NormalizeLeftWhitespace(slice []rune) []rune {
wordCount := CountWordsInRuneSlice(slice)
if !ContainsNonWhitespace(slice) || len(slice) < 1 || wordCount < 2 {
return slice
}
newSlice := slice
position := make([]int, wordCount)
OUTER:
for {
insideWord := false
//interior := false
if unicode.IsSpace(newSlice[0]) {
wordIndex := 0
for i, r := range newSlice {
bestIndex := CheckWhichPositionHasFewest(position)
// range through the runes and detect words
// place left spaces throughout the rune slice between words interior
if !unicode.IsSpace(r) {
if !insideWord {
insideWord = true
}
} else {
if insideWord {
insideWord = false
wordIndex++
}
}
if i == len(newSlice) {
// if we reached the end without finding a spot
// give up continue
break OUTER
}
if bestIndex == wordIndex {
if unicode.IsSpace(r) {
newSlice = newSlice[1:]
newSlice = append(newSlice[:i+1], newSlice[i:]...)
newSlice[i] = rune(' ')
position[wordIndex]++
break
}
}
}
} else {
break
}
}
return newSlice
}
// CountWordsInRuneSlice counts how many blocks of non-whitespace characters
// are inside the rune
//
// This can be used to traverse whitespace between the left and right boundaries
// use count-1 for right bound to prevent traversing outside of the word bounds
func CountWordsInRuneSlice(slice []rune) int {
count := 0
insideWord := false
for _, r := range slice {
if !unicode.IsSpace(r) {
if !insideWord {
insideWord = true
count++
}
} else {
if insideWord {
insideWord = false
}
}
}
return count
}
// CountWhiteSpace returns a count of the number of space characters in a []rune
func CountWhiteSpace(slice []rune) int {
count := 0
for _, r := range slice {
if unicode.IsSpace(r) {
count++
}
}
return count
}
// ContainsNonWhitespace returns true if a given rune slice has any non-whitespace
// runes
func ContainsNonWhitespace(slice []rune) bool {
for _, r := range slice {
if !unicode.IsSpace(r) {
return true
}
}
return false
}
func SelectColor(colors []Color, index int) Color {
return colors[index%len(colors)]
}
func SelectStyle(styles []Style, index int) Style {
return styles[index%len(styles)]
}
// Math ------------------------------------------------------------------------
func SumIntSlice(slice []int) int {
sum := 0
for _, val := range slice {
sum += val
}
return sum
}
func SumFloat64Slice(data []float64) float64 {
sum := 0.0
for _, v := range data {
sum += v
}
return sum
}
func GetMaxIntFromSlice(slice []int) (int, error) {
if len(slice) == 0 {
return 0, fmt.Errorf("cannot get max value from empty slice")
}
var max int
for _, val := range slice {
if val > max {
max = val
}
}
return max, nil
}
func GetMaxFloat64FromSlice(slice []float64) (float64, error) {
if len(slice) == 0 {
return 0, fmt.Errorf("cannot get max value from empty slice")
}
var max float64
for _, val := range slice {
if val > max {
max = val
}
}
return max, nil
}
func GetMaxFloat64From2dSlice(slices [][]float64) (float64, error) {
if len(slices) == 0 {
return 0, fmt.Errorf("cannot get max value from empty slice")
}
var max float64
for _, slice := range slices {
for _, val := range slice {
if val > max {
max = val
}
}
}
return max, nil
}
func RoundFloat64(x float64) float64 {
return math.Floor(x + 0.5)
}
func FloorFloat64(x float64) float64 {
return math.Floor(x)
}
func AbsInt(x int) int {
if x >= 0 {
return x
}
return -x
}
func MinFloat64(x, y float64) float64 {
if x < y {
return x
}
return y
}
func MaxFloat64(x, y float64) float64 {
if x > y {
return x
}
return y
}
func MaxInt(x, y int) int {
if x > y {
return x
}
return y
}
func MinInt(x, y int) int {
if x < y {
return x
}
return y
}