-
Notifications
You must be signed in to change notification settings - Fork 2
/
sgr.go
308 lines (231 loc) · 5.17 KB
/
sgr.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package ansiart2utf8
import (
"fmt"
"strconv"
"strings"
)
const (
SGR_BOLD uint32 = 1 << iota
SGR_FAINT
SGR_ITALIC
SGR_UNDERLINE
SGR_BLNK_SLOW
SGR_BLNK_FAST
SGR_INVERSE
SGR_CONCEAL
SGR_STRIKETHROUGH
)
const (
CIX_FG = iota
CIX_BG
CIX_MAX
)
const (
DEFAULT_FG int = 37
DEFAULT_BG int = 40
)
type SGR struct {
// Bold, Faint, Italic, Underline, Blink, Inverse, Conceal, Strikethrough bool
Flags uint32
Color [CIX_MAX][]int
}
func (pS *SGR) Fset(f uint32) {
pS.Flags |= f
}
func (pS *SGR) Fclr(f uint32) {
pS.Flags &^= f
}
func IaEqual(A, B []int) bool {
if len(A) != len(B) {
return false
}
for ix := range A {
if A[ix] != B[ix] {
return false
}
}
return true
}
func (pS *SGR) ToEsc(pPrev *SGR, bAsDiff, bXterm256, bFakeEscape bool) string {
sParts := []int{}
bsIter := []struct {
Flag uint32
Set int
Clear int
}{
{SGR_BOLD, 1, 22},
{SGR_FAINT, 2, 22},
{SGR_ITALIC, 3, 23},
{SGR_UNDERLINE, 4, 24},
{SGR_BLNK_SLOW, 5, 25},
{SGR_BLNK_FAST, 6, 25},
{SGR_INVERSE, 7, 27},
{SGR_CONCEAL, 8, 28},
{SGR_STRIKETHROUGH, 9, 29},
}
flagDiff := pS.Flags ^ pPrev.Flags
// APPEND ANSI CODES FOR TEXT STYLE
for _, sITER := range bsIter {
if bAsDiff {
// NOTE flagDiff (A XOR B) HAS 1s WHERE ATTRIBUTES DIFFER
if (sITER.Flag & flagDiff) != 0 {
if (sITER.Flag & pS.Flags) != 0 {
sParts = append(sParts, sITER.Set)
} else {
sParts = append(sParts, sITER.Clear)
}
}
} else if (sITER.Flag & pS.Flags) != 0 {
sParts = append(sParts, sITER.Set)
}
}
// APPEND ANSI CODES FOR FG/BG COLORS
for CIX := range []int{CIX_FG, CIX_BG} {
// NOTE: NEEDS TO PRE-NORMALIZE CELL COLOR TO CORRECTLY TRACK DIFFERENCES
// (i.e. interplay of bold brightening the color & xterm256 translation)
sClr := pS.GetColor(CIX, bXterm256)
if bAsDiff {
sClrPrev := pPrev.GetColor(CIX, bXterm256)
if IaEqual(sClr, sClrPrev) {
continue
}
}
sParts = append(sParts, sClr...)
}
// EARLY EXIT
if len(sParts) == 0 {
return ""
}
// GENERATE ESCAPE CODE
pfx := "\x1b["
if bFakeEscape {
pfx = pfx + "96m^[" + pfx + "0m["
}
sStr := make([]string, len(sParts))
for ix := range sParts {
sStr[ix] = strconv.FormatInt(int64(sParts[ix]), 10)
}
return pfx + strings.Join(sStr, ";") + "m"
}
func (pS *SGR) GetColor(CIX int, bXterm256 bool) (RET []int) {
mDefaultClr := map[int]int{
CIX_FG: DEFAULT_FG,
CIX_BG: DEFAULT_BG,
}
if CIX < len(pS.Color) {
if len(pS.Color[CIX]) == 0 {
RET = []int{mDefaultClr[CIX]}
} else {
RET = pS.Color[CIX]
}
if bXterm256 {
RET = TranslateColors(RET, (pS.Flags&SGR_BOLD) != 0)
}
}
return
}
/*
MergeCodes SGR int codes (like ESC[0m) into an existing SGR struct
*/
func (pS *SGR) MergeCodes(biCodes []int) error {
type Action struct {
Set bool
Flags uint32
}
ACT := map[int]Action{
1: Action{true, SGR_BOLD},
2: Action{true, SGR_FAINT},
3: Action{true, SGR_ITALIC},
4: Action{true, SGR_UNDERLINE},
5: Action{true, SGR_BLNK_SLOW},
6: Action{true, SGR_BLNK_FAST},
7: Action{true, SGR_INVERSE},
8: Action{true, SGR_CONCEAL},
9: Action{true, SGR_STRIKETHROUGH},
21: Action{false, SGR_BOLD},
22: Action{false, SGR_BOLD | SGR_FAINT},
23: Action{false, SGR_ITALIC},
24: Action{false, SGR_UNDERLINE},
25: Action{false, SGR_BLNK_SLOW | SGR_BLNK_FAST},
27: Action{false, SGR_INVERSE},
28: Action{false, SGR_CONCEAL},
29: Action{false, SGR_STRIKETHROUGH},
}
nCodes := len(biCodes)
for i := 0; i < nCodes; i++ {
switch biCodes[i] {
// RESET
case 0:
*pS = SGR{}
// DEFAULT FG
case 39:
pS.Color[CIX_FG] = []int{}
// DEFAULT BG
case 49:
pS.Color[CIX_BG] = []int{}
// HIGH COLOR FG
// HIGH COLOR BG
case 38, 48:
sColor, nAdvance := HighColor(biCodes[i:])
if nAdvance > 0 {
switch biCodes[i] {
case 38:
pS.Color[CIX_FG] = sColor
case 48:
pS.Color[CIX_BG] = sColor
}
i += nAdvance
continue
} else {
return fmt.Errorf("SGR-SKIP [HCOLOR]: %d", biCodes[i])
}
default:
if oA, bOK := ACT[biCodes[i]]; bOK {
// DISPLAY ATTRIBUTES (bold, underline, etc)
if oA.Set {
pS.Fset(oA.Flags)
} else {
pS.Fclr(oA.Flags)
}
} else if IsBtween(biCodes[i], 30, 37) || IsBtween(biCodes[i], 90, 97) {
// CLASSIC FG
pS.Color[CIX_FG] = []int{biCodes[i]}
} else if IsBtween(biCodes[i], 40, 47) || IsBtween(biCodes[i], 100, 107) {
// CLASSIC BG
pS.Color[CIX_BG] = []int{biCodes[i]}
} else {
return fmt.Errorf("SGR-SKIP [UNKWN]: %d", biCodes[i])
}
}
}
return nil
}
func IsBtween(v, lo, hi int) bool {
return (v >= lo) && (v <= hi)
}
/*
Formats high color SGR codes
*/
func HighColor(arCodes []int) ([]int, int) {
nCodes := len(arCodes)
fnKosher := func(i int) bool {
return ((i >= 0) && (i <= 255))
}
if nCodes >= 3 {
switch arCodes[1] {
// 5;n where n is color index (0..255)
case 5:
if fnKosher(arCodes[2]) {
return arCodes[0:3], 2
}
// 2;r;g;b where r,g,b are red, green and blue color channels (out of 255)
case 2:
if nCodes >= 5 {
if fnKosher(arCodes[2]) && fnKosher(arCodes[3]) && fnKosher(arCodes[4]) {
return arCodes[0:5], 4
}
}
}
}
return []int{}, 0
}