1
+ package promptui
2
+
3
+ import (
4
+ "fmt"
5
+ "strings"
6
+ )
7
+
8
+ // Any type can be given to the select's item as long as the templates properly implement the dot notation
9
+ // to display it.
10
+ type pepper struct {
11
+ Name string
12
+ HeatUnit int
13
+ Peppers int
14
+ }
15
+
16
+ // This examples shows a complex and customized select.
17
+ func ExampleSelect () {
18
+ // The select will show a series of peppers stored inside a slice of structs. To display the content of the struct,
19
+ // the usual dot notation is used inside the templates to select the fields and color them.
20
+ peppers := []pepper {
21
+ {Name : "Bell Pepper" , HeatUnit : 0 , Peppers : 0 },
22
+ {Name : "Banana Pepper" , HeatUnit : 100 , Peppers : 1 },
23
+ {Name : "Poblano" , HeatUnit : 1000 , Peppers : 2 },
24
+ {Name : "Jalapeño" , HeatUnit : 3500 , Peppers : 3 },
25
+ {Name : "Aleppo" , HeatUnit : 10000 , Peppers : 4 },
26
+ {Name : "Tabasco" , HeatUnit : 30000 , Peppers : 5 },
27
+ {Name : "Malagueta" , HeatUnit : 50000 , Peppers : 6 },
28
+ {Name : "Habanero" , HeatUnit : 100000 , Peppers : 7 },
29
+ {Name : "Red Savina Habanero" , HeatUnit : 350000 , Peppers : 8 },
30
+ {Name : "Dragon’s Breath" , HeatUnit : 855000 , Peppers : 9 },
31
+ }
32
+
33
+ // The Active and Selected templates set a small pepper icon next to the name colored and the heat unit for the
34
+ // active template. The details template is show at the bottom of the select's list and displays the full info
35
+ // for that pepper in a multi-line template.
36
+ templates := & SelectTemplates {
37
+ Label : "{{ . }}?" ,
38
+ Active : "\U0001F336 {{ .Name | cyan }} ({{ .HeatUnit | red }})" ,
39
+ Inactive : " {{ .Name | cyan }} ({{ .HeatUnit | red }})" ,
40
+ Selected : "\U0001F336 {{ .Name | red | cyan }}" ,
41
+ Details : `
42
+ --------- Pepper ----------
43
+ {{ "Name:" | faint }} {{ .Name }}
44
+ {{ "Heat Unit:" | faint }} {{ .HeatUnit }}
45
+ {{ "Peppers:" | faint }} {{ .Peppers }}` ,
46
+ }
47
+
48
+ // A searcher function is implemented which enabled the search mode for the select. The function follows
49
+ // the required searcher signature and finds any pepper whose name contains the searched string.
50
+ searcher := func (input string , index int ) bool {
51
+ pepper := peppers [index ]
52
+ name := strings .Replace (strings .ToLower (pepper .Name ), " " , "" , - 1 )
53
+ input = strings .Replace (strings .ToLower (input ), " " , "" , - 1 )
54
+
55
+ return strings .Contains (name , input )
56
+ }
57
+
58
+ prompt := Select {
59
+ Label : "Spicy Level" ,
60
+ Items : peppers ,
61
+ Templates : templates ,
62
+ Size : 4 ,
63
+ Searcher : searcher ,
64
+ }
65
+
66
+ i , _ , err := prompt .Run ()
67
+
68
+ if err != nil {
69
+ fmt .Printf ("Prompt failed %v\n " , err )
70
+ return
71
+ }
72
+
73
+ // The selected pepper will be displayed with its name and index in a formatted message.
74
+ fmt .Printf ("You choose number %d: %s\n " , i + 1 , peppers [i ].Name )
75
+ }
0 commit comments