File: main.go

package info (click to toggle)
golang-github-charmbracelet-huh 0.5.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 656 kB
  • sloc: makefile: 15
file content (133 lines) | stat: -rw-r--r-- 3,050 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main

import (
	"log"

	"github.com/charmbracelet/huh"
)

// TODO: ensure input is not plagiarized.
func checkForPlagiarism(s string) error { return nil }

// TODO: ensure input is food.
func isFood(s string) error { return nil }

// TODO: ensure input is a valid name.
func validateName(s string) error { return nil }

func main() {
	var (
		lunch    string
		story    string
		country  string
		toppings []string
		discount bool
	)

	// `Input`s are single line text fields.
	huh.NewInput().
		Title("What's for lunch?").
		Prompt("?").
		Validate(isFood).
		Value(&lunch)

	// `Text`s are multi-line text fields.
	huh.NewText().
		Title("Tell me a story.").
		Validate(checkForPlagiarism).
		Value(&story)

	// `Select`s are multiple choice questions.
	huh.NewSelect[string]().
		Title("Pick a country.").
		Options(
			huh.NewOption("United States", "US"),
			huh.NewOption("Germany", "DE"),
			huh.NewOption("Brazil", "BR"),
			huh.NewOption("Canada", "CA"),
		).
		Value(&country)

	// `MultiSelect`s allow multiple selections from a list of options.
	huh.NewMultiSelect[string]().
		Options(
			huh.NewOption("Cheese", "cheese").Selected(true),
			huh.NewOption("Lettuce", "lettuce").Selected(true),
			huh.NewOption("Corn", "corn"),
			huh.NewOption("Salsa", "salsa"),
			huh.NewOption("Sour Cream", "sour cream"),
			huh.NewOption("Tomatoes", "tomatoes"),
		).
		Title("Toppings").
		Limit(4).
		Value(&toppings)

	// `Confirm`s are a confirmation prompt.
	huh.NewConfirm().
		Title("Want a discount?").
		Affirmative("Yes!").
		Negative("No.").
		Value(&discount)

	// Form
	var (
		burger       string
		name         string
		instructions string
	)

	form := huh.NewForm(
		// Prompt the user to choose a burger.
		huh.NewGroup(
			huh.NewSelect[string]().
				Options(
					huh.NewOption("Charmburger Classic", "classic"),
					huh.NewOption("Chickwich", "chickwich"),
					huh.NewOption("Fishburger", "Fishburger"),
					huh.NewOption("Charmpossible™ Burger", "charmpossible"),
				).
				Title("Choose your burger").
				Value(&burger),
		),

		// Prompt for toppings and special instructions.
		// The customer can ask for up to 4 toppings.
		huh.NewGroup(
			huh.NewMultiSelect[string]().
				Options(
					huh.NewOption("Lettuce", "Lettuce").Selected(true),
					huh.NewOption("Tomatoes", "Tomatoes").Selected(true),
					huh.NewOption("Charm Sauce", "Charm Sauce"),
					huh.NewOption("Jalapeños", "Jalapeños"),
					huh.NewOption("Cheese", "Cheese"),
					huh.NewOption("Vegan Cheese", "Vegan Cheese"),
					huh.NewOption("Nutella", "Nutella"),
				).
				Title("Toppings").
				Limit(4).
				Value(&toppings),
		),

		// Gather final details for the order.
		huh.NewGroup(
			huh.NewInput().
				Title("What's your name?").
				Value(&name).
				Validate(validateName),

			huh.NewText().
				Title("Special Instructions").
				Value(&instructions).
				CharLimit(400),

			huh.NewConfirm().
				Title("Would you like 15% off").
				Value(&discount),
		),
	)

	err := form.Run()
	if err != nil {
		log.Fatal(err)
	}
}