Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(confirm,choose,file,input): timeout handling #718

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/term"

"github.com/charmbracelet/gum/internal/exit"
"github.com/charmbracelet/gum/internal/stdin"
)

Expand Down Expand Up @@ -76,8 +77,12 @@ func (o Options) Run() error {
WithWidth(width).
WithShowHelp(o.ShowHelp).
WithTheme(theme).
WithTimeout(o.Timeout).
Run()
if err != nil && !errors.Is(err, huh.ErrTimeout) {
if err != nil {
if errors.Is(err, huh.ErrTimeout) {
return exit.NewTimeout(o.Timeout)
}
return err
}
if len(choices) > 0 {
Expand All @@ -100,9 +105,13 @@ func (o Options) Run() error {
).
WithWidth(width).
WithTheme(theme).
WithTimeout(o.Timeout).
WithShowHelp(o.ShowHelp).
Run()
if err != nil && !errors.Is(err, huh.ErrTimeout) {
if err != nil {
if errors.Is(err, huh.ErrTimeout) {
return exit.NewTimeout(o.Timeout)
}
return err
}

Expand Down
16 changes: 4 additions & 12 deletions confirm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package confirm

import (
"errors"
"fmt"
"os"

"github.com/charmbracelet/gum/internal/exit"
"github.com/charmbracelet/huh"
)

Expand All @@ -31,11 +31,11 @@ func (o Options) Run() error {
WithTheme(theme).
WithShowHelp(o.ShowHelp).
Run()

if err != nil {
if !o.errIsValidTimeout(err) {
return fmt.Errorf("unable to run confirm: %w", err)
if errors.Is(err, huh.ErrTimeout) {
return exit.NewTimeout(o.Timeout)
}
return err
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
}

if !choice {
Expand All @@ -44,11 +44,3 @@ func (o Options) Run() error {

return nil
}

// errIsValidTimeout returns false unless 1) the user has specified a nonzero timeout and 2) the error is a huh.ErrTimeout.
func (o Options) errIsValidTimeout(err error) bool {
errWasTimeout := errors.Is(err, huh.ErrTimeout)
timeoutsExpected := o.Timeout > 0

return errWasTimeout && timeoutsExpected
}
54 changes: 0 additions & 54 deletions confirm/command_test.go

This file was deleted.

6 changes: 5 additions & 1 deletion file/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"path/filepath"

"github.com/charmbracelet/gum/internal/exit"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
)
Expand Down Expand Up @@ -49,12 +50,15 @@ func (o Options) Run() error {
Value(&path),
),
).
WithTimeout(o.Timeout).
WithShowHelp(o.ShowHelp).
WithKeyMap(keymap).
WithTheme(theme).
Run()

if err != nil {
if errors.Is(err, huh.ErrTimeout) {
return exit.NewTimeout(o.Timeout)
}
return err
}

Expand Down
6 changes: 6 additions & 0 deletions input/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package input

import (
"errors"
"fmt"
"os"

Expand All @@ -9,6 +10,7 @@ import (
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"

"github.com/charmbracelet/gum/internal/exit"
"github.com/charmbracelet/gum/internal/stdin"
)

Expand Down Expand Up @@ -53,10 +55,14 @@ func (o Options) Run() error {
WithWidth(o.Width).
WithTheme(theme).
WithKeyMap(keymap).
WithTimeout(o.Timeout).
WithShowHelp(o.ShowHelp).
WithProgramOptions(tea.WithOutput(os.Stderr)).
Run()
if err != nil {
if errors.Is(err, huh.ErrTimeout) {
return exit.NewTimeout(o.Timeout)
}
return err
}

Expand Down
30 changes: 28 additions & 2 deletions internal/exit/exit.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
package exit

import "fmt"
import (
"fmt"
"time"

"github.com/charmbracelet/huh"
)

// StatusTimeout is the exit code for timed out commands.
const StatusTimeout = 124

// StatusAborted is the exit code for aborted commands.
const StatusAborted = 130

// ErrAborted is the error to return when a gum command is aborted by Ctrl + C.
// ErrAborted is the error to return when a gum command is aborted by Ctrl+C.
var ErrAborted = fmt.Errorf("aborted")

// NewTimeout returns a new ErrTimeout.
func NewTimeout(d time.Duration) ErrTimeout {
return ErrTimeout{d: d}
}

// ErrTimeout is a time out error.
type ErrTimeout struct {
d time.Duration
}

func (e ErrTimeout) Error() string {
return "timed out after " + e.d.String()
}

func (e ErrTimeout) Unwrap() error {
return huh.ErrTimeout
}
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func main() {
},
)
if err := ctx.Run(); err != nil {
if errors.Is(err, huh.ErrTimeout) {
fmt.Println(err)
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(exit.StatusTimeout)
}
if errors.Is(err, exit.ErrAborted) || errors.Is(err, huh.ErrUserAborted) {
os.Exit(exit.StatusAborted)
}
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading