-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
155 lines (124 loc) · 3.48 KB
/
color.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
package fancylog
import "go.uber.org/zap/buffer"
type Color []byte
// UncheckedCustomColor wrapper, this does not validate color
func UncheckedCustomColor(b []byte) Color {
return Color(b)
}
type ColorLogger struct {
*buffer.Buffer
}
var (
_pool = buffer.NewPool()
// Get retrieves a buffer from the pool, creating one if necessary.
Get = _pool.Get
)
var (
colorOff Color = []byte("\u001B[0m")
ColorRed Color = []byte("\u001B[0;31m")
ColorGreen Color = []byte("\u001B[0;32m")
ColorOrange Color = []byte("\u001B[0;33m")
ColorBlue Color = []byte("\u001B[0;34m")
ColorPurple Color = []byte("\u001B[0;35m")
ColorCyan Color = []byte("\u001B[0;36m")
ColorGray Color = []byte("\u001B[0;37m")
ColorFatalRed Color = []byte("\u001b[1m\u001b[31m\u001b[7m")
ColorDarkOrange Color = []byte("\u001b[1m\u001b[38;5;202m")
ColorBrightWhite Color = []byte("\u001b[1m\u001b[38;5;255m")
ColorNicePurple Color = []byte("\u001b[1m\u001b[38;5;99m")
)
func NewColorLogger() ColorLogger {
return ColorLogger{Get()}
}
// Off apply no color to the data
func (cb *ColorLogger) Off() {
_, _ = cb.Write(colorOff)
}
// Red apply red color to the data
func (cb *ColorLogger) Red() {
_, _ = cb.Write(ColorRed)
}
// Green apply green color to the data
func (cb *ColorLogger) Green() {
_, _ = cb.Write(ColorGreen)
}
// Orange apply orange color to the data
func (cb *ColorLogger) Orange() {
_, _ = cb.Write(ColorOrange)
}
// Blue apply blue color to the data
func (cb *ColorLogger) Blue() {
_, _ = cb.Write(ColorBlue)
}
// Purple apply purple color to the data
func (cb *ColorLogger) Purple() {
_, _ = cb.Write(ColorPurple)
}
// Cyan apply cyan color to the data
func (cb *ColorLogger) Cyan() {
_, _ = cb.Write(ColorCyan)
}
// Gray apply gray color to the data
func (cb *ColorLogger) Gray() {
_, _ = cb.Write(ColorGray)
}
// White apply gray color to the data
func (cb *ColorLogger) White() {
_, _ = cb.Write(ColorBrightWhite)
}
// BrightOrange apply gray color to the data
func (cb *ColorLogger) BrightOrange() {
_, _ = cb.Write(ColorDarkOrange)
}
// NicePurple apply gray color to the data
func (cb *ColorLogger) NicePurple() {
_, _ = cb.Write(ColorNicePurple)
}
// WriteColor apply given color
func (cb *ColorLogger) WriteColor(color Color) {
_, _ = cb.Write(color)
}
func (cb *ColorLogger) AppendSpace() {
_, _ = cb.Write([]byte(" "))
}
// Append byte slice to buffer
func (cb *ColorLogger) Append(data []byte) {
_, _ = cb.Write(data)
}
// AppendWithColor byte slice to buffer
func (cb *ColorLogger) AppendWithColor(data []byte, color Color) {
_, _ = cb.Write(mixer(data, color))
}
// mixer mix the color on and off byte with the actual data
func mixer(data []byte, color []byte) []byte {
var result []byte
return append(append(append(result, color...), data...), colorOff...)
}
// Red apply red color to the data
func Red(data []byte) []byte {
return mixer(data, ColorRed)
}
// Green apply green color to the data
func Green(data []byte) []byte {
return mixer(data, ColorGreen)
}
// Orange apply orange color to the data
func Orange(data []byte) []byte {
return mixer(data, ColorOrange)
}
// Blue apply blue color to the data
func Blue(data []byte) []byte {
return mixer(data, ColorBlue)
}
// Purple apply purple color to the data
func Purple(data []byte) []byte {
return mixer(data, ColorPurple)
}
// Cyan apply cyan color to the data
func Cyan(data []byte) []byte {
return mixer(data, ColorCyan)
}
// Gray apply gray color to the data
func Gray(data []byte) []byte {
return mixer(data, ColorGray)
}