Debian Codegraph @

examples/readme/main/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "log"
5
6 "github.com/charmbracelet/huh"
7 )
8
9 // TODO: ensure input is not plagiarized.
10 func checkForPlagiarism(s string) error { return nil }
11
12 // TODO: ensure input is food.
13 func isFood(s string) error { return nil }
14
15 // TODO: ensure input is a valid name.
16 func validateName(s string) error { return nil }
17
18 func main() {
19 var (
20 lunch string
21 story string
22 country string
23 toppings []string
24 discount bool
25 )
26
27 // `Input`s are single line text fields.
28 huh.NewInput().
29 Title("What's for lunch?").
30 Prompt("?").
31 Validate(isFood).
32 Value(&lunch)
33
34 // `Text`s are multi-line text fields.
35 huh.NewText().
36 Title("Tell me a story.").
37 Validate(checkForPlagiarism).
38 Value(&story)
39
40 // `Select`s are multiple choice questions.
41 huh.NewSelect[string]().
42 Title("Pick a country.").
43 Options(
44 huh.NewOption("United States", "US"),
45 huh.NewOption("Germany", "DE"),
46 huh.NewOption("Brazil", "BR"),
47 huh.NewOption("Canada", "CA"),
48 ).
49 Value(&country)
50
51 // `MultiSelect`s allow multiple selections from a list of options.
52 huh.NewMultiSelect[string]().
53 Options(
54 huh.NewOption("Cheese", "cheese").Selected(true),
55 huh.NewOption("Lettuce", "lettuce").Selected(true),
56 huh.NewOption("Corn", "corn"),
57 huh.NewOption("Salsa", "salsa"),
58 huh.NewOption("Sour Cream", "sour cream"),
59 huh.NewOption("Tomatoes", "tomatoes"),
60 ).
61 Title("Toppings").
62 Limit(4).
63 Value(&toppings)
64
65 // `Confirm`s are a confirmation prompt.
66 huh.NewConfirm().
67 Title("Want a discount?").
68 Affirmative("Yes!").
69 Negative("No.").
70 Value(&discount)
71
72 // Form
73 var (
74 burger string
75 name string
76 instructions string
77 )
78
79 form := huh.NewForm(
80 // Prompt the user to choose a burger.
81 huh.NewGroup(
82 huh.NewSelect[string]().
83 Options(
84 huh.NewOption("Charmburger Classic", "classic"),
85 huh.NewOption("Chickwich", "chickwich"),
86 huh.NewOption("Fishburger", "Fishburger"),
87 huh.NewOption("Charmpossible™ Burger", "charmpossible"),
88 ).
89 Title("Choose your burger").
90 Value(&burger),
91 ),
92
93 // Prompt for toppings and special instructions.
94 // The customer can ask for up to 4 toppings.
95 huh.NewGroup(
96 huh.NewMultiSelect[string]().
97 Options(
98 huh.NewOption("Lettuce", "Lettuce").Selected(true),
99 huh.NewOption("Tomatoes", "Tomatoes").Selected(true),
100 huh.NewOption("Charm Sauce", "Charm Sauce"),
101 huh.NewOption("Jalapeños", "Jalapeños"),
102 huh.NewOption("Cheese", "Cheese"),
103 huh.NewOption("Vegan Cheese", "Vegan Cheese"),
104 huh.NewOption("Nutella", "Nutella"),
105 ).
106 Title("Toppings").
107 Limit(4).
108 Value(&toppings),
109 ),
110
111 // Gather final details for the order.
112 huh.NewGroup(
113 huh.NewInput().
114 Title("What's your name?").
115 Value(&name).
116 Validate(validateName),
117
118 huh.NewText().
119 Title("Special Instructions").
120 Value(&instructions).
121 CharLimit(400),
122
123 huh.NewConfirm().
124 Title("Would you like 15% off").
125 Value(&discount),
126 ),
127 )
128
129 err := form.Run()
130 if err != nil {
131 log.Fatal(err)
132 }
133 }