Skip to content

Instantly share code, notes, and snippets.

@benyanke

benyanke/fidget.sh

Last active Nov 5, 2017
Embed
What would you like to do?
Bash fidget spinner
# Bash Fidget Spinner
# Could also be included in a bashrc file
fidget() {
start_time="0.02";
spin_efficiency="0.99";
end_time="3";
time="$start_time";
printf "\e[92mPress any key to give another spin...\n\n";
printf "\e[1m"; # Make spinner bold
printf "\e[91m"; # Make spinner red
printf "\n\033[1A"; # Make newline, then move back up
printf "\033[s"; # Save beginning cursor position
sleep 0.75;
while true; do
for X in '-' '/' '|' '\'; do
# Slow down slightly
time=`echo "$time/$spin_efficiency" | bc -l`
printf "\b";
# if a "push" is detected, make it faster
read -n1 -s -t $time -p "${X}"
if [[ $? = 0 ]] ; then
# This statement is executed if a key press is detected
printf '\033[K' # Erase to end of line
printf '\033[10D' # Return cursor to beginning
printf "\033[u"; # Restore cursor to saved position (failsafe)
# Reset to full speed
time=$start_time;
fi
# If too slow, exit
if [[ `echo "$time>$end_time" | bc -l` = 1 ]] ; then
sleep 1;
return 0;
fi
done;
done
}
# Remove this line if using in .bashrc
fidget;
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Aug 15, 2017

That's awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment