Added progress bars

This commit is contained in:
Dylan Araps 2018-06-15 18:33:01 +10:00
parent c181b0afa9
commit 679e787bd2
2 changed files with 43 additions and 0 deletions

View File

@ -110,6 +110,7 @@ scripts and not full blown utilities.
* [Use `read` as an alternative to the `sleep` command.](#use-read-as-an-alternative-to-the-sleep-command)
* [Check if a program is in the user's PATH.](#check-if-a-program-is-in-the-users-path)
* [Get the current date using `strftime`.](#get-the-current-date-using-strftime)
* [Progress bars.](#progress-bars)
* [Bypass shell aliases.](#bypass-shell-aliases)
* [Bypass shell functions.](#bypass-shell-functions)
@ -1290,6 +1291,43 @@ $ printf '%s\n' "$date"
Fri 15 Jun - 10:00 AM
```
## Progress bars.
This is a simple way of drawing progress bars without needing a for loop
in the function itself.
**Example Function:**
```sh
bar() {
# Usage: bar 1 10
# ^----- Elapsed Percentage (0-100).
# ^-- Total length in chars.
((elapsed=$1*$2/100))
# Create the bar with spaces.
printf -v prog "%${elapsed}s"
printf -v total "%$(($2-elapsed))s"
printf '%s\r' "[${prog// /-}${total}]"
}
```
**Example Usage:**
```shell
for ((i=0;i<=100;i++)); do
# Pure bash micro sleeps (for the example).
(:;:) && (:;:) && (:;:) && (:;:) && (:;:)
# Print the bar.
bar "$i" "10"
done
printf '\n'
```
## Bypass shell aliases.
```shell

View File

@ -127,6 +127,11 @@ test_read_sleep() {
assert_equals "$((result+1))" "$SECONDS"
}
test_bar() {
result="$(bar 50 10)"
assert_equals "${result//$'\r'}" "[----- ]"
}
assert_equals() {
if [[ "$1" == "$2" ]]; then
((pass+=1))