Skip to content

Instantly share code, notes, and snippets.

@scottrigby
Created April 1, 2019 01:50
Show Gist options
  • Save scottrigby/906f72081bac084602929f15c488bb15 to your computer and use it in GitHub Desktop.
Save scottrigby/906f72081bac084602929f15c488bb15 to your computer and use it in GitHub Desktop.
Shell script to begin or end a spinner background process
#!/bin/sh
help() {
cat << EOF
spin(1)
NAME
spin - begin or end a spinner background process
SYNOPSIS
spin [-m message] [-b begin] [-e end (PID)] [-h help]
OPTIONS
-m message
Spinner message.
-b begin
Begins a spinner as a background process.
-e end (PID)
Ends a spinner process by PID.
-s speed
Optional replacement spinner speed in seconds. Default is '0.25'.
-c characters
Optional replacement characters. Example '-\|/'. Default is '⠈⠁⠈⠑⠈⠱⠈⡱⢀⡱⢄⡱⢄⡱⢆⡱⢎⡱⢎⡰⢎⡠⢎⡀⢎⠁⠎⠁⠊⠁'.
-i increment
Optional replacement character increment. Example '1'. Default is '2' (the default braile characters are grouped by two).
-f file
Optional replacement file location for storing the spinner process PID. Default is '$HOME/spin.pid'
-h help
Prints this help message.
EOF
}
begin() {
local MESSAGE=$1
local SPEED=$2
local CHARACTERS=$3
local INCREMENT=$4
i=0;
while true; do
printf "\r${CHARACTERS:i%${#CHARACTERS}:$INCREMENT} $MESSAGE"; i=$((i+INCREMENT)); sleep $SPEED
done
}
end() {
local PID=$1
local FINISHED=' ✓'
local MESSAGE=$2
printf "\r\033[K$FINISHED $MESSAGE\n"
kill $PID
}
while getopts 'hbe:m:s:c:i:f:' OPTION; do
case $OPTION in
m) export MESSAGE="${OPTARG}" ;;
b) BEGIN=1 ;;
e) PID="${OPTARG}" ;;
s) SPEED="${OPTARG}" ;;
c) CHARACTERS="${OPTARG}" ;;
i) INCREMENT="${OPTARG}" ;;
f) FILE="${OPTARG}" ;;
h) help; exit 0 ;;
*) echo Invalid arg; exit 1 ;;
esac
done
shift $(($OPTIND - 1))
if ([ -n "${BEGIN}" ] || [ -n "${PID}" ]) && [ -z "${MESSAGE}" ]; then
echo "-m (message) must be included when -b (begin) or -e (end) are specified." >&2
exit 1
fi
if [ $BEGIN ]; then
DEFAULT_SPEED='0.25'
DEFAULT_CHARACTERS='⠈⠁⠈⠑⠈⠱⠈⡱⢀⡱⢄⡱⢄⡱⢆⡱⢎⡱⢎⡰⢎⡠⢎⡀⢎⠁⠎⠁⠊⠁'
DEFAULT_INCREMENT='2'
DEFAULT_FILE="${HOME}/spin.pid"
begin "${MESSAGE:-}" ${SPEED:-$DEFAULT_SPEED} "${CHARACTERS:-$DEFAULT_CHARACTERS}" ${INCREMENT:-$DEFAULT_INCREMENT} &
PID=$!
echo $PID > ${FILE:-$DEFAULT_FILE}
elif [ $PID ]; then
end $PID "$MESSAGE"
fi
#!/bin/sh
# Set replacement values.
BEGIN_MESSAGE="Testing 😬"
SPEED='0.1'
CHARACTERS='-\|/'
INCREMENT='1'
END_MESSAGE="Tested 👍"
FILE=`mktemp`
./spin.sh -b -m "$BEGIN_MESSAGE" -s $SPEED -c $CHARACTERS -i $INCREMENT -f $FILE
for i in {1..5}; do sleep 1; done
PID=`cat $FILE`
./spin.sh -e $PID -m "$END_MESSAGE"
# Cleanup.
rm $FILE
# Check cleanup.
ps | grep -v grep | grep --quiet "$PID"
[ "$?" = 0 ] && echo $PID was still running 👎 && kill $PID && exit 1 || echo ' ✓' PID $PID correctly killed 👍
ls $FILE >/dev/null 2>&1
[ "$?" = 0 ] && echo $FILE was still present 👎 && rm $FILE && exit 1 || echo ' ✓' Temp file $FILE correctly removed 👍
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment