-
Notifications
You must be signed in to change notification settings - Fork 4
/
csi.go
225 lines (191 loc) · 4.17 KB
/
csi.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
package crt
import (
"github.com/muesli/termenv"
"strconv"
"strings"
"sync"
)
var csiMtx = &sync.Mutex{}
var csiCache = map[string]any{}
type CursorUpSeq struct {
Count int
}
type CursorDownSeq struct {
Count int
}
type CursorForwardSeq struct {
Count int
}
type CursorBackSeq struct {
Count int
}
type CursorNextLineSeq struct {
Count int
}
type CursorPreviousLineSeq struct {
Count int
}
type CursorHorizontalSeq struct {
Count int
}
type CursorPositionSeq struct {
Row int
Col int
}
type EraseDisplaySeq struct {
Type int
}
type EraseLineSeq struct {
Type int
}
type ScrollUpSeq struct {
Count int
}
type ScrollDownSeq struct {
Count int
}
type SaveCursorPositionSeq struct{}
type RestoreCursorPositionSeq struct{}
type ChangeScrollingRegionSeq struct {
Top int
Bottom int
}
type InsertLineSeq struct {
Count int
}
type DeleteLineSeq struct {
Count int
}
type CursorShowSeq struct{}
type CursorHideSeq struct{}
// extractCSI extracts a CSI sequence from the beginning of a string.
// It returns the sequence without any suffix, and a boolean indicating
// whether a sequence was found.
func extractCSI(s string) (string, bool) {
if !strings.HasPrefix(s, termenv.CSI) {
return "", false
}
s = s[len(termenv.CSI):]
if len(s) == 0 {
return "", false
}
for i, c := range s {
if c >= '@' && c <= '~' {
return termenv.CSI + s[:i+1], true
}
}
return "", false
}
// parseCSI parses a CSI sequence and returns a struct representing the sequence.
func parseCSI(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
}
csiMtx.Lock()
if cached, ok := csiCache[s]; ok {
csiMtx.Unlock()
return cached, true
}
csiMtx.Unlock()
if val, ok := parseCSIStruct(s); ok {
csiMtx.Lock()
csiCache[s] = val
csiMtx.Unlock()
return val, true
}
return nil, false
}
func parseCSIStruct(s string) (any, bool) {
switch s {
case termenv.ShowCursorSeq:
return CursorShowSeq{}, true
case termenv.HideCursorSeq:
return CursorHideSeq{}, true
}
switch s[len(s)-1] {
case 'A':
if count, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return CursorUpSeq{Count: count}, true
}
case 'B':
if count, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return CursorDownSeq{Count: count}, true
}
case 'C':
if count, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return CursorForwardSeq{Count: count}, true
}
case 'D':
if count, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return CursorBackSeq{Count: count}, true
}
case 'E':
if count, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return CursorNextLineSeq{Count: count}, true
}
case 'F':
if count, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return CursorPreviousLineSeq{Count: count}, true
}
case 'G':
if count, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return CursorHorizontalSeq{Count: count}, true
}
case 'H':
if strings.Contains(s, ";") {
parts := strings.Split(s[:len(s)-1], ";")
if len(parts) != 2 {
return nil, false
}
row, err := strconv.Atoi(parts[0])
if err != nil {
return nil, false
}
col, err := strconv.Atoi(parts[1])
if err != nil {
return nil, false
}
return CursorPositionSeq{Row: row, Col: col}, true
}
return nil, false
case 'J':
if t, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return EraseDisplaySeq{Type: t}, true
}
case 'K':
if t, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return EraseLineSeq{Type: t}, true
}
case 'S':
if count, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return ScrollUpSeq{Count: count}, true
}
case 'T':
if count, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return ScrollDownSeq{Count: count}, true
}
case 's':
if len(s) == 1 {
return SaveCursorPositionSeq{}, true
}
case 'u':
if len(s) == 1 {
return RestoreCursorPositionSeq{}, true
}
case 'r':
// TODO: implement
case 'L':
if count, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return InsertLineSeq{Count: count}, true
}
case 'M':
if count, err := strconv.Atoi(s[:len(s)-1]); err == nil {
return DeleteLineSeq{Count: count}, true
}
}
return nil, false
}