-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.go
103 lines (88 loc) · 2.23 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
package percbar
import (
"strings"
"github.com/fatih/color"
)
func trimlow(s string) string {
s = strings.TrimSpace(s)
s = strings.ToLower(s)
return s
}
// (c) https://github.com/fatih/color/blob/main/color.go
var colormap = map[string]color.Attribute{
"black": color.FgBlack,
"red": color.FgRed,
"green": color.FgGreen,
"yellow": color.FgYellow,
"blue": color.FgBlue,
"magenta": color.FgMagenta,
"cyan": color.FgCyan,
"white": color.FgWhite,
"hiblack": color.FgHiBlack,
"hired": color.FgHiRed,
"higreen": color.FgHiGreen,
"hiyellow": color.FgHiYellow,
"hiblue": color.FgHiBlue,
"himagenta": color.FgHiMagenta,
"hicyan": color.FgHiCyan,
"hiwhite": color.FgHiWhite,
"bgblack": color.BgBlack,
"bgred": color.BgRed,
"bggreen": color.BgGreen,
"bgyellow": color.BgYellow,
"bgblue": color.BgBlue,
"bgmagenta": color.BgMagenta,
"bgcyan": color.BgCyan,
"bgwhite": color.BgWhite,
"bghiblack": color.BgHiBlack,
"bghired": color.BgHiRed,
"bghigreen": color.BgHiGreen,
"bghiyellow": color.BgHiYellow,
"bghiblue": color.BgHiBlue,
"bghimagenta": color.BgHiMagenta,
"bghicyan": color.BgHiCyan,
"bghiwhite": color.BgHiWhite,
"+bold": color.Bold,
"+faint": color.Faint,
"+italic": color.Italic,
"+underline": color.Underline,
"+blinkslow": color.BlinkSlow,
"+blinkrapid": color.BlinkRapid,
"+reversevideo": color.ReverseVideo,
"+concealed": color.Concealed,
"+crossedout": color.CrossedOut,
}
func textToColors(s string) []*color.Color {
var colors []*color.Color
arr := strings.Split(s, ",")
for _, c := range arr {
colors = append(colors, asColor(c))
}
return colors
}
func asColor(s string) *color.Color {
s = trimlow(s)
arr := strings.Split(s, ":")
for i, s := range arr {
arr[i] = trimlow(s)
}
var c = color.New()
// foreground color
if attr, has := colormap[arr[0]]; has {
c = c.Add(attr)
}
// background color or color attribute
if len(arr) >= 2 {
for _, attr := range arr[1:] {
// background color
if attr, has := colormap["bg"+attr]; has {
c = c.Add(attr)
}
// attribute
if attr, has := colormap["+"+attr]; has {
c = c.Add(attr)
}
}
}
return c
}