-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput.go
208 lines (191 loc) · 4.74 KB
/
input.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
// seehuhn.de/go/ncurses - a Go-wrapper for the ncurses library
// Copyright (C) 2018 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package ncurses
import (
"fmt"
"unicode"
"unicode/utf8"
)
// #include <ncurses.h>
import "C"
// SetTimeout sets blocking or non-blocking read for a given window.
// If `delay` is negative, blocking read is used (i.e., waits
// indefinitely for input). If `delay` is zero, then non-blocking
// read is used (i.e., `GetCh()` returns `KeyTimeout` if no input is
// waiting). If `delay` is positive, then read blocks for `delay`
// milliseconds, and returns `KeyTimeout` if there is still no input.
func (w *Window) SetTimeout(delay int) {
C.wtimeout(w.ptr, C.int(delay))
w.timeout = delay
}
// GetTimeout returns the currently used timeout for input. See
// `SetTimeout()` for the meaning of the returned value.
func (w *Window) GetTimeout() int {
return w.timeout
}
// GetCh reads a character from the window. Input is expected to be
// utf-8 encoded and is converted to `rune`.
//
// Function key presses (e.g. cursor keys) are reported as runes in
// the "unicode private use area". The constants KeyA1, ..., KeyF24
// give all supported function keys.
//
// Refresh() is called before any character is read.
func (w *Window) GetCh() rune {
var buf []byte
for !utf8.FullRune(buf) {
c := C.wgetch(w.ptr)
if c == C.ERR {
return KeyTimeout
}
if len(buf) == 0 {
key, found := keyLookup[c]
if found {
return key
}
}
if c < 0 || c > 255 {
return 0
}
buf = append(buf, byte(c))
}
res, _ := utf8.DecodeRune(buf)
return res
}
func control(c rune) rune {
return c - 'A' + 1
}
// Readline allows the user to enter a line of text. Simple
// line-editing is provided and up to `maxLen` (unicode) characters of
// text can be entered.
//
// Refresh() is called before any character is read.
func (w *Window) Readline(maxLen int) string {
oldCurs, cerr := CursSet(CursorOn)
y, x := w.GetYX()
_, width := w.GetMaxYX()
if x+maxLen > width {
maxLen = width - x
}
var res []rune
pos := 0
for {
s := string(res)
s = fmt.Sprintf("%-*s", maxLen, s)
w.MvAddStr(y, x, s)
if pos >= maxLen {
w.Move(y, x+maxLen-1)
} else {
w.Move(y, x+pos)
}
c := w.GetCh()
if unicode.IsPrint(c) {
if len(res) < maxLen {
res = append(res[:pos], append([]rune{c}, res[pos:]...)...)
pos++
} else {
Beep()
}
} else if c == KeyEnter || c == '\n' || c == '\r' {
break
} else if c == KeyLeft {
if pos > 0 {
pos--
} else {
Beep()
}
} else if c == KeyRight {
if pos < len(res) {
pos++
} else {
Beep()
}
} else if c == KeyDc || c == control('D') {
if pos < len(res) {
res = append(res[:pos], res[pos+1:]...)
} else {
Beep()
}
} else if c == KeyBackspace || c == 127 || c == control('H') {
if pos > 0 {
res = append(res[:pos-1], res[pos:]...)
pos--
} else {
Beep()
}
} else if c == control('A') {
pos = 0
} else if c == control('E') {
pos = len(res)
} else if c == control('K') {
res = res[:pos]
} else if c == control('T') {
if pos > 0 && len(res) > 1 {
k := pos
if k > len(res)-1 {
k = len(res) - 1
} else {
pos = k + 1
}
res[k-1], res[k] = res[k], res[k-1]
} else {
Beep()
}
} else if c == 27 {
delay := w.GetTimeout()
w.SetTimeout(0)
c2 := w.GetCh()
w.SetTimeout(delay)
switch c2 {
case KeyBackspace, 127, control('H'):
old := pos
skip := true
for pos > 0 &&
(skip ||
unicode.IsLetter(res[pos-1]) ||
unicode.IsDigit(res[pos-1])) {
skip = skip &&
!unicode.IsLetter(res[pos-1]) &&
!unicode.IsDigit(res[pos-1])
pos--
}
res = append(res[:pos], res[old:]...)
case 'd':
old := pos
skip := true
for pos < len(res) &&
(skip ||
unicode.IsLetter(res[pos]) ||
unicode.IsDigit(res[pos])) {
skip = skip &&
!unicode.IsLetter(res[pos]) &&
!unicode.IsDigit(res[pos])
pos++
}
res = append(res[:old], res[pos:]...)
pos = old
default:
Beep()
}
} else {
Beep()
}
}
if cerr == nil {
CursSet(oldCurs)
}
return string(res)
}