/
colorable_output.go
133 lines (116 loc) · 2.89 KB
/
colorable_output.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
package rrh
import (
"strings"
"github.com/gookit/color"
)
type colorSettings map[string]string
type colorFuncs map[string](func(r string) string)
/*
Color struct shows the color settings of RRH.
*/
type Color struct {
settings colorSettings
funcs colorFuncs
}
var colorLabels = []string{
"repository", "group", "label", "configValue",
}
/*
ColorizedRepositoryID returns the colorized repository id string from configuration.
*/
func (c *Color) ColorizedRepositoryID(repoID string) string {
return c.executeColorFunc("repository", repoID)
}
/*
ColorizedGroupName returns the colorized group name string from configuration.
*/
func (c *Color) ColorizedGroupName(groupName string) string {
return c.executeColorFunc("group", groupName)
}
/*
ColorizeConfigValue returns the coloried config value from configuration.
*/
func (c *Color) ColorizeConfigValue(value string) string {
return c.executeColorFunc("configValue", value)
}
/*
ColorizedLabel returns the colorized label string from configuration.
*/
func (c *Color) ColorizedLabel(label string) string {
return c.executeColorFunc("label", label)
}
func (c *Color) executeColorFunc(label string, value string) string {
var f, ok = c.funcs[label]
if ok {
return f(value)
}
return value
}
/*
ClearColorize clears the color settings.
*/
func (c *Color) ClearColorize() {
c.parse("")
}
func (c *Color) parse(colorSettings string) {
for _, label := range colorLabels {
c.settings[label] = ""
}
var colors = strings.Split(colorSettings, "+")
for _, eachColor := range colors {
c.parseEach(eachColor)
}
c.updateFuncs()
}
func (c *Color) parseEach(eachColor string) {
var typeAndValue = strings.Split(eachColor, ":")
if contains(colorLabels, typeAndValue[0]) {
c.settings[typeAndValue[0]] = color.ParseCodeFromAttr(typeAndValue[1])
}
}
func (c *Color) updateFuncs() {
for _, label := range colorLabels {
var targetColor, ok = c.settings[label]
if ok {
c.funcs[label] = generateColorFunc(targetColor)
} else {
c.funcs[label] = generateColorFunc("")
}
}
}
func generateColorFunc(targetColor string) func(s string) string {
if targetColor != "" {
var printer = color.NewPrinter(targetColor)
return func(r string) string {
return printer.Sprint(r)
}
}
return func(r string) string {
return r
}
}
/*
SetColorize sets to enable colorization.
*/
func (c *Color) SetColorize(enable bool) {
if !enable {
for _, label := range colorLabels {
c.funcs[label] = generateColorFunc("")
}
} else {
c.updateFuncs()
}
}
/*
InitializeColor is the initialization function of the colorized output.
The function is automatically called on loading the config file.
*/
func InitializeColor(config *Config) *Color {
var color = Color{colorSettings{}, colorFuncs{}}
var settingString = config.GetValue(ColorSetting)
if config.IsSet(EnableColorized) && settingString != "" {
color.parse(settingString)
}
color.updateFuncs()
return &color
}