diff --git a/progress-bar.sh b/progress-bar.sh index fb4c75e..db21ad4 100755 --- a/progress-bar.sh +++ b/progress-bar.sh @@ -1,6 +1,9 @@ #!/usr/bin/env bash -SLEEP_DURATION=${SLEEP_DURATION:=1} # default to 1 second, use to speed up tests +: "${SLEEP_DURATION:=1}" # default to 1 second, use to speed up tests + +: "${BAR_FILL:=▇}" # Make sure both BAR_FILL and BAR_EMPTY have +: "${BAR_EMPTY:= }" # the same number of characters. progress-bar() { local duration @@ -13,18 +16,12 @@ progress-bar() { duration=${1} columns=$(tput cols) space_available=$(( columns-space_reserved )) + fit_to_screen=$(( (duration / space_available) + 1 )) - if (( duration < space_available )); then - fit_to_screen=1; - else - fit_to_screen=$(( duration / space_available )); - fit_to_screen=$((fit_to_screen+1)); - fi - - already_done() { for ((done=0; done<(elapsed / fit_to_screen) ; done=done+1 )); do printf "▇"; done } - remaining() { for (( remain=(elapsed/fit_to_screen) ; remain<(duration/fit_to_screen) ; remain=remain+1 )); do printf " "; done } - percentage() { printf "| %s%%" $(( ((elapsed)*100)/(duration)*100/100 )); } - clean_line() { printf "\r"; } + already_done() { for ((done=0; done<(elapsed / fit_to_screen) ; done=done+1 )); do printf '%b' "$BAR_FILL"; done } + remaining() { for (( remain=(elapsed/fit_to_screen) ; remain<(duration/fit_to_screen) ; remain=remain+1 )); do printf '%b' "$BAR_EMPTY"; done } + percentage() { printf "| %s%%" $(( (elapsed*100)/duration )); } + clean_line() { printf "\r\e[K"; } for (( elapsed=1; elapsed<=duration; elapsed=elapsed+1 )); do already_done; remaining; percentage