├── LICENSE ├── README.md ├── main.sh ├── progress.sh ├── progress_advanced.sh └── spinner.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Leandro Nunes 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.md: -------------------------------------------------------------------------------- 1 | # bash-progress-indicator 2 | Pure Bash script progress indicators 3 | 4 | ## Usage 5 | ``` 6 | chmod +x main.sh 7 | ./main.sh {progress_indicator_type} 8 | ``` 9 | 10 | ## progress_indicator_type 11 | #### progress 12 | ![type_progress](https://user-images.githubusercontent.com/2450417/37887129-6a7fafec-3096-11e8-8bc0-7768e958119e.gif) 13 | 14 | #### progress_advanced 15 | ![type_progress_advanced](https://user-images.githubusercontent.com/2450417/37887130-6ddce736-3096-11e8-9696-25b86becbce3.gif) 16 | 17 | #### spinner1 18 | ![type_spinner1](https://user-images.githubusercontent.com/2450417/37887236-0e8ccf0c-3097-11e8-9c6d-c6f4fc60b712.gif) 19 | #### spinner2 20 | ![type_spinner2](https://user-images.githubusercontent.com/2450417/37887241-12e1c814-3097-11e8-91b2-e43e96ff8cd0.gif) 21 | #### spinner3 22 | ![type_spinner3](https://user-images.githubusercontent.com/2450417/37887242-13012e98-3097-11e8-80cb-ebca61da3109.gif) 23 | 24 | ## I wrote an article about this technique [pt-br] 25 | [Medium - [Bash] Construindo indicador de progresso](https://medium.com/meninunes/bash-construindo-indicador-de-progresso-8de94c37683) 26 | 27 | ## License 28 | [MIT License](LICENSE) © [Leandro Nunes](https://lnfnunes.com.br) 29 | -------------------------------------------------------------------------------- /main.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # shellcheck disable=SC1091 3 | 4 | declare -rx STEPS=( 5 | 'pre-install' 6 | 'install' 7 | 'post-install' 8 | ) 9 | declare -rx CMDS=( 10 | 'sleep 1' 11 | 'sleep 1' 12 | 'sleep 1' 13 | ) 14 | 15 | case $1 in 16 | progress) 17 | . 'progress.sh' 18 | ;; 19 | progress_advanced) 20 | . 'progress_advanced.sh' 21 | ;; 22 | spinner*) 23 | . 'spinner.sh' 24 | ;; 25 | *) 26 | echo "Invalid option $1!" 27 | exit 1 28 | esac 29 | 30 | start 31 | -------------------------------------------------------------------------------- /progress.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | declare -rx BAR_SIZE="##########" 4 | declare -rx CLEAR_LINE="\\033[K" 5 | 6 | start() { 7 | local MAX_STEPS=${#STEPS[@]} 8 | local MAX_BAR_SIZE="${#BAR_SIZE}" 9 | 10 | tput civis -- invisible 11 | 12 | echo -ne "\\r[${BAR_SIZE:0:0}] 0 %$CLEAR_LINE" 13 | for step in "${!STEPS[@]}"; do 14 | ${CMDS[$step]} 15 | 16 | perc=$(((step + 1) * 100 / MAX_STEPS)) 17 | percBar=$((perc * MAX_BAR_SIZE / 100)) 18 | echo -ne "\\r[${BAR_SIZE:0:percBar}] $perc %$CLEAR_LINE" 19 | done 20 | echo "" 21 | 22 | tput cnorm -- normal 23 | } 24 | -------------------------------------------------------------------------------- /progress_advanced.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | start() { 4 | local MAX_STEPS=${#STEPS[@]} 5 | local BAR_SIZE="##########" 6 | local MAX_BAR_SIZE="${#BAR_SIZE}" 7 | local CLEAR_LINE="\\033[K" 8 | 9 | tput civis -- invisible 10 | 11 | for step in "${!STEPS[@]}"; do 12 | perc=$((step * 100 / MAX_STEPS)) 13 | percBar=$((perc * MAX_BAR_SIZE / 100)) 14 | echo -ne "\\r- ${STEPS[step]} [ ]$CLEAR_LINE\\n" 15 | echo -ne "\\r[${BAR_SIZE:0:percBar}] $perc %$CLEAR_LINE" 16 | 17 | ${CMDS[$step]} 18 | 19 | perc=$(((step + 1) * 100 / MAX_STEPS)) 20 | percBar=$((perc * MAX_BAR_SIZE / 100)) 21 | echo -ne "\\r\\033[1A- ${STEPS[step]} [✔]$CLEAR_LINE\\n" 22 | echo -ne "\\r[${BAR_SIZE:0:percBar}] $perc %$CLEAR_LINE" 23 | done 24 | echo "" 25 | 26 | tput cnorm -- normal 27 | } 28 | -------------------------------------------------------------------------------- /spinner.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | declare -x FRAME 4 | declare -x FRAME_INTERVAL 5 | 6 | set_spinner() { 7 | case $1 in 8 | spinner1) 9 | FRAME=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏") 10 | FRAME_INTERVAL=0.1 11 | ;; 12 | spinner2) 13 | FRAME=("-" "\\" "|" "/") 14 | FRAME_INTERVAL=0.25 15 | ;; 16 | spinner3) 17 | FRAME=("◐" "◓" "◑" "◒") 18 | FRAME_INTERVAL=0.5 19 | ;; 20 | spinner4) 21 | FRAME=(":(" ":|" ":)" ":D") 22 | FRAME_INTERVAL=0.5 23 | ;; 24 | spinner5) 25 | FRAME=("◇" "◈" "◆") 26 | FRAME_INTERVAL=0.5 27 | ;; 28 | spinner6) 29 | FRAME=("⚬" "⚭" "⚮" "⚯") 30 | FRAME_INTERVAL=0.25 31 | ;; 32 | spinner7) 33 | FRAME=("░" "▒" "▓" "█" "▓" "▒") 34 | FRAME_INTERVAL=0.25 35 | ;; 36 | spinner8) 37 | FRAME=("☉" "◎" "◉" "●" "◉") 38 | FRAME_INTERVAL=0.1 39 | ;; 40 | spinner9) 41 | FRAME=("❤" "♥" "♡") 42 | FRAME_INTERVAL=0.15 43 | ;; 44 | spinner10) 45 | FRAME=("✧" "☆" "★" "✪" "◌" "✲") 46 | FRAME_INTERVAL=0.1 47 | ;; 48 | spinner11) 49 | FRAME=("●" "◕" "☯" "◔" "◕") 50 | FRAME_INTERVAL=0.25 51 | ;; 52 | *) 53 | echo "No spinner is defined for $1" 54 | exit 1 55 | esac 56 | } 57 | 58 | start() { 59 | local step=0 60 | 61 | tput civis -- invisible 62 | 63 | while [ "$step" -lt "${#CMDS[@]}" ]; do 64 | ${CMDS[$step]} & pid=$! 65 | 66 | while ps -p $pid &>/dev/null; do 67 | echo -ne "\\r[ ] ${STEPS[$step]} ..." 68 | 69 | for k in "${!FRAME[@]}"; do 70 | echo -ne "\\r[ ${FRAME[k]} ]" 71 | sleep $FRAME_INTERVAL 72 | done 73 | done 74 | 75 | echo -ne "\\r[ ✔ ] ${STEPS[$step]}\\n" 76 | step=$((step + 1)) 77 | done 78 | 79 | tput cnorm -- normal 80 | } 81 | 82 | set_spinner "$1" 83 | --------------------------------------------------------------------------------