-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogreshbar
executable file
·61 lines (50 loc) · 1.35 KB
/
progreshbar
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
#!/bin/bash
usage(){
echo '
Usage:
$0 [-p] [--no-trim] iteration_number total [item_name]
Example:
list=$(find .)
list_count=$(echo "$list"|wc -l)
i=1
for l in $list; do
progreshbar -p $i $list_count $l
((i++))
done
'
exit 1
}
template='[ ${progress_bar_prefix}=>${progress_bar_suffix}] ${progress_bar_percentage} $iteration / $total $item'
# Export templated values
export progress_bar_prefix
export progress_bar_suffix
export progress_bar_percentage
export iteration
export total
export item
percentage=false
[ "$1" == "-p" ] && { percentage=true; shift 1; }
trim=true
[ "$1" == "--no-trim" ] && { trim=false; shift 1; }
iteration=$1
total=$2
item=$3
progress_bar_num=$iteration
progress_bar_max=$total
[ "$iteration" ] || usage
[ "$total" ] || usage
if [ "$percentage" == "true" ]; then
progress_bar_num=$(((100*$iteration)/$total))
progress_bar_max=100
export progress_bar_percentage="${progress_bar_num}%"
fi
progress_bar_rest=$(($progress_bar_max-$progress_bar_num))
progress_bar_prefix=$(printf %${progress_bar_num}s)
progress_bar_suffix=$(printf %${progress_bar_rest}s)
if [ "$trim" == "true" ]; then
num=$(( $(tput cols) - $(echo -n "$template" | envsubst | wc -c) - 1 ))
item=${item:0:$num}
fi
echo -n $'\r\033[K'
echo -n "$template" | envsubst
[ $iteration == $total ] && echo ""