Debian Codegraph @

examples/conditional/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "fmt"
5 "os"
6
7 "github.com/charmbracelet/huh"
8 )
9
10 type consumable int
11
12 const (
13 fruits consumable = iota
14 vegetables
15 drinks
16 )
17
18 func (c consumable) String() string {
19 return [...]string{"fruit", "vegetable", "drink"}[c]
20 }
21
22 func main() {
23
24 var category consumable
25 type opts []huh.Option[string]
26
27 var choice string
28
29 // Then ask for a specific food item based on the previous answer.
30 err :=
31 huh.NewForm(
32 huh.NewGroup(
33 huh.NewSelect[consumable]().
34 Title("What are you in the mood for?").
35 Value(&category).
36 Options(
37 huh.NewOption("Some fruit", fruits),
38 huh.NewOption("A vegetable", vegetables),
39 huh.NewOption("A drink", drinks),
40 ),
41
42 huh.NewSelect[string]().
43 Value(&choice).
44 Height(7).
45 TitleFunc(func() string {
46 return fmt.Sprintf("Okay, what kind of %s are you in the mood for?", category)
47 }, &category).
48 OptionsFunc(func() []huh.Option[string] {
49 switch category {
50 case fruits:
51 return []huh.Option[string]{
52 huh.NewOption("Tangerine", "tangerine"),
53 huh.NewOption("Canteloupe", "canteloupe"),
54 huh.NewOption("Pomelo", "pomelo"),
55 huh.NewOption("Grapefruit", "grapefruit"),
56 }
57 case vegetables:
58 return []huh.Option[string]{
59 huh.NewOption("Carrot", "carrot"),
60 huh.NewOption("Jicama", "jicama"),
61 huh.NewOption("Kohlrabi", "kohlrabi"),
62 huh.NewOption("Fennel", "fennel"),
63 huh.NewOption("Ginger", "ginger"),
64 }
65 default:
66 return []huh.Option[string]{
67 huh.NewOption("Coffee", "coffee"),
68 huh.NewOption("Tea", "tea"),
69 huh.NewOption("Bubble Tea", "bubble tea"),
70 huh.NewOption("Agua Fresca", "agua-fresca"),
71 }
72 }
73 }, &category),
74 ),
75 ).Run()
76
77 if err != nil {
78 fmt.Println("Trouble in food paradise:", err)
79 os.Exit(1)
80 }
81
82 fmt.Printf("One %s coming right up!\n", choice)
83 }