From 867a10f03f1049c182fcd008e8e09e554f82e2f8 Mon Sep 17 00:00:00 2001 From: tteckster Date: Sat, 6 Jan 2024 17:15:45 -0500 Subject: [PATCH] Add new element - add spinner --- ct/create_lxc.sh | 31 +++++++++++++++++++++++++------ misc/build.func | 24 ++++++++++++++++++++++-- misc/install.func | 32 ++++++++++++++++++++++++++------ 3 files changed, 73 insertions(+), 14 deletions(-) diff --git a/ct/create_lxc.sh b/ct/create_lxc.sh index 34b78f09..ee29e706 100644 --- a/ct/create_lxc.sh +++ b/ct/create_lxc.sh @@ -25,6 +25,8 @@ trap 'error_handler $LINENO "$BASH_COMMAND"' ERR # This function handles errors function error_handler() { + if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID; fi + printf "\e[?25h" local exit_code="$?" local line_number="$1" local command="$2" @@ -32,20 +34,37 @@ function error_handler() { echo -e "\n$error_message\n" } -# This function prints an informational message -function msg_info() { - local msg="$1" - echo -ne " ${HOLD} ${YW}${msg}..." +# This function displays a spinner. +function spinner() { + printf "\e[?25l" + spinner="/-\\|/-\\|" + spin_i=0 + while true; do + printf "\b%s" "${spinner:spin_i++%${#spinner}:1}" + sleep 0.1 + done } -# This function prints a success message +# This function displays an informational message with a yellow color. +function msg_info() { + local msg="$1" + echo -ne " ${HOLD} ${YW}${msg} " + spinner & + SPINNER_PID=$! +} + +# This function displays a success message with a green color. function msg_ok() { + if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID; fi + printf "\e[?25h" local msg="$1" echo -e "${BFR} ${CM} ${GN}${msg}${CL}" } -# This function prints an error message +# This function displays a error message with a red color. function msg_error() { + if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID; fi + printf "\e[?25h" local msg="$1" echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}" } diff --git a/misc/build.func b/misc/build.func index ed04d262..37dec4f0 100644 --- a/misc/build.func +++ b/misc/build.func @@ -27,6 +27,8 @@ catch_errors() { # This function is called when an error occurs. It receives the exit code, line number, and command that caused the error, and displays an error message. error_handler() { + if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID; fi + printf "\e[?25h" local exit_code="$?" local line_number="$1" local command="$2" @@ -34,20 +36,38 @@ error_handler() { echo -e "\n$error_message\n" } +# This function displays a spinner. +spinner() { + printf "\e[?25l" + spinner="/-\\|/-\\|" + spin_i=0 + while true; do + printf "\b%s" "${spinner:spin_i++%${#spinner}:1}" + sleep 0.1 + done +} + # This function displays an informational message with a yellow color. msg_info() { local msg="$1" - echo -ne " ${HOLD} ${YW}${msg}..." + echo -ne " ${HOLD} ${YW}${msg} " + spinner & + SPINNER_PID=$! } + # This function displays a success message with a green color. msg_ok() { + if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID; fi + printf "\e[?25h" local msg="$1" echo -e "${BFR} ${CM} ${GN}${msg}${CL}" } -# This function displays an error message with a red color. +# This function displays a error message with a red color. msg_error() { + if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID; fi + printf "\e[?25h" local msg="$1" echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}" } diff --git a/misc/install.func b/misc/install.func index e3527fb4..66da53d2 100644 --- a/misc/install.func +++ b/misc/install.func @@ -36,6 +36,8 @@ catch_errors() { # This function handles errors error_handler() { + if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID; fi + printf "\e[?25h" local exit_code="$?" local line_number="$1" local command="$2" @@ -46,20 +48,38 @@ error_handler() { fi } -# This function prints an informational message -msg_info() { - local msg="$1" - echo -ne " ${HOLD} ${YW}${msg}..." +# This function displays a spinner. +spinner() { + printf "\e[?25l" + spinner="/-\\|/-\\|" + spin_i=0 + while true; do + printf "\b%s" "${spinner:spin_i++%${#spinner}:1}" + sleep 0.1 + done } -# This function prints a success message +# This function displays an informational message with a yellow color. +msg_info() { + local msg="$1" + echo -ne " ${HOLD} ${YW}${msg} " + spinner & + SPINNER_PID=$! +} + + +# This function displays a success message with a green color. msg_ok() { + if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID; fi + printf "\e[?25h" local msg="$1" echo -e "${BFR} ${CM} ${GN}${msg}${CL}" } -# This function prints an error message +# This function displays a error message with a red color. msg_error() { + if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID; fi + printf "\e[?25h" local msg="$1" echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}" }