-
Notifications
You must be signed in to change notification settings - Fork 1
/
spinner.sh
126 lines (101 loc) · 5.33 KB
/
spinner.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env bash
# -------------------------------------------------------------------------------- #
# Description #
# -------------------------------------------------------------------------------- #
# Display a small progress spinner in bash while running your commands with an #
# optional message. #
# -------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------- #
# Draw Spinner #
# -------------------------------------------------------------------------------- #
# Draw the actual spinner on the screen, suffixed by the message (if given). This #
# function will sit in an infinite loop as the stop script is used to terminate #
# the function. #
# #
# NOTE: Do not call this function directly! #
# -------------------------------------------------------------------------------- #
function draw_spinner()
{
# shellcheck disable=SC1003
local -a marks=( '/' '-' '\' '|' )
local i=0
delay=${SPINNER_DELAY:-0.25}
message=${1:-}
while :; do
printf '%s\r' "${marks[i++ % ${#marks[@]}]} $message"
sleep "${delay}"
done
}
# -------------------------------------------------------------------------------- #
# Start Spinner #
# -------------------------------------------------------------------------------- #
# A wrapper for starting the spiner, it will pass the message if supplied, and #
# store the PID of the process for later termination. #
# -------------------------------------------------------------------------------- #
function start_spinner()
{
message=${1:-} # Set optional message
draw_spinner "${message}" & # Start the Spinner:
SPIN_PID=$! # Make a note of its Process ID (PID):
declare -g SPIN_PID
trap stop_spinner $(seq 0 15)
}
# -------------------------------------------------------------------------------- #
# Draw Spinner Eval #
# -------------------------------------------------------------------------------- #
# Draw the actual spinner on the screen, after evaluating the command to use the #
# result as the message, function will sit in an infinite loop as the stop script #
# is used to terminate the function. #
# #
# NOTE: Do not call this function directly! #
# -------------------------------------------------------------------------------- #
function draw_spinner_eval()
{
# shellcheck disable=SC1003
local -a marks=( '/' '-' '\' '|' )
local i=0
delay=${SPINNER_DELAY:-0.25}
message=${1:-}
while :; do
message=$(eval "$1")
printf '%s\r' "${marks[i++ % ${#marks[@]}]} $message"
sleep "${delay}"
printf '\033[2K'
done
}
# -------------------------------------------------------------------------------- #
# Start Spinner Eval #
# -------------------------------------------------------------------------------- #
# A wrapper for starting the spiner, it will pass the command to be evaluated, and #
# store the PID of the process for later termination. #
# -------------------------------------------------------------------------------- #
function start_spinner_eval()
{
command=${1} # Set the command
if [[ -z "${command}" ]]; then
echo "You MUST supply a command"
exit
fi
draw_spinner_eval "${command}" & # Start the Spinner:
SPIN_PID=$! # Make a note of its Process ID (PID):
declare -g SPIN_PID
trap stop_spinner $(seq 0 15)
}
# -------------------------------------------------------------------------------- #
# Stop Spinner #
# -------------------------------------------------------------------------------- #
# A wrapper for stopping the spinner, this simply kills off the process. #
# -------------------------------------------------------------------------------- #
function stop_spinner()
{
if [[ "${SPIN_PID}" -gt 0 ]]; then
kill -9 $SPIN_PID > /dev/null 2>&1;
fi
SPIN_PID=0
printf '\033[2K'
}
# -------------------------------------------------------------------------------- #
# End of Script #
# -------------------------------------------------------------------------------- #
# This is the end - nothing more to see here. #
# -------------------------------------------------------------------------------- #