Debian Codegraph @

examples/burger/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "errors"
5 "fmt"
6 "os"
7 "strconv"
8 "strings"
9 "time"
10
11 "github.com/charmbracelet/huh"
12 "github.com/charmbracelet/huh/spinner"
13 "github.com/charmbracelet/lipgloss"
14 xstrings "github.com/charmbracelet/x/exp/strings"
15 )
16
17 type Spice int
18
19 const (
20 Mild Spice = iota + 1
21 Medium
22 Hot
23 )
24
25 func (s Spice) String() string {
26 switch s {
27 case Mild:
28 return "Mild "
29 case Medium:
30 return "Medium-Spicy "
31 case Hot:
32 return "Spicy-Hot "
33 default:
34 return ""
35 }
36 }
37
38 type Order struct {
39 Burger Burger
40 Side string
41 Name string
42 Instructions string
43 Discount bool
44 }
45
46 type Burger struct {
47 Type string
48 Toppings []string
49 Spice Spice
50 }
51
52 func main() {
53 var burger Burger
54 order := Order{Burger: burger}
55
56 // Should we run in accessible mode?
57 accessible, _ := strconv.ParseBool(os.Getenv("ACCESSIBLE"))
58
59 form := huh.NewForm(
60 huh.NewGroup(huh.NewNote().
61 Title("Charmburger").
62 Description("Welcome to _Charmburger™_.\n\nHow may we take your order?").
63 Next(true).
64 NextLabel("Next"),
65 ),
66
67 // Choose a burger.
68 // We'll need to know what topping to add too.
69 huh.NewGroup(
70 huh.NewSelect[string]().
71 Options(huh.NewOptions("Charmburger Classic", "Chickwich", "Fishburger", "Charmpossible™ Burger")...).
72 Title("Choose your burger").
73 Description("At Charm we truly have a burger for everyone.").
74 Validate(func(t string) error {
75 if t == "Fishburger" {
76 return fmt.Errorf("no fish today, sorry")
77 }
78 return nil
79 }).
80 Value(&order.Burger.Type),
81
82 huh.NewMultiSelect[string]().
83 Title("Toppings").
84 Description("Choose up to 4.").
85 Options(
86 huh.NewOption("Lettuce", "Lettuce").Selected(true),
87 huh.NewOption("Tomatoes", "Tomatoes").Selected(true),
88 huh.NewOption("Charm Sauce", "Charm Sauce"),
89 huh.NewOption("Jalapeños", "Jalapeños"),
90 huh.NewOption("Cheese", "Cheese"),
91 huh.NewOption("Vegan Cheese", "Vegan Cheese"),
92 huh.NewOption("Nutella", "Nutella"),
93 ).
94 Validate(func(t []string) error {
95 if len(t) <= 0 {
96 return fmt.Errorf("at least one topping is required")
97 }
98 return nil
99 }).
100 Value(&order.Burger.Toppings).
101 Filterable(true).
102 Limit(4),
103 ),
104
105 // Prompt for toppings and special instructions.
106 // The customer can ask for up to 4 toppings.
107 huh.NewGroup(
108 huh.NewSelect[Spice]().
109 Title("Spice level").
110 Options(
111 huh.NewOption("Mild", Mild).Selected(true),
112 huh.NewOption("Medium", Medium),
113 huh.NewOption("Hot", Hot),
114 ).
115 Value(&order.Burger.Spice),
116
117 huh.NewSelect[string]().
118 Options(huh.NewOptions("Fries", "Disco Fries", "R&B Fries", "Carrots")...).
119 Value(&order.Side).
120 Title("Sides").
121 Description("You get one free side with this order."),
122 ),
123
124 // Gather final details for the order.
125 huh.NewGroup(
126 huh.NewInput().
127 Value(&order.Name).
128 Title("What's your name?").
129 Placeholder("Margaret Thatcher").
130 Validate(func(s string) error {
131 if s == "Frank" {
132 return errors.New("no franks, sorry")
133 }
134 return nil
135 }).
136 Description("For when your order is ready."),
137
138 huh.NewText().
139 Value(&order.Instructions).
140 Placeholder("Just put it in the mailbox please").
141 Title("Special Instructions").
142 Description("Anything we should know?").
143 CharLimit(400).
144 Lines(5),
145
146 huh.NewConfirm().
147 Title("Would you like 15% off?").
148 Value(&order.Discount).
149 Affirmative("Yes!").
150 Negative("No."),
151 ),
152 ).WithAccessible(accessible)
153
154 err := form.Run()
155 if err != nil {
156 fmt.Println("Uh oh:", err)
157 os.Exit(1)
158 }
159
160 prepareBurger := func() {
161 time.Sleep(2 * time.Second)
162 }
163
164 _ = spinner.New().Title("Preparing your burger...").Accessible(accessible).Action(prepareBurger).Run()
165
166 // Print order summary.
167 {
168 var sb strings.Builder
169 keyword := func(s string) string {
170 return lipgloss.NewStyle().Foreground(lipgloss.Color("212")).Render(s)
171 }
172 fmt.Fprintf(&sb,
173 "%s\n\nOne %s%s, topped with %s with %s on the side.",
174 lipgloss.NewStyle().Bold(true).Render("BURGER RECEIPT"),
175 keyword(order.Burger.Spice.String()),
176 keyword(order.Burger.Type),
177 keyword(xstrings.EnglishJoin(order.Burger.Toppings, true)),
178 keyword(order.Side),
179 )
180
181 name := order.Name
182 if name != "" {
183 name = ", " + name
184 }
185 fmt.Fprintf(&sb, "\n\nThanks for your order%s!", name)
186
187 if order.Discount {
188 fmt.Fprint(&sb, "\n\nEnjoy 15% off.")
189 }
190
191 fmt.Println(
192 lipgloss.NewStyle().
193 Width(40).
194 BorderStyle(lipgloss.RoundedBorder()).
195 BorderForeground(lipgloss.Color("63")).
196 Padding(1, 2).
197 Render(sb.String()),
198 )
199 }
200 }