-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo-progress-bar
executable file
·43 lines (39 loc) · 1.02 KB
/
echo-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
36
37
38
39
40
41
42
43
#!/bin/bash
currentValue=$1
maxValue=$2
outputWidth=$3
progress=$((100*$currentValue/$maxValue))
# at the end of the progress bar there is the percentange
# 1 space + % sign (=2)
outputWidth=$(($outputWidth-2))
# substract the width of percentange
if [ $progress -ge 0 -a $progress -lt 10 ]; then
outputWidth=$(($outputWidth-1))
elif [ $progress -ge 10 -a $progress -lt 100 ]; then
outputWidth=$(($outputWidth-2))
elif [ $progress -ge 100 -a $progress -lt 999 ]; then
outputWidth=$(($outputWidth-3))
else
echo "progress invalid"
exit 2
fi
# [] must also be substracted von outputWidth
outputWidth=$(($outputWidth-3))
# TODO why 3?
# fix zero
filledChars=$(( $outputWidth*$progress/100-1 ))
emptyChars=$(( $outputWidth-$filledChars ))
echo -n "["
if [ $filledChars -gt 0 ]; then
printf '=%.0s' $(seq 1 $filledChars)
fi
echo -n ">"
# clear rest of line
echo -ne "\033[K"
# move cursor forward by $emptyChars
echo -ne "$(printf "\033[%dC" $emptyChars)"
echo -n "] $progress%"
if [ "$4" != "-n" ]; then
echo
fi
exit 0