From dd0bc70233f2d6298e84195459c7a2b2c8814874 Mon Sep 17 00:00:00 2001 From: xshin404 Date: Tue, 5 Oct 2021 23:00:06 +0700 Subject: [PATCH] Add bash animation --- .animation/ghost.sh | 115 ++++++++++++++++++++++++++++++++++++++++++++ .animation/test.sh | 7 +++ 2 files changed, 122 insertions(+) create mode 100755 .animation/ghost.sh create mode 100755 .animation/test.sh diff --git a/.animation/ghost.sh b/.animation/ghost.sh new file mode 100755 index 0000000..6b8c5c3 --- /dev/null +++ b/.animation/ghost.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash + +# Credits: +# Bash Spinner : https://github.com/tlatsas/bash-spinner +# WhiteSur-GTK-Theme (Line 148-217) : https://github.com/vinceliuice/WhiteSur-gtk-theme/blob/master/lib-core.sh + +# Author: xShin +# +# Display awesome looping loading +# +# Usage: +# 1. Source this file on your shell script +# 2. Start the animation: +# start_ghost "[parameter1]" -> this parameter will display your message +# 3. Run your command +# 4. stop_ghost "[parameter1]" -> this parameter your command's exit status +# +# Also see: test.sh + +export COLOR_DEFAULT="\033[0m" +export COLOR_BLUE="\033[1;34m" +export COLOR_MAGENTA="\033[1;35m" +export COLOR_CYAN="\033[1;36m" +export COLOR_GREEN="\033[1;32m" +export COLOR_RED="\033[1;31m" +export COLOR_YELLOW="\033[1;33m" + +ICON="•" #  • +OK="SUCCESS" +NO="FAILED" + +ARRAY_ANIMATION=( + "${COLOR_BLUE}${ICON}${COLOR_GREEN}${ICON}${COLOR_YELLOW}${ICON}${COLOR_RED}${ICON}${COLOR_MAGENTA}${ICON} " + " ${COLOR_GREEN}${ICON}${COLOR_YELLOW}${ICON}${COLOR_RED}${ICON}${COLOR_MAGENTA}${ICON}${COLOR_BLUE}${ICON} " + " ${COLOR_RED}${ICON}${COLOR_MAGENTA}${ICON}${COLOR_YELLOW}${ICON}${COLOR_BLUE}${ICON}${COLOR_GREEN}${ICON} " + " ${COLOR_MAGENTA}${ICON}${COLOR_BLUE}${ICON}${COLOR_GREEN}${COLOR_YELLOW}${ICON}${ICON}${COLOR_RED}${ICON} " + " ${COLOR_BLUE}${ICON}${COLOR_GREEN}${ICON}${COLOR_RED}${ICON}${COLOR_YELLOW}${ICON}${COLOR_MAGENTA}${ICON}" +) + +ghost() { + + case $1 in + + start ) + + # let column=$(tput cols)-${#2}-8 + let column=$(echo $COLUMNS)-${#2}-8 + + printf "%${column}s" + + while true; do + for i in {0..4}; do + printf "\b\r\033[2K${2} ${ARRAY_ANIMATION[i]}" + sleep 0.15 + done + for i in {4..0}; do + printf "\b\r\033[2K${2} ${ARRAY_ANIMATION[i]}" + sleep 0.15 + done + done + + ;; + + stop ) + + if [[ -z ${3} ]]; then + echo "ghost not running" + exit 1 + fi + + kill ${3} > /dev/null 2>&1 + + echo -en "\b${COLOR_DEFAULT}> [" + + if [[ $2 -eq 0 ]]; then + echo -en " ${COLOR_GREEN}${OK}${COLOR_DEFAULT} " + else + echo -en " ${COLOR_RED}${NO}${COLOR_DEFAULT} " + fi + + echo -e "${COLOR_DEFAULT}]" + + ;; + + * ) + + echo "invalid argument, try again with {start/stop}" + exit 1 + + ;; + + + esac +} + +function start_ghost() { + + setterm -cursor off + ghost "start" "${1}" & + ghost_pid=${!} + disown + +} + +function stop_ghost() { + + ghost "stop" $1 $ghost_pid + unset $ghost_pid + setterm -cursor on + +} + +# start_ghost "test" +# sleep 2s +# stop_ghost $? \ No newline at end of file diff --git a/.animation/test.sh b/.animation/test.sh new file mode 100755 index 0000000..a073e8b --- /dev/null +++ b/.animation/test.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +source $(pwd)/ghost.sh + +start_ghost "[ TEST ] > bash-animation" +sleep 2s +stop_ghost $? \ No newline at end of file