Skip to content

Commit a6cd9c2

Browse files
committed
Merge branch 'improvements'
2 parents 18235e1 + 910c8eb commit a6cd9c2

2 files changed

Lines changed: 37 additions & 76 deletions

File tree

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
> An asynchronous progressbar for bash shell scripts inspired by APT for bash4 and zsh
33
44
## Table of contents
5-
<!-- MarkdownTOC -->
65

76
- [Installation](#installation)
87
- [Usage](#usage)
98
- [Example](#example)
109
- [Customization](#customization)
1110

12-
<!-- /MarkdownTOC -->
13-
1411
![bar](https://raw.githubusercontent.com/phenonymous/shell-progressbar/master/images/progressbar.gif)
1512

1613
For a POSIX compliant version see instructions in branch POSIX, this includes mac users
@@ -89,4 +86,4 @@ You can change foreground and background color by setting these variables
8986
foreground="$(tput setaf 0)" # black
9087
background="$(tput setab 2)" # green
9188
```
92-
you can also tweak how often reporting should be done (in case of great number of steps and quick progressing) by setting `reporting_steps` to a value bigger than 1
89+
you can also tweak how often reporting should be done (in case of great number of steps and quick progressing) by setting `reporting_steps` to a value bigger than 1

progress.sh

Lines changed: 36 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <https://www.gnu.org/licenses/>
18+
set -a
1819

1920
# The following terms are used for maintainment
2021
# FIXME : Code that needs to improved or does not work
@@ -24,15 +25,6 @@
2425
# FUTURE : For future changes, this must be considered
2526
# IDEA : Ideas for future improvement or added features
2627

27-
typeset -gA FUNCTION_OUTPUT
28-
29-
#-- Static global variables
30-
typeset -g background foreground reset_color
31-
32-
#-- Dynamic global variables
33-
typeset -g progress_str percentage
34-
typeset -gi HEIGHT WIDTH last_reported_progress reporting_steps
35-
3628
percentage="0.0"
3729
last_reported_progress=-1
3830

@@ -59,62 +51,45 @@ move_to='tput cup'
5951
move_up='tput cuu'
6052
flush='tput ed'
6153

62-
#-- TODO: Replace FUNCTION_OUTPUT with subshells, it's less confusing and the performance gain is negletible
6354

6455
# Bash does not handle floats
6556
# This section defines some math functions using awk
6657
# ==================================================
6758

59+
6860
math::floor() {
6961
#-- This function takes a pseudo-floating point as argument
7062
#-- and rounds down to nearest integer
71-
[[ -n "${FUNCTION_OUTPUT[floor]:-}" ]] && FUNCTION_OUTPUT[floor]=''
72-
FUNCTION_OUTPUT[floor]="$(awk -v f="$1" 'BEGIN{f=int(f); print f}')"
73-
}
74-
75-
math::ceiling() {
76-
#-- This function takes a pseudo-floating point as argument
77-
#-- and rounds up to nearest integer
78-
[[ -n "${FUNCTION_OUTPUT[ceiling]:-}" ]] && FUNCTION_OUTPUT[ceiling]=''
79-
FUNCTION_OUTPUT[ceiling]="$(awk -v f="$1" 'BEGIN{f=int(f)+1; print f}')"
63+
awk -v f="$1" 'BEGIN{f=int(f); print f}'
8064
}
8165

8266
math::round() {
8367
#-- This function takes a pseudo-floating point as argument
8468
#-- and rounds to nearest integer
85-
[[ -n "${FUNCTION_OUTPUT[round]:-}" ]] && FUNCTION_OUTPUT[round]=''
86-
FUNCTION_OUTPUT[round]="$(awk -v f="$1" 'BEGIN {printf "%.0f\n", f}')"
69+
awk -v f="$1" 'BEGIN {printf "%.0f\n", f}'
8770
}
8871

8972
math::min() {
9073
#-- Takes two values as arguments and compare them
91-
[[ -n "${FUNCTION_OUTPUT[min]:-}" ]] && FUNCTION_OUTPUT[min]=''
92-
FUNCTION_OUTPUT[min]="$(awk -v f1="$1" -v f2="$2" 'BEGIN{if (f1<=f2) min=f1; else min=f2; printf min "\n"}')"
74+
awk -v f1="$1" -v f2="$2" 'BEGIN{if (f1<=f2) min=f1; else min=f2; printf min "\n"}'
9375
}
9476

9577
math::max() {
9678
#-- Takes two values as arguments and compare them
97-
[[ -n "${FUNCTION_OUTPUT[max]:-}" ]] && FUNCTION_OUTPUT[max]=''
98-
FUNCTION_OUTPUT[max]="$(awk -v f1="$1" -v f2="$2" 'BEGIN{if (f1>f2) max=f1; else max=f2; printf max "\n"}')"
99-
}
100-
101-
math::float_multiplication() {
102-
#-- Takes two floats and multiply them
103-
[[ -n "${FUNCTION_OUTPUT[multiplication]:-}" ]] && FUNCTION_OUTPUT[multiplication]=''
104-
FUNCTION_OUTPUT[multiplication]="$(awk -v f1="$1" -v f2="$2" 'BEGIN{print f1 * f2}') "
79+
awk -v f1="$1" -v f2="$2" 'BEGIN{if (f1>f2) max=f1; else max=f2; printf max "\n"}'
10580
}
10681

107-
math::float_division() {
108-
#-- Takes two floats and divide them
109-
[[ -n "${FUNCTION_OUTPUT[division]:-}" ]] && FUNCTION_OUTPUT[division]=''
110-
FUNCTION_OUTPUT[division]="$(awk -v f1="$1" -v f2="$2" 'BEGIN{print f1 / f2}')"
82+
math::calc() {
83+
#-- Normal calculator
84+
awk "BEGIN{print $*}"
11185
}
11286

11387

11488
####################################################
11589

11690

11791

92+
11893
# The main function stack
11994
# ==================================================
12095

@@ -126,13 +101,13 @@ __tty_size(){
126101
}
127102

128103
__change_scroll_area() {
129-
typeset -i n_rows=$1
104+
local -i n_rows=$1
130105
#-- Return if number of lines is 1
131106
if (( n_rows <= 1)); then
132107
return 1
133108
fi
134109

135-
((n_rows=n_rows-2))
110+
((n_rows-=2))
136111

137112
#-- Go down one line to avoid visual glitch
138113
#-- when terminal scroll region shrinks by 1
@@ -149,20 +124,21 @@ __change_scroll_area() {
149124

150125
#-- Move up 1 line in case cursor was saved outside scroll region
151126
eval "${move_up} 2"
127+
152128
echo
153129

154130
#-- Set tty size to reflect changes to scroll region
155131
#-- this is to avoid i.e pagers to override the progress bar
156132
((++n_rows))
157-
133+
158134
#-- Temporarily disabling SIGWINCH to avoid a loop caused by stty sending SIGWINCH whenever theres a change in size
159135
trap '' WINCH
160136
stty rows $n_rows
161137
trap handle_sigwinch WINCH
162138
}
163139

164140
__status_changed() {
165-
typeset -i StepsDone TotalSteps __int_percentage
141+
local -i StepsDone TotalSteps __int_percentage
166142

167143
((StepsDone=$1))
168144
((TotalSteps=$2))
@@ -171,16 +147,11 @@ __status_changed() {
171147
#-- Sanity check reporting_steps, if this value is too big no progress will be written
172148
#-- Should that really be checked here?
173149

174-
math::float_division $StepsDone $TotalSteps
175-
math::float_multiplication "${FUNCTION_OUTPUT[division]}" "100.00"
176-
percentage="${FUNCTION_OUTPUT[multiplication]}"
177-
178-
math::round "$percentage"
150+
percentage=$(math::calc "$(math::calc "$StepsDone/$TotalSteps")*100.00")
179151

180-
((__int_percentage=FUNCTION_OUTPUT[round]))
152+
((__int_percentage=$(math::round "$percentage")))
181153

182-
#-- FUTURE: printf -v is non-standard, for POSIX replace with subshell
183-
builtin printf -v progress_str "Progress: [%3li%%]" $__int_percentage
154+
printf -v progress_str "Progress: [%3li%%]" $__int_percentage
184155

185156
if (( __int_percentage < (last_reported_progress + reporting_steps) )); then
186157
return 1
@@ -190,29 +161,22 @@ __status_changed() {
190161
}
191162

192163
__progress_string() {
193-
[[ -n ${FUNCTION_OUTPUT[progress]:-} ]] && FUNCTION_OUTPUT[progress]=''
194-
195164
local output Percent
196-
typeset -i OutputSize BarSize BarDone it
165+
local -i OutputSize BarSize BarDone it
197166

198167
output=""
199168
Percent="$1"
200169
((OutputSize=$2))
201170

202171
#-- Return an empty string if OutputSize is less than 3
203172
if ((OutputSize < 3)); then
204-
FUNCTION_OUTPUT[progress]="$output"
173+
echo "$output"
205174
return 1
206175
fi
207176

208177
((BarSize=OutputSize-2))
209178

210-
math::float_multiplication "$Percent" $BarSize
211-
math::floor "${FUNCTION_OUTPUT[multiplication]}"
212-
math::min $BarSize "${FUNCTION_OUTPUT[floor]}"
213-
math::max 0 "${FUNCTION_OUTPUT[min]}"
214-
215-
((BarDone=FUNCTION_OUTPUT[max]))
179+
BarDone=$(math::max 0 "$(math::min $BarSize "$(math::floor "$(math::calc "$Percent*$BarSize")")")")
216180

217181
output="${LEFT_BRACKET}"
218182
for (( it = 0; it < BarDone; it++ )); do
@@ -222,20 +186,24 @@ __progress_string() {
222186
output+="${REMAIN}"
223187
done
224188
output+="${RIGHT_BRACKET}"
225-
FUNCTION_OUTPUT[progress]="$output"
189+
190+
echo "$output"
191+
226192
return 0
227193
}
228194

229195
__draw_status_line(){
230196
__tty_size
231-
if (( HEIGHT < 1 || WIDTH< 1 )); then
197+
if (( HEIGHT < 1 || WIDTH < 1 )); then
232198
return 1
233199
fi
234200

235-
local current_percent
236-
typeset -i padding __int_percentage progressbar_size
201+
local current_percent progress_bar
202+
local -i padding progressbar_size
237203
((padding=4))
238204

205+
progress_bar=""
206+
239207
#-- Save the cursor
240208
eval "${save_cursor}"
241209
#-- Make cursor invisible
@@ -246,25 +214,22 @@ __draw_status_line(){
246214
printf '%s' "${background}${foreground}${progress_str}${reset_color}"
247215

248216
((progressbar_size=WIDTH-padding-${#progress_str}))
249-
math::float_division "$percentage" "100.00"
250-
current_percent="${FUNCTION_OUTPUT[division]}"
217+
current_percent=$(math::calc "$percentage/100.00")
251218

252-
__progress_string "${current_percent}" ${progressbar_size}
219+
progress_bar="$(__progress_string "${current_percent}" ${progressbar_size})"
253220

254-
printf '%s' " ${FUNCTION_OUTPUT[progress]} "
221+
printf '%s' " ${progress_bar} "
255222

256223
#-- Restore the cursor
257224
eval "${restore_cursor}"
258225
eval "${enable_cursor}"
259226

260-
math::round "$percentage"
261-
((__int_percentage=FUNCTION_OUTPUT[round]))
262-
263-
((last_reported_progress=__int_percentage))
227+
((last_reported_progress=$(math::round "$percentage")))
264228

265229
return 0
266230
}
267231

232+
268233
bar::start() {
269234
#-- TODO: Track process that called this function
270235
# proc...
@@ -335,8 +300,7 @@ bar::status_changed() {
335300

336301
handle_sigwinch(){
337302
__tty_size
338-
typeset -i n_rows
339-
((n_rows=HEIGHT))
303+
n_rows=$HEIGHT
340304
__change_scroll_area $n_rows
341305
__draw_status_line
342306
}
@@ -352,4 +316,4 @@ handle_exit(){
352316

353317

354318
trap handle_sigwinch WINCH
355-
trap handle_exit EXIT HUP INT QUIT PIPE TERM
319+
trap handle_exit EXIT HUP INT QUIT PIPE TERM

0 commit comments

Comments
 (0)