-
Notifications
You must be signed in to change notification settings - Fork 4
/
sgr.go
159 lines (131 loc) · 2.72 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
package crt
import (
"fmt"
"github.com/muesli/termenv"
"strings"
"sync"
)
var sgrMtx = &sync.Mutex{}
var sgrCache = map[string][]any{}
// extractSGR extracts an SGR ansi sequence from the beginning of the string.
func extractSGR(s string) (string, bool) {
if len(s) < 2 {
return "", false
}
if !strings.HasPrefix(s, termenv.CSI) {
return "", false
}
for i := 2; i < len(s); i++ {
if s[i] == ' ' || s[i] == termenv.CSI[0] {
return "", false
}
if s[i] == 'm' {
return s[:i+1], true
}
}
return "", false
}
type SGRReset struct{}
type SGRBold struct{}
type SGRUnsetBold struct{}
type SGRItalic struct{}
type SGRUnsetItalic struct{}
type SGRFgTrueColor struct {
R, G, B byte
}
type SGRBgTrueColor struct {
R, G, B byte
}
type SGRFgColor struct {
Id int
}
type SGRBgColor struct {
Id int
}
// parseSGR parses a single SGR ansi sequence and returns a struct representing the sequence.
func parseSGR(s string) ([]any, bool) {
if !strings.HasPrefix(s, termenv.CSI) {
return nil, false
}
s = s[len(termenv.CSI):]
if len(s) == 0 {
return nil, false
}
sgrMtx.Lock()
if cached, ok := sgrCache[s]; ok {
sgrMtx.Unlock()
return cached, true
}
sgrMtx.Unlock()
full := s
if !strings.HasSuffix(s, "m") {
return nil, false
}
s = s[:len(s)-1]
if len(s) == 0 {
return nil, false
}
var skips int
var res []any
for len(s) > 0 {
code := strings.SplitN(s, ";", 2)[0]
if skips > 0 {
skips--
} else {
switch code {
case "0":
res = append(res, SGRReset{})
case "1":
res = append(res, SGRBold{})
case "22":
res = append(res, SGRUnsetBold{})
case "3":
res = append(res, SGRItalic{})
case "23":
res = append(res, SGRUnsetItalic{})
default:
if strings.HasPrefix(s, "38;2;") {
var r, g, b byte
_, err := fmt.Sscanf(s, "38;2;%d;%d;%d", &r, &g, &b)
if err == nil {
skips = 4
res = append(res, SGRFgTrueColor{r, g, b})
continue
}
} else if strings.HasPrefix(s, "48;2;") {
var r, g, b byte
_, err := fmt.Sscanf(s, "48;2;%d;%d;%d", &r, &g, &b)
if err == nil {
skips = 4
res = append(res, SGRBgTrueColor{r, g, b})
continue
}
} else if strings.HasPrefix(s, "38;5;") {
var id int
_, err := fmt.Sscanf(s, "38;5;%d", &id)
if err == nil {
skips = 2
res = append(res, SGRFgColor{id})
continue
}
} else if strings.HasPrefix(s, "48;5;") {
var id int
_, err := fmt.Sscanf(s, "48;5;%d", &id)
if err == nil {
skips = 2
res = append(res, SGRBgColor{id})
continue
}
}
}
}
if len(code) >= len(s) {
break
}
s = s[len(code)+1:]
}
sgrMtx.Lock()
sgrCache[full] = res
sgrMtx.Unlock()
return res, len(res) > 0
}