forked from pollev/bash_progress_bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress_bar.sh
309 lines (262 loc) · 9.32 KB
/
progress_bar.sh
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/bin/bash
# https://github.com/pollev/bash_progress_bar - See license at end of file
# Constants
CODE_SAVE_CURSOR="\033[s"
CODE_RESTORE_CURSOR="\033[u"
CODE_CURSOR_IN_SCROLL_AREA="\033[1A"
COLOR_FG="\e[30m"
COLOR_BG="\e[42m"
COLOR_BG_BLOCKED="\e[43m"
RESTORE_FG="\e[39m"
RESTORE_BG="\e[49m"
# Initialize internal used variables
function _load_variables() {
PROGRESS_BLOCKED="false"
TRAPPING_ENABLED="false"
ETA_ENABLED="false"
TRAP_SET="false"
PERCENT_SCALE=2
CURRENT_NR_LINES=0
PROGRESS_TITLE="Progress"
PROGRESS_TOTAL=100
PROGRESS_START=0
BLOCKED_START=0
}
# shellcheck disable=SC2120
setup_scroll_area() {
# If trapping is enabled, we will want to activate it whenever we setup the scroll area and remove it when we break the scroll area
if [ "$TRAPPING_ENABLED" = "true" ]; then
trap_on_interrupt
fi
# Handle first parameter: alternative progress bar title
[ -n "$1" ] && PROGRESS_TITLE=$1
# Handle second parameter : alternative total count
[ -n "$2" ] && PROGRESS_TOTAL=$2
lines=$(tput lines)
CURRENT_NR_LINES=$lines
lines=$((lines - 1))
# Scroll down a bit to avoid visual glitch when the screen area shrinks by one row
echo -en "\n"
# Save cursor
echo -en "$CODE_SAVE_CURSOR"
# Set scroll region (this will place the cursor in the top left)
echo -en "\033[0;${lines}r"
# Restore cursor but ensure its inside the scrolling area
echo -en "$CODE_RESTORE_CURSOR"
echo -en "$CODE_CURSOR_IN_SCROLL_AREA"
# Store start timestamp to compute ETA
if [ "$ETA_ENABLED" = "true" ]; then
# use bash variable $SECONDS to count time
PROGRESS_START=$SECONDS
fi
# Start empty progress bar
draw_progress_bar "$_current_progress"
}
function create_progress_bar() {
local _short='N:tp:'
local _longs='trap,eta,size:,precision:'
local _parsed
_parsed=$(getopt --options=$_short --longoptions=$_longs --name "$0" -- "$@")
eval set -- "$_parsed"
_load_variables
declare -i _num_total_tasks
while true; do
case $1 in
-N | --size) _num_total_tasks=$2 && shift ;;
-p | --precision) PERCENT_SCALE=$2 && shift ;;
-t | --eta) ETA_ENABLED=true ;;
--trap) TRAPPING_ENABLED=true ;;
--)
shift && break # break the while loop
;;
esac
shift
done
local _progress_bar_title="$1"
setup_scroll_area "$_progress_bar_title" "$_num_total_tasks" 1>&2
}
destroy_scroll_area() {
lines=$(tput lines)
# Save cursor
echo -en "$CODE_SAVE_CURSOR" 1>&2
# Set scroll region (this will place the cursor in the top left)
echo -en "\033[0;${lines}r" 1>&2
# Restore cursor but ensure its inside the scrolling area
echo -en "$CODE_RESTORE_CURSOR" 1>&2
echo -en "$CODE_CURSOR_IN_SCROLL_AREA" 1>&2
# We are done so clear the scroll bar
clear_progress_bar 1>&2
# Scroll down a bit to avoid visual glitch when the screen area grows by one row
echo -en "\n\n" 1>&2
# Once the scroll area is cleared, we want to remove any trap previously set. Otherwise, ctrl+c will exit our shell
if [ "$TRAP_SET" = "true" ]; then
trap - EXIT
fi
}
format_eta() {
local T=$1
local D=$((T / 60 / 60 / 24))
local H=$((T / 60 / 60 % 24))
local M=$((T / 60 % 60))
local S=$((T % 60))
[[ $D -eq 0 && $H -eq 0 && $M -eq 0 && $S -eq 0 ]] && echo "--:--:--" && return
[ $D -gt 0 ] && printf '%d days, ' $D
printf 'ETA: %d:%02.f:%02.f' $H $M $S
}
function _float_divide() {
local x=$1 y=$2 res_var=${3:-'_float_res'} scale=${PERCENT_SCALE:-2} int_scale
#! integer division with x smaller than y, output is 0, but will return status code of 1.
#* we will ignore this error.
((int_scale = 10 * 10 ** scale, a = x / y, b = (x % y) * int_scale / y)) && true
printf -v "$res_var" "%.${scale}f" "$a.$b"
}
function _percent() {
local _float_res # _float_res is updated by _float_divide()
_float_divide "$(($1 * 100))" "$2"
local res_var=${3:-'_percent_res'}
printf -v "$res_var" '%s%%' "$_float_res"
}
draw_progress_bar() {
#* if arg1 is empty, set `_current_progress` to 0
#* `_current_progress` will be refered if we resizing the terminal.
_current_progress=${1:-'0'}
eta=""
if [[ "$ETA_ENABLED" = "true" && "$_current_progress" -gt 0 ]]; then
# use bash variable $SECONDS to count time
if [ "$PROGRESS_BLOCKED" = "true" ]; then
blocked_duration=$((SECONDS - BLOCKED_START))
PROGRESS_START=$((PROGRESS_START + blocked_duration))
fi
running_time=$((SECONDS - PROGRESS_START))
total_time=$((PROGRESS_TOTAL * running_time / _current_progress))
eta=$(format_eta $((total_time - running_time)))
fi
# compute percentage as
_percent "$_current_progress" "$PROGRESS_TOTAL" percentage
extra=$2 # supplying extra message showing in progress bar
lines=$(tput lines)
lines=$((lines))
# If the window has been resized, reset the scroll area
#* Since this is an internal update, we do not provide the title and total
#* number parameter for the setup function. Hence, the corresponding
#* title and progress information remain consistent before/after resizing.
if [ "$lines" -ne "$CURRENT_NR_LINES" ]; then
setup_scroll_area 1>&2
fi
# Save cursor
echo -en "$CODE_SAVE_CURSOR" 1>&2
# Move cursor position to last row
echo -en "\033[${lines};0f" 1>&2
# Clear progress bar
tput el 1>&2
# Draw progress bar
PROGRESS_BLOCKED="false"
print_bar_text "$percentage" "$extra" "$eta" 1>&2
# Restore cursor position
echo -en "$CODE_RESTORE_CURSOR" 1>&2
}
block_progress_bar() {
percentage=$1
lines=$(tput lines)
lines=$((lines))
# Save cursor
echo -en "$CODE_SAVE_CURSOR" 1>&2
# Move cursor position to last row
echo -en "\033[${lines};0f" 1>&2
# Clear progress bar
tput el 1>&2
# Draw progress bar
PROGRESS_BLOCKED="true"
# use bash variable $SECONDS to count time
BLOCKED_START=$SECONDS
print_bar_text "$percentage" 1>&2
# Restore cursor position
echo -en "$CODE_RESTORE_CURSOR" 1>&2
}
clear_progress_bar() {
lines=$(tput lines)
lines=$((lines))
# Save cursor
echo -en "$CODE_SAVE_CURSOR"
# Move cursor position to last row
echo -en "\033[${lines};0f"
# clear progress bar
tput el
# Restore cursor position
echo -en "$CODE_RESTORE_CURSOR"
}
print_bar_text() {
local percentage=$1
local extra=$2
[ -n "$extra" ] && extra=" ($extra)"
local eta=$3
if [ -n "$eta" ]; then
[ -n "$extra" ] && extra="$extra "
extra="$extra$eta"
fi
local cols
cols=$(tput cols)
# hard coded width includes space between fields (3), progress delimiter "[", "]"
# we intentionally allocated two more columns in case the ETA hour field overflow
bar_size=$((cols - ${#percentage} - ${#PROGRESS_TITLE} - ${#extra} - 7))
local color="${COLOR_FG}${COLOR_BG}"
if [ "$PROGRESS_BLOCKED" = "true" ]; then
color="${COLOR_FG}${COLOR_BG_BLOCKED}"
fi
# Prepare progress bar
local _n_percent_minor_digits=$((PERCENT_SCALE + 2)) # decimal mark, digits + "%"
local _progress=${percentage[*]::-${_n_percent_minor_digits}} #* quote does not allow after '::'
complete_size=$(((bar_size * _progress) / 100))
remainder_size=$((bar_size - complete_size))
progress_bar=$(
echo -ne "["
echo -en "${color}"
printf_new "#" $complete_size
echo -en "${RESTORE_FG}${RESTORE_BG}"
printf_new "." $remainder_size
echo -ne "]"
)
# Print progress bar
echo -ne "$PROGRESS_TITLE ${percentage} ${progress_bar} ${extra}"
}
enable_trapping() {
TRAPPING_ENABLED="true"
}
trap_on_interrupt() {
# If this function is called, we setup an interrupt handler to cleanup the progress bar
TRAP_SET="true"
#! trap will not work if your declare the trap in an interactive shell.
#! So, always use progress bar in a bash script!
trap cleanup_on_interrupt EXIT
}
cleanup_on_interrupt() {
destroy_scroll_area
exit
}
printf_new() {
str=$1
num=$2
v=$(printf "%-${num}s" "$str")
echo -ne "${v// /$str}"
}
# SPDX-License-Identifier: MIT
#
# Copyright (c) 2018--2020 Polle Vanhoof
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.