diff --git a/README.md b/README.md index 0f991ee..ffba0b6 100644 --- a/README.md +++ b/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) * [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 diff --git a/test.sh b/test.sh index a05f1b3..80f4d10 100755 --- a/test.sh +++ b/test.sh @@ -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))