-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress-bar
35 lines (29 loc) · 1.05 KB
/
progress-bar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env bash
SLEEP_DURATION=${SLEEP_DURATION:=1} # default to 1 second, use to speed up tests
progress-bar() {
local duration
local columns
local space_available
local fit_to_screen
local space_reserved
space_reserved=6 # reserved width for the percentage value
duration=${1}
columns=$(tput cols)
space_available=$(( columns-space_reserved ))
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"; }
for (( elapsed=1; elapsed<=duration; elapsed=elapsed+1 )); do
already_done; remaining; percentage
sleep "$SLEEP_DURATION"
clean_line
done
clean_line
}