forked from charmbracelet/huh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_confirm.go
232 lines (195 loc) Β· 4.9 KB
/
field_confirm.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
package huh
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh/accessibility"
"github.com/charmbracelet/lipgloss"
)
// Confirm is a form confirm field.
type Confirm struct {
value *bool
key string
// customization
title string
description string
affirmative string
negative string
// error handling
validate func(bool) error
err error
// state
focused bool
// options
width int
accessible bool
theme *Theme
keymap *ConfirmKeyMap
}
// NewConfirm returns a new confirm field.
func NewConfirm() *Confirm {
return &Confirm{
value: new(bool),
affirmative: "Yes",
negative: "No",
validate: func(bool) error { return nil },
}
}
// Validate sets the validation function of the confirm field.
func (c *Confirm) Validate(validate func(bool) error) *Confirm {
c.validate = validate
return c
}
// Error returns the error of the confirm field.
func (c *Confirm) Error() error {
return c.err
}
// Affirmative sets the affirmative value of the confirm field.
func (c *Confirm) Affirmative(affirmative string) *Confirm {
c.affirmative = affirmative
return c
}
// Negative sets the negative value of the confirm field.
func (c *Confirm) Negative(negative string) *Confirm {
c.negative = negative
return c
}
// Value sets the value of the confirm field.
func (c *Confirm) Value(value *bool) *Confirm {
c.value = value
return c
}
// Key sets the key of the confirm field.
func (c *Confirm) Key(key string) *Confirm {
c.key = key
return c
}
// Title sets the title of the confirm field.
func (c *Confirm) Title(title string) *Confirm {
c.title = title
return c
}
// Description sets the description of the confirm field.
func (c *Confirm) Description(description string) *Confirm {
c.description = description
return c
}
// Focus focuses the confirm field.
func (c *Confirm) Focus() tea.Cmd {
c.focused = true
return nil
}
// Blur blurs the confirm field.
func (c *Confirm) Blur() tea.Cmd {
c.focused = false
c.err = c.validate(*c.value)
return nil
}
// KeyBinds returns the help message for the confirm field.
func (c *Confirm) KeyBinds() []key.Binding {
return []key.Binding{c.keymap.Toggle, c.keymap.Next, c.keymap.Prev}
}
// Init initializes the confirm field.
func (c *Confirm) Init() tea.Cmd {
return nil
}
// Update updates the confirm field.
func (c *Confirm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
c.err = nil
switch {
case key.Matches(msg, c.keymap.Toggle):
v := !*c.value
*c.value = v
case key.Matches(msg, c.keymap.Prev):
cmds = append(cmds, prevField)
case key.Matches(msg, c.keymap.Next):
cmds = append(cmds, nextField)
}
}
return c, tea.Batch(cmds...)
}
// View renders the confirm field.
func (c *Confirm) View() string {
styles := c.theme.Blurred
if c.focused {
styles = c.theme.Focused
}
var sb strings.Builder
sb.WriteString(styles.Title.Render(c.title))
if c.err != nil {
sb.WriteString(styles.ErrorIndicator.String())
}
if c.description != "" {
sb.WriteString("\n")
sb.WriteString(styles.Description.Render(c.description))
}
sb.WriteString("\n")
sb.WriteString("\n")
if *c.value {
sb.WriteString(lipgloss.JoinHorizontal(
lipgloss.Center,
styles.FocusedButton.Render(c.affirmative),
styles.BlurredButton.Render(c.negative),
))
} else {
sb.WriteString(lipgloss.JoinHorizontal(
lipgloss.Center,
styles.BlurredButton.Render(c.affirmative),
styles.FocusedButton.Render(c.negative),
))
}
return styles.Base.Render(sb.String())
}
// Run runs the confirm field in accessible mode.
func (c *Confirm) Run() error {
if c.accessible {
return c.runAccessible()
}
return Run(c)
}
// runAccessible runs the confirm field in accessible mode.
func (c *Confirm) runAccessible() error {
fmt.Println(c.theme.Blurred.Base.Render(c.theme.Focused.Title.Render(c.title)))
fmt.Println()
*c.value = accessibility.PromptBool()
fmt.Println(c.theme.Focused.SelectedOption.Render("Chose: "+c.String()) + "\n")
return nil
}
func (c *Confirm) String() string {
if *c.value {
return c.affirmative
}
return c.negative
}
// WithTheme sets the theme of the confirm field.
func (c *Confirm) WithTheme(theme *Theme) Field {
c.theme = theme
return c
}
// WithKeyMap sets the keymap of the confirm field.
func (c *Confirm) WithKeyMap(k *KeyMap) Field {
c.keymap = &k.Confirm
return c
}
// WithAccessible sets the accessible mode of the confirm field.
func (c *Confirm) WithAccessible(accessible bool) Field {
c.accessible = accessible
return c
}
// WithWidth sets the accessible mode of the confirm field.
func (c *Confirm) WithWidth(width int) Field {
c.width = width
return c
}
// GetKey returns the key of the field.
func (c *Confirm) GetKey() string {
return c.key
}
// GetValue returns the value of the field.
func (c *Confirm) GetValue() any {
return *c.value
}