armbian-next: countdown: split off file for countdown functions (countdown-to-abort and countdown-to-continue)

- introduce `countdown.sh`, move `traps.sh::exit_if_countdown_not_aborted()` there, introduce `exit_if_countdown_not_aborted()`
This commit is contained in:
Ricardo Pardini
2023-01-20 00:59:16 +01:00
parent 2c9582f9ba
commit 8af14d4f8c
3 changed files with 64 additions and 33 deletions

View File

@@ -186,36 +186,3 @@ function exit_with_error() {
exit 43
}
# This exits, unless user presses ENTER. If not interactive, user will be unable to comply and the script will exit.
function exit_if_countdown_not_aborted() {
# parse
declare -i loops="${1}"
declare reason="${2}"
# validate
[[ -z "${loops}" ]] && exit_with_error "countdown_to_exit_or_just_exit_if_noninteractive() called without a number of loops"
[[ -z "${reason}" ]] && exit_with_error "countdown_to_exit_or_just_exit_if_noninteractive() called without a reason"
# If not interactive, just exit.
if [[ ! -t 1 ]]; then
exit_with_error "Exiting due to '${reason}' - not interactive, exiting immediately."
fi
display_alert "Problem detected" "${reason}" "err"
display_alert "Exiting in ${loops} seconds" "Press <Ctrl-C> to abort, <Enter> to ignore and continue" "err"
echo -n "Counting down: "
for i in $(seq 1 "${loops}"); do
declare stop_waiting=0
declare keep_waiting=0
timeout --foreground 1 bash -c "read -n1; echo \$REPLY" && stop_waiting=1 || keep_waiting=1
if [[ "$stop_waiting" == "1" ]]; then
display_alert "User pressed ENTER, continuing, albeit" "${reason}" "wrn"
return 0
fi
echo -n "$((10 - i))... " >&2
done
echo "" >&2 # No newlines during countdown, so break one here
# Countdown finished, exit.
exit_with_error "Exiting due to '${reason}'"
}