forked from charmbracelet/huh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
251 lines (203 loc) · 5.21 KB
/
group.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package huh
import (
"strings"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/paginator"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/exp/ordered"
)
// Group is a collection of fields that are displayed together with a page of
// the form. While a group is displayed the form completer can switch between
// fields in the group.
//
// If any of the fields in a group have errors, the form will not be able to
// progress to the next group.
type Group struct {
// collection of fields
fields []Field
// information
title string
description string
// navigation
paginator paginator.Model
// help
showHelp bool
help help.Model
// errors
showErrors bool
// group options
width int
theme *Theme
keymap *KeyMap
hide func() bool
}
// NewGroup returns a new group with the given fields.
func NewGroup(fields ...Field) *Group {
p := paginator.New()
p.SetTotalPages(len(fields))
return &Group{
fields: fields,
paginator: p,
help: help.New(),
showHelp: true,
showErrors: true,
}
}
// Title sets the group's title.
func (g *Group) Title(title string) *Group {
g.title = title
return g
}
// Description sets the group's description.
func (g *Group) Description(description string) *Group {
g.description = description
return g
}
// WithShowHelp sets whether or not the group's help should be shown.
func (g *Group) WithShowHelp(show bool) *Group {
g.showHelp = show
return g
}
// WithShowErrors sets whether or not the group's errors should be shown.
func (g *Group) WithShowErrors(show bool) *Group {
g.showErrors = show
return g
}
// WithTheme sets the theme on a group.
func (g *Group) WithTheme(t *Theme) *Group {
g.theme = t
for _, field := range g.fields {
field.WithTheme(t)
}
return g
}
// WithKeyMap sets the keymap on a group.
func (g *Group) WithKeyMap(k *KeyMap) *Group {
g.keymap = k
for _, field := range g.fields {
field.WithKeyMap(k)
}
return g
}
// WithWidth sets the width on a group.
func (g *Group) WithWidth(width int) *Group {
g.width = width
for _, field := range g.fields {
field.WithWidth(width)
}
return g
}
// WithHide sets whether this group should be skipped.
func (g *Group) WithHide(hide bool) *Group {
g.WithHideFunc(func() bool { return hide })
return g
}
// WithHideFunc sets the function that checks if this group should be skipped.
func (g *Group) WithHideFunc(hideFunc func() bool) *Group {
g.hide = hideFunc
return g
}
// Errors returns the groups' fields' errors.
func (g *Group) Errors() []error {
var errs []error
for _, field := range g.fields {
if err := field.Error(); err != nil {
errs = append(errs, err)
}
}
return errs
}
// nextFieldMsg is a message to move to the next field,
//
// each field controls when to send this message such that it is able to use
// different key bindings or events to trigger group progression.
type nextFieldMsg struct{}
// prevFieldMsg is a message to move to the previous field.
//
// each field controls when to send this message such that it is able to use
// different key bindings or events to trigger group progression.
type prevFieldMsg struct{}
// nextField is the command to move to the next field.
func nextField() tea.Msg {
return nextFieldMsg{}
}
// prevField is the command to move to the previous field.
func prevField() tea.Msg {
return prevFieldMsg{}
}
// Init initializes the group.
func (g *Group) Init() tea.Cmd {
cmds := make([]tea.Cmd, len(g.fields)+1)
for i, field := range g.fields {
cmds[i] = field.Init()
}
cmd := g.fields[g.paginator.Page].Focus()
cmds = append(cmds, cmd)
return tea.Batch(cmds...)
}
// setCurrent sets the current field.
func (g *Group) setCurrent(current int) tea.Cmd {
var (
cmds []tea.Cmd
cmd tea.Cmd
)
cmd = g.fields[g.paginator.Page].Blur()
cmds = append(cmds, cmd)
g.paginator.Page = ordered.Clamp(current, 0, len(g.fields)-1)
cmd = g.fields[g.paginator.Page].Focus()
cmds = append(cmds, cmd)
return tea.Batch(cmds...)
}
// Update updates the group.
func (g *Group) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
m, cmd := g.fields[g.paginator.Page].Update(msg)
g.fields[g.paginator.Page] = m.(Field)
cmds = append(cmds, cmd)
switch msg.(type) {
case nextFieldMsg:
current := g.paginator.Page
cmd = g.setCurrent(current + 1)
if current >= g.paginator.TotalPages-1 {
cmds = append(cmds, nextGroup)
break
}
cmds = append(cmds, cmd)
case prevFieldMsg:
current := g.paginator.Page
cmd = g.setCurrent(current - 1)
if current == 0 {
cmds = append(cmds, prevGroup)
break
}
cmds = append(cmds, cmd)
}
return g, tea.Batch(cmds...)
}
// View renders the group.
func (g *Group) View() string {
var s strings.Builder
gap := g.theme.FieldSeparator.String()
if gap == "" {
gap = "\n"
}
for i, field := range g.fields {
s.WriteString(field.View())
if i < len(g.fields)-1 {
s.WriteString(gap)
}
}
errors := g.Errors()
s.WriteString(gap)
if g.showHelp && len(errors) <= 0 {
s.WriteString(g.help.ShortHelpView(g.fields[g.paginator.Page].KeyBinds()))
}
if !g.showErrors {
return s.String()
}
for _, err := range errors {
s.WriteString(g.theme.Focused.ErrorMessage.Render(err.Error()))
s.WriteString("\n")
}
return s.String()
}