-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorful.go
94 lines (86 loc) · 1.59 KB
/
colorful.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
package log
import (
"log/slog"
"strconv"
)
type Color int32
const (
reset = "\033[0m"
black Color = 30
red Color = 31
green Color = 32
yellow Color = 33
blue Color = 34
magenta Color = 35
cyan Color = 36
lightGray Color = 37
darkGray Color = 90
lightRed Color = 91
lightGreen Color = 92
lightYellow Color = 93
lightBlue Color = 94
lightMagenta Color = 95
lightCyan Color = 96
white Color = 97
)
func (c Color) String() string {
switch c {
case black:
return "30"
case red:
return "31"
case green:
return "32"
case yellow:
return "33"
case blue:
return "34"
case magenta:
return "35"
case cyan:
return "36"
case lightGray:
return "37"
case darkGray:
return "90"
case lightRed:
return "91"
case lightGreen:
return "92"
case lightYellow:
return "93"
case lightBlue:
return "94"
case lightMagenta:
return "95"
case lightCyan:
return "96"
case white:
return "97"
default:
return strconv.Itoa(int(c))
}
}
var (
colorHeader = []byte("\033[")
colorEnd = []byte(reset)
colorAgent byte = 'm'
)
func colorize(c Color, v string) []byte {
b := make([]byte, 0, 7+len(c.String())+len(v))
return append(append(append(append(append(b, colorHeader...), []byte(c.String())...), colorAgent), []byte(v)...), colorEnd...)
}
func getColor(level slog.Level) Color {
switch level {
case slog.LevelDebug:
return darkGray
case slog.LevelInfo:
return cyan
case slog.LevelWarn:
return lightYellow
case slog.LevelError:
return lightRed
default:
return black
}
}