├── LICENSE ├── README ├── spinner.sh └── test.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Tasos Latsas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | spinner.sh 2 | 3 | Display an awesome 'spinner' while running your long shell commands 4 | 5 | Do *NOT* call _spinner function directly. 6 | Use {start,stop}_spinner wrapper functions 7 | 8 | usage: 9 | 1. source this script in your's 10 | 2. start the spinner: 11 | start_spinner [display-message-here] 12 | 3. run your command 13 | 4. stop the spinner: 14 | stop_spinner [your command's exit status] 15 | 16 | Also see: test.sh 17 | -------------------------------------------------------------------------------- /spinner.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Author: Tasos Latsas 4 | 5 | # spinner.sh 6 | # 7 | # Display an awesome 'spinner' while running your long shell commands 8 | # 9 | # Do *NOT* call _spinner function directly. 10 | # Use {start,stop}_spinner wrapper functions 11 | 12 | # usage: 13 | # 1. source this script in your's 14 | # 2. start the spinner: 15 | # start_spinner [display-message-here] 16 | # 3. run your command 17 | # 4. stop the spinner: 18 | # stop_spinner [your command's exit status] 19 | # 20 | # Also see: test.sh 21 | 22 | 23 | function _spinner() { 24 | # $1 start/stop 25 | # 26 | # on start: $2 display message 27 | # on stop : $2 process exit status 28 | # $3 spinner function pid (supplied from stop_spinner) 29 | 30 | local on_success="DONE" 31 | local on_fail="FAIL" 32 | local white="\e[1;37m" 33 | local green="\e[1;32m" 34 | local red="\e[1;31m" 35 | local nc="\e[0m" 36 | 37 | case $1 in 38 | start) 39 | # calculate the column where spinner and status msg will be displayed 40 | let column=$(tput cols)-${#2}-8 41 | # display message and position the cursor in $column column 42 | echo -ne ${2} 43 | printf "%${column}s" 44 | 45 | # start spinner 46 | i=1 47 | sp='\|/-' 48 | delay=${SPINNER_DELAY:-0.15} 49 | 50 | while : 51 | do 52 | printf "\b${sp:i++%${#sp}:1}" 53 | sleep $delay 54 | done 55 | ;; 56 | stop) 57 | if [[ -z ${3} ]]; then 58 | echo "spinner is not running.." 59 | exit 1 60 | fi 61 | 62 | kill $3 > /dev/null 2>&1 63 | 64 | # inform the user uppon success or failure 65 | echo -en "\b[" 66 | if [[ $2 -eq 0 ]]; then 67 | echo -en "${green}${on_success}${nc}" 68 | else 69 | echo -en "${red}${on_fail}${nc}" 70 | fi 71 | echo -e "]" 72 | ;; 73 | *) 74 | echo "invalid argument, try {start/stop}" 75 | exit 1 76 | ;; 77 | esac 78 | } 79 | 80 | function start_spinner { 81 | # $1 : msg to display 82 | _spinner "start" "${1}" & 83 | # set global spinner pid 84 | _sp_pid=$! 85 | disown 86 | } 87 | 88 | function stop_spinner { 89 | # $1 : command exit status 90 | _spinner "stop" $1 $_sp_pid 91 | unset _sp_pid 92 | } 93 | 94 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source "$(pwd)/spinner.sh" 4 | 5 | # test success 6 | start_spinner 'sleeping for 2 secs...' 7 | sleep 2 8 | stop_spinner $? 9 | 10 | # test fail 11 | start_spinner 'copying non-existen files...' 12 | # use sleep to give spinner time to fork and run 13 | # because cp fails instantly 14 | sleep 1 15 | cp 'file1' 'file2' > /dev/null 2>&1 16 | stop_spinner $? 17 | 18 | --------------------------------------------------------------------------------