forked from liamg/darktile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sgr.go
264 lines (241 loc) · 8.46 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
package terminal
import (
"fmt"
"strconv"
"strings"
"github.com/liamg/aminal/buffer"
"github.com/liamg/aminal/config"
)
func sgrSequenceHandler(params []string, intermediate string, terminal *Terminal) error {
if len(params) == 0 {
params = []string{"0"}
}
for i := range params {
p := strings.Replace(strings.Replace(params[i], "[", "", -1), "]", "", -1)
switch p {
case "00", "0":
attr := terminal.ActiveBuffer().CursorAttr()
*attr = buffer.CellAttributes{
FgColour: terminal.config.ColourScheme.Foreground,
BgColour: terminal.config.ColourScheme.Background,
}
case "1", "01":
terminal.ActiveBuffer().CursorAttr().Bold = true
case "2", "02":
terminal.ActiveBuffer().CursorAttr().Dim = true
case "4", "04":
terminal.ActiveBuffer().CursorAttr().Underline = true
case "5", "05":
terminal.ActiveBuffer().CursorAttr().Blink = true
case "7", "07":
terminal.ActiveBuffer().CursorAttr().Reverse = true
case "8", "08":
terminal.ActiveBuffer().CursorAttr().Hidden = true
case "21":
terminal.ActiveBuffer().CursorAttr().Bold = false
case "22":
terminal.ActiveBuffer().CursorAttr().Dim = false
case "23":
// not italic
case "24":
terminal.ActiveBuffer().CursorAttr().Underline = false
case "25":
terminal.ActiveBuffer().CursorAttr().Blink = false
case "27":
terminal.ActiveBuffer().CursorAttr().Reverse = false
case "28":
terminal.ActiveBuffer().CursorAttr().Hidden = false
case "29":
// not strikethrough
case "39":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.Foreground
case "30":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.Black
case "31":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.Red
case "32":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.Green
case "33":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.Yellow
case "34":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.Blue
case "35":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.Magenta
case "36":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.Cyan
case "37":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.White
case "90":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.DarkGrey
case "91":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.LightRed
case "92":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.LightGreen
case "93":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.LightYellow
case "94":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.LightBlue
case "95":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.LightMagenta
case "96":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.LightCyan
case "97":
terminal.ActiveBuffer().CursorAttr().FgColour = terminal.config.ColourScheme.White
case "49":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.Background
case "40":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.Black
case "41":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.Red
case "42":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.Green
case "43":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.Yellow
case "44":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.Blue
case "45":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.Magenta
case "46":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.Cyan
case "47":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.White
case "100":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.DarkGrey
case "101":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.LightRed
case "102":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.LightGreen
case "103":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.LightYellow
case "104":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.LightBlue
case "105":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.LightMagenta
case "106":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.LightCyan
case "107":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.White
case "38": // set foreground
c, err := terminal.getANSIColour(params[i:])
if err != nil {
return err
}
terminal.ActiveBuffer().CursorAttr().FgColour = c
return nil
case "48": // set background
c, err := terminal.getANSIColour(params[i:])
if err != nil {
return err
}
terminal.ActiveBuffer().CursorAttr().BgColour = c
return nil
default:
return fmt.Errorf("Unknown SGR control sequence: (ESC[%s%sm)", params[i:], intermediate)
}
}
//terminal.logger.Debugf("SGR control sequence: (ESC[%s%sm)", param, intermediate)
return nil
}
func (terminal *Terminal) getANSIColour(params []string) (config.Colour, error) {
if len(params) > 2 {
switch params[1] {
case "5":
// 8 bit colour
colNum, err := strconv.Atoi(params[2])
if err != nil || colNum >= 256 || colNum < 0 {
return [3]float32{0, 0, 0}, fmt.Errorf("Invalid 8-bit colour specifier")
}
return terminal.get8BitSGRColour(uint8(colNum)), nil
case "2":
if len(params) < 4 {
return [3]float32{0, 0, 0}, fmt.Errorf("Invalid true colour specifier")
}
// 24 bit colour
if len(params) == 5 { // standard true colour
r, err := strconv.Atoi(params[2])
if err != nil {
return [3]float32{0, 0, 0}, fmt.Errorf("Invalid true colour specifier")
}
g, err := strconv.Atoi(params[3])
if err != nil {
return [3]float32{0, 0, 0}, fmt.Errorf("Invalid true colour specifier")
}
b, err := strconv.Atoi(params[4])
if err != nil {
return [3]float32{0, 0, 0}, fmt.Errorf("Invalid true colour specifier")
}
return [3]float32{
float32(r) / 0xff,
float32(g) / 0xff,
float32(b) / 0xff,
}, nil
} else if len(params) > 5 { // ISO/IEC International Standard 8613-6
r, err := strconv.Atoi(params[3])
if err != nil {
return [3]float32{0, 0, 0}, fmt.Errorf("Invalid true colour specifier")
}
g, err := strconv.Atoi(params[4])
if err != nil {
return [3]float32{0, 0, 0}, fmt.Errorf("Invalid true colour specifier")
}
b, err := strconv.Atoi(params[5])
if err != nil {
return [3]float32{0, 0, 0}, fmt.Errorf("Invalid true colour specifier")
}
return [3]float32{
float32(r) / 0xff,
float32(g) / 0xff,
float32(b) / 0xff,
}, nil
}
}
}
return [3]float32{}, fmt.Errorf("Unknown ANSI colour format identifier")
}
func (terminal *Terminal) get8BitSGRColour(colNum uint8) [3]float32 {
// https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
switch colNum {
case 0:
return terminal.config.ColourScheme.Black
case 1:
return terminal.config.ColourScheme.Red
case 2:
return terminal.config.ColourScheme.Green
case 3:
return terminal.config.ColourScheme.Yellow
case 4:
return terminal.config.ColourScheme.Blue
case 5:
return terminal.config.ColourScheme.Magenta
case 6:
return terminal.config.ColourScheme.Cyan
case 7:
return terminal.config.ColourScheme.White
case 8:
return terminal.config.ColourScheme.DarkGrey
case 9:
return terminal.config.ColourScheme.LightRed
case 10:
return terminal.config.ColourScheme.LightGreen
case 11:
return terminal.config.ColourScheme.LightYellow
case 12:
return terminal.config.ColourScheme.LightBlue
case 13:
return terminal.config.ColourScheme.LightMagenta
case 14:
return terminal.config.ColourScheme.LightCyan
case 15:
return terminal.config.ColourScheme.White
}
if colNum < 232 {
index := int(colNum - 16) // 0-216
rgb := (index * 0xffffff) / 216
r := float32((rgb&0xff0000)>>16) / 0xff
g := float32((rgb&0xff00)>>8) / 0xff
b := float32(rgb&0xff) / 0xff
return [3]float32{r, g, b}
}
c := float32(colNum-232) / 0x18
return [3]float32{c, c, c}
}