-
Notifications
You must be signed in to change notification settings - Fork 134
/
main.go
201 lines (176 loc) · 4.53 KB
/
main.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/spinner"
"github.com/charmbracelet/lipgloss"
xstrings "github.com/charmbracelet/x/exp/strings"
)
type Spice int
const (
Mild Spice = iota + 1
Medium
Hot
)
func (s Spice) String() string {
switch s {
case Mild:
return "Mild "
case Medium:
return "Medium-Spicy "
case Hot:
return "Spicy-Hot "
default:
return ""
}
}
type Order struct {
Burger Burger
Side string
Name string
Instructions string
Discount bool
}
type Burger struct {
Type string
Toppings []string
Spice Spice
}
func main() {
var burger Burger
var order = Order{Burger: burger}
// Should we run in accessible mode?
accessible, _ := strconv.ParseBool(os.Getenv("ACCESSIBLE"))
form := huh.NewForm(
huh.NewGroup(huh.NewNote().
Title("Charmburger").
Description("Welcome to _Charmburger™_.\n\nHow may we take your order?\n\n").
Next(true).
NextLabel("Next"),
),
// Choose a burger.
// We'll need to know what topping to add too.
huh.NewGroup(
huh.NewSelect[string]().
Options(huh.NewOptions("Charmburger Classic", "Chickwich", "Fishburger", "Charmpossible™ Burger")...).
Title("Choose your burger").
Description("At Charm we truly have a burger for everyone.").
Validate(func(t string) error {
if t == "Fishburger" {
return fmt.Errorf("no fish today, sorry")
}
return nil
}).
Value(&order.Burger.Type),
huh.NewMultiSelect[string]().
Title("Toppings").
Description("Choose up to 4.").
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"),
).
Validate(func(t []string) error {
if len(t) <= 0 {
return fmt.Errorf("at least one topping is required")
}
return nil
}).
Value(&order.Burger.Toppings).
Filterable(true).
Limit(4),
),
// Prompt for toppings and special instructions.
// The customer can ask for up to 4 toppings.
huh.NewGroup(
huh.NewSelect[Spice]().
Title("Spice level").
Options(
huh.NewOption("Mild", Mild).Selected(true),
huh.NewOption("Medium", Medium),
huh.NewOption("Hot", Hot),
).
Value(&order.Burger.Spice),
huh.NewSelect[string]().
Options(huh.NewOptions("Fries", "Disco Fries", "R&B Fries", "Carrots")...).
Value(&order.Side).
Title("Sides").
Description("You get one free side with this order."),
),
// Gather final details for the order.
huh.NewGroup(
huh.NewInput().
Value(&order.Name).
Title("What's your name?").
Placeholder("Margaret Thatcher").
Validate(func(s string) error {
if s == "Frank" {
return errors.New("no franks, sorry")
}
return nil
}).
Description("For when your order is ready."),
huh.NewText().
Value(&order.Instructions).
Placeholder("Just put it in the mailbox please").
Title("Special Instructions").
Description("Anything we should know?").
CharLimit(400).
Lines(5),
huh.NewConfirm().
Title("Would you like 15% off?").
Value(&order.Discount).
Affirmative("Yes!").
Negative("No."),
),
).WithAccessible(accessible)
err := form.Run()
if err != nil {
fmt.Println("Uh oh:", err)
os.Exit(1)
}
prepareBurger := func() {
time.Sleep(2 * time.Second)
}
_ = spinner.New().Title("Preparing your burger...").Accessible(accessible).Action(prepareBurger).Run()
// Print order summary.
{
var sb strings.Builder
keyword := func(s string) string {
return lipgloss.NewStyle().Foreground(lipgloss.Color("212")).Render(s)
}
fmt.Fprintf(&sb,
"%s\n\nOne %s%s, topped with %s with %s on the side.",
lipgloss.NewStyle().Bold(true).Render("BURGER RECEIPT"),
keyword(order.Burger.Spice.String()),
keyword(order.Burger.Type),
keyword(xstrings.EnglishJoin(order.Burger.Toppings, true)),
keyword(order.Side),
)
name := order.Name
if name != "" {
name = ", " + name
}
fmt.Fprintf(&sb, "\n\nThanks for your order%s!", name)
if order.Discount {
fmt.Fprint(&sb, "\n\nEnjoy 15% off.")
}
fmt.Println(
lipgloss.NewStyle().
Width(40).
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("63")).
Padding(1, 2).
Render(sb.String()),
)
}
}