1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package cobra
19
20 import (
21 "fmt"
22 "io"
23 "os"
24 "reflect"
25 "strconv"
26 "strings"
27 "text/template"
28 "time"
29 "unicode"
30 )
31
32 var templateFuncs = template.FuncMap{
33 "trim": strings.TrimSpace,
34 "trimRightSpace": trimRightSpace,
35 "trimTrailingWhitespaces": trimRightSpace,
36 "appendIfNotPresent": appendIfNotPresent,
37 "rpad": rpad,
38 "gt": Gt,
39 "eq": Eq,
40 }
41
42 var initializers []func()
43 var finalizers []func()
44
45 const (
46 defaultPrefixMatching = false
47 defaultCommandSorting = true
48 defaultCaseInsensitive = false
49 defaultTraverseRunHooks = false
50 )
51
52
53
54
55 var EnablePrefixMatching = defaultPrefixMatching
56
57
58
59 var EnableCommandSorting = defaultCommandSorting
60
61
62 var EnableCaseInsensitive = defaultCaseInsensitive
63
64
65
66 var EnableTraverseRunHooks = defaultTraverseRunHooks
67
68
69
70
71
72 var MousetrapHelpText = `This is a command line tool.
73
74 You need to open cmd.exe and run it from there.
75 `
76
77
78
79
80
81 var MousetrapDisplayDuration = 5 * time.Second
82
83
84
85 func AddTemplateFunc(name string, tmplFunc interface{}) {
86 templateFuncs[name] = tmplFunc
87 }
88
89
90
91 func AddTemplateFuncs(tmplFuncs template.FuncMap) {
92 for k, v := range tmplFuncs {
93 templateFuncs[k] = v
94 }
95 }
96
97
98
99 func OnInitialize(y ...func()) {
100 initializers = append(initializers, y...)
101 }
102
103
104
105 func OnFinalize(y ...func()) {
106 finalizers = append(finalizers, y...)
107 }
108
109
110
111
112
113
114 func Gt(a interface{}, b interface{}) bool {
115 var left, right int64
116 av := reflect.ValueOf(a)
117
118 switch av.Kind() {
119 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
120 left = int64(av.Len())
121 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
122 left = av.Int()
123 case reflect.String:
124 left, _ = strconv.ParseInt(av.String(), 10, 64)
125 }
126
127 bv := reflect.ValueOf(b)
128
129 switch bv.Kind() {
130 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
131 right = int64(bv.Len())
132 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
133 right = bv.Int()
134 case reflect.String:
135 right, _ = strconv.ParseInt(bv.String(), 10, 64)
136 }
137
138 return left > right
139 }
140
141
142
143
144 func Eq(a interface{}, b interface{}) bool {
145 av := reflect.ValueOf(a)
146 bv := reflect.ValueOf(b)
147
148 switch av.Kind() {
149 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
150 panic("Eq called on unsupported type")
151 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
152 return av.Int() == bv.Int()
153 case reflect.String:
154 return av.String() == bv.String()
155 }
156 return false
157 }
158
159 func trimRightSpace(s string) string {
160 return strings.TrimRightFunc(s, unicode.IsSpace)
161 }
162
163
164
165
166 func appendIfNotPresent(s, stringToAppend string) string {
167 if strings.Contains(s, stringToAppend) {
168 return s
169 }
170 return s + " " + stringToAppend
171 }
172
173
174 func rpad(s string, padding int) string {
175 formattedString := fmt.Sprintf("%%-%ds", padding)
176 return fmt.Sprintf(formattedString, s)
177 }
178
179
180 func tmpl(w io.Writer, text string, data interface{}) error {
181 t := template.New("top")
182 t.Funcs(templateFuncs)
183 template.Must(t.Parse(text))
184 return t.Execute(w, data)
185 }
186
187
188 func ld(s, t string, ignoreCase bool) int {
189 if ignoreCase {
190 s = strings.ToLower(s)
191 t = strings.ToLower(t)
192 }
193 d := make([][]int, len(s)+1)
194 for i := range d {
195 d[i] = make([]int, len(t)+1)
196 }
197 for i := range d {
198 d[i][0] = i
199 }
200 for j := range d[0] {
201 d[0][j] = j
202 }
203 for j := 1; j <= len(t); j++ {
204 for i := 1; i <= len(s); i++ {
205 if s[i-1] == t[j-1] {
206 d[i][j] = d[i-1][j-1]
207 } else {
208 min := d[i-1][j]
209 if d[i][j-1] < min {
210 min = d[i][j-1]
211 }
212 if d[i-1][j-1] < min {
213 min = d[i-1][j-1]
214 }
215 d[i][j] = min + 1
216 }
217 }
218
219 }
220 return d[len(s)][len(t)]
221 }
222
223 func stringInSlice(a string, list []string) bool {
224 for _, b := range list {
225 if b == a {
226 return true
227 }
228 }
229 return false
230 }
231
232
233 func CheckErr(msg interface{}) {
234 if msg != nil {
235 fmt.Fprintln(os.Stderr, "Error:", msg)
236 os.Exit(1)
237 }
238 }
239
240
241 func WriteStringAndCheck(b io.StringWriter, s string) {
242 _, err := b.WriteString(s)
243 CheckErr(err)
244 }
245
View as plain text