Last active
November 5, 2017 07:13
-
-
Save benyanke/6923e4c3aff9197e079826e0b17a09d3 to your computer and use it in GitHub Desktop.
Bash fidget spinner
This file contains hidden or 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
# 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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's awesome!