Skip to content

Instantly share code, notes, and snippets.

@aprell
Created August 26, 2019 12:37
Show Gist options
  • Select an option

  • Save aprell/30055e8caed4f79be99989ce4cb969ca to your computer and use it in GitHub Desktop.

Select an option

Save aprell/30055e8caed4f79be99989ce4cb969ca to your computer and use it in GitHub Desktop.
A simple spinning wheel
#!/usr/bin/env bash
# ShellChecked with no issues detected
set -eu
set -o pipefail
spinning_wheel() {
local spinning=("|" "/" "-" "\\")
local i=0
trap spinning_complete SIGINT SIGTERM
while true; do
echo -ne "\b${spinning[$i]}"
sleep 0.2
i=$(((i + 1) % ${#spinning[@]}))
done
}
spinning_complete() {
echo -ne "\b\u2714 "
exit 0
}
start_spinning_wheel() {
spinning_wheel &
spinning_wheel_pid=$!
# spinning_wheel should have a chance to run and install its signal handler
sleep 0.1
}
stop_spinning_wheel() {
if [ -n "$spinning_wheel_pid" ]; then
kill -SIGTERM $spinning_wheel_pid
wait $spinning_wheel_pid #2> /dev/null
fi
}
test_spinning_wheel() {
echo -n "Test: "
for _ in {1..3}; do
start_spinning_wheel
# Do something
eval "$1"
stop_spinning_wheel
done
echo ""
}
trap stop_spinning_wheel SIGINT SIGTERM
test_spinning_wheel "sleep 1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment