From b610d01325e64203d37841a7c91bb070d48bb262 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Thu, 15 May 2025 10:02:47 +0200 Subject: [PATCH] add msg_progress --- misc/core.func | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/misc/core.func b/misc/core.func index 2c240ca..66967d9 100644 --- a/misc/core.func +++ b/misc/core.func @@ -326,6 +326,40 @@ msg_custom() { printf "\r\e[2K%s %b\n" "$symbol" "${color}${msg}${CL}" >&2 } +msg_progress() { + local current="$1" + local total="$2" + local label="$3" + local width=40 + local filled + local percent + local bar + local empty + local fill_char="#" + local empty_char="-" + + # Sanitize and validate input + if ! [[ "$current" =~ ^[0-9]+$ ]] || ! [[ "$total" =~ ^[0-9]+$ ]] || [ "$total" -eq 0 ]; then + printf "\r\e[2K%s %b\n" "$CROSS" "${RD}Invalid progress input${CL}" >&2 + return + fi + + percent=$(((current * 100) / total)) + filled=$(((current * width) / total)) + empty=$((width - filled)) + + bar=$(printf "%${filled}s" | tr ' ' "$fill_char") + bar+=$(printf "%${empty}s" | tr ' ' "$empty_char") + + # Print the progress line + printf "\r\e[2K%s [%s] %3d%% %s" "${TAB}" "$bar" "$percent" "$label" >&2 + + # Final newline when complete + if [ "$current" -eq "$total" ]; then + printf "\n" >&2 + fi +} + # # ------------------------------------------------------------------------------ # # Spinner trap: ensures spinner is stopped on termination signals. # # ------------------------------------------------------------------------------