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) --timeout was ignored, now works as documented #697

Merged

Conversation

dpritchett
Copy link
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
Member

@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!

confirm/command.go Outdated Show resolved Hide resolved
confirm/command.go Outdated Show resolved Hide resolved
@dpritchett
Copy link
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
7 checks passed
@caarlos0
Copy link
Member

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