-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslices_flagValue_util.go
62 lines (53 loc) · 1.39 KB
/
slices_flagValue_util.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
package main
import (
"fmt"
"strconv"
"strings"
"unicode"
)
// Based on https://lawlessguy.wordpress.com/2013/07/23/filling-a-slice-using-command-line-flags-in-go-golang/ & https://stackoverflow.com/questions/28322997/how-to-get-a-list-of-values-into-a-flag-in-golang
// IntSlice is a slice of ints that implements the flag.Value interface.
type IntSlice []int
func (i *IntSlice) String() string {
return fmt.Sprintf("%d", *i)
}
// Set parses a flag value into the slice.
func (i *IntSlice) Set(value string) error {
tmp, err := strconv.Atoi(value)
if err != nil {
*i = append(*i, -1)
} else {
*i = append(*i, tmp)
}
return nil
}
// StringSlice is a slice of strings that implements the flag.Value interface.
type StringSlice []string
func (s *StringSlice) String() string {
b := strings.Builder{}
for _, v := range *s {
b.WriteString(ellipticalTruncate(v, 15))
b.WriteString(":")
}
return b.String()
}
// Set parses a flag value into the slice.
func (s *StringSlice) Set(value string) error {
*s = append(*s, value)
return nil
}
func ellipticalTruncate(text string, maxLen int) string {
// https://stackoverflow.com/questions/59955085/how-can-i-elliptically-truncate-text-in-golang
lastSpaceIx := maxLen
curLen := 0
for i, r := range text {
if unicode.IsSpace(r) {
lastSpaceIx = i
}
curLen++
if curLen > maxLen {
return text[:lastSpaceIx] + "…"
}
}
return text
}