Skip to content

Commit

Permalink
feat: return error if user aborted form (charmbracelet#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani authored Nov 1, 2023
1 parent 322a16a commit f691c7f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions examples/theme/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func main() {
).Run()

if err != nil {
if err == huh.ErrUserAborted {
os.Exit(130)
}
fmt.Println(err)
os.Exit(1)
}
Expand All @@ -49,6 +52,9 @@ func main() {
),
).WithTheme(theme).Run()
if err != nil {
if err == huh.ErrUserAborted {
os.Exit(130)
}
fmt.Println(err)
os.Exit(1)
}
Expand Down
13 changes: 12 additions & 1 deletion form.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package huh

import (
"errors"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/paginator"
tea "github.com/charmbracelet/bubbletea"
)

var (
ErrUserAborted = errors.New("user aborted")
)

// Form is a collection of groups that are displayed one at a time on a "page".
//
// The form can navigate between groups and is complete once all the groups are
Expand All @@ -23,6 +29,7 @@ type Form struct {
accessible bool

quitting bool
aborted bool

// options
width int
Expand Down Expand Up @@ -192,6 +199,7 @@ func (f *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch {
case key.Matches(msg, f.keymap.Quit):
f.aborted = true
f.quitting = true
return f, tea.Quit
}
Expand Down Expand Up @@ -244,7 +252,10 @@ func (f *Form) Run() error {

// run runs the form in normal mode.
func (f *Form) run() error {
_, err := tea.NewProgram(f).Run()
m, err := tea.NewProgram(f).Run()
if m.(*Form).aborted {
err = ErrUserAborted
}
return err
}

Expand Down

0 comments on commit f691c7f

Please sign in to comment.