Skip to content

Idea: adding a progress bar #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ source "$BASHUNIT_ROOT_DIR/src/assertions.sh"
source "$BASHUNIT_ROOT_DIR/src/logger.sh"
source "$BASHUNIT_ROOT_DIR/src/runner.sh"
source "$BASHUNIT_ROOT_DIR/src/bashunit.sh"
source "$BASHUNIT_ROOT_DIR/src/shload.sh"
source "$BASHUNIT_ROOT_DIR/src/main.sh"

_ASSERT_FN=""
Expand Down
7 changes: 5 additions & 2 deletions src/default_env_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ function find_terminal_width() {
cols=$(stty size 2>/dev/null | cut -d' ' -f2)
fi

# Directly echo the value with fallback
echo "${cols:-$_DEFAULT_TERMINAL_WIDTH}"
if [ -z "$cols" ] || [ "$cols" -eq 0 ]; then
cols="$_DEFAULT_TERMINAL_WIDTH"
fi

echo "$cols"
}

TERMINAL_WIDTH="$(find_terminal_width)"
Expand Down
12 changes: 12 additions & 0 deletions src/runner.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#!/bin/bash

CURRENT_TEST_NUMBER=0

function runner::load_test_files() {
local filter=$1
local files=("${@:2}") # Store all arguments starting from the second as an array

local total_tests
total_tests="$(helpers::find_total_tests "$filter" "${files[@]}")"

shload_setup "$total_tests" "-"

for test_file in "${files[@]}"; do
if [[ ! -f $test_file ]]; then
continue
Expand All @@ -20,6 +27,8 @@ function runner::load_test_files() {
runner::run_tear_down_after_script
runner::clean_set_up_and_tear_down_after_script
done

shload_update "$total_tests"
}

function runner::functions_for_script() {
Expand Down Expand Up @@ -62,6 +71,9 @@ function runner::call_test_functions() {
helper::check_duplicate_functions "$script" || true

for function_name in "${functions_to_run[@]}"; do
((CURRENT_TEST_NUMBER++))
shload_update "$CURRENT_TEST_NUMBER"

local provider_data=()
while IFS=" " read -r line; do
provider_data+=("$line")
Expand Down
63 changes: 63 additions & 0 deletions src/shload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/sh

############################################################
# https://github.com/kndndrj/shload #
# Setup the progress bar #
# Usage: #
# shload_setup <maximum_value> <loading_symbol> #
############################################################
shload_setup() {
# Progress bar variables
shload_percent=$1
shload_symbol="$2"

# Bar width depends on terminal size, max width is 96
shload_width=$TERMINAL_WIDTH
shload_delimiter=$shload_percent
while [ $(($shload_width + 20)) -gt $TERMINAL_WIDTH ]; do
shload_width=$(($shload_width / 2))
shload_delimiter=$(($shload_delimiter * 2))
done

# If maximum count is less than bar width,
# adjust symbol width and delimiter (when will the bar update)
shload_count=$shload_width
while [ $1 -lt $shload_count ]; do
shload_delimiter=$(($shload_delimiter * 2))
shload_symbol="$shload_symbol$shload_symbol"
shload_count=$(($shload_count / 2))
done

# Empty bar and completion variable
shload_bar=""
shload_completion_old=0

# Print the skeleton and save cursor location
printf "\033[1;032mProgress:\033[0m \033[${shload_width}C\r"
# Add 1 to the width (less math later)
shload_width=$(($shload_width + 1))
}

############################################################
# Update the progress bar #
# Usage: #
# shload_update <current_value> #
############################################################
shload_update() {
shload_count=$(($1 * 100))
shload_completion=$(($shload_count / $shload_percent))

if [ $shload_completion -ne $shload_completion_old ]; then
if [ $shload_completion -lt 101 ]; then
# Make the bar itself, by printing the number of characters needed
shload_bar=$(printf "%0.s${shload_symbol}" $(seq -s " " 1 $(($shload_count / $shload_delimiter))))
else
shload_completion=100
shload_bar=$(printf "%0.s${shload_symbol}" $(seq -s " " 1 $(($((shload_percent * 100)) / shload_delimiter))))
fi
shload_completion_old=$shload_completion
fi

# Print progress bar and percentage, overwrite the line (\r returns cursor to start)
printf "\rProgress: [%-${shload_width}s] %d%%\r" "$shload_bar" "$shload_completion"
}
Loading