-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcolors.go
178 lines (149 loc) · 4.08 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
172
173
174
175
176
177
178
package main
import (
"fmt"
"strconv"
"strings"
)
// ColorStub is the text string stub which the shell
// can interpret as an ANSI color
const ColorStub = "\033["
// Colorer is the convenience function to color things
var Colorer = Colors{}
// Palette stores the color code to int map
var Palette = map[string]int{
"default": 0,
"bold": 1,
"dim": 2,
"italics": 3,
"underline": 4,
"black": 30,
"white": 97,
"gray": 90,
"grey": 90,
"red": 31,
"yellow": 33,
"green": 32,
"cyan": 36,
"blue": 34,
"violet": 35,
"bgwhite": 47,
"bgblack": 40,
"bgred": 41,
"bgyellow": 43,
"bggreen": 42,
"bgcyan": 46,
"bgblue": 44,
"bgviolet": 45,
"lgray": 37,
"lgrey": 37,
"lred": 91,
"lyellow": 93,
"lgreen": 92,
"lcyan": 96,
"lblue": 94,
"lviolet": 95,
}
// Color applies the color code :color to the string :value
func Color(color string, value string) string {
format := ColorStub + strconv.Itoa(Palette[color]) + "m"
unformat := ColorStub + "0m"
finalValue := value
if strings.Contains(value, unformat) {
finalValue = strings.Replace(value, unformat, unformat+format, -1)
}
return fmt.Sprintf("%s%s%s%s", ColorStub, format, finalValue, unformat)
}
// Colors defines a struct for the Colorer
type Colors struct{}
// Default sets the string :value to the default color
func (*Colors) Default(value string) string {
return Color("default", value)
}
// Bold sets the string :value to bold
func (*Colors) Bold(value string) string {
return Color("bold", value)
}
// Dim dims the string :value
func (*Colors) Dim(value string) string {
return Color("dim", value)
}
// Italics italicises the string :value
func (*Colors) Italics(value string) string {
return Color("italics", value)
}
// Underline underlines the string :value
func (*Colors) Underline(value string) string {
return Color("underline", value)
}
// Black sets the string :value to black
func (*Colors) Black(value string) string {
return Color("black", value)
}
// Gray sets the string :value to gray
func (*Colors) Gray(value string) string {
return Color("gray", value)
}
// Grey sets the string :value to grey
func (*Colors) Grey(value string) string {
return Color("grey", value)
}
// Red sets the string :value to red
func (*Colors) Red(value string) string {
return Color("red", value)
}
// LightRed sets the string :value to light red
func (*Colors) LightRed(value string) string {
return Color("lred", value)
}
// Green sets the string :value to green
func (*Colors) Green(value string) string {
return Color("green", value)
}
// LightGreen sets the string :value to light green
func (*Colors) LightGreen(value string) string {
return Color("lgreen", value)
}
// Yellow sets the string :value to yellow
func (*Colors) Yellow(value string) string {
return Color("yellow", value)
}
// LightYellow sets the string :value to light yellow
func (*Colors) LightYellow(value string) string {
return Color("lyellow", value)
}
// Blue sets the string :value to blue
func (*Colors) Blue(value string) string {
return Color("blue", value)
}
// LightBlue sets the string :value to light blue
func (*Colors) LightBlue(value string) string {
return Color("lblue", value)
}
// Violet sets the string :value to violet
func (*Colors) Violet(value string) string {
return Color("violet", value)
}
// LightViolet sets the string :value to light violet
func (*Colors) LightViolet(value string) string {
return Color("lviolet", value)
}
// Cyan sets the string :value to cyan
func (*Colors) Cyan(value string) string {
return Color("cyan", value)
}
// LightCyan sets the string :value to light cyan
func (*Colors) LightCyan(value string) string {
return Color("lcyan", value)
}
// LightGray sets the string :value to light gray
func (*Colors) LightGray(value string) string {
return Color("lgray", value)
}
// LightGrey sets the string :value to light grey
func (*Colors) LightGrey(value string) string {
return Color("lgrey", value)
}
// White sets the string :value to light white
func (*Colors) White(value string) string {
return Color("white", value)
}