Skip to content

Instantly share code, notes, and snippets.

@OlegGorj
Created September 22, 2019 19:47
Show Gist options
  • Save OlegGorj/12cc2e3735e069c490771064eb7c76f6 to your computer and use it in GitHub Desktop.
Save OlegGorj/12cc2e3735e069c490771064eb7c76f6 to your computer and use it in GitHub Desktop.
Spinner Animation and echo command
#!/bin/bash

## spinner takes the pid of the process as the first argument and
#  string to display as second argument (default provided) and spins
#  until the process completes.
spinner() {
    local PROC="$1"
    local str="${2:-'Copyright of KatworX© Tech. Developed by Arjun Singh Kathait and Debugged by the ☆Stack Overflow Community☆'}"
    local delay="0.1"
    tput civis  # hide cursor
    printf "\033[1;34m"
    while [ -d /proc/$PROC ]; do
        printf '\033[s\033[u[ / ] %s\033[u' "$str"; sleep "$delay"
        printf '\033[s\033[u[ — ] %s\033[u' "$str"; sleep "$delay"
        printf '\033[s\033[u[ \ ] %s\033[u' "$str"; sleep "$delay"
        printf '\033[s\033[u[ | ] %s\033[u' "$str"; sleep "$delay"
    done
    printf '\033[s\033[u%*s\033[u\033[0m' $((${#str}+6)) " "  # return to normal
    tput cnorm  # restore cursor
    return 0
}

## simple example with sleep
sleep 5 &

spinner $!

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