forked from charmbracelet/huh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_input.go
261 lines (216 loc) · 5.51 KB
/
field_input.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
252
253
254
255
256
257
258
259
260
261
package huh
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh/accessibility"
)
// Input is a form input field.
type Input struct {
value *string
key string
// customization
title string
description string
inline bool
charlimit int
// error handling
validate func(string) error
err error
// model
textinput textinput.Model
// state
focused bool
// options
width int
accessible bool
theme *Theme
keymap *InputKeyMap
}
// NewInput returns a new input field.
func NewInput() *Input {
input := textinput.New()
i := &Input{
value: new(string),
textinput: input,
validate: func(string) error { return nil },
}
return i
}
// Value sets the value of the input field.
func (i *Input) Value(value *string) *Input {
i.value = value
i.textinput.SetValue(*value)
return i
}
// Key sets the key of the input field.
func (i *Input) Key(key string) *Input {
i.key = key
return i
}
// Title sets the title of the input field.
func (i *Input) Title(title string) *Input {
i.title = title
return i
}
// Description sets the description of the input field.
func (i *Input) Description(description string) *Input {
i.description = description
return i
}
// Prompt sets the prompt of the input field.
func (i *Input) Prompt(prompt string) *Input {
i.textinput.Prompt = prompt
return i
}
// CharLimit sets the character limit of the input field.
func (i *Input) CharLimit(charlimit int) *Input {
i.charlimit = charlimit
return i
}
// Password sets whether or not to hide the input while the user is typing.
func (i *Input) Password(password bool) *Input {
if password {
i.textinput.EchoMode = textinput.EchoPassword
} else {
i.textinput.EchoMode = textinput.EchoNormal
}
return i
}
// Placeholder sets the placeholder of the text input.
func (i *Input) Placeholder(str string) *Input {
i.textinput.Placeholder = str
return i
}
// Inline sets whether the title and input should be on the same line.
func (i *Input) Inline(inline bool) *Input {
i.inline = inline
return i
}
// Validate sets the validation function of the input field.
func (i *Input) Validate(validate func(string) error) *Input {
i.validate = validate
return i
}
// Error returns the error of the input field.
func (i *Input) Error() error {
return i.err
}
// Focus focuses the input field.
func (i *Input) Focus() tea.Cmd {
i.focused = true
return i.textinput.Focus()
}
// Blur blurs the input field.
func (i *Input) Blur() tea.Cmd {
i.focused = false
i.textinput.Blur()
return nil
}
// KeyBinds returns the help message for the input field.
func (i *Input) KeyBinds() []key.Binding {
return []key.Binding{i.keymap.Next, i.keymap.Prev}
}
// Init initializes the input field.
func (i *Input) Init() tea.Cmd {
i.textinput.Blur()
i.err = i.validate(*i.value)
return nil
}
// Update updates the input field.
func (i *Input) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var cmd tea.Cmd
i.textinput, cmd = i.textinput.Update(msg)
cmds = append(cmds, cmd)
*i.value = i.textinput.Value()
switch msg := msg.(type) {
case tea.KeyMsg:
i.err = nil
switch {
case key.Matches(msg, i.keymap.Prev):
cmds = append(cmds, prevField)
case key.Matches(msg, i.keymap.Next):
cmds = append(cmds, nextField)
}
}
return i, tea.Batch(cmds...)
}
// View renders the input field.
func (i *Input) View() string {
styles := i.theme.Blurred
if i.focused {
styles = i.theme.Focused
}
// NB: since the method is on a pointer receiver these are being mutated.
// Because this runs on every render this shouldn't matter in practice,
// however.
i.textinput.PlaceholderStyle = styles.TextInput.Placeholder
i.textinput.PromptStyle = styles.TextInput.Prompt
i.textinput.Cursor.Style = styles.TextInput.Cursor
i.textinput.TextStyle = styles.TextInput.Text
var sb strings.Builder
if i.title != "" {
sb.WriteString(styles.Title.Render(i.title))
if !i.inline {
sb.WriteString("\n")
}
}
if i.description != "" {
sb.WriteString(styles.Description.Render(i.description))
if !i.inline {
sb.WriteString("\n")
}
}
sb.WriteString(i.textinput.View())
return styles.Base.Render(sb.String())
}
// Run runs the input field in accessible mode.
func (i *Input) Run() error {
if i.accessible {
return i.runAccessible()
}
return i.run()
}
// run runs the input field.
func (i *Input) run() error {
return Run(i)
}
// runAccessible runs the input field in accessible mode.
func (i *Input) runAccessible() error {
fmt.Println(i.theme.Blurred.Base.Render(i.theme.Focused.Title.Render(i.title)))
fmt.Println()
*i.value = accessibility.PromptString("Input: ", i.validate)
fmt.Println(i.theme.Focused.SelectedOption.Render("Input: " + *i.value + "\n"))
return nil
}
// WithKeyMap sets the keymap on an input field.
func (i *Input) WithKeyMap(k *KeyMap) Field {
i.keymap = &k.Input
return i
}
// WithAccessible sets the accessible mode of the input field.
func (i *Input) WithAccessible(accessible bool) Field {
i.accessible = accessible
return i
}
// WithTheme sets the theme of the input field.
func (i *Input) WithTheme(theme *Theme) Field {
i.theme = theme
return i
}
// WithWidth sets the width of the input field.
func (i *Input) WithWidth(width int) Field {
i.width = width
return i
}
// GetKey returns the key of the field.
func (i *Input) GetKey() string {
return i.key
}
// GetValue returns the value of the field.
func (i *Input) GetValue() any {
return *i.value
}