-
Notifications
You must be signed in to change notification settings - Fork 19
/
theme.go
505 lines (463 loc) · 16.6 KB
/
theme.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package config
import (
"fmt"
"reflect"
"strings"
"github.com/kubecolor/kubecolor/internal/stringutil"
"github.com/spf13/viper"
)
// NewBaseTheme returns the base color schema depending on the dark/light setting
func NewBaseTheme(preset Preset) *Theme {
switch preset {
case PresetDark:
return &Theme{
Default: MustParseColor("green"),
Base: ThemeBase{
Key: MustParseColorSlice("hicyan / cyan"),
Info: MustParseColor("white"),
Primary: MustParseColor("magenta"),
Secondary: MustParseColor("cyan"),
Success: MustParseColor("green"),
Warning: MustParseColor("yellow"),
Danger: MustParseColor("red"),
Muted: MustParseColor("gray:italic"),
},
Table: ThemeTable{
Header: MustParseColor("bold"),
},
Data: ThemeData{
String: MustParseColor("hiyellow"),
},
}
case PresetLight:
return &Theme{
Default: MustParseColor("green"),
Base: ThemeBase{
Key: MustParseColorSlice("hiblue / blue"),
Info: MustParseColor("black"),
Primary: MustParseColor("magenta"),
Secondary: MustParseColor("blue"),
Success: MustParseColor("green"),
Warning: MustParseColor("yellow"),
Danger: MustParseColor("red"),
Muted: MustParseColor("gray:italic"),
},
Table: ThemeTable{
Header: MustParseColor("bold"),
},
Data: ThemeData{
String: MustParseColor("yellow"),
},
}
// Special Preset for Protanopias
case PresetProtDark:
return &Theme{
Default: MustParseColor("white"),
Base: ThemeBase{
Key: MustParseColorSlice("#feb927 / #fe6e1a"),
Info: MustParseColor("white"),
Primary: MustParseColor("#4860e6"), // magenta
Secondary: MustParseColor("#2aabee"), // cyan
Success: MustParseColor("#6afd6a:bold"), // bold green
Warning: MustParseColor("#feb927:italic"), // yellow
Danger: MustParseColor("fg=white:bg=#c2270a"), // red background
Muted: MustParseColor("#2ee5ae:italic"), // white-ish
},
Data: ThemeData{
String: MustParseColor("#2aabee"),
},
Table: ThemeTable{
Header: MustParseColor("white:bold"),
Columns: MustParseColorSlice("#2aabee / #6afd6a:bold / #4860e6 / white / #feb927"),
},
}
case PresetProtLight:
return &Theme{
Default: MustParseColor("black"),
Base: ThemeBase{
Key: MustParseColorSlice("#feb927 / #fe6e1a"),
Info: MustParseColor("black"),
Primary: MustParseColor("#4860e6"),
Secondary: MustParseColor("#2aabee"),
Success: MustParseColor("#6afd6a:bold"),
Warning: MustParseColor("#feb927:italic"),
Danger: MustParseColor("fg=black:bg=#c2270a"),
Muted: MustParseColor("#2ee5ae:italic"),
},
Data: ThemeData{
String: MustParseColor("#2aabee"),
},
Table: ThemeTable{
Header: MustParseColor("black:bold"),
Columns: MustParseColorSlice("#2aabee / #6afd6a:bold / #4860e6 / black / #feb927"),
},
}
// Special Preset for Deuteranopia
case PresetDeutDark:
return &Theme{
Default: MustParseColor("white"),
Base: ThemeBase{
Key: MustParseColorSlice("#feb927 / #fe6e1a"),
Info: MustParseColor("white"),
Primary: MustParseColor("#4860e6"),
Secondary: MustParseColor("#2aabee"),
Success: MustParseColor("#6afd6a:bold"),
Warning: MustParseColor("#feb927:italic"),
Danger: MustParseColor("fg=white:bg=#c2270a"),
Muted: MustParseColor("#2ee5ae"),
},
Data: ThemeData{
String: MustParseColor("#2aabee"),
},
Table: ThemeTable{
Header: MustParseColor("white:bold"),
Columns: MustParseColorSlice("#2aabee / #6afd6a:bold / #4860e6 / white / #feb927"),
},
}
case PresetDeutLight:
return &Theme{
Default: MustParseColor("black"),
Base: ThemeBase{
Key: MustParseColorSlice("#feb927 / #fe6e1a"),
Info: MustParseColor("black"),
Primary: MustParseColor("#4860e6"),
Secondary: MustParseColor("#2aabee"),
Success: MustParseColor("#6afd6a:bold"),
Warning: MustParseColor("#feb927:italic"),
Danger: MustParseColor("fg=black:bg=#c2270a"),
Muted: MustParseColor("#2ee5ae:italic"),
},
Data: ThemeData{
String: MustParseColor("#2aabee"),
},
Table: ThemeTable{
Header: MustParseColor("black:bold"),
Columns: MustParseColorSlice("#2aabee / #6afd6a:bold / #4860e6 / black / #feb927"),
},
}
// Special Preset for Tritanopia
case PresetTritDark:
return &Theme{
Default: MustParseColor("white"),
Base: ThemeBase{
Key: MustParseColorSlice("#feb927 / #fe6e1a"),
Info: MustParseColor("white"),
Primary: MustParseColor("#4860e6"),
Secondary: MustParseColor("#2aabee"),
Success: MustParseColor("#6afd6a:bold"),
Warning: MustParseColor("#feb927:italic"),
Danger: MustParseColor("fg=white:bg=#c2270a"),
Muted: MustParseColor("#2ee5ae"),
},
Data: ThemeData{
String: MustParseColor("#2aabee"),
},
Table: ThemeTable{
Header: MustParseColor("white:bold"),
Columns: MustParseColorSlice("#2aabee / #6afd6a:bold / #4860e6 / white / #feb927"),
},
}
case PresetTritLight:
return &Theme{
Default: MustParseColor("black"),
Base: ThemeBase{
Key: MustParseColorSlice("#feb927 / #fe6e1a"),
Info: MustParseColor("black"),
Primary: MustParseColor("#4860e6"),
Secondary: MustParseColor("#2aabee"),
Success: MustParseColor("#6afd6a:bold"),
Warning: MustParseColor("#feb927:italic"),
Danger: MustParseColor("fg=black:bg=#c2270a"),
Muted: MustParseColor("#2ee5ae:italic"),
},
Data: ThemeData{
String: MustParseColor("#2aabee"),
},
Table: ThemeTable{
Header: MustParseColor("black:bold"),
Columns: MustParseColorSlice("#2aabee / #6afd6a:bold / #4860e6 / black / #feb927"),
},
}
// Pre-v0.3.0
case PresetPre030Dark:
return &Theme{
Default: MustParseColor("green"),
Base: ThemeBase{
Key: MustParseColorSlice("yellow / white"),
Info: MustParseColor("white"),
Primary: MustParseColor("magenta"),
Secondary: MustParseColor("cyan"),
Success: MustParseColor("green"),
Warning: MustParseColor("yellow"),
Danger: MustParseColor("red"),
Muted: MustParseColor("yellow"),
},
Options: ThemeOptions{
Flag: MustParseColor("yellow"),
},
}
case PresetPre030Light:
return &Theme{
Default: MustParseColor("green"),
Base: ThemeBase{
Key: MustParseColorSlice("yellow / black"),
Info: MustParseColor("black"),
Primary: MustParseColor("magenta"),
Secondary: MustParseColor("blue"),
Success: MustParseColor("green"),
Warning: MustParseColor("yellow"),
Danger: MustParseColor("red"),
Muted: MustParseColor("yellow"),
},
Options: ThemeOptions{
Flag: MustParseColor("yellow"),
},
}
// Pre-v0.0.21
case PresetPre0021Dark:
return &Theme{
Default: MustParseColor("green"),
Base: ThemeBase{
Key: MustParseColorSlice("yellow / white"),
Info: MustParseColor("white"),
Primary: MustParseColor("magenta"),
Secondary: MustParseColor("cyan"),
Success: MustParseColor("green"),
Warning: MustParseColor("yellow"),
Danger: MustParseColor("red"),
Muted: MustParseColor("yellow"),
},
Data: ThemeData{
String: MustParseColor("cyan"),
},
Table: ThemeTable{
Columns: MustParseColorSlice("cyan / green / magenta / white / yellow"),
},
Status: ThemeStatus{
Success: MustParseColor("none"),
},
Options: ThemeOptions{
Flag: MustParseColor("yellow"),
},
}
case PresetPre0021Light:
return &Theme{
Default: MustParseColor("green"),
Base: ThemeBase{
Key: MustParseColorSlice("yellow / black"),
Info: MustParseColor("white"),
Primary: MustParseColor("magenta"),
Secondary: MustParseColor("cyan"),
Success: MustParseColor("green"),
Warning: MustParseColor("yellow"),
Danger: MustParseColor("red"),
Muted: MustParseColor("yellow"),
},
Data: ThemeData{
String: MustParseColor("blue"),
},
Table: ThemeTable{
Columns: MustParseColorSlice("cyan / green / magenta / black / yellow / blue"),
},
Status: ThemeStatus{
Success: MustParseColor("none"),
},
Options: ThemeOptions{
Flag: MustParseColor("yellow"),
},
}
default:
// Empty theme
return &Theme{}
}
}
// Theme is the root theme config.
type Theme struct {
// Base colors must be first so they're applied first
Base ThemeBase // base colors for themes
Default Color // default when no specific mapping is found for the command
Data ThemeData // colors for representing data
Status ThemeStatus // generic status coloring (e.g "Ready", "Terminating")
Table ThemeTable // used in table output, e.g "kubectl get" and parts of "kubectl describe"
Stderr ThemeStderr // used in kubectl's stderr output
Describe ThemeDescribe // used in "kubectl describe"
Apply ThemeApply // used in "kubectl apply"
Explain ThemeExplain // used in "kubectl explain"
Options ThemeOptions // used in "kubectl options"
Version ThemeVersion // used in "kubectl version"
}
// ThemeBase contains base colors that other theme fields can default to,
// just to make overriding themes easier.
//
// These fields should never be referenced in the printers.
// Instead, they should use the more specific fields, such as [ThemeApply.Created]
type ThemeBase struct {
Info Color // general color for when things are informational
Primary Color // general color for when things are focus
Secondary Color // general color for when things are secondary focus
Success Color // general color for when things are good
Warning Color // general color for when things are wrong
Danger Color // general color for when things are bad
Muted Color // general color for when things are less relevant
Key ColorSlice `defaultFromMany:"theme.base.secondary"` // general color for keys
}
// ThemeData holds colors for when representing parsed data.
// Such as in YAML, JSON, and even some "kubectl describe" values
type ThemeData struct {
Key ColorSlice `defaultFrom:"theme.base.key"` // used for the key
String Color `defaultFrom:"theme.base.info"` // used when value is a string
True Color `defaultFrom:"theme.base.success"` // used when value is true
False Color `defaultFrom:"theme.base.danger"` // used when value is false
Number Color `defaultFrom:"theme.base.primary"` // used when the value is a number
Null Color `defaultFrom:"theme.base.muted"` // used when the value is null, nil, or none
Quantity Color `defaultFrom:"theme.data.number"` // used when the value is a quantity, e.g "100m" or "5Gi"
Duration Color `` // used when the value is a duration, e.g "12m" or "1d12h"
DurationFresh Color `defaultFrom:"theme.base.success"` // color used when the time value is under a certain delay
Ratio ThemeDataRatio
}
type ThemeDataRatio struct {
Zero Color `defaultFrom:"theme.base.muted"` // used for "0/0"
Equal Color `` // used for "n/n", e.g "1/1"
Unequal Color `defaultFrom:"theme.base.warning"` // used for "n/m", e.g "0/1"
}
// ThemeStatus holds colors for status texts, used in for example
// the "kubectl get" status column
type ThemeStatus struct {
Success Color `defaultFrom:"theme.base.success"` // used in status keywords, e.g "Running", "Ready"
Warning Color `defaultFrom:"theme.base.warning"` // used in status keywords, e.g "Terminating"
Error Color `defaultFrom:"theme.base.danger"` // used in status keywords, e.g "Failed", "Unhealthy"
}
// ThemeTable holds colors for table output
type ThemeTable struct {
Header Color `defaultFrom:"theme.base.info"` // used on table headers
Columns ColorSlice `defaultFromMany:"theme.base.info,theme.base.secondary"` // used on table columns when no other coloring applies such as status or duration coloring. The multiple colors are cycled based on column ID, from left to right.
}
// ThemeStderr holds generic colors for kubectl's stderr output.
type ThemeStderr struct {
Default Color `defaultFrom:"theme.base.info"` // default when no specific mapping is found for the output line
Error Color `defaultFrom:"theme.base.danger"` // e.g when text contains "error"
}
// ThemeApply holds colors for the "kubectl apply" output.
type ThemeDescribe struct {
Key ColorSlice `defaultFrom:"theme.base.key"` // used on keys. The multiple colors are cycled based on indentation.
}
// ThemeApply holds colors for the "kubectl apply" output.
type ThemeApply struct {
Created Color `defaultFrom:"theme.base.success"` // used on "deployment.apps/foo created"
Configured Color `defaultFrom:"theme.base.warning"` // used on "deployment.apps/bar configured"
Unchanged Color `defaultFrom:"theme.base.primary"` // used on "deployment.apps/quux unchanged"
DryRun Color `defaultFrom:"theme.base.secondary"` // used on "deployment.apps/quux created (dry-run)"
Fallback Color `defaultFrom:"theme.base.success"` // used when "kubectl apply" outputs unknown format
}
// ThemeExplain holds colors for the "kubectl explain" output.
type ThemeExplain struct {
Key ColorSlice `defaultFrom:"theme.base.key"` // used on keys. The multiple colors are cycled based on indentation.
Required Color `defaultFrom:"theme.base.danger"` // used on the trailing "-required-" string
}
// ThemeOptions holds colors for the "kubectl options" output.
type ThemeOptions struct {
Flag Color `defaultFrom:"theme.base.secondary"` // e.g "--kubeconfig"
}
// ThemeVersion holds colors for the "kubectl version" output.
type ThemeVersion struct {
Key ColorSlice `defaultFrom:"theme.base.key"` // used on the key
}
func applyViperDefaults(theme *Theme, v *viper.Viper) {
themeVal := reflect.ValueOf(theme).Elem()
themeViperVisitor{
viper: v,
}.walkFields(themeVal, "theme")
}
type themeViperVisitor struct {
viper *viper.Viper
}
func (t themeViperVisitor) walkFields(val reflect.Value, viperKey string) {
typ := val.Type()
for i := range val.NumField() {
fieldTyp := typ.Field(i)
if fieldTyp.Anonymous || !fieldTyp.IsExported() {
continue
}
fieldVal := val.Field(i)
// e.g "theme" + field "Default" => "theme.default"
newViperKey := fmt.Sprintf("%s.%s", viperKey, strings.ToLower(fieldTyp.Name))
// Only dig deeper if its a theme struct, e.g ThemeApply
if strings.HasPrefix(fieldTyp.Type.Name(), "Theme") {
t.walkFields(fieldVal, newViperKey)
continue
}
fieldValInterface := fieldVal.Interface()
t.visitField(newViperKey, fieldValInterface, fieldTyp.Tag)
}
}
func (t themeViperVisitor) visitField(viperKey string, value any, tags reflect.StructTag) {
switch value := value.(type) {
case Color:
if _, ok := tags.Lookup("defaultFromMany"); ok {
panic(fmt.Errorf("%s: cannot use defaultFromMany tag on a Color field", viperKey))
}
if defaultFrom, ok := tags.Lookup("defaultFrom"); ok {
t.setColorOrKey(viperKey, value, defaultFrom)
} else {
t.setColor(viperKey, value)
}
case ColorSlice:
if defaultFrom, ok := tags.Lookup("defaultFrom"); ok {
t.setColorSliceOrKey(viperKey, value, defaultFrom)
} else if defaultFromMany, ok := tags.Lookup("defaultFromMany"); ok {
split := stringutil.SplitAndTrimSpace(defaultFromMany, ",")
t.setColorSliceOrManyKeys(viperKey, value, split)
} else {
t.setColorSlice(viperKey, value)
}
default:
panic(fmt.Errorf("%s: unsupported field type: %T", viperKey, value))
}
}
func (t themeViperVisitor) setColorOrKey(key string, value Color, otherKey string) {
if t.setColor(key, value) {
return
}
t.viper.SetDefault(key, t.viper.Get(otherKey))
}
func (t themeViperVisitor) setColor(key string, value Color) bool {
if value == (Color{}) {
t.viper.SetDefault(key, Color{})
return false
}
t.viper.SetDefault(key, value)
return true
}
func (t themeViperVisitor) setColorSliceOrKey(key string, value ColorSlice, otherKey string) {
if t.setColorSlice(key, value) {
return
}
t.viper.SetDefault(key, t.viper.Get(otherKey))
}
func (t themeViperVisitor) setColorSliceOrManyKeys(key string, value ColorSlice, otherKeys []string) {
if t.setColorSlice(key, value) {
return
}
values := make(ColorSlice, 0, len(otherKeys))
for _, k := range otherKeys {
val := t.viper.Get(k)
col, ok := val.(Color)
if !ok {
col = MustParseColor(fmt.Sprint(val))
}
if val != nil {
values = append(values, col)
}
}
if len(values) > 0 {
t.viper.SetDefault(key, values)
}
}
func (t themeViperVisitor) setColorSlice(key string, value ColorSlice) bool {
if len(value) == 0 {
t.viper.SetDefault(key, ColorSlice{})
return false
}
t.viper.SetDefault(key, value)
return true
}