Skip to content

feat: improve handling ctrl+c and timeouts#747

Merged
caarlos0 merged 1 commit intomainfrom
interrupt
Dec 9, 2024
Merged

feat: improve handling ctrl+c and timeouts#747
caarlos0 merged 1 commit intomainfrom
interrupt

Conversation

@caarlos0
Copy link
Copy Markdown
Contributor

@caarlos0 caarlos0 commented Dec 6, 2024

  • make ctrl+c interrupt and exit properly
  • handle timeouts with a context.Context and exit properly

Copy link
Copy Markdown
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

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

I know it's a draft, but I looked at it

Comment thread choose/choose.go Outdated
Comment thread confirm/confirm.go Outdated
Comment thread pager/command.go Outdated
Comment thread pager/pager.go Outdated
Copy link
Copy Markdown
Contributor

@aymanbagabas aymanbagabas left a comment

Choose a reason for hiding this comment

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

Looks good, few questions

Comment thread choose/choose.go
Comment thread pager/pager.go
Base automatically changed from huh-rev to main December 9, 2024 16:18
@bashbunni
Copy link
Copy Markdown
Contributor

This works great for the Gum commands, though when working with a script (e.g. demo.sh) hitting ctrl+c skips the current gum command, but it would be great to be able to quit the whole script with ctrl+d. The changes here work as expected :) would be great to hear your thoughts on how we could exit the script though without having to skip all of the remaining gum commands

@caarlos0
Copy link
Copy Markdown
Contributor Author

caarlos0 commented Dec 9, 2024

@bashbunni it does work, but the script might need the set -euo pipefail options, e.g.:

#!/bin/bash
set -euo pipefail
time=$(gum input --placeholder "Timeout (in minutes)")
gum spin --timeout "${time}m" --show-output -- ping -c 50 google.com
echo "Done"
  • -e: exits on non-zero status
  • -u: errors on unset variables (not necessary for this example though)
  • -o pipefail: if a command in a pipe fails, returns its status code

these options are generally good to have on any scripts as they help preventing errors.

@caarlos0 caarlos0 merged commit 4f46952 into main Dec 9, 2024
@caarlos0 caarlos0 deleted the interrupt branch December 9, 2024 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants