From 4a2ff9fcab10a87923c4da8573759b10c9223846 Mon Sep 17 00:00:00 2001 From: Cheng-Yang Chou Date: Mon, 17 Mar 2025 23:52:43 +0800 Subject: [PATCH] Simplify progress bar display logic Refactor the progress bar logic to remove redundant conditional handling for the final 100% case. Instead of using a special case to clear and reprint the final bar, the update now ensures a consistent format for all progress levels. Co-authored-by: Po-Ying Chiu Change-Id: Id2760db0c4ff2b4eaa75e97f04b0178de4ca63a3 --- scripts/common.sh | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/scripts/common.sh b/scripts/common.sh index a41097b95..8597511eb 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -54,15 +54,12 @@ progress() { local bar_left bar_left=$(printf "%${left}s") - # If no leftover space remains, we have presumably reached 100%. - if [ "$left" -eq 0 ]; then - # Clear the existing progress line - printf "\r\033[K" - # FIXME: remove this hack to print the final 100% bar with a newline - printf "Progress: [########################################] 100%%\n" - else - # Update the bar in place (no extra newline) - printf "\rProgress: [${bar_done// /#}${bar_left// /-}] ${percentage}%%" + # Print the progress bar in a single line, updating it dynamically. + printf "\rProgress: [%s%s] %d%%" "${bar_done// /#}" "${bar_left// /-}" "$percentage" + + # When reaching 100%, automatically print a newline to finalize the output. + if [ "$percentage" -eq 100 ]; then + printf "\n" fi }