Added progress bars
This commit is contained in:
38
README.md
38
README.md
@@ -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)
|
* [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)
|
* [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)
|
* [Get the current date using `strftime`.](#get-the-current-date-using-strftime)
|
||||||
|
* [Progress bars.](#progress-bars)
|
||||||
* [Bypass shell aliases.](#bypass-shell-aliases)
|
* [Bypass shell aliases.](#bypass-shell-aliases)
|
||||||
* [Bypass shell functions.](#bypass-shell-functions)
|
* [Bypass shell functions.](#bypass-shell-functions)
|
||||||
|
|
||||||
@@ -1290,6 +1291,43 @@ $ printf '%s\n' "$date"
|
|||||||
Fri 15 Jun - 10:00 AM
|
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.
|
## Bypass shell aliases.
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
|
|||||||
5
test.sh
5
test.sh
@@ -127,6 +127,11 @@ test_read_sleep() {
|
|||||||
assert_equals "$((result+1))" "$SECONDS"
|
assert_equals "$((result+1))" "$SECONDS"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test_bar() {
|
||||||
|
result="$(bar 50 10)"
|
||||||
|
assert_equals "${result//$'\r'}" "[----- ]"
|
||||||
|
}
|
||||||
|
|
||||||
assert_equals() {
|
assert_equals() {
|
||||||
if [[ "$1" == "$2" ]]; then
|
if [[ "$1" == "$2" ]]; then
|
||||||
((pass+=1))
|
((pass+=1))
|
||||||
|
|||||||
Reference in New Issue
Block a user