-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# spinner.sh | ||
|
||
Display an awesome 'spinner' while running your long shell commands. | ||
|
||
Do *NOT* call `_spinner` function directly. Use `{start,stop}_spinner` wrapper functions | ||
|
||
## Usage | ||
|
||
1. source this script in your's | ||
2. start the spinner: `start_spinner [display-message-here]` | ||
3. run your command | ||
4. stop the spinner: `stop_spinner [your command's exit status]` | ||
|
||
Also see: `test.sh` | ||
|
||
## Source | ||
|
||
https://github.com/tlatsas/bash-spinner and https://github.com/tlatsas/bash-spinner/pull/3/files |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/bin/bash | ||
|
||
# Author: Tasos Latsas | ||
|
||
# spinner.sh | ||
# | ||
# Display an awesome 'spinner' while running your long shell commands | ||
# | ||
# Do *NOT* call _spinner function directly. | ||
# Use {start,stop}_spinner wrapper functions | ||
|
||
# usage: | ||
# 1. source this script in your's | ||
# 2. start the spinner: | ||
# start_spinner [display-message-here] | ||
# 3. run your command | ||
# 4. stop the spinner: | ||
# stop_spinner [your command's exit status] | ||
# | ||
# Also see: test.sh | ||
|
||
|
||
function _spinner() { | ||
# $1 start/stop | ||
# | ||
# on start: $2 display message | ||
# on stop : $2 process exit status | ||
# $3 spinner function pid (supplied from stop_spinner) | ||
|
||
local on_success="DONE" | ||
local on_fail="FAIL" | ||
local white="\e[1;37m" | ||
local green="\e[1;32m" | ||
local red="\e[1;31m" | ||
local nc="\e[0m" | ||
|
||
case $1 in | ||
start) | ||
# calculate the column where spinner and status msg will be displayed | ||
let column=$(tput cols)-${#2}-8 | ||
# display message and position the cursor in $column column | ||
printf "${2}" | ||
printf "%${column}s" | ||
|
||
# start spinner | ||
i=1 | ||
sp='\|/-' | ||
delay=${SPINNER_DELAY:-0.15} | ||
|
||
while : | ||
do | ||
printf "\b${sp:$((i++%${#sp})):1}" | ||
sleep $delay | ||
done | ||
;; | ||
stop) | ||
if [[ -z ${3} ]]; then | ||
printf "spinner is not running..\n" | ||
exit 1 | ||
fi | ||
|
||
kill $3 > /dev/null 2>&1 | ||
|
||
# inform the user uppon success or failure | ||
printf "\b[" | ||
if [[ $2 -eq 0 ]]; then | ||
printf "${green}${on_success}${nc}" | ||
else | ||
printf "${red}${on_fail}${nc}" | ||
fi | ||
printf "]\n" | ||
;; | ||
*) | ||
printf "invalid argument, try {start/stop}\n" | ||
exit 1 | ||
;; | ||
esac | ||
} | ||
|
||
function start_spinner { | ||
# $1 : msg to display | ||
_spinner "start" "${1}" & | ||
# set global spinner pid | ||
_sp_pid=$! | ||
disown | ||
} | ||
|
||
function stop_spinner { | ||
# $1 : command exit status | ||
_spinner "stop" $1 $_sp_pid | ||
unset _sp_pid | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/bash | ||
|
||
source "$(pwd)/spinner.sh" | ||
|
||
# test success | ||
start_spinner 'sleeping for 2 secs...' | ||
sleep 2 | ||
stop_spinner $? | ||
|
||
# test fail | ||
start_spinner 'copying non-existen files...' | ||
# use sleep to give spinner time to fork and run | ||
# because cp fails instantly | ||
sleep 1 | ||
cp 'file1' 'file2' > /dev/null 2>&1 | ||
stop_spinner $? | ||
|