-
Notifications
You must be signed in to change notification settings - Fork 32
/
colorize.go
365 lines (284 loc) · 9.21 KB
/
colorize.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2019-2023 Intel Corporation
package colorize
import (
"fmt"
"strings"
"github.com/gdamore/tcell/v2"
)
// colorizeInfo structure
type colorizeInfo struct {
defWidth int
floatPrecision int
defForeground string
defBackground string
defFlags string
}
var colorInfo colorizeInfo
// Default values for width and precision
const (
defWidth = int(0)
defPrecision = int(2)
)
// Color constant names we can use
const (
NoColor = ""
DefaultColor = "white"
YellowColor = "yellow"
GreenColor = "green"
GoldenRodColor = "goldenrod"
OrangeColor = "orange"
TealColor = "teal"
CornSilkColor = "cornsilk"
DeepPinkColor = "deeppink"
RedColor = "red"
BlueColor = "blue"
LightBlueColor = "lightblue"
LightCoralColor = "lightcoral"
LightCyanColor = "lightcyan"
LavenderColor = "lavender"
LightSalmonColor = "lightsalmon"
MediumBlueColor = "mediumblue"
MistyRoseColor = "mistyrose"
SkyBlueColor = "skyblue"
LightSkyBlueColor = "lightskyblue"
MediumSpringGreenColor = "mediumspringgreen"
WheatColor = "wheat"
YellowGreenColor = "yellowgreen"
LightYellowColor = "lightyellow"
DarkOrangeColor = "darkorange"
LightGreenColor = "lightgreen"
DarkMagentaColor = "darkmagenta"
CyanColor = "aqua"
)
// SetDefault - create a colorize instance
func SetDefault(foreground, background string, width, precision int, flags string) {
// when precision is negative then set to the default value
if precision < 0 {
precision = defPrecision
}
colorInfo = colorizeInfo{
defWidth: width,
floatPrecision: precision,
defForeground: foreground,
defBackground: background,
defFlags: flags,
}
}
// DefaultForegroundColor returns the default color
func DefaultForegroundColor() string {
return colorInfo.defForeground
}
// SetDefaultForegroundColor - Set the default foreground color
func SetDefaultForegroundColor(color string) {
colorInfo.defForeground = color
}
// DefaultBackgroundColor returns the default color
func DefaultBackgroundColor() string {
return colorInfo.defBackground
}
// SetDefaultBackgroundColor - Set the default background color
func SetDefaultBackgroundColor(color string) {
colorInfo.defBackground = color
}
// DefaultWidth returns the default width
func DefaultWidth() int {
return colorInfo.defWidth
}
// SetDefaultWidth - Set the default width
func SetDefaultWidth(w int) {
colorInfo.defWidth = w
}
// SetFloatPrecision - Set float precision
func SetFloatPrecision(w int) {
colorInfo.floatPrecision = w
}
// FloatPrecision returns the default precision
func FloatPrecision() int {
return colorInfo.floatPrecision
}
// DefaultFlags returns the default flags
func DefaultFlags() string {
return colorInfo.defFlags
}
// SetDefaultFlags - Set flags
func SetDefaultFlags(f string) {
colorInfo.defFlags = f
}
// Colorize - Add color to the value passed, w size can be 0, 1 or 2
//
// w[0] is the width and w[1] is precision or a float value if present
// w[1] is not present then use default colorInfo.floatPrecision
// w[2] is the foreground color
// w[3] is the background color
// w[4] is the attribute of the color
func Colorize(color string, v interface{}, w ...interface{}) string {
if colorInfo.defForeground == "" {
colorInfo.defForeground = "ivory"
}
width := int(0)
precision := colorInfo.floatPrecision
foreground := colorInfo.defForeground
if len(color) > 0 {
foreground = color
}
background := colorInfo.defBackground
flags := colorInfo.defFlags
for i, v := range w {
switch i {
case 0: // Width of the field
p := v.(int)
if p >= 0 {
width = p
}
case 1: // Precision of the float value
p := v.(int)
if p >= 0 {
precision = p
}
case 2: // foreground color */
s := v.(string)
if len(s) > 0 {
foreground = s
}
case 3: // background color
s := v.(string)
if len(s) > 0 {
background = s
}
case 4: // flags used for color attributes
s := v.(string)
if len(s) > 0 {
flags = s
}
}
}
// Build up the color tag strings for begin and end of the field to be printed
str := fmt.Sprintf("[%s:%s:%s]", foreground, background, flags)
def := fmt.Sprintf("[%s:%s:%s]", colorInfo.defForeground, colorInfo.defBackground, colorInfo.defFlags)
switch v.(type) {
case string:
return fmt.Sprintf("%[1]s%[3]*[2]s%[4]s", str, v, width, def)
case uint64, uint32, uint16, uint8:
return fmt.Sprintf("%[1]s%[3]*[2]d%[4]s", str, v, width, def)
case int, int64, int32, int16, int8:
return fmt.Sprintf("%[1]s%[3]*[2]d%[4]s", str, v, width, def)
case float64, float32:
return fmt.Sprintf("%[1]s%[3]*.[4]*[2]f%[5]s", str, v, width, precision, def)
default:
return fmt.Sprintf("%[1]s%[2]v%s", str, v, def)
}
}
// ColorWithName - Find and set the color by name
func ColorWithName(color string, a interface{}, w ...interface{}) string {
color = strings.ToLower(color)
_, ok := tcell.ColorNames[color]
if !ok {
color = OrangeColor
}
return Colorize(color, a, w...)
}
// Yellow - return string based on the color given
func Yellow(a interface{}, w ...interface{}) string {
return ColorWithName(YellowColor, a, w...)
}
// DarkMagenta - return string based on the color given
func DarkMagenta(a interface{}, w ...interface{}) string {
return ColorWithName(DarkMagentaColor, a, w...)
}
// Green - return string based on the color given
func Green(a interface{}, w ...interface{}) string {
return ColorWithName(GreenColor, a, w...)
}
// GoldenRod - return string based on the color given
func GoldenRod(a interface{}, w ...interface{}) string {
return ColorWithName(GoldenRodColor, a, w...)
}
// Orange - return string based on the color given
func Orange(a interface{}, w ...interface{}) string {
return ColorWithName(OrangeColor, a, w...)
}
// Teal - return string based on the color given
func Teal(a interface{}, w ...interface{}) string {
return ColorWithName(TealColor, a, w...)
}
// CornSilk - return string based on the color given
func CornSilk(a interface{}, w ...interface{}) string {
return ColorWithName(CornSilkColor, a, w...)
}
// DeepPink - return string based on the color given
func DeepPink(a interface{}, w ...interface{}) string {
return ColorWithName(DeepPinkColor, a, w...)
}
// Red - return string based on the color given
func Red(a interface{}, w ...interface{}) string {
return ColorWithName(RedColor, a, w...)
}
// Blue - return string based on the color given
func Blue(a interface{}, w ...interface{}) string {
return ColorWithName(BlueColor, a, w...)
}
// LightBlue - return string based on the color given
func LightBlue(a interface{}, w ...interface{}) string {
return ColorWithName(LightBlueColor, a, w...)
}
// LightCoral - return string based on the color given
func LightCoral(a interface{}, w ...interface{}) string {
return ColorWithName(LightCoralColor, a, w...)
}
// LightCyan - return string based on the color given
func LightCyan(a interface{}, w ...interface{}) string {
return ColorWithName(LightCyanColor, a, w...)
}
// Cyan - return string based on the color given
func Cyan(a interface{}, w ...interface{}) string {
return ColorWithName(CyanColor, a, w...)
}
// Lavender - return string based on the color given
func Lavender(a interface{}, w ...interface{}) string {
return ColorWithName(LavenderColor, a, w...)
}
// LightSalmon - return string based on the color given
func LightSalmon(a interface{}, w ...interface{}) string {
return ColorWithName(LightSalmonColor, a, w...)
}
// MediumBlue - return string based on the color given
func MediumBlue(a interface{}, w ...interface{}) string {
return ColorWithName(MediumBlueColor, a, w...)
}
// MistyRose - return string based on the color given
func MistyRose(a interface{}, w ...interface{}) string {
return ColorWithName(MistyRoseColor, a, w...)
}
// SkyBlue - return string based on the color given
func SkyBlue(a interface{}, w ...interface{}) string {
return ColorWithName(SkyBlueColor, a, w...)
}
// LightSkyBlue - return string based on the color given
func LightSkyBlue(a interface{}, w ...interface{}) string {
return ColorWithName(LightSkyBlueColor, a, w...)
}
// MediumSpringGreen - return string based on the color given
func MediumSpringGreen(a interface{}, w ...interface{}) string {
return ColorWithName(MediumSpringGreenColor, a, w...)
}
// Wheat - return string based on the color given
func Wheat(a interface{}, w ...interface{}) string {
return ColorWithName(WheatColor, a, w...)
}
// YellowGreen - return string based on the color given
func YellowGreen(a interface{}, w ...interface{}) string {
return ColorWithName(YellowGreenColor, a, w...)
}
// LightYellow - return string based on the color given
func LightYellow(a interface{}, w ...interface{}) string {
return ColorWithName(LightYellowColor, a, w...)
}
// DarkOrange - return string based on the color given
func DarkOrange(a interface{}, w ...interface{}) string {
return ColorWithName(DarkOrangeColor, a, w...)
}
// LightGreen - return string based on the color given
func LightGreen(a interface{}, w ...interface{}) string {
return ColorWithName(LightGreenColor, a, w...)
}