From fabdacc12969ac74ec5155099d55c6fbde097fe0 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 21 May 2022 00:44:05 -0700 Subject: [PATCH] added PoC's for spinner and progressbar --- demo.sh | 9 ++++++++- functions | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/demo.sh b/demo.sh index d9b6c5d..0061b9f 100644 --- a/demo.sh +++ b/demo.sh @@ -104,4 +104,11 @@ idx=0 for option in "${my_options[@]}"; do echo -e "$option\t=> ${result[idx]}" ((idx++)) -done \ No newline at end of file +done + +# Example usage for above: + until $(sleep 5); do + spin + sleep 0.2 + done + endspin \ No newline at end of file diff --git a/functions b/functions index c5bb400..9bf3228 100644 --- a/functions +++ b/functions @@ -281,6 +281,41 @@ ctrl_c(){ # ensure ctrl-c to cancel script ends the entire process and not just the current function # trap ctrl_c INT # Commented out by default to prevent abnormal background exits +# Function for spinner status +sp="/-\|" +sc=0 +spin() { + printf "\b${sp:sc++:1}" + ((sc==${#sp})) && sc=0 +} +endspin() { + printf "\r%s\n" "$@" +} + +# Example usage for above: +# until work_done; do +# spin +# some_work ... +# done +# endspin + +# https://github.com/fearside/ProgressBar/blob/master/progressbar.sh +progress-bar() { +# Process data + let _progress=(${1}*100/${2}*100)/100 + let _done=(${_progress}*4)/10 + let _left=40-$_done +# Build progressbar string lengths + _done=$(printf "%${_done}s") + _left=$(printf "%${_left}s") + +# 1.2 Build progressbar strings and print the ProgressBar line +# 1.2.1 Output example: +# 1.2.1.1 Progress : [########################################] 100% +printf "\rProgress : [${_done// /#}${_left// /-}] ${_progress}%%" + +} + # Renders a text based list of options that can be selected by the # user using up, down and enter keys and returns the chosen option. # @@ -297,9 +332,9 @@ select_option(){ print_selected() { printf " $ESC[7m $1 $ESC[27m"; } get_cursor_row() { IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; } key_input() { read -s -n3 key 2>/dev/null >&2 - if [[ $key = $ESC[A ]]; then echo up; fi - if [[ $key = $ESC[B ]]; then echo down; fi - if [[ $key = "" ]]; then echo enter; fi; } + if [[ $key = $ESC[A ]]; then echo up; fi + if [[ $key = $ESC[B ]]; then echo down; fi + if [[ $key = "" ]]; then echo enter; fi; } # initially print empty new lines (scroll down if at bottom of screen) for opt; do printf "\n"; done