Skip to content

Commit

Permalink
fix: no timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Jun 8, 2024
1 parent 763c10b commit 8ebd7d6
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const (
// ErrUserAborted is the error returned when a user exits the form before submitting.
var ErrUserAborted = errors.New("user aborted")

// ErrTimeout is the error returned when the timeout is reached.
var ErrTimeout = errors.New("timeout")

// 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 Down Expand Up @@ -599,12 +602,19 @@ func (f *Form) Run() error {

// run runs the form in normal mode.
func (f *Form) run() error {
ctx, cancel := context.WithTimeout(context.Background(), f.timeout)
m, err := tea.NewProgram(f, append(f.teaOptions, tea.WithContext(ctx))...).Run()
startTime := time.Now()
if f.timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), f.timeout)
defer cancel()
f.teaOptions = append(f.teaOptions, tea.WithContext(ctx))
}
m, err := tea.NewProgram(f, f.teaOptions...).Run()
if m.(*Form).aborted {
err = ErrUserAborted
}
cancel()
if err != nil && time.Since(startTime) >= f.timeout {
err = ErrTimeout
}
return err
}

Expand Down

0 comments on commit 8ebd7d6

Please sign in to comment.