Created
March 15, 2021 02:36
-
-
Save rickd-uk/efbc0694e10365713241acaa01439053 to your computer and use it in GitHub Desktop.
BASH - SPINNER 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
**ONE MORE SPINNER** | |
DEFAULT_SpinnerFrames=("—" "\\" "|" "/") | |
##@function: spinner(action, label, &spinnerFramesRef[]) | |
## | |
##@description: Perform an action asynchronously and display | |
##spinner till action is completed | |
## | |
##@param action: The action the execute | |
##@param label: The label to display while waiting | |
##@param spinnerRef: In case you feel like a custom spinner, pass a ref to an array of strings | |
spinner() { | |
local frameRef | |
local action="${1}" | |
local label="${2} " | |
local spinnerRef="${3-DEFAULT_SpinnerFrames}" | |
local spinnerFrames=$(eval "echo \${!${spinnerRef}[@]}") | |
spinnerRun() { | |
while true; do | |
for frame in ${spinnerFrames[@]}; do | |
frameRef="${spinnerRef}[${frame}]" | |
echo "${label}${!frameRef}" | |
tput cuu1 tput el | |
sleep 0.2 | |
done | |
done | |
echo -e "\r" | |
} | |
spinnerRun & | |
local spinnerPid=$! | |
${action} | |
kill "${spinnerPid}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment