Skip to content

Commit

Permalink
fix(confirm,choose,file,input): timeout handling (#718)
Browse files Browse the repository at this point in the history
* fix(confirm,choose,file,input): timeout handling

- some fields were not actually using the `--timeout` value
- some fields had different behavior when a timeout did occur. On this
  matter, it seems to me the best way forward is to specifically say it
  timed out, and after how long
- added exit status 124 (copied from `timeout` from coreutils) (fixes #684)

Signed-off-by: Carlos Alexandro Becker <[email protected]>

* Update main.go

Co-authored-by: Ayman Bagabas <[email protected]>

* Update internal/exit/exit.go

Co-authored-by: ccoVeille <[email protected]>

* fix: improve

Signed-off-by: Carlos Alexandro Becker <[email protected]>

* fix: stderr

---------

Signed-off-by: Carlos Alexandro Becker <[email protected]>
Co-authored-by: Ayman Bagabas <[email protected]>
Co-authored-by: ccoVeille <[email protected]>
  • Loading branch information
3 people authored Nov 18, 2024
1 parent 3cec9b7 commit c868aa1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
11 changes: 7 additions & 4 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,9 +77,10 @@ func (o Options) Run() error {
WithWidth(width).
WithShowHelp(o.ShowHelp).
WithTheme(theme).
WithTimeout(o.Timeout).
Run()
if err != nil && !errors.Is(err, huh.ErrTimeout) {
return err
if err != nil {
return exit.Handle(err, o.Timeout)
}
if len(choices) > 0 {
s := strings.Join(choices, "\n")
Expand All @@ -100,10 +102,11 @@ func (o Options) Run() error {
).
WithWidth(width).
WithTheme(theme).
WithTimeout(o.Timeout).
WithShowHelp(o.ShowHelp).
Run()
if err != nil && !errors.Is(err, huh.ErrTimeout) {
return err
if err != nil {
return exit.Handle(err, o.Timeout)
}

if term.IsTerminal(os.Stdout.Fd()) {
Expand Down
7 changes: 2 additions & 5 deletions confirm/command.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package confirm

import (
"errors"
"fmt"
"os"

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

Expand Down Expand Up @@ -32,9 +31,7 @@ func (o Options) Run() error {
WithShowHelp(o.ShowHelp).
Run()
if err != nil {
if o.Timeout > 0 && errors.Is(err, huh.ErrTimeout) {
return fmt.Errorf("unable to run confirm: %w", err)
}
return exit.Handle(err, o.Timeout)
}

if !choice {
Expand Down
5 changes: 3 additions & 2 deletions 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 @@ -51,14 +52,14 @@ func (o Options) Run() error {
Value(&path),
),
).
WithTimeout(o.Timeout).
WithShowHelp(o.ShowHelp).
WithKeyMap(keymap).
WithTheme(theme).
Run()
if err != nil {
return err
return exit.Handle(err, o.Timeout)
}

fmt.Println(path)
return nil
}
4 changes: 3 additions & 1 deletion input/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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,11 +54,12 @@ 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 {
return err
return exit.Handle(err, o.Timeout)
}

fmt.Println(value)
Expand Down
23 changes: 20 additions & 3 deletions internal/exit/exit.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
package exit

import "fmt"
import (
"errors"
"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.
var ErrAborted = fmt.Errorf("aborted")
// ErrAborted is the error to return when a gum command is aborted by Ctrl+C.
var ErrAborted = huh.ErrUserAborted

// 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
}
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ func main() {
},
)
if err := ctx.Run(); err != nil {
if errors.Is(err, exit.ErrAborted) || errors.Is(err, huh.ErrUserAborted) {
if errors.Is(err, huh.ErrTimeout) {
fmt.Fprintln(os.Stderr, err)
os.Exit(exit.StatusTimeout)
}
if errors.Is(err, huh.ErrUserAborted) {
os.Exit(exit.StatusAborted)
}
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

0 comments on commit c868aa1

Please sign in to comment.