forked from gyozatech/noodlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.go
171 lines (148 loc) · 4.59 KB
/
colors.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
package noodlog
import "strconv"
type Color struct {
Code *string
}
var colorMap = map[string]string{
traceLabel: colorReset,
infoLabel: colorReset,
debugLabel: NewColor(Green).ToCode(),
warnLabel: NewColor(Yellow).ToCode(),
errorLabel: NewColor(Red).ToCode(),
}
var colors = map[string]string{
defaultColor: colorReset,
redColor: colorRed,
greenColor: colorGreen,
yellowColor: colorYellow,
blueColor: colorBlue,
purpleColor: colorPurple,
cyanColor: colorCyan,
}
var backgroundColors = map[string]string{
defaultColor: colorReset,
redColor: backgroundRed,
greenColor: backgroundGreen,
yellowColor: backgroundYellow,
blueColor: backgroundBlue,
purpleColor: backgroundPurple,
cyanColor: backgroundCyan,
}
var colorEnabled = false
// IsValidTrueColor check if a true color is valid, it has to be included between 0 and 255
func IsValidTrueColor(color int) bool {
return color >= 0 && color <= 255
}
// NewColor set the color of the text using a string as identifiers
func NewColor(color *string) Color {
code := colorReset
colorCode := colors[*color]
if colorCode != "" {
code = "\033[" + colorCode + "m"
}
return Color{Code: &code}
}
// NewColorRGB set the color of the text using RGB Notations
func NewColorRGB(red int, green int, blue int) Color {
code := colorReset
if IsValidTrueColor(red) && IsValidTrueColor(green) && IsValidTrueColor(blue) {
code = "\033[38;2;" + strconv.Itoa(red) + ";" + strconv.Itoa(green) + ";" + strconv.Itoa(blue) + "m"
}
return Color{Code: &code}
}
// Background set the background using a string as identifiers
func Background(color *string) Color {
code := colorReset
colorCode := backgroundColors[*color]
if colorCode != "" {
code = "\033[" + colorCode + "m"
}
return Color{Code: &code}
}
// BackgroundRGB set the background using RGB Notations
func BackgroundRGB(red int, green int, blue int) Color {
code := colorReset
if IsValidTrueColor(red) && IsValidTrueColor(green) && IsValidTrueColor(blue) {
code = "\033[48;2;" + strconv.Itoa(red) + ";" + strconv.Itoa(green) + ";" + strconv.Itoa(blue) + "m"
}
return Color{Code: &code}
}
// From a given Color it set the background using a string as identifier
func (c Color) Background(color *string) Color {
code := *(c.Code) + *Background(color).Code
return Color{Code: &code}
}
// From a given Color it set the background using RGB Notations
func (c Color) BackgroundRGB(red int, green int, blue int) Color {
code := *(c.Code) + *BackgroundRGB(red, green, blue).Code
return Color{Code: &code}
}
// ToCode returns code used into bash for colored log
func (c Color) ToCode() string {
if c.Code != nil {
return *c.Code
}
return colorReset
}
// EnableColors function enables colored logging
func EnableColors() {
colorEnabled = true
}
// DisableColors function disables colored logging
func DisableColors() {
colorEnabled = false
}
// SetTraceColor overrides the trace level log color with the one specified in input
func SetTraceColor(color Color) {
colorMap[traceLabel] = color.ToCode()
}
// SetDebugColor overrides the debug level log color with the one specified in input
func SetDebugColor(color Color) {
colorMap[debugLabel] = color.ToCode()
}
// SetInfoColor overrides the info level log color with the one specified in input
func SetInfoColor(color Color) {
colorMap[infoLabel] = color.ToCode()
}
// SetWarnColor overrides the warn level log color with the one specified in input
func SetWarnColor(color Color) {
colorMap[warnLabel] = color.ToCode()
}
// SetErrorColor overrides the error level log color with the one specified in input
func SetErrorColor(color Color) {
colorMap[errorLabel] = color.ToCode()
}
// DetectColor return Color struct given a generic interface{}. It is an empty Color struct if not a valid type
func DetectColor(color interface{}) Color {
empty := Color{}
switch color.(type) {
case *string:
return NewColor(color.(*string))
case Color:
if color != empty {
return color.(Color)
}
return empty
default:
return empty
}
}
// setCustomColors overrides defaultColor when custom color is passed into CustomColor configs
func setCustomColors(colors CustomColors) {
empty := Color{}
if traceColor := DetectColor(colors.Trace); traceColor != empty {
SetTraceColor(traceColor)
}
if debugColor := DetectColor(colors.Debug); debugColor != empty {
SetDebugColor(debugColor)
}
if infoColor := DetectColor(colors.Info); infoColor != empty {
SetInfoColor(infoColor)
}
if warnColor := DetectColor(colors.Warn); warnColor != empty {
SetWarnColor(warnColor)
}
if errorColor := DetectColor(colors.Error); errorColor != empty {
SetErrorColor(errorColor)
}
}