forked from sohomdatta1/promptui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
640 lines (523 loc) · 17.6 KB
/
select.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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package promptui
import (
"bytes"
"fmt"
"io"
"text/tabwriter"
"text/template"
"github.com/ergochat/readline"
"github.com/NozomiNetworks/promptui/list"
"github.com/NozomiNetworks/promptui/screenbuf"
)
// SelectedAdd is used internally inside SelectWithAdd when the add option is selected in select mode.
// Since -1 is not a possible selected index, this ensure that add mode is always unique inside
// SelectWithAdd's logic.
const SelectedAdd = -1
// Select represents a list of items used to enable selections, they can be used as search engines, menus
// or as a list of items in a cli based prompt.
type Select struct {
// Label is the text displayed on top of the list to direct input. The IconInitial value "?" will be
// appended automatically to the label so it does not need to be added.
//
// The value for Label can be a simple string or a struct that will need to be accessed by dot notation
// inside the templates. For example, `{{ .Name }}` will display the name property of a struct.
Label interface{}
// Items are the items to display inside the list. It expect a slice of any kind of values, including strings.
//
// If using a slice of strings, promptui will use those strings directly into its base templates or the
// provided templates. If using any other type in the slice, it will attempt to transform it into a string
// before giving it to its templates. Custom templates will override this behavior if using the dot notation
// inside the templates.
//
// For example, `{{ .Name }}` will display the name property of a struct.
Items interface{}
// Size is the number of items that should appear on the select before scrolling is necessary. Defaults to 5.
Size int
// CursorPos is the initial position of the cursor.
CursorPos int
// IsVimMode sets whether to use vim mode when using readline in the command prompt. Look at
// https://godoc.org/github.com/chzyer/readline#Config for more information on readline.
IsVimMode bool
// HideHelp sets whether to hide help information.
HideHelp bool
// HideSelected sets whether to hide the text displayed after an item is successfully selected.
HideSelected bool
// Templates can be used to customize the select output. If nil is passed, the
// default templates are used. See the SelectTemplates docs for more info.
Templates *SelectTemplates
// Keys is the set of keys used in select mode to control the command line interface. See the SelectKeys docs for
// more info.
Keys *SelectKeys
// Searcher is a function that can be implemented to refine the base searching algorithm in selects.
//
// Search is a function that will receive the searched term and the item's index and should return a boolean
// for whether or not the terms are alike. It is unimplemented by default and search will not work unless
// it is implemented.
Searcher list.Searcher
// StartInSearchMode sets whether or not the select mode should start in search mode or selection mode.
// For search mode to work, the Search property must be implemented.
StartInSearchMode bool
list *list.List
// A function that determines how to render the cursor
Pointer Pointer
Stdin io.Reader
Stdout io.Writer
}
// SelectKeys defines the available keys used by select mode to enable the user to move around the list
// and trigger search mode. See the Key struct docs for more information on keys.
type SelectKeys struct {
// Next is the key used to move to the next element inside the list. Defaults to down arrow key.
Next Key
// Prev is the key used to move to the previous element inside the list. Defaults to up arrow key.
Prev Key
// PageUp is the key used to jump back to the first element inside the list. Defaults to left arrow key.
PageUp Key
// PageUp is the key used to jump forward to the last element inside the list. Defaults to right arrow key.
PageDown Key
// Search is the key used to trigger the search mode for the list. Default to the "/" key.
Search Key
}
// Key defines a keyboard code and a display representation for the help menu.
type Key struct {
// Code is a rune that will be used to compare against typed keys with readline.
// Check https://github.com/ergochat/readline for a list of codes
Code rune
// Display is the string that will be displayed inside the help menu to help inform the user
// of which key to use on his keyboard for various functions.
Display string
}
// SelectTemplates allow a select list to be customized following stdlib
// text/template syntax. Custom state, colors and background color are available for use inside
// the templates and are documented inside the Variable section of the docs.
//
// # Examples
//
// text/templates use a special notation to display programmable content. Using the double bracket notation,
// the value can be printed with specific helper functions. For example
//
// This displays the value given to the template as pure, unstylized text. Structs are transformed to string
// with this notation.
//
// '{{ . }}'
//
// This displays the name property of the value colored in cyan
//
// '{{ .Name | cyan }}'
//
// This displays the label property of value colored in red with a cyan background-color
//
// '{{ .Label | red | cyan }}'
//
// See the doc of text/template for more info: https://golang.org/pkg/text/template/
//
// # Notes
//
// Setting any of these templates will remove the icons from the default templates. They must
// be added back in each of their specific templates. The styles.go constants contains the default icons.
type SelectTemplates struct {
// Label is a text/template for the main command line label. Defaults to printing the label as it with
// the IconInitial.
Label string
// Active is a text/template for when an item is currently active within the list.
Active string
// Inactive is a text/template for when an item is not currently active inside the list. This
// template is used for all items unless they are active or selected.
Inactive string
// Selected is a text/template for when an item was successfully selected.
Selected string
// Details is a text/template for when an item current active to show
// additional information. It can have multiple lines.
//
// Detail will always be displayed for the active element and thus can be used to display additional
// information on the element beyond its label.
//
// promptui will not trim spaces and tabs will be displayed if the template is indented.
Details string
// Help is a text/template for displaying instructions at the top. By default
// it shows keys for movement and search.
Help string
// FuncMap is a map of helper functions that can be used inside of templates according to the text/template
// documentation.
//
// By default, FuncMap contains the color functions used to color the text in templates. If FuncMap
// is overridden, the colors functions must be added in the override from promptui.FuncMap to work.
FuncMap template.FuncMap
label *template.Template
active *template.Template
inactive *template.Template
selected *template.Template
details *template.Template
help *template.Template
}
// SearchPrompt is the prompt displayed in search mode.
var SearchPrompt = "Search: "
// Run executes the select list. It displays the label and the list of items, asking the user to chose any
// value within to list. Run will keep the prompt alive until it has been canceled from
// the command prompt or it has received a valid value. It will return the value and an error if any
// occurred during the select's execution.
func (s *Select) Run() (int, string, error) {
return s.RunCursorAt(s.CursorPos, 0)
}
// RunCursorAt executes the select list, initializing the cursor to the given
// position. Invalid cursor positions will be clamped to valid values. It
// displays the label and the list of items, asking the user to chose any value
// within to list. Run will keep the prompt alive until it has been canceled
// from the command prompt or it has received a valid value. It will return
// the value and an error if any occurred during the select's execution.
func (s *Select) RunCursorAt(cursorPos, scroll int) (int, string, error) {
if s.Size == 0 {
s.Size = 5
}
l, err := list.New(s.Items, s.Size)
if err != nil {
return 0, "", err
}
l.Searcher = s.Searcher
s.list = l
s.setKeys()
err = s.prepareTemplates()
if err != nil {
return 0, "", err
}
return s.innerRun(cursorPos, scroll, ' ')
}
func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error) {
c := &readline.Config{
Stdin: s.Stdin,
Stdout: s.Stdout,
}
if s.IsVimMode {
c.VimMode = true
}
c.HistoryLimit = -1
rl, err := readline.NewFromConfig(c)
if err != nil {
return 0, "", err
}
rl.Write([]byte(hideCursor))
sb := screenbuf.New(rl)
cur := NewCursor("", s.Pointer, false)
canSearch := s.Searcher != nil
searchMode := s.StartInSearchMode
s.list.SetCursor(cursorPos)
s.list.SetStart(scroll)
c.Listener = func(line []rune, pos int, key rune) ([]rune, int, bool) {
switch {
case key == KeyEnter:
return nil, 0, true
case key == s.Keys.Next.Code || (key == 'j' && !searchMode):
s.list.Next()
case key == s.Keys.Prev.Code || (key == 'k' && !searchMode):
s.list.Prev()
case key == s.Keys.Search.Code:
if !canSearch {
break
}
if searchMode {
searchMode = false
cur.Replace("")
s.list.CancelSearch()
} else {
searchMode = true
}
case key == KeyBackspace || key == KeyCtrlH:
if !canSearch || !searchMode {
break
}
cur.Backspace()
if len(cur.Get()) > 0 {
s.list.Search(cur.Get())
} else {
s.list.CancelSearch()
}
case key == s.Keys.PageUp.Code || (key == 'h' && !searchMode):
s.list.PageUp()
case key == s.Keys.PageDown.Code || (key == 'l' && !searchMode):
s.list.PageDown()
default:
if canSearch && searchMode {
cur.Update(string(line))
s.list.Search(cur.Get())
}
}
if searchMode {
header := SearchPrompt + cur.Format()
sb.WriteString(header)
} else if !s.HideHelp {
help := s.renderHelp(canSearch)
sb.Write(help)
}
label := render(s.Templates.label, s.Label)
sb.Write(label)
items, idx := s.list.Items()
last := len(items) - 1
for i, item := range items {
page := " "
switch i {
case 0:
if s.list.CanPageUp() {
page = "↑"
} else {
page = string(top)
}
case last:
if s.list.CanPageDown() {
page = "↓"
}
}
output := []byte(page + " ")
if i == idx {
output = append(output, render(s.Templates.active, item)...)
} else {
output = append(output, render(s.Templates.inactive, item)...)
}
sb.Write(output)
}
if idx == list.NotFound {
sb.WriteString("")
sb.WriteString("No results")
} else {
active := items[idx]
details := s.renderDetails(active)
for _, d := range details {
sb.Write(d)
}
}
sb.Flush()
return nil, 0, true
}
for {
_, err = rl.Readline()
if err != nil {
switch {
case err == readline.ErrInterrupt, err.Error() == "Interrupt":
err = ErrInterrupt
case err == io.EOF:
err = ErrEOF
}
break
}
_, idx := s.list.Items()
if idx != list.NotFound {
break
}
}
if err != nil {
if err.Error() == "Interrupt" {
err = ErrInterrupt
}
sb.Reset()
sb.WriteString("")
sb.Flush()
rl.Write([]byte(showCursor))
rl.Close()
return 0, "", err
}
items, idx := s.list.Items()
item := items[idx]
if s.HideSelected {
clearScreen(sb)
} else {
sb.Reset()
sb.Write(render(s.Templates.selected, item))
sb.Flush()
}
rl.Write([]byte(showCursor))
rl.Close()
return s.list.Index(), fmt.Sprintf("%v", item), err
}
// ScrollPosition returns the current scroll position.
func (s *Select) ScrollPosition() int {
return s.list.Start()
}
func (s *Select) prepareTemplates() error {
tpls := s.Templates
if tpls == nil {
tpls = &SelectTemplates{}
}
if tpls.FuncMap == nil {
tpls.FuncMap = FuncMap
}
if tpls.Label == "" {
tpls.Label = fmt.Sprintf("%s {{.}}: ", IconInitial)
}
tpl, err := template.New("").Funcs(tpls.FuncMap).Parse(tpls.Label)
if err != nil {
return err
}
tpls.label = tpl
if tpls.Active == "" {
tpls.Active = fmt.Sprintf("%s {{ . | underline }}", IconSelect)
}
tpl, err = template.New("").Funcs(tpls.FuncMap).Parse(tpls.Active)
if err != nil {
return err
}
tpls.active = tpl
if tpls.Inactive == "" {
tpls.Inactive = " {{.}}"
}
tpl, err = template.New("").Funcs(tpls.FuncMap).Parse(tpls.Inactive)
if err != nil {
return err
}
tpls.inactive = tpl
if tpls.Selected == "" {
tpls.Selected = fmt.Sprintf(`{{ "%s" | green }} {{ . | faint }}`, IconGood)
}
tpl, err = template.New("").Funcs(tpls.FuncMap).Parse(tpls.Selected)
if err != nil {
return err
}
tpls.selected = tpl
if tpls.Details != "" {
tpl, err = template.New("").Funcs(tpls.FuncMap).Parse(tpls.Details)
if err != nil {
return err
}
tpls.details = tpl
}
if tpls.Help == "" {
tpls.Help = fmt.Sprintf(`{{ "Use the following keys to navigate:" | faint }}` +
`{{ if .IsVimMode }} {{ "j (Up), k (Down), h (Page Up), l (Page Down)" | faint }} {{ else }} {{ .NextKey | faint }} ` +
`{{ .PrevKey | faint }} {{ .PageDownKey | faint }} {{ .PageUpKey | faint }} {{ "or vim keys" | faint }} {{ end }}` +
`{{ if .Search }} {{ "and" | faint }} {{ .SearchKey | faint }} {{ "toggles search" | faint }}{{ end }}`)
}
tpl, err = template.New("").Funcs(tpls.FuncMap).Parse(tpls.Help)
if err != nil {
return err
}
tpls.help = tpl
s.Templates = tpls
return nil
}
// SelectWithAdd represents a list for selecting a single item inside a list of items with the possibility to
// add new items to the list.
type SelectWithAdd struct {
// Label is the text displayed on top of the list to direct input. The IconInitial value "?" will be
// appended automatically to the label so it does not need to be added.
Label string
// Items are the items to display inside the list. Each item will be listed individually with the
// AddLabel as the first item of the list.
Items []string
// AddLabel is the label used for the first item of the list that enables adding a new item.
// Selecting this item in the list displays the add item prompt using promptui/prompt.
AddLabel string
// Validate is an optional function that fill be used against the entered value in the prompt to validate it.
// If the value is valid, it is returned to the callee to be added in the list.
Validate ValidateFunc
// IsVimMode sets whether to use vim mode when using readline in the command prompt. Look at
// https://godoc.org/github.com/chzyer/readline#Config for more information on readline.
IsVimMode bool
// a function that defines how to render the cursor
Pointer Pointer
// HideHelp sets whether to hide help information.
HideHelp bool
Stdin io.Reader
Stdout io.Writer
}
// Run executes the select list. Its displays the label and the list of items, asking the user to chose any
// value within to list or add his own. Run will keep the prompt alive until it has been canceled from
// the command prompt or it has received a valid value.
//
// If the addLabel is selected in the list, this function will return a -1 index with the added label and no error.
// Otherwise, it will return the index and the value of the selected item. In any case, if an error is triggered, it
// will also return the error as its third return value.
func (sa *SelectWithAdd) Run() (int, string, error) {
if len(sa.Items) > 0 {
newItems := append([]string{sa.AddLabel}, sa.Items...)
list, err := list.New(newItems, 5)
if err != nil {
return 0, "", err
}
s := Select{
Label: sa.Label,
Items: newItems,
IsVimMode: sa.IsVimMode,
HideHelp: sa.HideHelp,
Size: 5,
list: list,
Pointer: sa.Pointer,
Stdin: sa.Stdin,
Stdout: sa.Stdout,
}
s.setKeys()
err = s.prepareTemplates()
if err != nil {
return 0, "", err
}
selected, value, err := s.innerRun(1, 0, '+')
if err != nil || selected != 0 {
return selected - 1, value, err
}
// XXX run through terminal for windows
sa.Stdout.Write([]byte(upLine(1) + "\r" + clearLine))
}
p := Prompt{
Label: sa.AddLabel,
Validate: sa.Validate,
IsVimMode: sa.IsVimMode,
Pointer: sa.Pointer,
}
value, err := p.Run()
return SelectedAdd, value, err
}
func (s *Select) setKeys() {
if s.Keys != nil {
return
}
s.Keys = &SelectKeys{
Prev: Key{Code: KeyPrev, Display: KeyPrevDisplay},
Next: Key{Code: KeyNext, Display: KeyNextDisplay},
PageUp: Key{Code: KeyBackward, Display: KeyBackwardDisplay},
PageDown: Key{Code: KeyForward, Display: KeyForwardDisplay},
Search: Key{Code: '/', Display: "/"},
}
}
func (s *Select) renderDetails(item interface{}) [][]byte {
if s.Templates.details == nil {
return nil
}
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 8, ' ', 0)
err := s.Templates.details.Execute(w, item)
if err != nil {
fmt.Fprintf(w, "%v", item)
}
w.Flush()
output := buf.Bytes()
return bytes.Split(output, []byte("\n"))
}
func (s *Select) renderHelp(b bool) []byte {
keys := struct {
NextKey string
PrevKey string
PageDownKey string
PageUpKey string
Search bool
SearchKey string
IsVimMode bool
}{
NextKey: s.Keys.Next.Display,
PrevKey: s.Keys.Prev.Display,
PageDownKey: s.Keys.PageDown.Display,
PageUpKey: s.Keys.PageUp.Display,
SearchKey: s.Keys.Search.Display,
Search: b,
IsVimMode: s.IsVimMode,
}
return render(s.Templates.help, keys)
}
func render(tpl *template.Template, data interface{}) []byte {
var buf bytes.Buffer
err := tpl.Execute(&buf, data)
if err != nil {
return []byte(fmt.Sprintf("%v", data))
}
return buf.Bytes()
}
func clearScreen(sb *screenbuf.ScreenBuf) {
sb.Reset()
sb.Clear()
sb.Flush()
}