Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Added unicode progress bar #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2127,6 +2127,41 @@ done
printf '\n'
```

You may also consider using unicode characters to draw a more granular progress bar:

**Example Function:**

```sh
function progress () {
local symbols=(" " "\u258F" "\u258E" "\u258D" "\u258C" "\u258B" "\u258A" "\u2589" "\u2588")
local n=$(($1/8))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not define 8, which is used multiple times, dynamically like e.g. length=$((${#symbols[@]}-1))?

Copy link
Author

@mickvav mickvav Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no chance that unicode character table will change in future to have something else, thus 8 is a true constant here.
In the same time, determining array length on every function call will eat some CPU on every execution and give no value.

(although I completely agree with gut instinct of "constant that is > 1 should be named and computed once" - this is very good default assumption that makes perfect sense in 90-99% cases)

local m=$(($1%8))
local i=0
local k=$(($2/8))
for((i=0;i<n;i++));do
echo -ne "${symbols[8]}"
done
echo -ne "${symbols[$m]}"
for((i=n+1;i<=k;i++));do
echo -ne " "
done
echo "$1 / $2"
}
```

**Example Usage:**

```shell
for ((i=0;i<=100;i++)); do
# Pure bash micro sleeps (for the example).
(:;:) && (:;:) && (:;:) && (:;:) && (:;:)

# Print the bar.
progress "$i" "100"
done
printf '\n'
```

## Get the list of functions in a script

```sh
Expand Down
35 changes: 35 additions & 0 deletions manuscript/chapter19.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,41 @@ done
printf '\n'
```

You may also consider using unicode characters to draw a more granular progress bar:

**Example Function:**

```sh
function progress () {
local symbols=(" " "\u258F" "\u258E" "\u258D" "\u258C" "\u258B" "\u258A" "\u2589" "\u2588")
local n=$(($1/8))
local m=$(($1%8))
local i=0
local k=$(($2/8))
for((i=0;i<n;i++));do
echo -ne "${symbols[8]}"
done
echo -ne "${symbols[$m]}"
for((i=n+1;i<=k;i++));do
echo -ne " "
done
echo "$1 / $2"
}
```

**Example Usage:**

```shell
for ((i=0;i<=100;i++)); do
# Pure bash micro sleeps (for the example).
(:;:) && (:;:) && (:;:) && (:;:) && (:;:)

# Print the bar.
progress "$i" "100"
done
printf '\n'
```

## Get the list of functions in a script

```sh
Expand Down
5 changes: 5 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ test_bar() {
assert_equals "${result//$'\r'}" "[----- ]"
}

test_progress() {
result="$(progress 13 100)"
assert_equals '█▋ 13 / 100'
}

test_get_functions() {
IFS=$'\n' read -d "" -ra functions < <(get_functions)
assert_equals "${functions[0]}" "assert_equals"
Expand Down