-
Notifications
You must be signed in to change notification settings - Fork 7
/
coloredcobra.go
350 lines (305 loc) · 8.56 KB
/
coloredcobra.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
// ColoredCobra allows you to colorize Cobra's text output,
// making it look better using simple settings to customize
// individual parts of console output.
//
// Usage example:
//
// 1. Insert in cmd/root.go file of your project :
//
// import cc "github.com/ivanpirog/coloredcobra"
//
//
// 2. Put the following code to the beginning of the Execute() function:
//
// cc.Init(&cc.Config{
// RootCmd: rootCmd,
// Headings: cc.Bold + cc.Underline,
// Commands: cc.Yellow + cc.Bold,
// ExecName: cc.Bold,
// Flags: cc.Bold,
// })
//
//
// 3. Build & execute your code.
//
//
// Copyright © 2022 Ivan Pirog <[email protected]>.
// Released under the MIT license.
// Project home: https://github.com/ivanpirog/coloredcobra
//
package coloredcobra
import (
"regexp"
"strings"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
// Config is a settings structure which sets styles for individual parts of Cobra text output.
//
// Note that RootCmd is required.
//
// Example:
//
// c := &cc.Config{
// RootCmd: rootCmd,
// Headings: cc.HiWhite + cc.Bold + cc.Underline,
// Commands: cc.Yellow + cc.Bold,
// CmdShortDescr: cc.Cyan,
// ExecName: cc.Bold,
// Flags: cc.Bold,
// Aliases: cc.Bold,
// Example: cc.Italic,
// }
type Config struct {
RootCmd *cobra.Command
Headings uint8
Commands uint8
CmdShortDescr uint8
ExecName uint8
Flags uint8
FlagsDataType uint8
FlagsDescr uint8
Aliases uint8
Example uint8
NoExtraNewlines bool
NoBottomNewline bool
}
// Constants for colors and B, I, U
const (
None = 0
Black = 1
Red = 2
Green = 3
Yellow = 4
Blue = 5
Magenta = 6
Cyan = 7
White = 8
HiRed = 9
HiGreen = 10
HiYellow = 11
HiBlue = 12
HiMagenta = 13
HiCyan = 14
HiWhite = 15
Bold = 16
Italic = 32
Underline = 64
)
// Init patches Cobra's usage template with configuration provided.
func Init(cfg *Config) {
if cfg.RootCmd == nil {
panic("coloredcobra: Root command pointer is missing.")
}
// Get usage template
tpl := cfg.RootCmd.UsageTemplate()
//
// Add extra line breaks for headings
//
if cfg.NoExtraNewlines == false {
tpl = strings.NewReplacer(
"Usage:", "\nUsage:\n",
"Aliases:", "\nAliases:\n",
"Examples:", "\nExamples:\n",
"Available Commands:", "\nAvailable Commands:\n",
"Global Flags:", "\nGlobal Flags:\n",
"Additional help topics:", "\nAdditional help topics:\n",
"Use \"", "\nUse \"",
).Replace(tpl)
re := regexp.MustCompile(`(?m)^Flags:$`)
tpl = re.ReplaceAllString(tpl, "\nFlags:\n")
}
//
// Styling headers
//
if cfg.Headings != None {
ch := getColor(cfg.Headings)
// Add template function to style the headers
cobra.AddTemplateFunc("HeadingStyle", ch.SprintFunc())
// Wrap template headers into a new function
tpl = strings.NewReplacer(
"Usage:", `{{HeadingStyle "Usage:"}}`,
"Aliases:", `{{HeadingStyle "Aliases:"}}`,
"Examples:", `{{HeadingStyle "Examples:"}}`,
"Available Commands:", `{{HeadingStyle "Available Commands:"}}`,
"Global Flags:", `{{HeadingStyle "Global Flags:"}}`,
"Additional help topics:", `{{HeadingStyle "Additional help topics:"}}`,
).Replace(tpl)
re := regexp.MustCompile(`(?m)^(\s*)Flags:(\s*)$`)
tpl = re.ReplaceAllString(tpl, `$1{{HeadingStyle "Flags:"}}$2`)
}
//
// Styling commands
//
if cfg.Commands != None {
cc := getColor(cfg.Commands)
// Add template function to style commands
cobra.AddTemplateFunc("CommandStyle", cc.SprintFunc())
cobra.AddTemplateFunc("sum", func(a, b int) int {
return a + b
})
// Patch usage template
re := regexp.MustCompile(`(?i){{\s*rpad\s+.Name\s+.NamePadding\s*}}`)
tpl = re.ReplaceAllLiteralString(tpl, "{{rpad (CommandStyle .Name) (sum .NamePadding 12)}}")
re = regexp.MustCompile(`(?i){{\s*rpad\s+.CommandPath\s+.CommandPathPadding\s*}}`)
tpl = re.ReplaceAllLiteralString(tpl, "{{rpad (CommandStyle .CommandPath) (sum .CommandPathPadding 12)}}")
}
//
// Styling a short desription of commands
//
if cfg.CmdShortDescr != None {
csd := getColor(cfg.CmdShortDescr)
cobra.AddTemplateFunc("CmdShortStyle", csd.SprintFunc())
re := regexp.MustCompile(`(?ism)({{\s*range\s+.Commands\s*}}.*?){{\s*.Short\s*}}`)
tpl = re.ReplaceAllString(tpl, `$1{{CmdShortStyle .Short}}`)
}
//
// Styling executable file name
//
if cfg.ExecName != None {
cen := getColor(cfg.ExecName)
// Add template functions
cobra.AddTemplateFunc("ExecStyle", cen.SprintFunc())
cobra.AddTemplateFunc("UseLineStyle", func(s string) string {
spl := strings.Split(s, " ")
spl[0] = cen.Sprint(spl[0])
return strings.Join(spl, " ")
})
// Patch usage template
re := regexp.MustCompile(`(?i){{\s*.CommandPath\s*}}`)
tpl = re.ReplaceAllLiteralString(tpl, "{{ExecStyle .CommandPath}}")
re = regexp.MustCompile(`(?i){{\s*.UseLine\s*}}`)
tpl = re.ReplaceAllLiteralString(tpl, "{{UseLineStyle .UseLine}}")
}
//
// Styling flags
//
var cf, cfd, cfdt *color.Color
if cfg.Flags != None {
cf = getColor(cfg.Flags)
}
if cfg.FlagsDescr != None {
cfd = getColor(cfg.FlagsDescr)
}
if cfg.FlagsDataType != None {
cfdt = getColor(cfg.FlagsDataType)
}
if cf != nil || cfd != nil || cfdt != nil {
cobra.AddTemplateFunc("FlagStyle", func(s string) string {
// Flags info section is multi-line.
// Let's split these lines and iterate them.
lines := strings.Split(s, "\n")
for k := range lines {
// Styling short and full flags (-f, --flag)
if cf != nil {
re := regexp.MustCompile(`(--?\S+)`)
for _, flag := range re.FindAllString(lines[k], 2) {
lines[k] = strings.Replace(lines[k], flag, cf.Sprint(flag), 1)
}
}
// If no styles for flag data types and description - continue
if cfd == nil && cfdt == nil {
continue
}
// Split line into two parts: flag data type and description
// Tip: Use debugger to understand the logic
re := regexp.MustCompile(`\s{2,}`)
spl := re.Split(lines[k], -1)
if len(spl) != 3 {
continue
}
// Styling the flag description
if cfd != nil {
lines[k] = strings.Replace(lines[k], spl[2], cfd.Sprint(spl[2]), 1)
}
// Styling flag data type
// Tip: Use debugger to understand the logic
if cfdt != nil {
re = regexp.MustCompile(`\s+(\w+)$`) // the last word after spaces is the flag data type
m := re.FindAllStringSubmatch(spl[1], -1)
if len(m) == 1 && len(m[0]) == 2 {
lines[k] = strings.Replace(lines[k], m[0][1], cfdt.Sprint(m[0][1]), 1)
}
}
}
s = strings.Join(lines, "\n")
return s
})
// Patch usage template
re := regexp.MustCompile(`(?i)(\.(InheritedFlags|LocalFlags)\.FlagUsages)`)
tpl = re.ReplaceAllString(tpl, "FlagStyle $1")
}
//
// Styling aliases
//
if cfg.Aliases != None {
ca := getColor(cfg.Aliases)
cobra.AddTemplateFunc("AliasStyle", ca.SprintFunc())
re := regexp.MustCompile(`(?i){{\s*.NameAndAliases\s*}}`)
tpl = re.ReplaceAllLiteralString(tpl, "{{AliasStyle .NameAndAliases}}")
}
//
// Styling the example text
//
if cfg.Example != None {
ce := getColor(cfg.Example)
cobra.AddTemplateFunc("ExampleStyle", ce.SprintFunc())
re := regexp.MustCompile(`(?i){{\s*.Example\s*}}`)
tpl = re.ReplaceAllLiteralString(tpl, "{{ExampleStyle .Example}}")
}
// Adding a new line to the end
if !cfg.NoBottomNewline {
tpl += "\n"
}
// Apply patched template
cfg.RootCmd.SetUsageTemplate(tpl)
// Debug line, uncomment when needed
// fmt.Println(tpl)
}
// getColor decodes color param and returns color.Color object
func getColor(param uint8) (c *color.Color) {
switch param & 15 {
case None:
c = color.New(color.FgWhite)
case Black:
c = color.New(color.FgBlack)
case Red:
c = color.New(color.FgRed)
case Green:
c = color.New(color.FgGreen)
case Yellow:
c = color.New(color.FgYellow)
case Blue:
c = color.New(color.FgBlue)
case Magenta:
c = color.New(color.FgMagenta)
case Cyan:
c = color.New(color.FgCyan)
case White:
c = color.New(color.FgWhite)
case HiRed:
c = color.New(color.FgHiRed)
case HiGreen:
c = color.New(color.FgHiGreen)
case HiYellow:
c = color.New(color.FgHiYellow)
case HiBlue:
c = color.New(color.FgHiBlue)
case HiMagenta:
c = color.New(color.FgHiMagenta)
case HiCyan:
c = color.New(color.FgHiCyan)
case HiWhite:
c = color.New(color.FgHiWhite)
}
if param&Bold == Bold {
c.Add(color.Bold)
}
if param&Italic == Italic {
c.Add(color.Italic)
}
if param&Underline == Underline {
c.Add(color.Underline)
}
return
}