Skip to content

Commit

Permalink
fix: improve
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Nov 18, 2024
1 parent b14d8a8 commit e6b624c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 28 deletions.
10 changes: 2 additions & 8 deletions choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()) {
Expand Down
6 changes: 1 addition & 5 deletions confirm/command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package confirm

import (
"errors"
"os"

"github.com/charmbracelet/gum/internal/exit"
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions file/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 1 addition & 5 deletions input/command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package input

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions internal/exit/exit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exit

import (
"errors"
"fmt"
"time"

Expand All @@ -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
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e6b624c

Please sign in to comment.