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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
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 {
accessor Accessor[bool]
key string
id int
// customization
title Eval[string]
description Eval[string]
affirmative string
negative string
// error handling
validate func(bool) error
err error
// state
focused bool
// options
width int
height int
inline bool
accessible bool
theme *Theme
keymap ConfirmKeyMap
}
// NewConfirm returns a new confirm field.
func NewConfirm() *Confirm {
return &Confirm{
accessor: &EmbeddedAccessor[bool]{},
id: nextID(),
title: Eval[string]{cache: make(map[uint64]string)},
description: Eval[string]{cache: make(map[uint64]string)},
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
}
// Skip returns whether the confirm should be skipped or should be blocking.
func (*Confirm) Skip() bool {
return false
}
// Zoom returns whether the input should be zoomed.
func (*Confirm) Zoom() bool {
return false
}
// 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 {
return c.Accessor(NewPointerAccessor(value))
}
// Accessor sets the accessor of the confirm field.
func (c *Confirm) Accessor(accessor Accessor[bool]) *Confirm {
c.accessor = accessor
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.val = title
c.title.fn = nil
return c
}
// TitleFunc sets the title func of the confirm field.
func (c *Confirm) TitleFunc(f func() string, bindings any) *Confirm {
c.title.fn = f
c.title.bindings = bindings
return c
}
// Description sets the description of the confirm field.
func (c *Confirm) Description(description string) *Confirm {
c.description.val = description
c.description.fn = nil
return c
}
// DescriptionFunc sets the description function of the confirm field.
func (c *Confirm) DescriptionFunc(f func() string, bindings any) *Confirm {
c.description.fn = f
c.description.bindings = bindings
return c
}
// Inline sets whether the field should be inline.
func (c *Confirm) Inline(inline bool) *Confirm {
c.inline = inline
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.accessor.Get())
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.Prev, c.keymap.Submit, c.keymap.Next, c.keymap.Accept, c.keymap.Reject}
}
// 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 updateFieldMsg:
if ok, hash := c.title.shouldUpdate(); ok {
c.title.bindingsHash = hash
if !c.title.loadFromCache() {
c.title.loading = true
cmds = append(cmds, func() tea.Msg {
return updateTitleMsg{id: c.id, title: c.title.fn(), hash: hash}
})
}
}
if ok, hash := c.description.shouldUpdate(); ok {
c.description.bindingsHash = hash
if !c.description.loadFromCache() {
c.description.loading = true
cmds = append(cmds, func() tea.Msg {
return updateDescriptionMsg{id: c.id, description: c.description.fn(), hash: hash}
})
}
}
case updateTitleMsg:
if msg.id == c.id && msg.hash == c.title.bindingsHash {
c.title.val = msg.title
c.title.loading = false
}
case updateDescriptionMsg:
if msg.id == c.id && msg.hash == c.description.bindingsHash {
c.description.val = msg.description
c.description.loading = false
}
case tea.KeyMsg:
c.err = nil
switch {
case key.Matches(msg, c.keymap.Toggle):
if c.negative == "" {
break
}
c.accessor.Set(!c.accessor.Get())
case key.Matches(msg, c.keymap.Prev):
cmds = append(cmds, PrevField)
case key.Matches(msg, c.keymap.Next, c.keymap.Submit):
cmds = append(cmds, NextField)
case key.Matches(msg, c.keymap.Accept):
c.accessor.Set(true)
cmds = append(cmds, NextField)
case key.Matches(msg, c.keymap.Reject):
c.accessor.Set(false)
cmds = append(cmds, NextField)
}
}
return c, tea.Batch(cmds...)
}
func (c *Confirm) activeStyles() *FieldStyles {
theme := c.theme
if theme == nil {
theme = ThemeCharm()
}
if c.focused {
return &theme.Focused
}
return &theme.Blurred
}
// View renders the confirm field.
func (c *Confirm) View() string {
styles := c.activeStyles()
var sb strings.Builder
sb.WriteString(styles.Title.Render(c.title.val))
if c.err != nil {
sb.WriteString(styles.ErrorIndicator.String())
}
description := styles.Description.Render(c.description.val)
if !c.inline && (c.description.val != "" || c.description.fn != nil) {
sb.WriteString("\n")
}
sb.WriteString(description)
if !c.inline {
sb.WriteString("\n")
sb.WriteString("\n")
}
var negative string
var affirmative string
if c.negative != "" {
if c.accessor.Get() {
affirmative = styles.FocusedButton.Render(c.affirmative)
negative = styles.BlurredButton.Render(c.negative)
} else {
affirmative = styles.BlurredButton.Render(c.affirmative)
negative = styles.FocusedButton.Render(c.negative)
}
c.keymap.Reject.SetHelp("n", c.negative)
} else {
affirmative = styles.FocusedButton.Render(c.affirmative)
c.keymap.Reject.SetEnabled(false)
}
c.keymap.Accept.SetHelp("y", c.affirmative)
buttonsRow := lipgloss.JoinHorizontal(lipgloss.Center, affirmative, negative)
promptWidth := lipgloss.Width(sb.String())
buttonsWidth := lipgloss.Width(buttonsRow)
renderWidth := promptWidth
if buttonsWidth > renderWidth {
renderWidth = buttonsWidth
}
style := lipgloss.NewStyle().Width(renderWidth).Align(lipgloss.Center)
sb.WriteString(style.Render(buttonsRow))
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 {
styles := c.activeStyles()
fmt.Println(styles.Title.Render(c.title.val))
fmt.Println()
c.accessor.Set(accessibility.PromptBool())
fmt.Println(styles.SelectedOption.Render("Chose: "+c.String()) + "\n")
return nil
}
func (c *Confirm) String() string {
if c.accessor.Get() {
return c.affirmative
}
return c.negative
}
// WithTheme sets the theme of the confirm field.
func (c *Confirm) WithTheme(theme *Theme) Field {
if c.theme != nil {
return c
}
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 width of the confirm field.
func (c *Confirm) WithWidth(width int) Field {
c.width = width
return c
}
// WithHeight sets the height of the confirm field.
func (c *Confirm) WithHeight(height int) Field {
c.height = height
return c
}
// WithPosition sets the position of the confirm field.
func (c *Confirm) WithPosition(p FieldPosition) Field {
c.keymap.Prev.SetEnabled(!p.IsFirst())
c.keymap.Next.SetEnabled(!p.IsLast())
c.keymap.Submit.SetEnabled(p.IsLast())
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.accessor.Get()
}
|