spinner - scripts - various script and utils
HTML git clone git://z3bra.org/scripts
DIR Log
DIR Files
DIR Refs
---
spinner (1225B)
---
1 #!/bin/sh
2 #
3 # Display a spinner for as long as a lockfile is taken.
4 # Take a lock with flock(1):
5 # flock /tmp/sleep.lock sleep 20 &
6 # spinner /tmp/sleep.lock
7
8 # https://antofthy.gitlab.io/info/ascii/Spinners.txt
9 randspin() {
10 n=4
11 c=${1:-$(seq $n|shuf|head -n1)}
12 case $c in
13 worm|1) echo "⠋ ⠙ ⠹ ⠸ ⢰ ⣰ ⣠ ⣄ ⣆ ⡆ ⠇ ⠏" ;;
14 hole|2) echo "⣾ ⣽ ⣻ ⢿ ⡿ ⣟ ⣯ ⣷" ;;
15 sand|3) echo "⠁ ⠂ ⠄ ⡀ ⡈ ⡐ ⡠ ⣀ ⣁ ⣂ ⣄ ⣌ ⣔ ⣤ ⣥ ⣦ ⣮ ⣶ ⣷ ⣿ ⡿ ⠿ ⢟ ⠟ ⡛ ⠛ ⠫ ⢋ ⠋ ⠍ ⡉ ⠉ ⠑ ⠡ ⢁" ;;
16 leap|4) echo "⣀ ⢄ ⢂ ⢁ ⡈ ⡐ ⡠" ;;
17 esac
18 }
19
20 usage() {
21 echo "usage: $(basename $0) [-s spinner] [-p prefix] lockfile" >&2
22 }
23
24 spinner=$(randspin)
25 while getopts "hp:s:" OPT; do
26 case "$OPT" in
27 s) spinner=$(randspin $OPTARG);;
28 p) prefix="$OPTARG";;
29 h) usage; exit 0;;
30 *) usage; exit 1;;
31 esac
32 done
33 shift $((OPTIND - 1))
34
35 if [ $# -ne 1 ]; then
36 usage
37 exit 1
38 fi
39
40 # Exit immediately if lockfile doesn't exists
41 [ -e "$1" ] || exit
42
43 printf '%s[?25l ' "$prefix"
44 trap "printf '[?25h\b🗸\n'; exit" EXIT INT
45 until flock -n "$1" true; do
46 for c in $spinner; do
47 if flock -n "$1" true; then
48 exit 0
49 else
50 printf "\b%s" "$c"
51 sleep 0.08
52 fi
53 done
54 done