Last active
January 2, 2016 12:09
-
-
Save Gisleburt/b7e346268982355e54d1 to your computer and use it in GitHub Desktop.
Show a rotating cursor while running long commands
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 | |
# Spinner Source: http://it.toolbox.com/blogs/locutus/bash-bits-nibbles-and-bytes-a-rotating-cursor-while-you-wait-22867 | |
# Parallel process Source: http://stackoverflow.com/questions/6384013/run-bash-commands-in-parallel-track-results-and-count#6384467 | |
# Hide job status source: http://superuser.com/questions/377775/how-do-i-keep-background-jobs-from-telling-me-theyre-done | |
# Spin the spinner. Must be called repeatedly to animate | |
function spin() { | |
case $toggle | |
in | |
1) | |
echo -n " \ "$1 | |
echo -ne "\r" | |
toggle="2" | |
;; | |
2) | |
echo -n " | "$1 | |
echo -ne "\r" | |
toggle="3" | |
;; | |
3) | |
echo -n " / "$1 | |
echo -ne "\r" | |
toggle="4" | |
;; | |
*) | |
echo -n " - "$1 | |
echo -ne "\r" | |
toggle="1" | |
;; | |
esac | |
} | |
# Close off the spinner | |
function complete() { | |
echo \* $1 [$?] | |
} | |
# Run a command and spin the spinner | |
function run() { | |
command=${1} | |
(sleep 1 && $command) 2> errors.log & disown | |
pid1=$! | |
running1=`ps -p $pid1 --no-headers | wc -l` | |
while ( [ $running1 != 0 ] ) do | |
spin "$command" | |
sleep 1 | |
running1=`ps -p $pid1 --no-headers | wc -l` | |
done | |
complete "$command" | |
} | |
#usage | |
command="command constructed as a string" | |
run $command |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment