-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressBar.sh
More file actions
executable file
·212 lines (196 loc) · 5.61 KB
/
Copy pathProgressBar.sh
File metadata and controls
executable file
·212 lines (196 loc) · 5.61 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env bash
#/**
# * @link https://github.com/NRZCode/ProgressBar
# * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU GENERAL PUBLIC LICENSE
# * @author NRZ Code <nrzcode@protonmail.com>
# */
version=0.0.1
usage() {
printf "${*:+$*\n} %s\n%22s%s\n" \
'Usage: ProgressBar.sh [-i|--initial NUM] [-m|--message MESSAGE] [-p|--pid PID] [-s|--speed SPEED]' \
'' '[-t|--total NUM] [-w|--width NUM]'
cat <<EOM
Options:
General Options:
-i, --initial Initial value percentage
(default: 0)
-h, --help Print this help text and exit
-m, --message Set initial progress text message
(default: Progress)
-p, --pid Monitora a execução do processo do pid passado encerrando
a barra de progresso junto com o encerramento do processo
-s, --speed Speed bar progress incremental value.
accepts one of values [fast|normal|slow|slowest|zero]
(default: slow)
-t, --total Total value percentage
(default: 100)
-w, --width Set progressbar's width
EOM
}
ProgressBar.init() {
in_array() {
local needle haystack
printf -v haystack '%s|' "${@:2}"
[[ $1 == @(${haystack%|}) ]]
}
check_arg_type() {
# valor de um parâmetro (arg: 3) não pode começar com - 'hifen'
if [[ -z $3 || "$3" =~ ^- ]]; then
echo "Opção $2 requer parâmetro." >&2
return 1
fi
case $1 in
bool) re='^(on|off|true|false|1|0)$';;
string) re='^[[:print:]]+$';;
int) re='^[-+]?[[:digit:]]+$';;
float) re='^[-+]?[0-9]+([.,][0-9]+)?$';;
esac
[[ ${3,,} =~ $re ]]
}
cleanup() {
printf '\r\e[KConcluído [100%%]\n'
tput cnorm
rm -fr $tmp
}
trap cleanup EXIT INT KILL
read_stdin() {
read -t .001
[[ $? == 1 ]] && return 1
[[ $REPLY ]] && ProgressBar.setProgress "$REPLY"
return 0
}
##
# ProgressBar
#
# Progress: [ 67%] [#####################................] [ETA 2m37s]
#
ProgressBar.print() {
local partial=$1 \
total=${2:-100} \
msg=${3:-Progress} \
cols offset p percento pad bar_on bar_off
cols=${width:-$COLUMNS}
p=$((partial*100/total))
percento=$((p > 100 ? 100 : p))
offset=$((cols-${#msg}-13))
printf -v pad '%*s' $((percento*offset/100))
bar_on=${pad// /$bar_char_on}
printf -v pad '%*s' $((offset-${#bar_on}))
bar_off=${pad// /$bar_char_off}
printf "$bar_format\r" "$msg" $percento "$4" $bar_on $bar_off
}
ProgressBar.run() {
local msg
declare -i nivel=${initial:-0}
pidmain=${pidmain:-$!}
pidmain=${pidmain:-1}
total=${total:-100}
forward=${forward:-$forward_default}
tput civis
while :; do
ps -p $pidmain > /dev/null || { cleanup; break; }
if [[ $forward != 'zero' ]]; then
nivel=$((++i % forward ? nivel : nivel+1))
fi
read_stdin || break
read -t .001 -u $fdprogress str
if [[ ${str%% *} =~ ^[0-9]+$ ]]; then
nivel=${str%% *}
str="${str#* }"
fi
msg="${str:-$msg}"
((i % 7)) || s=$((s < ${#spinner_chars}-1 ? ++s : 0))
ProgressBar.print "$nivel" "$total" "$msg" "${spinner_chars:s:1}"
done
}
ProgressBar.setProgress() {
[ $# -gt 0 ] && echo $@ >&${fdprogress}
}
export -f ProgressBar.setProgress
shopt -s extglob
tmp=$(mktemp -d)
pipeprogress=$(mktemp -u --tmpdir=$tmp)
mkfifo $pipeprogress
exec {fdprogress}<>$pipeprogress
export fdprogress
bar_char_on='#'
bar_char_off='.'
bar_text_fgcolor='\e[30m'
bar_text_bgcolor='\e[42m'
bar_progress_fgcolor='\e[1;37m'
bar_progress_bgcolor=''
color_reset='\e[m'
# Progress: [ 67%] ⣾ [#####################................] [ETA 2m37s]
# bar_fmt="${text_description}${percentage}${spinner}${progress}${eta}"
bar_format="${bar_text_fgcolor}${bar_text_bgcolor}%s: [%3d%%]${color_reset} %s ${bar_progress_fgcolor}${bar_progress_bgcolor}[%s%s]${color_reset}"
forward_default=1000
declare -A speed=(
[fast]=20
[normal]=200
[slow]=1000
[slowest]=2000
[zero]=zero
)
while [[ $1 ]]; do
case $1 in
-i|--initial)
check_arg_type string $1 $2 || { usage; return 1; }
initial=$2
shift 2
;;
-h|--help|help|-\?)
usage; return;
;;
-m|--message)
ProgressBar.setProgress "$2"
shift 2
;;
-p|--pid)
pidmain=$OPTARG
;;
-s|--speed)
check_arg_type string $1 $2 || { usage; return 1; }
if in_array $2 ${!speed[@]}; then
forward=${speed[$2]}
fi
shift 2
;;
-t|--total)
check_arg_type string $1 $2 || { usage; return 1; }
total=$2
shift 2
;;
-w|--width)
width=$2
shift 2
;;
-z)
forward=zero
shift
;;
*)
if [[ $1 =~ ^- ]]; then
ProgressBar.usage "Opção $1 desconhecida (?)"; return 1;
fi
args[${#args[@]}]=$1
shift
esac
done
if [ ${#args[@]} -gt 0 ]; then
if [ -f "$args" ]; then
bash "$args" &>${fdprogress} &
else
bash -c "${args[*]}" &>${fdprogress} &
fi
fi
}
ProgressBar.main() {
ProgressBar.init "$@"
ProgressBar.run
}
script=$(realpath $BASH_SOURCE)
dirname=${script%/*}
spinner_chars='⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈'
[[ -f $PROGRESSBARRC ]] && source "$PROGRESSBARRC"
[[ -r "$HOME/.progressbarrc" ]] && source "$HOME/.progressbarrc"
[[ $BASH_SOURCE == $0 ]] && ProgressBar.main "$@"