From e6b624ce77b41ad0f2d588ad51cd2aa338c9ce3a Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 18 Nov 2024 09:22:25 -0300 Subject: [PATCH] fix: improve Signed-off-by: Carlos Alexandro Becker --- choose/command.go | 10 ++-------- confirm/command.go | 6 +----- file/command.go | 6 +----- input/command.go | 6 +----- internal/exit/exit.go | 12 ++++++++---- main.go | 2 +- 6 files changed, 14 insertions(+), 28 deletions(-) diff --git a/choose/command.go b/choose/command.go index 9d3d4508c..d4b1c4219 100644 --- a/choose/command.go +++ b/choose/command.go @@ -80,10 +80,7 @@ func (o Options) Run() error { WithTimeout(o.Timeout). Run() if err != nil { - if errors.Is(err, huh.ErrTimeout) { - return exit.NewTimeout(o.Timeout) - } - return err + return exit.Handle(err, o.Timeout) } if len(choices) > 0 { s := strings.Join(choices, "\n") @@ -109,10 +106,7 @@ func (o Options) Run() error { WithShowHelp(o.ShowHelp). Run() if err != nil { - if errors.Is(err, huh.ErrTimeout) { - return exit.NewTimeout(o.Timeout) - } - return err + return exit.Handle(err, o.Timeout) } if term.IsTerminal(os.Stdout.Fd()) { diff --git a/confirm/command.go b/confirm/command.go index 18bcaac61..3653502d3 100644 --- a/confirm/command.go +++ b/confirm/command.go @@ -1,7 +1,6 @@ package confirm import ( - "errors" "os" "github.com/charmbracelet/gum/internal/exit" @@ -32,10 +31,7 @@ func (o Options) Run() error { WithShowHelp(o.ShowHelp). Run() if err != nil { - if errors.Is(err, huh.ErrTimeout) { - return exit.NewTimeout(o.Timeout) - } - return err + return exit.Handle(err, o.Timeout) } if !choice { diff --git a/file/command.go b/file/command.go index ed98547f9..c5bf71b1d 100644 --- a/file/command.go +++ b/file/command.go @@ -56,12 +56,8 @@ func (o Options) Run() error { WithTheme(theme). Run() if err != nil { - if errors.Is(err, huh.ErrTimeout) { - return exit.NewTimeout(o.Timeout) - } - return err + return exit.Handle(err, o.Timeout) } - fmt.Println(path) return nil } diff --git a/input/command.go b/input/command.go index 81b9a80d8..d35515eac 100644 --- a/input/command.go +++ b/input/command.go @@ -1,7 +1,6 @@ package input import ( - "errors" "fmt" "os" @@ -60,10 +59,7 @@ func (o Options) Run() error { WithProgramOptions(tea.WithOutput(os.Stderr)). Run() if err != nil { - if errors.Is(err, huh.ErrTimeout) { - return exit.NewTimeout(o.Timeout) - } - return err + return exit.Handle(err, o.Timeout) } fmt.Println(value) diff --git a/internal/exit/exit.go b/internal/exit/exit.go index b1cc27304..e79d7c22f 100644 --- a/internal/exit/exit.go +++ b/internal/exit/exit.go @@ -1,6 +1,7 @@ package exit import ( + "errors" "fmt" "time" @@ -14,9 +15,12 @@ const StatusTimeout = 124 const StatusAborted = 130 // ErrAborted is the error to return when a gum command is aborted by Ctrl+C. -var ErrAborted = fmt.Errorf("aborted") +var ErrAborted = huh.ErrUserAborted -// NewTimeout returns a new ErrTimeout. -func NewTimeout(d time.Duration) ErrTimeout { - return fmt.Errorf("timed out after %s: %w", d, huh.ErrTimeout +// Handle handles the error. +func Handle(err error, d time.Duration) error { + if errors.Is(err, huh.ErrTimeout) { + return fmt.Errorf("%w after %s", huh.ErrTimeout, d) + } + return err } diff --git a/main.go b/main.go index 9f748ff2a..5321fcdc3 100644 --- a/main.go +++ b/main.go @@ -77,7 +77,7 @@ func main() { fmt.Fprintln(os.Stderr, err) os.Exit(exit.StatusTimeout) } - if errors.Is(err, exit.ErrAborted) || errors.Is(err, huh.ErrUserAborted) { + if errors.Is(err, huh.ErrUserAborted) { os.Exit(exit.StatusAborted) } fmt.Println(err)