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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
// Copyright 2013-2015 Bowery, Inc.
package prompt
import (
"bufio"
"errors"
"io"
"os"
"strings"
)
var (
// ErrCTRLC is returned when CTRL+C is pressed stopping the prompt.
ErrCTRLC = errors.New("Interrupted (CTRL+C)")
// ErrEOF is returned when CTRL+D is pressed stopping the prompt.
ErrEOF = errors.New("EOF (CTRL+D)")
)
// Possible events that may occur when reading from input.
const (
evChar = iota
evSkip
evReturn
evEOF
evCtrlC
evBack
evClear
evHome
evEnd
evUp
evDown
evRight
evLeft
evDel
)
// IsNotTerminal checks if an error is related to the input not being a terminal.
func IsNotTerminal(err error) bool {
return isNotTerminal(err)
}
// TerminalSize retrieves the columns/rows for the terminal connected to out.
func TerminalSize(out *os.File) (int, int, error) {
return terminalSize(out)
}
// Terminal contains the state for raw terminal input.
type Terminal struct {
In *os.File
Out *os.File
History []string
histIdx int
simpleReader *bufio.Reader
t *terminal
}
// NewTerminal creates a terminal and sets it to raw input mode.
func NewTerminal() (*Terminal, error) {
in := os.Stdin
term, err := newTerminal(in)
if err != nil {
return nil, err
}
return &Terminal{
In: in,
Out: os.Stdout,
History: make([]string, 0, 10),
histIdx: -1,
t: term,
}, nil
}
// Basic gets input and if required tests to ensure input was given.
func (term *Terminal) Basic(prefix string, required bool) (string, error) {
return term.Custom(prefix, func(input string) (string, bool) {
if required && input == "" {
return "", false
}
return input, true
})
}
// BasicDefault gets input and if empty uses the given default.
func (term *Terminal) BasicDefault(prefix, def string) (string, error) {
return term.Custom(prefix+" (Default: "+def+")", func(input string) (string, bool) {
if input == "" {
input = def
}
return input, true
})
}
// Ask gets input and checks if it's truthy or not, and returns that
// in a boolean fashion.
func (term *Terminal) Ask(question string) (bool, error) {
input, err := term.Custom(question+"? (y/n)", func(input string) (string, bool) {
if input == "" {
return "", false
}
input = strings.ToLower(input)
if input == "y" || input == "yes" {
return "yes", true
}
return "", true
})
var ok bool
if input != "" {
ok = true
}
return ok, err
}
// Custom gets input and calls the given test function with the input to
// check if the input is valid, a true return will return the string.
func (term *Terminal) Custom(prefix string, test func(string) (string, bool)) (string, error) {
var err error
var input string
var ok bool
for !ok {
input, err = term.GetPrompt(prefix)
if err != nil && err != io.EOF {
return "", err
}
input, ok = test(input)
}
return input, nil
}
// Password retrieves a password from stdin without echoing it.
func (term *Terminal) Password(prefix string) (string, error) {
var err error
var input string
for input == "" {
input, err = term.GetPassword(prefix)
if err != nil && err != io.EOF {
return "", err
}
}
return input, nil
}
// GetPrompt gets a line with the prefix and echos input.
func (term *Terminal) GetPrompt(prefix string) (string, error) {
prefix += " "
if !term.t.supportsEditing {
return term.simplePrompt(prefix)
}
buf := NewBuffer(prefix, term.Out, true)
return term.prompt(buf, NewAnsiReader(term.In))
}
// GetPassword gets a line with the prefix and doesn't echo input.
func (term *Terminal) GetPassword(prefix string) (string, error) {
prefix += " "
if !term.t.supportsEditing {
return term.simplePrompt(prefix)
}
buf := NewBuffer(prefix, term.Out, false)
return term.password(buf, NewAnsiReader(term.In))
}
// Close calls close on the internal terminal.
func (term *Terminal) Close() error {
return term.t.Close()
}
// Reopen re-opens an internal terminal.
func (term *Terminal) Reopen() error {
in := os.Stdin
t, err := newTerminal(in)
if err != nil {
return err
}
term.t = t
return nil
}
// simplePrompt is a fallback prompt without line editing support.
func (term *Terminal) simplePrompt(prefix string) (string, error) {
if term.simpleReader == nil {
term.simpleReader = bufio.NewReader(term.In)
}
_, err := term.Out.Write([]byte(prefix))
if err != nil {
return "", err
}
line, err := term.simpleReader.ReadString('\n')
line = strings.TrimRight(line, "\r\n ")
line = strings.TrimLeft(line, " ")
return line, err
}
// setup initializes a prompt.
func (term *Terminal) setup(buf *Buffer, in io.Reader) (*bufio.Reader, error) {
cols, _, err := TerminalSize(buf.Out)
if err != nil {
return nil, err
}
buf.Cols = cols
input := bufio.NewReader(in)
err = buf.Refresh()
if err != nil {
return nil, err
}
return input, nil
}
// read reads a rune and parses ANSI escape sequences found
func (term *Terminal) read(in *bufio.Reader) (int, rune, error) {
char, _, err := in.ReadRune()
if err != nil {
return 0, 0, err
}
switch char {
default:
// Standard chars.
return evChar, char, nil
case tabKey, ctrlA, ctrlB, ctrlE, ctrlF, ctrlG, ctrlH, ctrlJ, ctrlK, ctrlN,
ctrlO, ctrlP, ctrlQ, ctrlR, ctrlS, ctrlT, ctrlU, ctrlV, ctrlW, ctrlX,
ctrlY, ctrlZ:
// Skip.
return evSkip, char, nil
case returnKey:
// End of line.
return evReturn, char, nil
case ctrlD:
// End of file.
return evEOF, char, nil
case ctrlC:
// End of line, interrupted.
return evCtrlC, char, nil
case backKey:
// Backspace.
return evBack, char, nil
case ctrlL:
// Clear screen.
return evClear, char, nil
case escKey:
// Functions like arrows, home, etc.
esc := make([]byte, 2)
_, err = in.Read(esc)
if err != nil {
return -1, char, err
}
// Home, end.
if esc[0] == 'O' {
switch esc[1] {
case 'H':
// Home.
return evHome, char, nil
case 'F':
// End.
return evEnd, char, nil
}
return evSkip, char, nil
}
// Arrows, delete, pgup, pgdown, insert.
if esc[0] == '[' {
switch esc[1] {
case 'A':
// Up.
return evUp, char, nil
case 'B':
// Down.
return evDown, char, nil
case 'C':
// Right.
return evRight, char, nil
case 'D':
// Left.
return evLeft, char, nil
}
// Delete, pgup, pgdown, insert.
if esc[1] > '0' && esc[1] < '7' {
extEsc := make([]byte, 3)
_, err = in.Read(extEsc)
if err != nil {
return -1, char, err
}
if extEsc[0] == '~' {
switch esc[1] {
case '2', '5', '6':
// Insert, pgup, pgdown.
return evSkip, char, err
case '3':
// Delete.
return evDel, char, err
}
}
}
}
}
return evSkip, char, nil
}
// prompt reads from in and parses ANSI escapes writing to buf.
func (term *Terminal) prompt(buf *Buffer, in io.Reader) (string, error) {
input, err := term.setup(buf, in)
if err != nil {
return "", err
}
term.History = append(term.History, "")
term.histIdx = len(term.History) - 1
curHistIdx := term.histIdx
for {
typ, char, err := term.read(input)
if err != nil {
return buf.String(), err
}
switch typ {
case evChar:
err = buf.Insert(char)
if err != nil {
return buf.String(), err
}
term.History[curHistIdx] = buf.String()
case evSkip:
continue
case evReturn:
err = buf.EndLine()
if err != nil {
return buf.String(), err
}
line := buf.String()
if line == "" {
term.histIdx = curHistIdx - 1
term.History = term.History[:curHistIdx]
} else {
term.History[curHistIdx] = line
}
return line, nil
case evEOF:
err = buf.EndLine()
if err == nil {
err = ErrEOF
}
return buf.String(), err
case evCtrlC:
err = buf.EndLine()
if err == nil {
err = ErrCTRLC
}
return buf.String(), err
case evBack:
err = buf.DelLeft()
if err != nil {
return buf.String(), err
}
term.History[curHistIdx] = buf.String()
case evClear:
err = buf.ClsScreen()
if err != nil {
return buf.String(), err
}
case evHome:
err = buf.Start()
if err != nil {
return buf.String(), err
}
case evEnd:
err = buf.End()
if err != nil {
return buf.String(), err
}
case evUp:
idx := term.histIdx
if term.histIdx > 0 {
idx--
}
err = buf.Set([]rune(term.History[idx])...)
if err != nil {
return buf.String(), err
}
term.histIdx = idx
case evDown:
idx := term.histIdx
if term.histIdx < len(term.History)-1 {
idx++
}
err = buf.Set([]rune(term.History[idx])...)
if err != nil {
return buf.String(), err
}
term.histIdx = idx
case evRight:
err = buf.Right()
if err != nil {
return buf.String(), err
}
case evLeft:
err = buf.Left()
if err != nil {
return buf.String(), err
}
case evDel:
err = buf.Del()
if err != nil {
return buf.String(), err
}
term.History[curHistIdx] = buf.String()
}
}
}
// password reads from in and parses restricted ANSI escapes writing to buf.
func (term *Terminal) password(buf *Buffer, in io.Reader) (string, error) {
input, err := term.setup(buf, in)
if err != nil {
return "", err
}
for {
typ, char, err := term.read(input)
if err != nil {
return buf.String(), err
}
switch typ {
case evChar:
err = buf.Insert(char)
if err != nil {
return buf.String(), err
}
case evSkip, evHome, evEnd, evUp, evDown, evRight, evLeft, evDel:
continue
case evReturn:
err = buf.EndLine()
return buf.String(), err
case evEOF:
err = buf.EndLine()
if err == nil {
err = ErrEOF
}
return buf.String(), err
case evCtrlC:
err = buf.EndLine()
if err == nil {
err = ErrCTRLC
}
return buf.String(), err
case evBack:
err = buf.DelLeft()
if err != nil {
return buf.String(), err
}
case evClear:
err = buf.ClsScreen()
if err != nil {
return buf.String(), err
}
}
}
}
|