-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.go
More file actions
333 lines (318 loc) · 6.83 KB
/
Copy pathinput.go
File metadata and controls
333 lines (318 loc) · 6.83 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package agentui
import (
"io"
"strconv"
"strings"
"time"
"unicode/utf8"
)
type inputRead struct {
data []byte
err error
}
func (p *Program) readInput() {
reads := make(chan inputRead, 1)
go readInputChunks(p.cfg.input, reads)
var pending []byte
var timer *time.Timer
var timerC <-chan time.Time
stopTimer := func() {
if timer == nil {
return
}
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
timer = nil
timerC = nil
}
startTimer := func() {
stopTimer()
if p.cfg.escTimeout <= 0 || !shouldTimeoutPending(pending) {
return
}
timer = time.NewTimer(p.cfg.escTimeout)
timerC = timer.C
}
for {
select {
case read := <-reads:
if len(read.data) > 0 {
pending = append(pending, read.data...)
pending = p.sendParsedInput(pending, false)
}
if read.err != nil {
stopTimer()
if len(pending) > 0 {
p.sendParsedInput(pending, true)
}
p.Send(QuitMsg{})
return
}
if len(pending) > 0 {
startTimer()
} else {
stopTimer()
}
case <-timerC:
timerC = nil
if len(pending) > 0 {
pending = p.sendParsedInput(pending, true)
}
if len(pending) > 0 {
startTimer()
}
case <-p.done:
stopTimer()
return
}
}
}
func readInputChunks(r io.Reader, reads chan<- inputRead) {
buf := make([]byte, 4096)
for {
n, err := r.Read(buf)
read := inputRead{err: err}
if n > 0 {
read.data = append([]byte(nil), buf[:n]...)
}
reads <- read
if err != nil {
return
}
}
}
func (p *Program) sendParsedInput(data []byte, flush bool) []byte {
msgs, rest := parseInput(data, flush)
for _, msg := range msgs {
p.Send(msg)
}
return rest
}
// ParseInput converts terminal bytes into messages. The returned rest contains
// incomplete trailing escape or UTF-8 sequences.
func ParseInput(data []byte) ([]Msg, []byte) {
return parseInput(data, false)
}
func parseInput(data []byte, flush bool) ([]Msg, []byte) {
var msgs []Msg
for len(data) > 0 {
if data[0] == 0x1b {
msg, n, complete := parseEscape(data)
if !complete {
if flush {
msgs = append(msgs, KeyMsg{Type: KeyEsc})
data = data[1:]
continue
}
return msgs, data
}
if msg != nil {
msgs = append(msgs, msg)
}
data = data[n:]
continue
}
b := data[0]
switch b {
case '\r', '\n':
msgs = append(msgs, KeyMsg{Type: KeyEnter})
data = data[1:]
case '\t':
msgs = append(msgs, KeyMsg{Type: KeyTab})
data = data[1:]
case 0x7f:
msgs = append(msgs, KeyMsg{Type: KeyBackspace})
data = data[1:]
default:
if b < 0x20 {
if t := ctrlKeyType(b); t != KeyUnknown {
msgs = append(msgs, KeyMsg{Type: t})
}
data = data[1:]
continue
}
r, n := utf8.DecodeRune(data)
if r == utf8.RuneError && n == 1 && !utf8.FullRune(data) {
return msgs, data
}
if r == ' ' {
msgs = append(msgs, KeyMsg{Type: KeySpace})
} else {
msgs = append(msgs, KeyMsg{Type: KeyRunes, Runes: []rune{r}})
}
data = data[n:]
}
}
return msgs, nil
}
func shouldTimeoutPending(data []byte) bool {
if len(data) == 0 || data[0] != 0x1b {
return false
}
return !strings.HasPrefix(string(data), "\x1b[200~")
}
func parseEscape(data []byte) (Msg, int, bool) {
if len(data) == 1 {
return KeyMsg{Type: KeyEsc}, 1, false
}
if strings.HasPrefix(string(data), "\x1b[200~") {
end := strings.Index(string(data), "\x1b[201~")
if end < 0 {
return nil, 0, false
}
payload := string(data[len("\x1b[200~"):end])
return KeyMsg{Type: KeyRunes, Runes: []rune(payload), Paste: true}, end + len("\x1b[201~"), true
}
if strings.HasPrefix(string(data), "\x1b[<") {
if msg, n, ok := parseSGRMouse(data); ok {
return msg, n, true
}
return nil, 0, false
}
if len(data) >= 3 {
switch string(data[:3]) {
case "\x1b[A":
return KeyMsg{Type: KeyUp}, 3, true
case "\x1b[B":
return KeyMsg{Type: KeyDown}, 3, true
case "\x1b[C":
return KeyMsg{Type: KeyRight}, 3, true
case "\x1b[D":
return KeyMsg{Type: KeyLeft}, 3, true
case "\x1b[H":
return KeyMsg{Type: KeyHome}, 3, true
case "\x1b[F":
return KeyMsg{Type: KeyEnd}, 3, true
case "\x1b[I":
return FocusMsg{}, 3, true
case "\x1b[O":
return BlurMsg{}, 3, true
}
}
if len(data) >= 4 {
switch string(data[:4]) {
case "\x1b[3~":
return KeyMsg{Type: KeyDelete}, 4, true
case "\x1b[5~":
return KeyMsg{Type: KeyPgUp}, 4, true
case "\x1b[6~":
return KeyMsg{Type: KeyPgDown}, 4, true
case "\x1b[1~", "\x1b[7~":
return KeyMsg{Type: KeyHome}, 4, true
case "\x1b[4~", "\x1b[8~":
return KeyMsg{Type: KeyEnd}, 4, true
}
}
if len(data) >= 6 {
switch string(data[:6]) {
case "\x1b[1;5C":
return KeyMsg{Type: KeyCtrlRight}, 6, true
case "\x1b[1;5D":
return KeyMsg{Type: KeyCtrlLeft}, 6, true
}
}
if len(data) >= 2 {
if data[1] == '\r' || data[1] == '\n' {
return KeyMsg{Type: KeyEnter, Alt: true}, 2, true
}
if data[1] == 'b' || data[1] == 'B' {
return KeyMsg{Type: KeyLeft, Alt: true}, 2, true
}
if data[1] == 'f' || data[1] == 'F' {
return KeyMsg{Type: KeyRight, Alt: true}, 2, true
}
r, n := utf8.DecodeRune(data[1:])
if r == utf8.RuneError && n == 1 && !utf8.FullRune(data[1:]) {
return nil, 0, false
}
if n > 0 && r != utf8.RuneError {
return KeyMsg{Type: KeyRunes, Runes: []rune{r}, Alt: true}, 1 + n, true
}
}
return KeyMsg{Type: KeyEsc}, 1, true
}
func parseSGRMouse(data []byte) (MouseMsg, int, bool) {
s := string(data)
end := strings.IndexAny(s, "Mm")
if end < 0 {
return MouseMsg{}, 0, false
}
final := s[end]
body := strings.TrimPrefix(s[:end], "\x1b[<")
parts := strings.Split(body, ";")
if len(parts) != 3 {
return MouseMsg{}, end + 1, true
}
code, _ := strconv.Atoi(parts[0])
x, _ := strconv.Atoi(parts[1])
y, _ := strconv.Atoi(parts[2])
msg := MouseMsg{X: x, Y: y}
if final == 'm' {
msg.Action = MouseActionRelease
} else {
msg.Action = MouseActionPress
}
switch {
case code&64 != 0 && code&1 == 0:
msg.Button = MouseButtonWheelUp
case code&64 != 0 && code&1 == 1:
msg.Button = MouseButtonWheelDown
case code&3 == 0:
msg.Button = MouseButtonLeft
case code&3 == 1:
msg.Button = MouseButtonMiddle
case code&3 == 2:
msg.Button = MouseButtonRight
default:
msg.Button = MouseButtonUnknown
}
return msg, end + 1, true
}
func ctrlKeyType(b byte) KeyType {
switch b {
case 0x01:
return KeyCtrlA
case 0x02:
return KeyCtrlB
case 0x03:
return KeyCtrlC
case 0x04:
return KeyCtrlD
case 0x05:
return KeyCtrlE
case 0x06:
return KeyCtrlF
case 0x07:
return KeyCtrlG
case 0x08:
return KeyCtrlH
case 0x0a:
return KeyCtrlJ
case 0x0b:
return KeyCtrlK
case 0x0c:
return KeyCtrlL
case 0x0d:
return KeyCtrlM
case 0x0e:
return KeyCtrlN
case 0x0f:
return KeyCtrlO
case 0x10:
return KeyCtrlP
case 0x12:
return KeyCtrlR
case 0x14:
return KeyCtrlT
case 0x15:
return KeyCtrlU
case 0x17:
return KeyCtrlW
default:
return KeyUnknown
}
}