From a42bc7502b6e4d6cfe6c937e4a65a6c56194d70f Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Thu, 8 May 2025 11:33:00 +0200 Subject: [PATCH] Update core.func --- misc/core.func | 201 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 157 insertions(+), 44 deletions(-) diff --git a/misc/core.func b/misc/core.func index ea4bd82..a9deb64 100644 --- a/misc/core.func +++ b/misc/core.func @@ -212,23 +212,38 @@ __curl_err_handler() { return 1 } -# ------------------------------------------------------------------------------ -# Spinner trap: ensures spinner is stopped on termination signals. -# ------------------------------------------------------------------------------ -trap 'stop_spinner' EXIT INT TERM HUP +### dev spinner ### +SPINNER_ACTIVE=0 +SPINNER_PID="" +SPINNER_MSG="" +declare -A MSG_INFO_SHOWN=() + +# Trap cleanup on various signals +trap 'cleanup_spinner' EXIT INT TERM HUP + +# Cleans up spinner process on exit +cleanup_spinner() { + stop_spinner + # Additional cleanup if needed +} -# ------------------------------------------------------------------------------ -# Starts a spinner animation for ongoing async operations. -# ------------------------------------------------------------------------------ start_spinner() { - local msg="$1" + local msg="${1:-Processing...}" local frames=(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏) local spin_i=0 local interval=0.1 + # Set message and clear current line SPINNER_MSG="$msg" printf "\r\e[2K" >&2 + # Stop any existing spinner + stop_spinner + + # Set active flag + SPINNER_ACTIVE=1 + + # Start spinner in background { while [[ "$SPINNER_ACTIVE" -eq 1 ]]; do printf "\r\e[2K%s %b" "${TAB}${frames[spin_i]}${TAB}" "${YW}${SPINNER_MSG}${CL}" >&2 @@ -238,63 +253,161 @@ start_spinner() { } & SPINNER_PID=$! - disown "$SPINNER_PID" + + # Disown to prevent getting "Terminated" messages + disown "$SPINNER_PID" 2>/dev/null || true } -# ------------------------------------------------------------------------------ -# Stops the spinner animation and resets its state. -# ------------------------------------------------------------------------------ stop_spinner() { - if [[ -n "${SPINNER_PID:-}" ]] && kill -0 "$SPINNER_PID" 2>/dev/null; then - kill "$SPINNER_PID" 2>/dev/null - sleep 0.1 - kill -0 "$SPINNER_PID" 2>/dev/null && kill -9 "$SPINNER_PID" 2>/dev/null - wait "$SPINNER_PID" 2>/dev/null || true - fi - SPINNER_ACTIVE=0 - unset SPINNER_PID -} - -# ------------------------------------------------------------------------------ -# Stops the spinner if active, used to avoid multiple spinners. -# ------------------------------------------------------------------------------ -spinner_guard() { - if [[ "$SPINNER_ACTIVE" -eq 1 ]] && [[ -n "${SPINNER_PID:-}" ]]; then - kill "$SPINNER_PID" 2>/dev/null - wait "$SPINNER_PID" 2>/dev/null || true + # Check if spinner is active and PID exists + if [[ "$SPINNER_ACTIVE" -eq 1 ]] && [[ -n "${SPINNER_PID}" ]]; then SPINNER_ACTIVE=0 - unset SPINNER_PID + + if kill -0 "$SPINNER_PID" 2>/dev/null; then + kill "$SPINNER_PID" 2>/dev/null + # Give it a moment to terminate + sleep 0.1 + # Force kill if still running + if kill -0 "$SPINNER_PID" 2>/dev/null; then + kill -9 "$SPINNER_PID" 2>/dev/null + fi + # Wait for process but ignore errors + wait "$SPINNER_PID" 2>/dev/null || true + fi + + # Clear spinner line + printf "\r\e[2K" >&2 + SPINNER_PID="" + fi +} + +spinner_guard() { + # Safely stop spinner if it's running + if [[ "$SPINNER_ACTIVE" -eq 1 ]] && [[ -n "${SPINNER_PID}" ]]; then + stop_spinner fi } -# ------------------------------------------------------------------------------ -# Displays an informational spinner once per message. -# ------------------------------------------------------------------------------ msg_info() { - local msg="$1" - [[ -n "${MSG_INFO_SHOWN["$msg"]+x}" ]] && return + local msg="${1:-Information message}" + + # Only show each message once unless reset + if [[ -n "${MSG_INFO_SHOWN["$msg"]+x}" ]]; then + return + fi MSG_INFO_SHOWN["$msg"]=1 spinner_guard - SPINNER_ACTIVE=1 start_spinner "$msg" } -# ------------------------------------------------------------------------------ -# Displays a success message and stops spinner. -# ------------------------------------------------------------------------------ msg_ok() { - local msg="$1" + local msg="${1:-Operation completed successfully}" stop_spinner printf "\r\e[2K%s %b\n" "${CM}" "${GN}${msg}${CL}" >&2 + + # Remove from shown messages to allow it to be shown again unset MSG_INFO_SHOWN["$msg"] } -# ------------------------------------------------------------------------------ -# Displays an error message and stops spinner. -# ------------------------------------------------------------------------------ msg_error() { + local msg="${1:-An error occurred}" stop_spinner - local msg="$1" printf "\r\e[2K%s %b\n" "${CROSS}" "${RD}${msg}${CL}" >&2 } + +# Helper function to display a message with custom symbol and color +msg_custom() { + local symbol="${1:-*}" + local color="${2:-$CL}" + local msg="${3:-Custom message}" + stop_spinner + printf "\r\e[2K%s %b\n" "$symbol" "${color}${msg}${CL}" >&2 +} + +# # ------------------------------------------------------------------------------ +# # Spinner trap: ensures spinner is stopped on termination signals. +# # ------------------------------------------------------------------------------ +# trap 'stop_spinner' EXIT INT TERM HUP + +# # ------------------------------------------------------------------------------ +# # Starts a spinner animation for ongoing async operations. +# # ------------------------------------------------------------------------------ +# start_spinner() { +# local msg="$1" +# local frames=(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏) +# local spin_i=0 +# local interval=0.1 + +# SPINNER_MSG="$msg" +# printf "\r\e[2K" >&2 + +# { +# while [[ "$SPINNER_ACTIVE" -eq 1 ]]; do +# printf "\r\e[2K%s %b" "${TAB}${frames[spin_i]}${TAB}" "${YW}${SPINNER_MSG}${CL}" >&2 +# spin_i=$(((spin_i + 1) % ${#frames[@]})) +# sleep "$interval" +# done +# } & + +# SPINNER_PID=$! +# disown "$SPINNER_PID" +# } + +# # ------------------------------------------------------------------------------ +# # Stops the spinner animation and resets its state. +# # ------------------------------------------------------------------------------ +# stop_spinner() { +# if [[ -n "${SPINNER_PID:-}" ]] && kill -0 "$SPINNER_PID" 2>/dev/null; then +# kill "$SPINNER_PID" 2>/dev/null +# sleep 0.1 +# kill -0 "$SPINNER_PID" 2>/dev/null && kill -9 "$SPINNER_PID" 2>/dev/null +# wait "$SPINNER_PID" 2>/dev/null || true +# fi +# SPINNER_ACTIVE=0 +# unset SPINNER_PID +# } + +# # ------------------------------------------------------------------------------ +# # Stops the spinner if active, used to avoid multiple spinners. +# # ------------------------------------------------------------------------------ +# spinner_guard() { +# if [[ "$SPINNER_ACTIVE" -eq 1 ]] && [[ -n "${SPINNER_PID:-}" ]]; then +# kill "$SPINNER_PID" 2>/dev/null +# wait "$SPINNER_PID" 2>/dev/null || true +# SPINNER_ACTIVE=0 +# unset SPINNER_PID +# fi +# } + +# # ------------------------------------------------------------------------------ +# # Displays an informational spinner once per message. +# # ------------------------------------------------------------------------------ +# msg_info() { +# local msg="$1" +# [[ -n "${MSG_INFO_SHOWN["$msg"]+x}" ]] && return +# MSG_INFO_SHOWN["$msg"]=1 + +# spinner_guard +# SPINNER_ACTIVE=1 +# start_spinner "$msg" +# } + +# # ------------------------------------------------------------------------------ +# # Displays a success message and stops spinner. +# # ------------------------------------------------------------------------------ +# msg_ok() { +# local msg="$1" +# stop_spinner +# printf "\r\e[2K%s %b\n" "${CM}" "${GN}${msg}${CL}" >&2 +# unset MSG_INFO_SHOWN["$msg"] +# } + +# # ------------------------------------------------------------------------------ +# # Displays an error message and stops spinner. +# # ------------------------------------------------------------------------------ +# msg_error() { +# stop_spinner +# local msg="$1" +# printf "\r\e[2K%s %b\n" "${CROSS}" "${RD}${msg}${CL}" >&2 +# }