Bash Spinner

2013-07-18

Just a quick post to say, I've added a nifty spinner to bashlib.

This will display "Working: *" where the * is a spinner of some sort. I've added a few styles to choose from (the standard /-\| is style 1):

~~~~
function __spinner__ () {
        if [[ ${1} == "stop" ]]; then
                echo -ne "** Done **\n"
        elif [[ ${1} == "start" ]]; then
        shift
                case ${1} in
                        1) s="/-\|" ;;
                        2) s=".oO00Oo." ;;
                        3) s=".:iIIi:." ;;
                esac
                [[ ${i} == ${#s} ]] && i=0
                echo -ne "Working: ${s:${i}:1} \r"
                i=$[${i}+1]
        fi
}
~~~~

This works really well in conjuction with an until loop, such as:

~~~~
until [[ $f == 10 ]]; do
        __spinner__ start 2
        f=$[${f}+1]
        sleep 0.2
done
__spinner__ stop
~~~~

This just means that the whole time your script is processing stuff, you can actually show that it's doing something (actively).