From a5ecf374d0e25596199de6cc0f02ebd00be356e8 Mon Sep 17 00:00:00 2001 From: Marco Massenzio <1153951+massenz@users.noreply.github.com> Date: Tue, 29 Nov 2022 10:59:40 -0800 Subject: [PATCH] Adds functions to prompt the user Adds an `ask` and `should_continue` functions to add interactivity during scripts. --- utils.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/utils.sh b/utils.sh index f58cd31..47da822 100644 --- a/utils.sh +++ b/utils.sh @@ -200,3 +200,31 @@ function newenv { fi success "Virtualenv ${envname} created" } + +# Inserts a pause in the progress, and an option to terminate early. +# +# Usage: should_continue +function should_continue { + if read -q "?Paused. Hit 'y' when ready to continue, or 'n' to terminate "; then + echo -e "\nOk, continuing...\n" + else + echo -e "\nAborting\n" + exit 1 + fi +} + +# Asks an optional question and returns a non-zero value if the user +# responds anything other than `y`. +# +# Should be used in an `if` statement: +# +# if ask "About to remove resource, continue?"; then +# ... do the deed +# fi +# Usage: ask [PROMPT] +function ask { + read -q "?${1:-Proceed?} [y/n] " + res=$? + echo -n "\n" + return $res +}