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 (83 lines) | stat: -rw-r--r-- 1,941 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
package main

import (
	"fmt"
	"os"

	"github.com/charmbracelet/huh"
)

type consumable int

const (
	fruits consumable = iota
	vegetables
	drinks
)

func (c consumable) String() string {
	return [...]string{"fruit", "vegetable", "drink"}[c]
}

func main() {

	var category consumable
	type opts []huh.Option[string]

	var choice string

	// Then ask for a specific food item based on the previous answer.
	err :=
		huh.NewForm(
			huh.NewGroup(
				huh.NewSelect[consumable]().
					Title("What are you in the mood for?").
					Value(&category).
					Options(
						huh.NewOption("Some fruit", fruits),
						huh.NewOption("A vegetable", vegetables),
						huh.NewOption("A drink", drinks),
					),

				huh.NewSelect[string]().
					Value(&choice).
					Height(7).
					TitleFunc(func() string {
						return fmt.Sprintf("Okay, what kind of %s are you in the mood for?", category)
					}, &category).
					OptionsFunc(func() []huh.Option[string] {
						switch category {
						case fruits:
							return []huh.Option[string]{
								huh.NewOption("Tangerine", "tangerine"),
								huh.NewOption("Canteloupe", "canteloupe"),
								huh.NewOption("Pomelo", "pomelo"),
								huh.NewOption("Grapefruit", "grapefruit"),
							}
						case vegetables:
							return []huh.Option[string]{
								huh.NewOption("Carrot", "carrot"),
								huh.NewOption("Jicama", "jicama"),
								huh.NewOption("Kohlrabi", "kohlrabi"),
								huh.NewOption("Fennel", "fennel"),
								huh.NewOption("Ginger", "ginger"),
							}
						default:
							return []huh.Option[string]{
								huh.NewOption("Coffee", "coffee"),
								huh.NewOption("Tea", "tea"),
								huh.NewOption("Bubble Tea", "bubble tea"),
								huh.NewOption("Agua Fresca", "agua-fresca"),
							}
						}
					}, &category),
			),
		).Run()

	if err != nil {
		fmt.Println("Trouble in food paradise:", err)
		os.Exit(1)
	}

	fmt.Printf("One %s coming right up!\n", choice)
}