From 3609a71eb413028576bda80bde6759a42f1fddff Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sun, 29 Sep 2024 21:17:58 +0200 Subject: [PATCH] feat: install progress bar using shload script by @kndndrj source code: https://github.com/kndndrj/shload --- bashunit | 1 + src/default_env_config.sh | 7 +++-- src/runner.sh | 12 ++++++++ src/shload.sh | 63 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100755 src/shload.sh diff --git a/bashunit b/bashunit index 8ac501e8..5dda36db 100755 --- a/bashunit +++ b/bashunit @@ -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="" diff --git a/src/default_env_config.sh b/src/default_env_config.sh index 9b067c17..ae50aa3b 100644 --- a/src/default_env_config.sh +++ b/src/default_env_config.sh @@ -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)" diff --git a/src/runner.sh b/src/runner.sh index 7e628933..8ae39dad 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -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 @@ -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() { @@ -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") diff --git a/src/shload.sh b/src/shload.sh new file mode 100755 index 00000000..9bae7839 --- /dev/null +++ b/src/shload.sh @@ -0,0 +1,63 @@ +#!/bin/sh + +############################################################ +# https://github.com/kndndrj/shload # +# Setup the progress bar # +# Usage: # +# shload_setup # +############################################################ +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 # +############################################################ +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" +}