forked from tigergraph/promptui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_select_test.go
75 lines (65 loc) · 2.54 KB
/
example_select_test.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
package promptui
import (
"fmt"
"strings"
)
// Any type can be given to the select's item as long as the templates properly implement the dot notation
// to display it.
type pepper struct {
Name string
HeatUnit int
Peppers int
}
// This examples shows a complex and customized select.
func ExampleSelect() {
// The select will show a series of peppers stored inside a slice of structs. To display the content of the struct,
// the usual dot notation is used inside the templates to select the fields and color them.
peppers := []pepper{
{Name: "Bell Pepper", HeatUnit: 0, Peppers: 0},
{Name: "Banana Pepper", HeatUnit: 100, Peppers: 1},
{Name: "Poblano", HeatUnit: 1000, Peppers: 2},
{Name: "Jalapeño", HeatUnit: 3500, Peppers: 3},
{Name: "Aleppo", HeatUnit: 10000, Peppers: 4},
{Name: "Tabasco", HeatUnit: 30000, Peppers: 5},
{Name: "Malagueta", HeatUnit: 50000, Peppers: 6},
{Name: "Habanero", HeatUnit: 100000, Peppers: 7},
{Name: "Red Savina Habanero", HeatUnit: 350000, Peppers: 8},
{Name: "Dragon’s Breath", HeatUnit: 855000, Peppers: 9},
}
// The Active and Selected templates set a small pepper icon next to the name colored and the heat unit for the
// active template. The details template is show at the bottom of the select's list and displays the full info
// for that pepper in a multi-line template.
templates := &SelectTemplates{
Label: "{{ . }}?",
Active: "\U0001F336 {{ .Name | cyan }} ({{ .HeatUnit | red }})",
Inactive: " {{ .Name | cyan }} ({{ .HeatUnit | red }})",
Selected: "\U0001F336 {{ .Name | red | cyan }}",
Details: `
--------- Pepper ----------
{{ "Name:" | faint }} {{ .Name }}
{{ "Heat Unit:" | faint }} {{ .HeatUnit }}
{{ "Peppers:" | faint }} {{ .Peppers }}`,
}
// A searcher function is implemented which enabled the search mode for the select. The function follows
// the required searcher signature and finds any pepper whose name contains the searched string.
searcher := func(input string, index int) bool {
pepper := peppers[index]
name := strings.Replace(strings.ToLower(pepper.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := Select{
Label: "Spicy Level",
Items: peppers,
Templates: templates,
Size: 4,
Searcher: searcher,
}
i, _, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
// The selected pepper will be displayed with its name and index in a formatted message.
fmt.Printf("You choose number %d: %s\n", i+1, peppers[i].Name)
}