-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgormatter.go
171 lines (134 loc) · 3.5 KB
/
gormatter.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
package gormatter
import (
"fmt"
"strconv"
)
const (
styleStart = "\033["
styleEnd = "m"
styleReset = "\033[0m"
)
const (
styleBold = 1
styleItalic = 3
styleUnderline = 4
)
const (
foregroundMod = 30
backgroundMod = 40
foregroundHighMod = 90
backgroundHighMod = 100
)
const None = -1
const (
Black = iota
Red
Green
Yellow
Blue
Magenta
Cyan
White
HighBlack = Black + 10
HighRed = Red + 10
HighGreen = Green + 10
HighYellow = Yellow + 10
HighBlue = Blue + 10
HighMagenta = Magenta + 10
HighCyan = Cyan + 10
HighWhite = White + 10
)
func Normal(foregroundColor int, backgroundColor int, text string) string {
style := styleStart
style += colors(foregroundColor, backgroundColor)
style += styleEnd
return fmt.Sprintf("%s%s%s", style, text, styleReset)
}
func Bold(foregroundColor int, backgroundColor int, text string) string {
style := styleStart
style += attr(styleBold, true)
style += colors(foregroundColor, backgroundColor)
style += styleEnd
return fmt.Sprintf("%s%s%s", style, text, styleReset)
}
func Italic(foregroundColor int, backgroundColor int, text string) string {
style := styleStart
style += attr(styleItalic, true)
style += colors(foregroundColor, backgroundColor)
style += styleEnd
return fmt.Sprintf("%s%s%s", style, text, styleReset)
}
func Underline(foregroundColor int, backgroundColor int, text string) string {
style := styleStart
style += attr(styleUnderline, true)
style += colors(foregroundColor, backgroundColor)
style += styleEnd
return fmt.Sprintf("%s%s%s", style, text, styleReset)
}
func BoldItalic(foregroundColor int, backgroundColor int, text string) string {
style := styleStart
style += attr(styleBold, true)
style += attr(styleItalic, true)
style += colors(foregroundColor, backgroundColor)
style += styleEnd
return fmt.Sprintf("%s%s%s", style, text, styleReset)
}
func BoldUnderline(foregroundColor int, backgroundColor int, text string) string {
style := styleStart
style += attr(styleBold, true)
style += attr(styleUnderline, true)
style += colors(foregroundColor, backgroundColor)
style += styleEnd
return fmt.Sprintf("%s%s%s", style, text, styleReset)
}
func BoldItalicUnderline(foregroundColor int, backgroundColor int, text string) string {
style := styleStart
style += attr(styleBold, true)
style += attr(styleItalic, true)
style += attr(styleUnderline, true)
style += colors(foregroundColor, backgroundColor)
style += styleEnd
return fmt.Sprintf("%s%s%s", style, text, styleReset)
}
func ItalicUnderline(foregroundColor int, backgroundColor int, text string) string {
style := styleStart
style += attr(styleItalic, true)
style += attr(styleUnderline, true)
style += colors(foregroundColor, backgroundColor)
style += styleEnd
return fmt.Sprintf("%s%s%s", style, text, styleReset)
}
func colors(foregroundColor, backgroundColor int) string {
var style string
if foregroundColor != None {
style += attr(fgColor(foregroundColor), false)
}
if backgroundColor != None {
if len(style) > 0 {
style += ";"
}
style += attr(bgColor(backgroundColor), false)
}
return style
}
func attr(a int, addSeparator bool) string {
attr := strconv.Itoa(a)
if addSeparator {
attr += ";"
}
return attr
}
func fgColor(color int) int {
highColor := color >= 10
if highColor {
return foregroundHighMod + (color - 10)
}
return foregroundMod + color
}
func bgColor(color int) int {
highColor := color >= 10
if highColor {
return backgroundHighMod + (color - 10)
}
return backgroundMod + color
}