-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregistry.go
170 lines (152 loc) · 5.94 KB
/
registry.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
package color
import (
"fmt"
"strings"
)
// Colors to use as default
var defaultColors = map[string]color{
// Red tones
"red": &rgb{255, 0, 0}, // Red
"red10": &rgb{240, 0, 0}, // Light Red
"red20": &rgb{224, 0, 0}, // Light Red
"red30": &rgb{208, 0, 0}, // Light Red
"red40": &rgb{192, 0, 0}, // Light Red
"red50": &rgb{176, 0, 0}, // Light Red
"red60": &rgb{160, 0, 0}, // Dark Red
"red70": &rgb{144, 0, 0}, // Dark Red
"red80": &rgb{128, 0, 0}, // Dark Red
"red90": &rgb{112, 0, 0}, // Dark Red
"red100": &rgb{96, 0, 0}, // Dark Red
// Green tones
"green": &rgb{0, 255, 0}, // Green
"green10": &rgb{0, 240, 0}, // Light Green
"green20": &rgb{0, 224, 0}, // Light Green
"green30": &rgb{0, 208, 0}, // Light Green
"green40": &rgb{0, 192, 0}, // Light Green
"green50": &rgb{0, 176, 0}, // Light Green
"green60": &rgb{0, 160, 0}, // Dark Green
"green70": &rgb{0, 144, 0}, // Dark Green
"green80": &rgb{0, 128, 0}, // Dark Green
"green90": &rgb{0, 112, 0}, // Dark Green
"green100": &rgb{0, 96, 0}, // Dark Green
// Yellow tones
"yellow": &rgb{255, 255, 0}, // Yellow
"yellow10": &rgb{240, 240, 0}, // Light Yellow
"yellow20": &rgb{224, 224, 0}, // Light Yellow
"yellow30": &rgb{208, 208, 0}, // Light Yellow
"yellow40": &rgb{192, 192, 0}, // Light Yellow
"yellow50": &rgb{176, 176, 0}, // Light Yellow
"yellow60": &rgb{160, 160, 0}, // Dark Yellow
"yellow70": &rgb{144, 144, 0}, // Dark Yellow
"yellow80": &rgb{128, 128, 0}, // Dark Yellow
"yellow90": &rgb{112, 112, 0}, // Dark Yellow
"yellow100": &rgb{96, 96, 0}, // Dark Yellow
// Cyan tones
"cyan": &rgb{0, 255, 255}, // Cyan
"cyan10": &rgb{0, 240, 240}, // Light Cyan
"cyan20": &rgb{0, 224, 224}, // Light Cyan
"cyan30": &rgb{0, 208, 208}, // Light Cyan
"cyan40": &rgb{0, 192, 192}, // Light Cyan
"cyan50": &rgb{0, 176, 176}, // Light Cyan
"cyan60": &rgb{0, 160, 160}, // Dark Cyan
"cyan70": &rgb{0, 144, 144}, // Dark Cyan
"cyan80": &rgb{0, 128, 128}, // Dark Cyan
"cyan90": &rgb{0, 112, 112}, // Dark Cyan
"cyan100": &rgb{0, 96, 96}, // Dark Cyan
// Blue tones
"blue": &rgb{0, 0, 255}, // Blue
"blue10": &rgb{0, 0, 240}, // Light Blue
"blue20": &rgb{0, 0, 224}, // Light Blue
"blue30": &rgb{0, 0, 208}, // Light Blue
"blue40": &rgb{0, 0, 192}, // Light Blue
"blue50": &rgb{0, 0, 176}, // Light Blue
"blue60": &rgb{0, 0, 160}, // Dark Blue
"blue70": &rgb{0, 0, 144}, // Dark Blue
"blue80": &rgb{0, 0, 128}, // Dark Blue
"blue90": &rgb{0, 0, 112}, // Dark Blue
"blue100": &rgb{0, 0, 96}, // Dark Blue
// Magenta tones
"magenta": &rgb{255, 0, 255}, // Magenta
"magenta10": &rgb{240, 0, 240}, // Light Magenta
"magenta20": &rgb{224, 0, 224}, // Light Magenta
"magenta30": &rgb{208, 0, 208}, // Light Magenta
"magenta40": &rgb{192, 0, 192}, // Light Magenta
"magenta50": &rgb{176, 0, 176}, // Light Magenta
"magenta60": &rgb{160, 0, 160}, // Dark Magenta
"magenta70": &rgb{144, 0, 144}, // Dark Magenta
"magenta80": &rgb{128, 0, 128}, // Dark Magenta
"magenta90": &rgb{112, 0, 112}, // Dark Magenta
"magenta100": &rgb{96, 0, 96}, // Dark Magenta
// White tones
"white": &rgb{255, 255, 255}, // White
"white10": &rgb{240, 240, 240}, // Light White
"white20": &rgb{224, 224, 224}, // Light White
"white30": &rgb{208, 208, 208}, // Light White
"white40": &rgb{192, 192, 192}, // Light White
"white50": &rgb{176, 176, 176}, // Light White
"white60": &rgb{160, 160, 160}, // Dark White
"white70": &rgb{144, 144, 144}, // Dark White
"white80": &rgb{128, 128, 128}, // Dark White
"white90": &rgb{112, 112, 112}, // Dark White
"white100": &rgb{96, 96, 96}, // Dark White
// Black tones
"black": &rgb{0, 0, 0}, // Black
"black10": &rgb{16, 16, 16}, // Dark Gray
"black20": &rgb{32, 32, 32}, // Dark Gray
"black30": &rgb{48, 48, 48}, // Dark Gray
"black40": &rgb{64, 64, 64}, // Gray
"black50": &rgb{80, 80, 80}, // Gray
"black60": &rgb{96, 96, 96}, // Light Gray
"black70": &rgb{112, 112, 112}, // Light Gray
"black80": &rgb{128, 128, 128}, // Light Gray
"black90": &rgb{144, 144, 144}, // Light Gray
"black100": &rgb{160, 160, 160}, // Light Gray
}
// New colors to register
var additionalColors = make(map[string]color)
var sgrParams = map[string]uint8{
"reset": 0,
"bold": 1,
"italic": 3,
"blink": 5,
"underline": 4,
"overline": 53,
"invert": 7,
"strike": 9,
}
// Escape function generates an escape sequence for the given color name,
// specifying whether it's for text (foreground) or background. If the color
// name exists in additionalColors or defaultColors, it returns the corresponding
// escape sequence along with true. If the color name is not found, it returns
// an empty string and false.
func escape(name string, back bool) (string, bool) {
value, exists := additionalColors[name]
if exists {
return value.escape(back), true
}
value, exists = defaultColors[name]
if exists {
return value.escape(back), true
}
return "", false
}
// RegisterAnsi function, registers an ANSI color with the given name and code.
func RegisterAnsi(name string, code uint8) error {
return register(name, &ansi{code})
}
// RegisterRgb function, registers an RGB color with the given name and RGB components.
func RegisterRgb(name string, r, g, b uint8) error {
return register(name, &rgb{r, g, b})
}
// register function adds the specified color object to the additionalColors map
// with the given name, converting the name to lowercase for case-insensitive lookup.
// If a color with the same name already exists in the defaultColors map, it returns
// an error indicating that the color already exists. Otherwise, it adds the color
// to the additionalColors map and returns nil, indicating a successful registration.
func register(name string, color color) error {
if _, exist := defaultColors[strings.ToLower(name)]; exist {
return fmt.Errorf("color '%s' already exists", name)
}
additionalColors[strings.ToLower(name)] = color
return nil
}