-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
462 lines (432 loc) · 12.2 KB
/
prompt.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package prompt
import (
"bufio"
"fmt"
"math"
"os"
"reflect"
"strconv"
"strings"
"syscall"
"time"
"github.com/araddon/dateparse"
)
var selectMaxLines = 25 // maximum number of lines to show
var selectScrollOffset = 5 // minimum number of lines above/below cursor
var optionSelected = fmt.Sprintf("%v[\u00D7] %%v%v", escBold, escReset)
var optionUnselected = "[ ] %v"
var keyInterrupt = fmt.Errorf("interrupt")
var keyEscape = fmt.Errorf("escape")
// Enter is a prompt that requires the Enter key to continue.
func Enter(label string) {
fmt.Printf("%v [enter]: ", label)
var res string
fmt.Scanln(&res)
}
// YesNo is a prompt that requires a yes or no answer. It returns true for any of (1,y,yes,t,true), and false for any of (0,n,no,f,false). It is case-insensitive.
func YesNo(label string, deflt bool) bool {
first := true
Prompt:
if deflt {
fmt.Printf("%v [Y/n]: ", label)
} else {
fmt.Printf("%v [y/N]: ", label)
}
fmt.Printf(escSavePos)
var res string
fmt.Scanln(&res)
res = strings.TrimSpace(res)
if res == "" {
fmt.Printf(escMoveUp + escMoveStart + escClearLine)
if deflt {
fmt.Printf("%v [Y/n]: yes\n", label)
} else {
fmt.Printf("%v [y/N]: no\n", label)
}
return deflt
} else {
res = strings.ToLower(res)
}
var b bool
var err error
if res == "y" || res == "yes" {
b = true
} else if res == "n" || res == "no" {
b = false
}
if err != nil {
first = false
fmt.Printf("%v%v%vERROR: %v%v%v", escClearLine, escRed, escBold, err, escReset, escMoveUp)
fmt.Printf(escMoveStart + escClearLine)
goto Prompt
} else if !first {
fmt.Printf(escClearLine) // clear error
}
return b
}
type defaultValue struct {
idst interface{}
ideflt interface{}
pos int
}
// NonZeroDefault is the default value used for Prompt. The default value will be empty when the destination is its zero value.
func NonZeroDefault(idst, ideflt interface{}) defaultValue {
dst := reflect.ValueOf(idst)
if dst.Kind() == reflect.Pointer && !dst.Elem().IsZero() {
return defaultValue{idst, dst.Elem().Interface(), -1}
}
return defaultValue{idst, nil, -1}
}
// Default is the default value used for Prompt.
func Default(idst, ideflt interface{}) defaultValue {
return defaultValue{idst, ideflt, -1}
}
// Default is the default value with the initial text caret position used for Prompt. A position of -1 means at the end of the value.
func DefaultWithCaret(idst, ideflt interface{}, pos int) defaultValue {
return defaultValue{idst, ideflt, pos}
}
// Prompt is a regular text prompt that can read into a (string,[]byte,bool,int,int8,int16,int32,int64,uint,uint8,uint16,uint32,uint64,float32,float64,time.Time) or a type that implements the Scanner interface. The idst must be a pointer to a variable, its value determines the default/initial value.
// The initial value will be editable in-place. To set the text caret initial position when idst is editable, use prompt.Default(value, position). When editing, you can use the Left or Ctrl+B, Right or Ctrl+F, Home or Ctrl+A, End or Ctrl+E to move around; Backspace and Delete to delete a character; Ctrl+U and Ctrl+K to delete from the caret to the beginning and the end of the line respectively; Ctrl+C and Escape to quit; and Ctrl+Z and Enter to confirm the input.
// All validators must be satisfies, otherwise an error is printed and the answer should be corrected.
func Prompt(idst interface{}, label string, validators ...Validator) error {
first := true
pos := -1
hasDeflt := false
var ideflt interface{}
if deflt, ok := idst.(defaultValue); ok {
idst = deflt.idst
ideflt = deflt.ideflt
pos = deflt.pos
hasDeflt = true
}
// get destination
dst := reflect.ValueOf(idst)
if dst.Kind() != reflect.Pointer {
return fmt.Errorf("destination must be a pointer to a variable")
}
idst = dst.Elem().Interface()
if !hasDeflt && ideflt == nil {
ideflt = idst
}
editDefault := false
switch idst.(type) {
case nil:
// ignore
case []byte, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, time.Time:
editDefault = true
default:
if _, ok := idst.(interface {
String() string
}); ok {
editDefault = true
if ideflt == nil {
ideflt = idst
}
}
}
var result []rune
if editDefault {
switch deflt := ideflt.(type) {
case nil:
// no-op
case []byte:
result = []rune(string(deflt))
case string:
result = []rune(deflt)
default:
result = []rune(fmt.Sprint(ideflt))
}
}
if pos == -1 {
pos = len(result)
} else if pos < 0 {
pos = 0
} else if len(result) < pos {
pos = len(result)
}
Prompt:
// prompt input
if _, ok := idst.(bool); ok {
if deflt, ok := ideflt.(bool); ok {
if deflt {
fmt.Printf("%v [Y/n]: ", label)
} else {
fmt.Printf("%v [y/N]: ", label)
}
} else {
fmt.Printf("%v [y/n]: ", label)
}
result = []rune{}
pos = 0
} else {
fmt.Printf("%v: %v", label, string(result))
fmt.Printf(strings.Repeat(escMoveLeft, len(result)-pos))
}
// make raw and hide input
restore, err := MakeRawTerminal(false)
if err != nil {
return err
}
func() {
defer restore()
// read input
input := bufio.NewReader(os.Stdin)
for {
var r rune
if r, _, err = input.ReadRune(); err != nil {
break
}
if r == '\x03' { // interrupt
err = keyInterrupt
break
} else if r == '\x04' || r == '\r' || r == '\n' { // select
break
} else if r == '\x7F' { // backspace
if pos != 0 {
result = append(result[:pos-1], result[pos:]...)
pos--
fmt.Printf(escMoveLeft+"%v "+strings.Repeat(escMoveLeft, len(result)+1-pos), string(result[pos:]))
}
} else if r == '\x1B' { // escape
if input.Buffered() == 0 {
err = keyEscape
break
} else if r, _, err = input.ReadRune(); err != nil {
break
} else if r == '[' { // CSI
if input.Buffered() == 0 {
// ignore
} else if r, _, err = input.ReadRune(); err != nil {
break
} else if r == 'D' { // left
if pos != 0 {
fmt.Printf(escMoveLeft)
pos--
}
} else if r == 'C' { // right
if pos != len(result) {
fmt.Printf(escMoveRight)
pos++
}
} else if r == 'H' { // home
fmt.Printf(strings.Repeat(escMoveLeft, pos))
pos = 0
} else if r == 'F' { // end
fmt.Printf(strings.Repeat(escMoveRight, len(result)-pos))
pos = len(result)
} else if r == '3' {
if input.Buffered() == 0 {
// ignore
} else if r, _, err = input.ReadRune(); err != nil {
break
} else if r == '~' { // delete
if pos != len(result) {
result = append(result[:pos], result[pos+1:]...)
fmt.Printf("%v "+strings.Repeat(escMoveLeft, len(result)+1-pos), string(result[pos:]))
}
}
}
}
} else if r == '\x01' { // Ctrl+A - move to start of line
fmt.Printf(strings.Repeat(escMoveLeft, pos))
pos = 0
} else if r == '\x02' { // Ctrl+B - move back
fmt.Printf(escMoveLeft)
pos--
} else if r == '\x05' { // Ctrl+E - move to end of line
fmt.Printf(strings.Repeat(escMoveRight, len(result)-pos))
pos = len(result)
} else if r == '\x06' { // Ctrl+F - move forward
fmt.Printf(escMoveRight)
pos++
} else if r == '\x0B' { // Ctrl+K - delete to end of line
fmt.Printf(strings.Repeat(" ", len(result)-pos))
fmt.Printf(strings.Repeat(escMoveLeft, len(result)-pos))
result = result[:pos]
} else if r == '\x15' { // Ctrl+U - delete to start of line
fmt.Printf(strings.Repeat(escMoveLeft, pos))
fmt.Printf("%v"+strings.Repeat(" ", pos), string(result[pos:]))
fmt.Printf(strings.Repeat(escMoveLeft, len(result)))
result = result[pos:]
pos = 0
} else if ' ' <= r {
result = append(result[:pos], append([]rune{r}, result[pos:]...)...)
fmt.Printf("%v"+strings.Repeat(escMoveLeft, len(result)-pos-1), string(result[pos:]))
pos++
}
}
}()
if err != nil {
if !first {
fmt.Printf(escMoveDown + escClearLine + escMoveUp)
}
if err == keyInterrupt {
fmt.Printf(strings.Repeat(escMoveRight, len(result)-pos) + "^C")
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}
fmt.Printf("\n")
return err
}
fmt.Println(escMoveStart)
// fill destination
res := strings.TrimSpace(string(result))
ival := ideflt
if editDefault || res != "" || ival == nil {
switch idst.(type) {
case []byte:
ival = []byte(res)
case string:
ival = res
case bool:
var b bool
if res == "y" || res == "Y" || res == "yes" || res == "YES" {
b = true
} else if res == "n" || res == "N" || res == "no" || res == "NO" {
b = false
} else {
var perr error
b, perr = strconv.ParseBool(res)
if perr != nil {
err = fmt.Errorf("invalid boolean")
}
}
ival = b
case int:
i, perr := strconv.ParseInt(res, 10, 64)
if perr != nil {
err = fmt.Errorf("invalid integer")
} else if math.MaxInt < i {
err = fmt.Errorf("integer overflow")
}
ival = int(i)
case int8:
i, perr := strconv.ParseInt(res, 10, 64)
if perr != nil {
err = fmt.Errorf("invalid integer")
} else if math.MaxInt8 < i {
err = fmt.Errorf("integer overflow")
}
ival = int8(i)
case int16:
i, perr := strconv.ParseInt(res, 10, 64)
if perr != nil {
err = fmt.Errorf("invalid integer")
} else if math.MaxInt16 < i {
err = fmt.Errorf("integer overflow")
}
ival = int16(i)
case int32:
i, perr := strconv.ParseInt(res, 10, 64)
if perr != nil {
err = fmt.Errorf("invalid integer")
} else if math.MaxInt64 < i {
err = fmt.Errorf("integer overflow")
}
ival = int32(i)
case int64:
i, perr := strconv.ParseInt(res, 10, 64)
if perr != nil {
err = fmt.Errorf("invalid integer")
}
ival = i
case uint:
u, perr := strconv.ParseUint(res, 10, 64)
if perr != nil {
err = fmt.Errorf("invalid positive integer")
} else if math.MaxInt < u {
err = fmt.Errorf("integer overflow")
}
ival = uint(u)
case uint8:
u, perr := strconv.ParseUint(res, 10, 64)
if perr != nil {
err = fmt.Errorf("invalid positive integer")
} else if math.MaxInt8 < u {
err = fmt.Errorf("integer overflow")
}
ival = uint8(u)
case uint16:
u, perr := strconv.ParseUint(res, 10, 64)
if perr != nil {
err = fmt.Errorf("invalid positive integer")
} else if math.MaxInt16 < u {
err = fmt.Errorf("integer overflow")
}
ival = uint16(u)
case uint32:
u, perr := strconv.ParseUint(res, 10, 64)
if perr != nil {
err = fmt.Errorf("invalid positive integer")
} else if math.MaxInt64 < u {
err = fmt.Errorf("integer overflow")
}
ival = uint32(u)
case uint64:
u, perr := strconv.ParseUint(res, 10, 64)
if perr != nil {
err = fmt.Errorf("invalid positive integer")
}
ival = u
case float32:
f, perr := strconv.ParseFloat(res, 32)
if perr.(*strconv.NumError).Err == strconv.ErrRange {
err = fmt.Errorf("floating point overflow")
} else if perr != nil {
err = fmt.Errorf("invalid floating point")
}
ival = float32(f)
case float64:
f, perr := strconv.ParseFloat(res, 64)
if perr.(*strconv.NumError).Err == strconv.ErrRange {
err = fmt.Errorf("floating point overflow")
} else if perr != nil {
err = fmt.Errorf("invalid floating point")
}
ival = f
case time.Time:
t, perr := dateparse.ParseAny(res)
if perr != nil {
err = fmt.Errorf("invalid datetime")
}
ival = t
default:
if scanner, ok := dst.Interface().(interface {
Scan(interface{}) error
}); ok {
// already sets value to dst
if perr := scanner.Scan(res); perr != nil {
err = fmt.Errorf("invalid %T: %w", idst, perr)
}
ival = dst.Elem().Interface()
} else {
return fmt.Errorf("unsupported destination type: %T", idst)
}
}
} else if deflt, ok := ideflt.(bool); ok {
fmt.Printf(escMoveUp + escMoveStart + escClearLine)
if deflt {
fmt.Printf("%v [Y/n]: yes\n", label)
} else {
fmt.Printf("%v [y/N]: no\n", label)
}
}
// validators
if err == nil {
for _, validator := range validators {
if verr := validator(ival); verr != nil {
err = verr
break
}
}
}
if err != nil {
first = false
fmt.Printf("%v%v%vERROR: %v%v%v", escClearLine, escRed, escBold, err, escReset, escMoveUp)
fmt.Printf(escMoveStart + escClearLine)
goto Prompt
} else if !first {
fmt.Printf(escClearLine)
}
dst.Elem().Set(reflect.ValueOf(ival))
return nil
}