Last active
November 30, 2017 05:46
-
-
Save paxperscientiam/0558efa42753cec47d1f099a106a1155 to your computer and use it in GitHub Desktop.
Simple 'spinner' for your scripts. Disclaimer: use at your own peril!
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
| #!/usr/bin/env bash | |
| : | |
| function spinner () { | |
| trap 'tput cnorm' RETURN SIGINT | |
| : | |
| sprite=('|' '/' '–' '\') | |
| SECONDS=0 | |
| TIME_LIMIT= | |
| : | |
| local OPTIND opt d | |
| while getopts ":d:" opt; do | |
| case "${opt}" in | |
| d ) | |
| TIME_LIMIT="${OPTARG}" | |
| ;; | |
| \?) | |
| printf 'Usage: %s [-d N]\n' "${FUNCNAME[0]}" | |
| printf 'Note: runs for N seconds.\n' | |
| return 0 | |
| ;; | |
| esac | |
| done | |
| while : | |
| do | |
| tput civis | |
| for frame in "${sprite[@]}" | |
| do | |
| printf ' %s\r' $frame | |
| sleep 0.1 | |
| tput dl 1 | |
| done | |
| if [ ! -z ${TIME_LIMIT} ]; then | |
| [[ "$SECONDS" -ge "$TIME_LIMIT" ]] && return 0 | |
| fi | |
| done | |
| } | |
| : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment