Last active
August 29, 2015 13:56
-
-
Save d11wtq/9248289 to your computer and use it in GitHub Desktop.
Bash spinner, WIP.
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 | |
TTY=/dev/tty | |
spin() { | |
width=20 | |
delta=1 | |
#fwd_chars=("\\" "|" "/" "-") | |
#rev_chars=("/" "|" "\\" "-") | |
fwd_chars=">" | |
rev_chars="<" | |
space=0 | |
"$@" >/dev/null & | |
pid=$! | |
tput civis | |
while kill -0 $pid 2>/dev/null | |
do | |
if ((delta>0)) | |
then | |
chars=(${fwd_chars[@]}) | |
else | |
chars=(${rev_chars[@]}) | |
fi | |
# make a line of dashes | |
for ((i=0; i<width; i++)) | |
do | |
echo -ne "-" >$TTY | |
done | |
# move to the right offset | |
for ((i=0; i<width-space; i++)) | |
do | |
echo -ne "\b" >$TTY | |
done | |
# animate | |
for c in ${chars[@]} | |
do | |
echo -ne "$c" >$TTY | |
sleep 0.075 | |
echo -ne "\b" >$TTY | |
done | |
# move back to the left | |
for ((i=0; i<space; i++)) | |
do | |
echo -ne "\b" >$TTY | |
done | |
# clear the line | |
for ((i=0; i<width; i++)) | |
do | |
echo -ne " " >$TTY | |
done | |
# back to the left | |
for ((i=0; i<width; i++)) | |
do | |
echo -ne "\b" >$TTY | |
done | |
((space+=delta)) | |
if ((space>=width)) | |
then | |
delta=-1 | |
((space+=delta)) | |
((space+=delta)) | |
elif ((space<0)) | |
then | |
delta=1 | |
((space+=delta)) | |
((space+=delta)) | |
fi | |
done | |
tput cnorm | |
wait $pid | |
} | |
spin sleep 15 | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment