Skip to content

fix(confirm) --timeout was ignored, now works as documented#697

Merged
caarlos0 merged 2 commits intocharmbracelet:mainfrom
radiusmethod:bug-confirm-timeout-was-missing
Nov 15, 2024
Merged

fix(confirm) --timeout was ignored, now works as documented#697
caarlos0 merged 2 commits intocharmbracelet:mainfrom
radiusmethod:bug-confirm-timeout-was-missing

Conversation

@dpritchett
Copy link
Copy Markdown
Contributor

@dpritchett dpritchett commented Oct 31, 2024

Fixes #698.

Relevant excerpts from gum confirm --help

Usage: gum confirm [<prompt>] [flags]

Ask a user to confirm an action

Arguments:
  [<prompt>]    Prompt to display.

Flags:
     --default              Default confirmation action
     --timeout=0            Timeout until confirm returns selected value or default if provided ($GUM_CONFIRM_TIMEOUT)

Changes

  • Chains a .WithTimeout(o.Timeout) in the confirm command's huh.NewForm() chain under confirm.Run()
  • Adds an errIsValidTimeout(error) bool method to confirm.Options to facilitate safely ignoring the timeout error returned in the course of the Run() context expiring due to timeout
  • Adds unit tests for errIsValidTimeout

Demos

Before: doesn't time out without outside help

Made with VHS

Note that five seconds elapse here before timeout kills gum, returning a nonzero exit code.

Set TypingSpeed 0.1
Output broken-main.gif
Set Shell zsh
Sleep 500ms
Type "git checkout main && git fetch upstream main && git rev-parse HEAD"
Enter
Type "date +%H:%M:%S.%1N && go run . confirm --timeout=1s --default='Yes' && echo 'Success'"
Enter
Sleep 4s
Ctrl+C
Type "date +%H:%M:%S.%1N"
Enter
Sleep 4s
Ctrl+D

After: times out as expected

Made with VHS

Times out in ~1 second as desired.

Output fixed.gif
Set Shell zsh
Sleep 500ms
Type "git checkout bug-confirm-timeout-was-missing && git rev-parse HEAD"
Enter
Sleep 1.5s
Type "go run . confirm --timeout=1s 'Am I going to time out?' --default='Yes' && echo Success"
Enter
Sleep 3s
Ctrl+D

Also works with GUM_CONFIRM_TIMEOUT.

@dpritchett dpritchett force-pushed the bug-confirm-timeout-was-missing branch from 049c7b9 to cf0a8d1 Compare October 31, 2024 21:56
@dpritchett dpritchett changed the title fix(confirm) Options.Timeout was ignored, now works as documented fix(confirm) --timeout was ignored, now works as documented Oct 31, 2024
Copy link
Copy Markdown
Contributor

@caarlos0 caarlos0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a couple of small things, looks good otherwise

thank so much for the PR!

Comment thread confirm/command.go Outdated
Comment thread confirm/command.go Outdated
@dpritchett
Copy link
Copy Markdown
Contributor Author

just a couple of small things, looks good otherwise

thank so much for the PR!

Nice suggestions, thanks. I've added them.

@caarlos0 caarlos0 merged commit 19e79b1 into charmbracelet:main Nov 15, 2024
@caarlos0
Copy link
Copy Markdown
Contributor

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

confirm command is ignoring its --timeout option

2 participants