forked from m-sroka/bash-progress-bar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressbar
More file actions
executable file
·89 lines (74 loc) · 1.88 KB
/
progressbar
File metadata and controls
executable file
·89 lines (74 loc) · 1.88 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
getColor() {
case "$1" in
black) color="0;30m";;
red) color="1;31m";;
green) color="1;32m";;
blue) color="1;34m";;
brown) color="0;33m";;
yellow) color="1;33m";;
white) color="1;37m";;
esac
echo "\033[$color"
}
printHelp() {
echo
echo 'usage: progress-bar [VALUE], VALUE percentage value, e.g. 10%, 50%, 100%, 110%'
echo
echo ' -h help'
echo
echo ' -l long output - allow more than 100%'
echo ' -s [SIZE], SIZE number of ■ showed when value is 100 (default: 10)'
echo ' -w [CHARACTER], CHARACTER characted that is being shown as progress bar tile (default: ■)'
echo ' -a append numeric value after progress bar'
echo ' -c [COLOR] black|red|green|blue|brown|yellow|white'
}
# DEFAULT CONFIG PARAMETERS
color=
allow_more_than_max=false
character=■
granularity=10
append_numeric_after_progressbar=false
# PARSE ARGS
while getopts 'als:c:w:h' OPT ${@:2}; do
case "$OPT" in
"l") allow_more_than_max=true ;;
"s") granularity=$OPTARG ;;
"w") character=$OPTARG ;;
"c") color=$(getColor $OPTARG) ;;
"a") append_numeric_after_progressbar=true ;;
"h") printHelp; exit 1 ;;
"?") printHelp >&2; exit 1 ;;
esac
done
if [ -p /dev/stdin ]; then
value=$(cat /dev/stdin | tr -d % | tr -d '\n')
else
value=$(echo $1 | tr -d %)
fi
let step_size=100/$granularity
let num_of_progress_items=$value/$step_size
if [ "$color" ]
then
printf $color
fi
printf '|'
for i in $(seq 0 $granularity)
do
[[ $i -le $num_of_progress_items ]] && printf "$character" || printf ' '
done
printf '|'
if [ "$allow_more_than_max" = true ] && [ $num_of_progress_items -gt $granularity ]
then
let num_of_excess_items=$num_of_progress_items-$granularity
for i in $(seq 1 $num_of_excess_items)
do
printf $character
done
fi
if $append_numeric_after_progressbar
then
printf ' %d%%' $value
fi
eval "tput sgr0" # reset color
echo