-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
441 lines (392 loc) · 8.03 KB
/
term.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package term
import (
"bufio"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"unsafe"
)
type Terminal struct {
in *os.File
inBuffer *bufio.Reader
out *os.File
runes chan rune
events chan *Event
running bool
originalMode *termios
sigwinch chan os.Signal
Width int
Height int
}
func NewTerminal() *Terminal {
return &Terminal{}
}
func (t *Terminal) Start() error {
var err error
if t.in == nil {
if t.in, err = os.OpenFile("/dev/tty", os.O_RDONLY, 0); err != nil {
return err
}
}
if t.inBuffer == nil {
t.inBuffer = bufio.NewReader(t.in)
} else {
t.inBuffer.Reset(t.in)
}
if t.out == nil {
if t.out, err = os.OpenFile("/dev/tty", os.O_WRONLY, 0); err != nil {
return err
}
}
t.running = true
fd := t.out.Fd()
t.originalMode, err = fetchMode(fd)
if err != nil {
return err
}
mode := cloneMode(t.originalMode)
mode.Iflag &^= syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK | syscall.ISTRIP | syscall.INLCR | syscall.IGNCR | syscall.ICRNL | syscall.IXON
mode.Oflag &^= syscall.OPOST
mode.Cflag |= syscall.CS8
mode.Cflag &^= syscall.CSIZE | syscall.PARENB
mode.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG | syscall.IEXTEN
err = setMode(fd, mode)
if err != nil {
return err
}
t.Width, t.Height, err = windowSize(t.out.Fd())
if err != nil {
return err
}
t.sigwinch = make(chan os.Signal, 1)
signal.Notify(t.sigwinch, syscall.SIGWINCH)
t.runes = make(chan rune, 500)
t.events = make(chan *Event, 500)
go t.readRunes()
go t.readEvents()
return nil
}
func (t *Terminal) Stop() {
t.running = false
close(t.runes)
close(t.events)
signal.Reset(syscall.SIGWINCH)
t.Puts(termShowCursor)
setMode(t.out.Fd(), t.originalMode)
// Make sure we stop reading text from stdin
t.inBuffer.Reset(&NopReader{})
}
type EventType int
const (
EventKey EventType = iota
EventResize
)
type Event struct {
Type EventType
Key Key
Rune rune
}
func (t *Terminal) readRunes() {
for {
r, _, err := t.inBuffer.ReadRune()
if !t.running {
return
}
if err != nil {
panic(err) // TODO don't panic
}
t.runes <- r
}
}
func (t *Terminal) readEvents() {
for {
select {
case _, ok := <-t.sigwinch:
if !ok {
return
}
var err error
t.Width, t.Height, err = windowSize(t.out.Fd())
if err != nil {
// TODO dont panic, send error event
t.Stop()
panic(err)
}
t.events <- &Event{Type: EventResize}
case r, ok := <-t.runes:
if !ok {
return
}
seq := []rune{r}
// Gather up a few more characters for 50ms
timeout := time.After(50 * time.Millisecond)
wait:
for {
select {
case r := <-t.runes:
seq = append(seq, r)
continue wait
case <-timeout:
break wait
}
}
i := 0
for i < len(seq) {
r := seq[i]
if r == KeyEsc {
if len(seq[i+1:]) >= 2 {
key := KeyUnknown
switch string(seq[i+1 : i+3]) {
case "[A":
key = KeyUp
case "[B":
key = KeyDown
case "[C":
key = KeyRight
case "[D":
key = KeyLeft
case "[H":
key = KeyHome
case "[F":
key = KeyEnd
case "[Z":
key = KeyShiftTab
case "OH":
key = KeyHome
case "OF":
key = KeyEnd
}
if key != KeyUnknown {
t.events <- &Event{Type: EventKey, Key: key}
i = i + 4
continue
}
}
if len(seq[i+1:]) >= 3 {
key := KeyUnknown
switch string(seq[i+1 : i+4]) {
case "[3~":
key = KeyDel
case "[5~":
key = KeyPageUp
case "[6~":
key = KeyPageDown
}
if key != KeyUnknown {
t.events <- &Event{Type: EventKey, Key: key}
i = i + 5
continue
}
}
r = rune(KeyEsc)
goto normal
}
if r < 27 || r == 127 {
t.events <- &Event{Type: EventKey, Key: Key(r)}
i++
continue
}
normal:
t.events <- &Event{Type: EventKey, Key: KeyRune, Rune: r}
i++
}
}
}
}
func (t *Terminal) Events() chan *Event {
return t.events
}
func (t *Terminal) Puts(f string, args ...interface{}) {
fmt.Fprintf(t.out, f, args...)
}
const (
termClear string = "\x1b[H\x1b[J"
termClearLine = "\x1b[0K"
termSetColor = "\x1b[%d;%dm"
termSetFgBg = "\x1b[3%d;4%dm"
termAttrOff = "\x1b[0;10m"
termReset = "\x1b[0m"
termEnterAcs = "\x1b[11m"
termExitAcs = "\x1b[10m"
termEnterCA = "\x1b[?1049h"
termExitCA = "\x1b[r\x1b[?1049l"
termHideCursor = "\x1b[?25l"
termShowCursor = "\x1b[?25h"
termSetCursor = "\x1b[%d;%dH"
termSetCursorColumn = "\x1b[%dG"
)
func (t *Terminal) Clear() {
t.Puts(termClear)
}
func (t *Terminal) ShowCursor() {
t.Puts(termShowCursor)
}
func (t *Terminal) HideCursor() {
t.Puts(termHideCursor)
}
func (t *Terminal) ClearLine() {
t.Puts(termClearLine)
}
func (t *Terminal) SetCursor(x, y int) {
t.Puts(termSetCursor, y+1, x+1)
}
func (t *Terminal) SetCursorColumn(x int) {
t.Puts(termSetCursorColumn, x+1)
}
func (t *Terminal) setColor(c Color, isBg bool) {
var brightValue = 0
if c > 8 && c <= 16 {
c -= 8
brightValue = 1
}
if isBg {
c += 40
} else {
c += 30
}
t.Puts(termSetColor, brightValue, c)
}
func (t *Terminal) SetFg(c Color) {
t.setColor(c, false)
}
func (t *Terminal) SetBg(c Color) {
t.setColor(c, true)
}
func (t *Terminal) Reset() {
t.Puts(termReset)
}
type Color int
const (
ColorBlack Color = iota
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorPurple
ColorCyan
ColorLightGray
ColorDefault
)
const (
ColorGray Color = iota + 9
ColorBrightRed
ColorBrightGreen
ColorBrightYellow
ColorBrightBlue
ColorBrightPurple
ColorBrightCyan
ColorWhite
)
type Key int
const (
KeyNull Key = 0
KeyCtrlA = 1
KeyCtrlB = 2
KeyCtrlC = 3
KeyCtrlD = 4
KeyCtrlE = 5
KeyCtrlF = 6
KeyCtrlG = 7
KeyCtrlH = 8
KeyTab = 9
KeyNl = 10
KeyCtrlK = 11
KeyCtrlL = 12
KeyCr = 13
KeyCtrlN = 14
KeyCtrlO = 15
KeyCtrlP = 16
KeyCtrlQ = 17
KeyCtrlR = 18
KeyCtrlS = 19
KeyCtrlT = 20
KeyCtrlU = 21
KeyCtrlV = 22
KeyCtrlW = 23
KeyCtrlX = 24
KeyCtrlY = 25
KeyCtrlZ = 26
KeyEsc = 27
KeyFs = 28
KeyGs = 29
KeyRs = 30
KeyUs = 31
KeyBackspace = 127
)
const (
KeyRune Key = iota + 1000
KeyUnknown
KeyLeft
KeyRight
KeyUp
KeyDown
KeyHome
KeyEnd
KeyInsert
KeyDel
KeyPageUp
KeyPageDown
KeyF1
KeyF2
KeyF3
KeyF4
KeyF5
KeyF6
KeyF7
KeyF8
KeyF9
KeyF10
KeyF11
KeyF12
KeyAltB
KeyAltF
KeyAltY
KeyShiftTab
KeyWordLeft
KeyWordRight
)
type termios struct {
syscall.Termios
}
func cloneMode(mode *termios) *termios {
clone := &termios{}
clone.Iflag = mode.Iflag
clone.Oflag = mode.Oflag
clone.Cflag = mode.Cflag
clone.Lflag = mode.Lflag
clone.Cc = mode.Cc
clone.Ispeed = mode.Ispeed
clone.Ospeed = mode.Ospeed
return clone
}
func setMode(fd uintptr, mode *termios) error {
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, termSetTermios, uintptr(unsafe.Pointer(mode)))
if errno != 0 {
return fmt.Errorf("Got error number %d setting terminal mode", errno)
}
return nil
}
func fetchMode(fd uintptr) (*termios, error) {
var mode termios
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, termGetTermios, uintptr(unsafe.Pointer(&mode)))
if errno != 0 {
return nil, fmt.Errorf("Got error number %d fetching terminal mode", errno)
}
return &mode, nil
}
func windowSize(fd uintptr) (int, int, error) {
dim := [4]uint16{}
dimp := uintptr(unsafe.Pointer(&dim))
ioc := uintptr(syscall.TIOCGWINSZ)
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL,
fd, ioc, dimp, 0, 0, 0); err != 0 {
return -1, -1, err
}
return int(dim[1]), int(dim[0]), nil
}
type NopReader struct{}
func (w *NopReader) Read(b []byte) (int, error) {
return len(b), nil
}