-
-
Save L1so/82f7bae295b429acb78399660a7cbab0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Shows a spinner while another command is running. Randomly picks one of 12 spinner styles. | |
# @args command to run (with any parameters) while showing a spinner. | |
# E.g. ‹spinner sleep 10› | |
Ye='\033[1;33m'; Wh='\033[1;37m'; Gr='\033[1;32m'; Re='\033[1;31m'; Cy='\033[1;36m'; Nc='\033[0m' | |
function shutdown() { | |
tput cnorm # reset cursor | |
} | |
trap shutdown EXIT | |
function spinner() { | |
# make sure we use non-unicode character type locale | |
# (that way it works for any locale as long as the font supports the characters) | |
local LC_CTYPE=C | |
local pid="$!" # Process Id of the previous running command | |
# set -x | |
local narrate="$1" | |
case $(($RANDOM % 2)) in | |
0) | |
local spin='-\|/' | |
local charwidth=1 | |
;; | |
1) | |
local spin='⠁⠂⠄⡀⢀⠠⠐⠈' | |
local charwidth=3 | |
;; | |
esac | |
local i=0 | |
tput civis # cursor invisible | |
while kill -0 $pid 2>/dev/null; do | |
local i=$(((i + $charwidth) % ${#spin})) | |
local cnar=$((charwidth + ${#narrate})) # Character length + Narrate length | |
#local cnar=$((cnar + 1)) | |
# set +x | |
#set +x | |
printf "%b %b" "${Wh}$narrate${Nc}" "${Cy}${spin:$i:$charwidth}${nc}" | |
echo -en "\033[${cnar}D" | |
sleep .1 | |
done | |
tput cnorm | |
wait $pid # capture exit code | |
local rv=$? | |
if [ "$rv" -eq 0 ]; then | |
printf "%b" "${Wh}$narrate \e[1;32m\xE2\x9C\x94${Nc}\n" | |
fi | |
return $rv | |
} | |
# NOT REQUIRED BUT PUT JUST FOR AN EXAMPLE | |
{ | |
sleep 1 | |
sleep 1 | |
sleep 1 | |
} > /dev/null 2>&1 & | |
spinner "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment