| 1 |
package huh |
| 2 |
|
| 3 |
import ( |
| 4 |
"cmp" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
"sync" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/charmbracelet/bubbles/help" |
| 14 |
"github.com/charmbracelet/bubbles/key" |
| 15 |
tea "github.com/charmbracelet/bubbletea" |
| 16 |
"github.com/charmbracelet/huh/internal/selector" |
| 17 |
) |
| 18 |
|
| 19 |
const defaultWidth = 80 |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
var ( |
| 24 |
lastID int |
| 25 |
idMtx sync.Mutex |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
func nextID() int { |
| 30 |
idMtx.Lock() |
| 31 |
defer idMtx.Unlock() |
| 32 |
lastID++ |
| 33 |
return lastID |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
type FormState int |
| 38 |
|
| 39 |
const ( |
| 40 |
|
| 41 |
StateNormal FormState = iota |
| 42 |
|
| 43 |
|
| 44 |
StateCompleted |
| 45 |
|
| 46 |
|
| 47 |
StateAborted |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
var ErrUserAborted = errors.New("user aborted") |
| 52 |
|
| 53 |
|
| 54 |
var ErrTimeout = errors.New("timeout") |
| 55 |
|
| 56 |
|
| 57 |
var ErrTimeoutUnsupported = errors.New("timeout is not supported in accessible mode") |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
type Form struct { |
| 64 |
|
| 65 |
selector *selector.Selector[*Group] |
| 66 |
|
| 67 |
results map[string]any |
| 68 |
|
| 69 |
|
| 70 |
SubmitCmd tea.Cmd |
| 71 |
CancelCmd tea.Cmd |
| 72 |
|
| 73 |
State FormState |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
accessible bool |
| 79 |
|
| 80 |
quitting bool |
| 81 |
aborted bool |
| 82 |
|
| 83 |
|
| 84 |
width int |
| 85 |
height int |
| 86 |
theme *Theme |
| 87 |
keymap *KeyMap |
| 88 |
timeout time.Duration |
| 89 |
teaOptions []tea.ProgramOption |
| 90 |
|
| 91 |
layout Layout |
| 92 |
|
| 93 |
|
| 94 |
output io.Writer |
| 95 |
input io.Reader |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
func NewForm(groups ...*Group) *Form { |
| 104 |
selector := selector.NewSelector(groups) |
| 105 |
|
| 106 |
f := &Form{ |
| 107 |
selector: selector, |
| 108 |
keymap: NewDefaultKeyMap(), |
| 109 |
results: make(map[string]any), |
| 110 |
layout: LayoutDefault, |
| 111 |
teaOptions: []tea.ProgramOption{ |
| 112 |
tea.WithOutput(os.Stderr), |
| 113 |
tea.WithReportFocus(), |
| 114 |
}, |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
f.WithKeyMap(f.keymap) |
| 120 |
f.WithWidth(f.width) |
| 121 |
f.WithHeight(f.height) |
| 122 |
f.UpdateFieldPositions() |
| 123 |
|
| 124 |
if os.Getenv("TERM") == "dumb" { |
| 125 |
f.WithWidth(defaultWidth) |
| 126 |
f.WithAccessible(true) |
| 127 |
} |
| 128 |
|
| 129 |
return f |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
type Field interface { |
| 139 |
|
| 140 |
Init() tea.Cmd |
| 141 |
Update(tea.Msg) (tea.Model, tea.Cmd) |
| 142 |
View() string |
| 143 |
|
| 144 |
|
| 145 |
Blur() tea.Cmd |
| 146 |
Focus() tea.Cmd |
| 147 |
|
| 148 |
|
| 149 |
Error() error |
| 150 |
|
| 151 |
|
| 152 |
Run() error |
| 153 |
|
| 154 |
|
| 155 |
RunAccessible(w io.Writer, r io.Reader) error |
| 156 |
|
| 157 |
|
| 158 |
Skip() bool |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
Zoom() bool |
| 163 |
|
| 164 |
|
| 165 |
KeyBinds() []key.Binding |
| 166 |
|
| 167 |
|
| 168 |
WithTheme(*Theme) Field |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
WithAccessible(bool) Field |
| 174 |
|
| 175 |
|
| 176 |
WithKeyMap(*KeyMap) Field |
| 177 |
|
| 178 |
|
| 179 |
WithWidth(int) Field |
| 180 |
|
| 181 |
|
| 182 |
WithHeight(int) Field |
| 183 |
|
| 184 |
|
| 185 |
WithPosition(FieldPosition) Field |
| 186 |
|
| 187 |
|
| 188 |
GetKey() string |
| 189 |
|
| 190 |
|
| 191 |
GetValue() any |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
type FieldPosition struct { |
| 196 |
Group int |
| 197 |
Field int |
| 198 |
FirstField int |
| 199 |
LastField int |
| 200 |
GroupCount int |
| 201 |
FirstGroup int |
| 202 |
LastGroup int |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
func (p FieldPosition) IsFirst() bool { |
| 207 |
return p.Field == p.FirstField && p.Group == p.FirstGroup |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
func (p FieldPosition) IsLast() bool { |
| 212 |
return p.Field == p.LastField && p.Group == p.LastGroup |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
type nextGroupMsg struct{} |
| 217 |
|
| 218 |
|
| 219 |
type prevGroupMsg struct{} |
| 220 |
|
| 221 |
|
| 222 |
func nextGroup() tea.Msg { |
| 223 |
return nextGroupMsg{} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
func prevGroup() tea.Msg { |
| 228 |
return prevGroupMsg{} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
func (f *Form) WithAccessible(accessible bool) *Form { |
| 238 |
f.accessible = accessible |
| 239 |
return f |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
func (f *Form) WithShowHelp(v bool) *Form { |
| 247 |
f.selector.Range(func(_ int, group *Group) bool { |
| 248 |
group.WithShowHelp(v) |
| 249 |
return true |
| 250 |
}) |
| 251 |
return f |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
func (f *Form) WithShowErrors(v bool) *Form { |
| 259 |
f.selector.Range(func(_ int, group *Group) bool { |
| 260 |
group.WithShowErrors(v) |
| 261 |
return true |
| 262 |
}) |
| 263 |
return f |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
func (f *Form) WithTheme(theme *Theme) *Form { |
| 272 |
if theme == nil { |
| 273 |
return f |
| 274 |
} |
| 275 |
f.theme = theme |
| 276 |
f.selector.Range(func(_ int, group *Group) bool { |
| 277 |
group.WithTheme(theme) |
| 278 |
return true |
| 279 |
}) |
| 280 |
return f |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
func (f *Form) WithKeyMap(keymap *KeyMap) *Form { |
| 287 |
if keymap == nil { |
| 288 |
return f |
| 289 |
} |
| 290 |
f.keymap = keymap |
| 291 |
f.selector.Range(func(_ int, group *Group) bool { |
| 292 |
group.WithKeyMap(keymap) |
| 293 |
return true |
| 294 |
}) |
| 295 |
f.UpdateFieldPositions() |
| 296 |
return f |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
func (f *Form) WithWidth(width int) *Form { |
| 305 |
if width <= 0 { |
| 306 |
return f |
| 307 |
} |
| 308 |
f.width = width |
| 309 |
f.selector.Range(func(_ int, group *Group) bool { |
| 310 |
width := f.layout.GroupWidth(f, group, width) |
| 311 |
group.WithWidth(width) |
| 312 |
return true |
| 313 |
}) |
| 314 |
return f |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
func (f *Form) WithHeight(height int) *Form { |
| 319 |
if height <= 0 { |
| 320 |
return f |
| 321 |
} |
| 322 |
f.height = height |
| 323 |
f.selector.Range(func(_ int, group *Group) bool { |
| 324 |
group.WithHeight(height) |
| 325 |
return true |
| 326 |
}) |
| 327 |
return f |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
func (f *Form) WithOutput(w io.Writer) *Form { |
| 333 |
f.output = w |
| 334 |
f.teaOptions = append(f.teaOptions, tea.WithOutput(w)) |
| 335 |
return f |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
func (f *Form) WithInput(r io.Reader) *Form { |
| 341 |
f.input = r |
| 342 |
f.teaOptions = append(f.teaOptions, tea.WithInput(r)) |
| 343 |
return f |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
func (f *Form) WithTimeout(t time.Duration) *Form { |
| 348 |
f.timeout = t |
| 349 |
return f |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
func (f *Form) WithProgramOptions(opts ...tea.ProgramOption) *Form { |
| 354 |
f.teaOptions = opts |
| 355 |
return f |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
func (f *Form) WithLayout(layout Layout) *Form { |
| 362 |
f.layout = layout |
| 363 |
return f |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
func (f *Form) UpdateFieldPositions() *Form { |
| 368 |
firstGroup := 0 |
| 369 |
lastGroup := f.selector.Total() - 1 |
| 370 |
|
| 371 |
|
| 372 |
f.selector.Range(func(_ int, g *Group) bool { |
| 373 |
if !f.isGroupHidden(g) { |
| 374 |
return false |
| 375 |
} |
| 376 |
firstGroup++ |
| 377 |
return true |
| 378 |
}) |
| 379 |
|
| 380 |
|
| 381 |
f.selector.ReverseRange(func(_ int, g *Group) bool { |
| 382 |
if !f.isGroupHidden(g) { |
| 383 |
return false |
| 384 |
} |
| 385 |
lastGroup-- |
| 386 |
return true |
| 387 |
}) |
| 388 |
|
| 389 |
f.selector.Range(func(g int, group *Group) bool { |
| 390 |
|
| 391 |
var firstField int |
| 392 |
group.selector.Range(func(_ int, field Field) bool { |
| 393 |
if !field.Skip() || group.selector.Total() == 1 { |
| 394 |
return false |
| 395 |
} |
| 396 |
firstField++ |
| 397 |
return true |
| 398 |
}) |
| 399 |
|
| 400 |
|
| 401 |
var lastField int |
| 402 |
group.selector.ReverseRange(func(i int, field Field) bool { |
| 403 |
lastField = i |
| 404 |
if !field.Skip() || group.selector.Total() == 1 { |
| 405 |
return false |
| 406 |
} |
| 407 |
return true |
| 408 |
}) |
| 409 |
|
| 410 |
group.selector.Range(func(i int, field Field) bool { |
| 411 |
field.WithPosition(FieldPosition{ |
| 412 |
Group: g, |
| 413 |
Field: i, |
| 414 |
FirstField: firstField, |
| 415 |
LastField: lastField, |
| 416 |
FirstGroup: firstGroup, |
| 417 |
LastGroup: lastGroup, |
| 418 |
}) |
| 419 |
return true |
| 420 |
}) |
| 421 |
|
| 422 |
return true |
| 423 |
}) |
| 424 |
return f |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
func (f *Form) Errors() []error { |
| 429 |
return f.selector.Selected().Errors() |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
func (f *Form) Help() help.Model { |
| 434 |
return f.selector.Selected().help |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
func (f *Form) KeyBinds() []key.Binding { |
| 439 |
group := f.selector.Selected() |
| 440 |
return group.selector.Selected().KeyBinds() |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
func (f *Form) Get(key string) any { |
| 445 |
return f.results[key] |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
func (f *Form) GetString(key string) string { |
| 450 |
v, ok := f.results[key].(string) |
| 451 |
if !ok { |
| 452 |
return "" |
| 453 |
} |
| 454 |
return v |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
func (f *Form) GetInt(key string) int { |
| 459 |
v, ok := f.results[key].(int) |
| 460 |
if !ok { |
| 461 |
return 0 |
| 462 |
} |
| 463 |
return v |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
func (f *Form) GetBool(key string) bool { |
| 468 |
v, ok := f.results[key].(bool) |
| 469 |
if !ok { |
| 470 |
return false |
| 471 |
} |
| 472 |
return v |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
func (f *Form) NextGroup() tea.Cmd { |
| 477 |
_, cmd := f.Update(nextGroup()) |
| 478 |
return cmd |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
func (f *Form) PrevGroup() tea.Cmd { |
| 483 |
_, cmd := f.Update(prevGroup()) |
| 484 |
return cmd |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
func (f *Form) NextField() tea.Cmd { |
| 489 |
_, cmd := f.Update(NextField()) |
| 490 |
return cmd |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
func (f *Form) PrevField() tea.Cmd { |
| 495 |
_, cmd := f.Update(PrevField()) |
| 496 |
return cmd |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
func (f *Form) GetFocusedField() Field { |
| 501 |
return f.selector.Selected().selector.Selected() |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
func (f *Form) Init() tea.Cmd { |
| 506 |
var cmds []tea.Cmd |
| 507 |
f.selector.Range(func(i int, group *Group) bool { |
| 508 |
if i == 0 { |
| 509 |
group.active = true |
| 510 |
} |
| 511 |
cmds = append(cmds, group.Init()) |
| 512 |
return true |
| 513 |
}) |
| 514 |
|
| 515 |
if f.isGroupHidden(f.selector.Selected()) { |
| 516 |
cmds = append(cmds, nextGroup) |
| 517 |
} |
| 518 |
|
| 519 |
cmds = append(cmds, tea.WindowSize()) |
| 520 |
return tea.Sequence(cmds...) |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
func (f *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 525 |
|
| 526 |
if f.State != StateNormal { |
| 527 |
return f, nil |
| 528 |
} |
| 529 |
|
| 530 |
group := f.selector.Selected() |
| 531 |
|
| 532 |
switch msg := msg.(type) { |
| 533 |
case tea.WindowSizeMsg: |
| 534 |
if f.width == 0 { |
| 535 |
f.selector.Range(func(_ int, group *Group) bool { |
| 536 |
width := f.layout.GroupWidth(f, group, msg.Width) |
| 537 |
group.WithWidth(width) |
| 538 |
return true |
| 539 |
}) |
| 540 |
} |
| 541 |
if f.height == 0 { |
| 542 |
|
| 543 |
|
| 544 |
neededHeight := 0 |
| 545 |
f.selector.Range(func(_ int, group *Group) bool { |
| 546 |
neededHeight = max(neededHeight, group.rawHeight()) |
| 547 |
return true |
| 548 |
}) |
| 549 |
|
| 550 |
f.selector.Range(func(_ int, group *Group) bool { |
| 551 |
group.WithHeight(min(neededHeight, msg.Height)) |
| 552 |
return true |
| 553 |
}) |
| 554 |
} |
| 555 |
|
| 556 |
case tea.KeyMsg: |
| 557 |
switch { |
| 558 |
case key.Matches(msg, f.keymap.Quit): |
| 559 |
f.aborted = true |
| 560 |
f.quitting = true |
| 561 |
f.State = StateAborted |
| 562 |
return f, f.CancelCmd |
| 563 |
} |
| 564 |
|
| 565 |
case nextFieldMsg: |
| 566 |
|
| 567 |
field := group.selector.Selected() |
| 568 |
f.results[field.GetKey()] = field.GetValue() |
| 569 |
|
| 570 |
case nextGroupMsg: |
| 571 |
if len(group.Errors()) > 0 { |
| 572 |
return f, nil |
| 573 |
} |
| 574 |
|
| 575 |
submit := func() (tea.Model, tea.Cmd) { |
| 576 |
f.quitting = true |
| 577 |
f.State = StateCompleted |
| 578 |
return f, f.SubmitCmd |
| 579 |
} |
| 580 |
|
| 581 |
if f.selector.OnLast() { |
| 582 |
return submit() |
| 583 |
} |
| 584 |
|
| 585 |
for i := f.selector.Index() + 1; i < f.selector.Total(); i++ { |
| 586 |
if !f.isGroupHidden(f.selector.Get(i)) { |
| 587 |
f.selector.SetIndex(i) |
| 588 |
break |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
if i == f.selector.Total()-1 { |
| 593 |
return submit() |
| 594 |
} |
| 595 |
} |
| 596 |
f.selector.Selected().active = true |
| 597 |
return f, f.selector.Selected().Init() |
| 598 |
|
| 599 |
case prevGroupMsg: |
| 600 |
if len(group.Errors()) > 0 { |
| 601 |
return f, nil |
| 602 |
} |
| 603 |
|
| 604 |
for i := f.selector.Index() - 1; i >= 0; i-- { |
| 605 |
if !f.isGroupHidden(f.selector.Get(i)) { |
| 606 |
f.selector.SetIndex(i) |
| 607 |
break |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
f.selector.Selected().active = true |
| 612 |
return f, f.selector.Selected().Init() |
| 613 |
} |
| 614 |
|
| 615 |
m, cmd := group.Update(msg) |
| 616 |
f.selector.Set(f.selector.Index(), m.(*Group)) |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
switch msg.(type) { |
| 621 |
case tea.KeyMsg: |
| 622 |
f.UpdateFieldPositions() |
| 623 |
} |
| 624 |
|
| 625 |
return f, cmd |
| 626 |
} |
| 627 |
|
| 628 |
func (f *Form) isGroupHidden(group *Group) bool { |
| 629 |
hide := group.hide |
| 630 |
if hide == nil { |
| 631 |
return false |
| 632 |
} |
| 633 |
return hide() |
| 634 |
} |
| 635 |
|
| 636 |
func (f *Form) getTheme() *Theme { |
| 637 |
if f.theme != nil { |
| 638 |
return f.theme |
| 639 |
} |
| 640 |
return ThemeCharm() |
| 641 |
} |
| 642 |
|
| 643 |
func (f *Form) styles() FormStyles { |
| 644 |
return f.getTheme().Form |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
func (f *Form) View() string { |
| 649 |
if f.quitting { |
| 650 |
return "" |
| 651 |
} |
| 652 |
|
| 653 |
return f.styles().Base.Render(f.layout.View(f)) |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
func (f *Form) Run() error { |
| 658 |
return f.RunWithContext(context.Background()) |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
func (f *Form) RunWithContext(ctx context.Context) error { |
| 663 |
f.SubmitCmd = tea.Quit |
| 664 |
f.CancelCmd = tea.Interrupt |
| 665 |
|
| 666 |
if f.selector.Total() == 0 { |
| 667 |
return nil |
| 668 |
} |
| 669 |
|
| 670 |
if f.accessible { |
| 671 |
return f.runAccessible( |
| 672 |
cmp.Or[io.Writer](f.output, os.Stdout), |
| 673 |
cmp.Or[io.Reader](f.input, os.Stdin), |
| 674 |
) |
| 675 |
} |
| 676 |
|
| 677 |
return f.run(ctx) |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
func (f *Form) run(ctx context.Context) error { |
| 682 |
var cancel context.CancelFunc |
| 683 |
if f.timeout > 0 { |
| 684 |
ctx, cancel = context.WithTimeout(ctx, f.timeout) |
| 685 |
defer cancel() |
| 686 |
} |
| 687 |
|
| 688 |
f.teaOptions = append(f.teaOptions, tea.WithContext(ctx)) |
| 689 |
m, err := tea.NewProgram(f, f.teaOptions...).Run() |
| 690 |
if m.(*Form).aborted || errors.Is(err, tea.ErrInterrupted) { |
| 691 |
return ErrUserAborted |
| 692 |
} |
| 693 |
if errors.Is(err, tea.ErrProgramKilled) { |
| 694 |
return ErrTimeout |
| 695 |
} |
| 696 |
if err != nil { |
| 697 |
return fmt.Errorf("huh: %w", err) |
| 698 |
} |
| 699 |
return nil |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
func (f *Form) runAccessible(w io.Writer, r io.Reader) error { |
| 704 |
|
| 705 |
if f.timeout > 0 { |
| 706 |
return ErrTimeoutUnsupported |
| 707 |
} |
| 708 |
|
| 709 |
f.selector.Range(func(_ int, group *Group) bool { |
| 710 |
group.selector.Range(func(_ int, field Field) bool { |
| 711 |
field.Init() |
| 712 |
field.Focus() |
| 713 |
_ = field.WithAccessible(true).RunAccessible(w, r) |
| 714 |
_, _ = fmt.Fprintln(w) |
| 715 |
return true |
| 716 |
}) |
| 717 |
return true |
| 718 |
}) |
| 719 |
|
| 720 |
return nil |
| 721 |
} |