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) }