-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathform.go
187 lines (158 loc) · 3.58 KB
/
form.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
package huh
import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/paginator"
tea "github.com/charmbracelet/bubbletea"
)
// Form represents a Huh? form.
// It is a collection of groups and controls navigation between pages.
type Form struct {
groups []*Group
paginator paginator.Model
accessible bool
showHelp bool
quitting bool
theme *Theme
keymap *KeyMap
}
// NewForm creates a new form with the given groups.
func NewForm(groups ...*Group) *Form {
p := paginator.New()
p.SetTotalPages(len(groups))
return &Form{
groups: groups,
paginator: p,
theme: NewCharmTheme(),
keymap: NewDefaultKeyMap(),
}
}
// Field is a form field.
type Field interface {
// Bubble Tea Model
Init() tea.Cmd
Update(tea.Msg) (tea.Model, tea.Cmd)
View() string
// Bubble Tea Events
Blur() tea.Cmd
Focus() tea.Cmd
// Errors and Validation
Error() error
// Accessible Prompt (non-redraw)
Run()
// Theme sets the theme on a field.
Theme(*Theme) Field
// KeyMap sets the keymap on a field.
KeyMap(*KeyMap) Field
KeyBinds() []key.Binding
}
type nextGroupMsg struct{}
type prevGroupMsg struct{}
func nextGroup() tea.Msg {
return nextGroupMsg{}
}
func prevGroup() tea.Msg {
return prevGroupMsg{}
}
// Accessible sets the form to run in accessible mode to avoid redrawing the
// views which makes it easier for screen readers to read and describe the form.
//
// This avoids using the Bubble Tea renderer and instead simply uses basic
// terminal prompting to gather input which degrades the user experience but
// provides accessibility.
func (f *Form) Accessible(b bool) *Form {
f.accessible = b
return f
}
// Theme sets the theme on a form.
func (f *Form) Theme(theme *Theme) *Form {
if theme != nil {
f.theme = theme
}
return f
}
// KeyMap sets the keymap on a form.
func (f *Form) KeyMap(keymap *KeyMap) *Form {
f.keymap = keymap
return f
}
// ShowHelp sets whether to show help on a form.
func (f *Form) ShowHelp(v bool) *Form {
for _, group := range f.groups {
group.ShowHelp(v)
}
return f
}
// Init initializes the form.
func (f *Form) Init() tea.Cmd {
var cmds []tea.Cmd
for _, group := range f.groups {
cmds = append(cmds, group.Init())
}
return tea.Batch(cmds...)
}
// Update updates the form.
func (f *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, f.keymap.Quit):
f.quitting = true
return f, tea.Quit
}
case nextGroupMsg:
if len(f.groups[f.paginator.Page].Errors()) > 0 {
return f, nil
}
if f.paginator.OnLastPage() {
f.quitting = true
return f, tea.Quit
}
f.paginator.NextPage()
case prevGroupMsg:
if len(f.groups[f.paginator.Page].Errors()) > 0 {
return f, nil
}
f.paginator.PrevPage()
}
m, cmd := f.groups[f.paginator.Page].Update(msg)
f.groups[f.paginator.Page] = m.(*Group)
return f, cmd
}
// View renders the form.
func (f *Form) View() string {
if f.quitting {
return ""
}
return f.groups[f.paginator.Page].View()
}
// Run runs the form.
func (f *Form) Run() error {
if len(f.groups) == 0 {
return nil
}
// Make theme and keymap available to groups and fields
for _, group := range f.groups {
group.Theme(f.theme)
group.KeyMap(f.keymap)
for _, field := range group.fields {
field.Theme(f.theme)
field.KeyMap(f.keymap)
}
}
if f.accessible {
for _, group := range f.groups {
for _, field := range group.fields {
field.Init()
field.Focus()
field.Run()
}
}
return nil
}
p := tea.NewProgram(f)
_, err := p.Run()
if err != nil {
return err
}
return nil
}