Skip to content

Instantly share code, notes, and snippets.

@benyanke
Last active November 5, 2017 07:13
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benyanke/6923e4c3aff9197e079826e0b17a09d3 to your computer and use it in GitHub Desktop.
Save benyanke/6923e4c3aff9197e079826e0b17a09d3 to your computer and use it in GitHub Desktop.
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;
Copy link

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