From 21b5f8ff2d2970f58884c4b28d89fe97562484f5 Mon Sep 17 00:00:00 2001 From: jutty Date: Tue, 27 Aug 2024 11:28:29 -0300 Subject: [PATCH] Add interactive dialog utility functions --- src/utility.sh | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/utility.sh b/src/utility.sh index 441ed38..bb2025f 100644 --- a/src/utility.sh +++ b/src/utility.sh @@ -5,11 +5,11 @@ log() { local message="$2" print_user_message() { - echo "[tori] $(date "+%H:%M:%S"): $1" 1>&2 + printf "%b\n" "[tori] $(date "+%H:%M:%S"): $1" 1>&2 } print_debug_message() { - echo "$(date "+%H:%M:%N") $1" 1>&2 + printf "%b\n" "$(date "+%H:%M:%N") $1" 1>&2 } if [ -z "$DEBUG" ]; then @@ -43,6 +43,39 @@ log() { fi } +confirm() { + local question="$1" + local answer= + read -rp "$question [y/N] " answer + + if [ "$answer" == y ] || [ "$answer" == Y ]; then + return 0; + else + return 1; + fi +} + +ask() { + local question="$1" + local options="$2" + local answer= + local options_count=0 + local dialog_options= + + local IFS=, + for option in $options; do + _=$((options_count+=1)) + dialog_options="$dialog_options\n [$options_count] $option" + done; + IFS= + + printf "%s" "$question" >&2 + printf "%b" "$dialog_options" >&2 + printf "\n%s" "Choose an option [1-$options_count] " >&2 + read -r answer + echo "$answer" +} + set_opts() { local target="$1" local sign=