forked from AlecAivazis/survey
/
runereader_posix.go
207 lines (172 loc) · 4.84 KB
/
runereader_posix.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
//go:build !windows
// +build !windows
// The terminal mode manipulation code is derived heavily from:
// https://github.com/golang/crypto/blob/master/ssh/terminal/util.go:
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package terminal
import (
"bufio"
"bytes"
"fmt"
"syscall"
"unsafe"
)
const (
normalKeypad = '['
applicationKeypad = 'O'
)
type runeReaderState struct {
term syscall.Termios
reader *bufio.Reader
buf *bytes.Buffer
}
func newRuneReaderState(input FileReader) runeReaderState {
buf := new(bytes.Buffer)
return runeReaderState{
reader: bufio.NewReader(&BufferedReader{
In: input,
Buffer: buf,
}),
buf: buf,
}
}
func (rr *RuneReader) Buffer() *bytes.Buffer {
return rr.state.buf
}
// For reading runes we just want to disable echo.
func (rr *RuneReader) SetTermMode() error {
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(rr.stdio.In.Fd()), ioctlReadTermios, uintptr(unsafe.Pointer(&rr.state.term)), 0, 0, 0); err != 0 {
return err
}
newState := rr.state.term
newState.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(rr.stdio.In.Fd()), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 {
return err
}
return nil
}
func (rr *RuneReader) RestoreTermMode() error {
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(rr.stdio.In.Fd()), ioctlWriteTermios, uintptr(unsafe.Pointer(&rr.state.term)), 0, 0, 0); err != 0 {
return err
}
return nil
}
func (rr *RuneReader) ReadRune() (rune, int, error) {
r, size, err := rr.state.reader.ReadRune()
if err != nil {
return r, size, err
}
// parse ^[ sequences to look for arrow keys
if r == '\033' {
if rr.state.reader.Buffered() == 0 {
// no more characters so must be `Esc` key
return KeyEscape, 1, nil
}
r, size, err = rr.state.reader.ReadRune()
if err != nil {
return r, size, err
}
switch r {
case normalKeypad:
return rr.readNormalKeypad()
case applicationKeypad:
return rr.readApplicationKeypad()
default:
return r, size, fmt.Errorf("unexpected escape sequence from terminal: %q", []rune{'\033', r})
}
}
return r, size, err
}
func (rr *RuneReader) readNormalKeypad() (rune, int, error) {
r, size, err := rr.state.reader.ReadRune()
if err != nil {
return r, size, err
}
switch r {
// ESC [ D
// https://vt100.net/docs/vt102-ug/appendixc.html#SC.2.2.3
// https://vt100.net/docs/vt102-ug/chapter5.html#S5.5.2.14
case 'D':
return KeyArrowLeft, 1, nil
// ESC [ C
// https://vt100.net/docs/vt102-ug/appendixc.html#SC.2.2.3
// https://vt100.net/docs/vt102-ug/chapter5.html#S5.5.2.14
case 'C':
return KeyArrowRight, 1, nil
// ESC [ A
// https://vt100.net/docs/vt102-ug/appendixc.html#SC.2.2.3
// https://vt100.net/docs/vt102-ug/chapter5.html#S5.5.2.14
case 'A':
return KeyArrowUp, 1, nil
// ESC [ B
// https://vt100.net/docs/vt102-ug/appendixc.html#SC.2.2.3
// https://vt100.net/docs/vt102-ug/chapter5.html#S5.5.2.14
case 'B':
return KeyArrowDown, 1, nil
// Home Key
// Cursor Position (Home) (CUP)
// ESC [ H
// https://vt100.net/docs/vt102-ug/appendixc.html#SC.2.2.9
case 'H':
return SpecialKeyHome, 1, nil
// End Key
// ESC [ F
case 'F':
return SpecialKeyEnd, 1, nil
// Delete Key
// Tabulation Clear (TBC)
// ESC [ 3
case '3':
// discard the following '~' key from buffer
_, _ = rr.state.reader.Discard(1)
return SpecialKeyDelete, 1, nil
default:
// discard the following '~' key from buffer
_, _ = rr.state.reader.Discard(1)
return IgnoreKey, 1, nil
}
}
func (rr *RuneReader) readApplicationKeypad() (rune, int, error) {
r, size, err := rr.state.reader.ReadRune()
if err != nil {
return r, size, err
}
switch r {
// ESC O D
// https://vt100.net/docs/vt102-ug/appendixc.html#SC.2.2.3
// https://vt100.net/docs/vt102-ug/chapter5.html#S5.5.2.14
case 'D':
return KeyArrowLeft, 1, nil
// ESC O C
// https://vt100.net/docs/vt102-ug/appendixc.html#SC.2.2.3
// https://vt100.net/docs/vt102-ug/chapter5.html#S5.5.2.14
case 'C':
return KeyArrowRight, 1, nil
// ESC O A
// https://vt100.net/docs/vt102-ug/appendixc.html#SC.2.2.3
// https://vt100.net/docs/vt102-ug/chapter5.html#S5.5.2.14
case 'A':
return KeyArrowUp, 1, nil
// ESC O B
// https://vt100.net/docs/vt102-ug/appendixc.html#SC.2.2.3
// https://vt100.net/docs/vt102-ug/chapter5.html#S5.5.2.14
case 'B':
return KeyArrowDown, 1, nil
// Home Key
// Cursor Position (Home) (CUP)
// ESC O H
// https://vt100.net/docs/vt102-ug/appendixc.html#SC.2.2.9
case 'H':
return SpecialKeyHome, 1, nil
// End Key
// ESC O F
case 'F':
return SpecialKeyEnd, 1, nil
default:
// discard the following '~' key from buffer
_, _ = rr.state.reader.Discard(1)
return IgnoreKey, 1, nil
}
}