/
linoise.go
369 lines (304 loc) · 7.18 KB
/
linoise.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
// Copyright 2010 The "go-linoise" Authors
//
// Use of this source code is governed by the Simplified BSD License
// that can be found in the LICENSE file.
//
// This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
// OR CONDITIONS OF ANY KIND, either express or implied. See the License
// for more details.
/* Important: linoise sets tty in 'raw mode' so there is to use CR+LF (\r\n) at
writing.
*/
package linoise
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"github.com/kless/term"
)
// Values by default for prompts.
var (
PS1 = "linoise$ "
PS2 = "> "
)
// Input / Output
var (
input = os.Stdin
output = os.Stdout
tty *term.Terminal
)
// === Init
// ===
// Init sets up our terminal for use.
// It must be run before anything else.
func Init() {
var err error
if tty, err = term.New(); err != nil {
log.Fatalf("%q fd: %d", err, term.InputFD)
}
}
// === Type
// ===
// Represents a line.
type Line struct {
useHistory bool
ps1Len int // Primary prompt size
ps1 string // Primary prompt
ps2 string // Command continuations
buf *buffer // Text buffer
hist *history // History file
}
// Gets a line type using the primary prompt by default. Sets the TTY raw mode.
func NewLine(hist *history) *Line {
// TODO(jwall): check errors?
tty.RawMode()
buf := newBuffer(len(PS1))
buf.insertRunes([]rune(PS1))
return &Line{
hasHistory(hist),
len(PS1),
PS1,
PS2,
buf,
hist,
}
}
// Gets a line type using the given prompt as primary. Sets the TTY raw mode.
// 'ansiLen' is the length of ANSI codes that the prompt could have.
func NewLinePrompt(prompt string, ansiLen int, hist *history) *Line {
// TODO(jwall): check errors?
tty.RawMode()
buf := newBuffer(len(prompt) - ansiLen)
buf.insertRunes([]rune(prompt))
return &Line{
hasHistory(hist),
len(prompt) - ansiLen,
prompt,
PS2,
buf,
hist,
}
}
// Restores terminal settings so it is disabled the raw mode.
func (ln *Line) RestoreTerm() {
tty.Restore()
}
// Tests if it has an history file.
func hasHistory(h *history) bool {
if h == nil {
return false
}
return true
}
// === Output
// ===
// Prints the primary prompt.
func (ln *Line) prompt() (err error) {
if _, err = output.Write(delLine_CR); err != nil {
return outputError(err.Error())
}
if _, err = fmt.Fprint(output, ln.ps1); err != nil {
return outputError(err.Error())
}
ln.buf.pos, ln.buf.size = ln.ps1Len, ln.ps1Len
return
}
// === Get
// ===
// Reads charactes from input to write them to output, allowing line editing.
// The errors that could return are to indicate if Ctrl-D was pressed, and for
// both input / output errors.
func (ln *Line) Read() (line string, err error) {
var anotherLine []rune // For lines got from history.
var isHistoryUsed bool // If the history has been accessed.
in := bufio.NewReader(input) // Read input.
seq := make([]byte, 2) // For escape sequences.
seq2 := make([]byte, 1) // Extended escape sequences.
// Print the primary prompt.
if err = ln.prompt(); err != nil {
return "", err
}
// === Detect change of window size.
wSize := term.DetectWinSize()
go func() {
for {
<-wSize.Change // Wait for.
// TODO(jwall): Check errors?
_, ln.buf.winColumns, _ = tty.GetSize()
ln.buf.refresh()
}
}()
for {
rune, _, err := in.ReadRune()
if err != nil {
return "", inputError(err.Error())
}
switch rune {
default:
if err = ln.buf.insertRune(rune); err != nil {
return "", err
}
continue
case 13: // enter
line = ln.buf.toString()
if ln.useHistory {
ln.hist.Add(line)
}
if _, err = output.Write(_CR_LF); err != nil {
return "", outputError(err.Error())
}
return strings.TrimSpace(line), nil
case 127, 8: // backspace, Ctrl-h
if err = ln.buf.deletePrev(); err != nil {
return "", err
}
continue
case 9: // horizontal tab
// TODO: disabled by now
continue
case 3: // Ctrl-c
if err = ln.buf.insertRunes(ctrlC); err != nil {
return "", err
}
if _, err = output.Write(_CR_LF); err != nil {
return "", outputError(err.Error())
}
if err = ln.prompt(); err != nil {
return "", err
}
continue
case 4: // Ctrl-d
if err = ln.buf.insertRunes(ctrlD); err != nil {
return "", err
}
if _, err = output.Write(_CR_LF); err != nil {
return "", outputError(err.Error())
}
return "", ErrCtrlD
// Escape sequence
case 27: // Escape: Ctrl-[ ("033" in octal, "\x1b" in hexadecimal)
if _, err = in.Read(seq); err != nil {
return "", inputError(err.Error())
}
if seq[0] == 79 { // 'O'
switch seq[1] {
case 72: // Home: "\x1bOH"
goto _start
case 70: // End: "\x1bOF"
goto _end
}
}
if seq[0] == 91 { // Left square bracket: "["
switch seq[1] {
case 68: // "\x1b[D"
goto _leftArrow
case 67: // "\x1b[C"
goto _rightArrow
case 65, 66: // Up: "\x1b[A"; Down: "\x1b[B"
goto _upDownArrow
}
// Extended escape.
if seq[1] > 48 && seq[1] < 55 {
if _, err = in.Read(seq2); err != nil {
return "", inputError(err.Error())
}
if seq2[0] == 126 { // '~'
switch seq[1] {
//case 50: // Insert: "\x1b[2~"
case 51: // Delete: "\x1b[3~"
if err = ln.buf.delete(); err != nil {
return "", err
}
//case 53: // RePag: "\x1b[5~"
//case 54: // AvPag: "\x1b[6~"
}
}
}
}
continue
case 20: // Ctrl-t, swap actual character by the previous one.
if err = ln.buf.swap(); err != nil {
return "", err
}
continue
case 21: // Ctrl+u, delete the whole line.
if err = ln.buf.deleteLine(); err != nil {
return "", err
}
if err = ln.prompt(); err != nil {
return "", err
}
continue
case 11: // Ctrl+k, delete from current to end of line.
if err = ln.buf.deleteRight(); err != nil {
return "", err
}
continue
case 1: // Ctrl+a, go to the start of the line.
goto _start
case 5: // Ctrl+e, go to the end of the line.
goto _end
case 2: // Ctrl-b
goto _leftArrow
case 6: // Ctrl-f
goto _rightArrow
case 16: // Ctrl-p
seq[1] = 65
goto _upDownArrow
case 14: // Ctrl-n
seq[1] = 66
goto _upDownArrow
}
_upDownArrow: // Up and down arrow: history
if !ln.useHistory {
continue
}
// Up
if seq[1] == 65 {
anotherLine, err = ln.hist.Prev()
// Down
} else {
anotherLine, err = ln.hist.Next()
}
if err != nil {
continue
}
// Update the current history entry before to overwrite it with
// the next one.
// TODO: it has to be removed before of to be saved the history
if !isHistoryUsed {
ln.hist.Add(ln.buf.toString())
}
isHistoryUsed = true
ln.buf.grow(len(anotherLine))
ln.buf.size = len(anotherLine) + ln.buf.promptLen
copy(ln.buf.data[ln.ps1Len:], anotherLine)
if err = ln.buf.refresh(); err != nil {
return "", err
}
continue
_leftArrow:
if err = ln.buf.backward(); err != nil {
return "", err
}
continue
_rightArrow:
if err = ln.buf.forward(); err != nil {
return "", err
}
continue
_start:
if err = ln.buf.start(); err != nil {
return "", err
}
continue
_end:
if _, err = ln.buf.end(); err != nil {
return "", err
}
continue
}
return
}