Skip to content

Instantly share code, notes, and snippets.

@paxperscientiam
Last active November 30, 2017 05:46
Show Gist options
  • Select an option

  • Save paxperscientiam/0558efa42753cec47d1f099a106a1155 to your computer and use it in GitHub Desktop.

Select an option

Save paxperscientiam/0558efa42753cec47d1f099a106a1155 to your computer and use it in GitHub Desktop.
Simple 'spinner' for your scripts. Disclaimer: use at your own peril!
#!/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