From 9bc982a6f162145124e5079930b95c0e37caad9c Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Mon, 16 Jun 2025 14:23:09 +0300 Subject: [PATCH] Features: 1) Added spinner function to enhance user feedback during command execution; 2) Improved cursor visibility control with hide_cursor and show_cursor functions; 3) Refined output messages for improved clarity. Fixes: 1) Fixed irregular Bash array formatting within SPINNER; 2) Resolved typo in COLOR array definition. Extra: Simplified docker commands by removing redundant `docker compose` prefix and replaced with shorter aliases; enhanced code readability and formatting. --- scripts/Unix/reboot.sh | 70 +++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/scripts/Unix/reboot.sh b/scripts/Unix/reboot.sh index bb14d815..a43e4c97 100755 --- a/scripts/Unix/reboot.sh +++ b/scripts/Unix/reboot.sh @@ -1,46 +1,52 @@ #!/usr/bin/env bash - set -euo pipefail -SPINNER=('|' '/' '-' '\\') -COLORS=(97 37 90) +SPINNER=('|' '/' '-' '\') +COLORS=(97 37) DELAY=0.1 -run_with_spinner() { - local cmd="$1" - local msg="$2" +hide_cursor() { tput civis 2>/dev/null || true; } +show_cursor() { tput cnorm 2>/dev/null || true; } - printf "\e[1m%s...\e[0m " "$msg" - eval "$cmd" &> /dev/null & +spinner() { + local pid=$1 msg=$2 i=0 frame color + hide_cursor + while kill -0 "$pid" 2>/dev/null; do + frame="${SPINNER[i % ${#SPINNER[@]}]}" + color="${COLORS[i % ${#COLORS[@]}]}" + printf "\r\e[36m%s... \e[0m\e[%sm%s\e[0m" "$msg" "$color" "$frame" + ((i++)) + sleep "$DELAY" + done + show_cursor +} + +run_with_spinner() { + local args="$1" msg="$2" rc + printf "\e[36m%s... \e[0m" "$msg" + + bash -c "docker --ansi never $args" &> /dev/null & local pid=$! - local i=0 - while kill -0 "$pid" 2>/dev/null; do - local frame=${SPINNER[i % ${#SPINNER[@]}]} - local color=${COLORS[i % ${#COLORS[@]}]} - printf "\e[${color}m%s\e[0m" "$frame" - sleep "$DELAY" - printf "\b" - ((i++)) - done + spinner "$pid" "$msg" - set +e - wait "$pid" - local rc=$? - set -e - if [[ $rc -ne 0 ]]; then - printf "\e[33m(! exit %d)\e[0m " "$rc" + wait "$pid" || rc=$? + rc=${rc:-0} + + if [[ $rc -eq 0 ]]; then + printf "\r\e[32m✔\e[0m %s\n" "$msg" + else + printf "\r\e[33m✖ (exit %d)\e[0m %s\n" "$rc" "$msg" fi - - printf "\e[32m✔\e[0m\n" } echo -run_with_spinner "docker compose --ansi never down || true" "Stopping services" -run_with_spinner "docker compose --ansi never up -d --build" "Rebuilding & starting services" -run_with_spinner "docker compose --ansi never exec app poetry run python manage.py migrate --no-input" "Applying database migrations" -run_with_spinner "docker compose --ansi never exec app poetry run python manage.py collectstatic --no-input" "Collecting static files" -run_with_spinner "docker compose --ansi never exec app poetry run python manage.py set_default_caches" "Setting default caches" -run_with_spinner "docker system prune -f" "Cleaning up unused Docker data" -echo -e "\e[1mAll done! Your application is up and running.\e[0m" +run_with_spinner "compose down" "Stopping services (down)" +run_with_spinner "compose up -d --build" "Rebuilding & starting services" +run_with_spinner "compose exec app poetry run python manage.py migrate --no-input" "Applying database migrations" +run_with_spinner "compose exec app poetry run python manage.py collectstatic --no-input" "Collecting static files" +run_with_spinner "compose exec app poetry run python manage.py set_default_caches" "Setting default caches" +run_with_spinner "system prune -f" "Cleaning up unused Docker data" + +echo -e "\n\e[1mAll done! Your application is up and running.\e[0m"