Update core.func

This commit is contained in:
CanbiZ 2025-05-08 11:33:00 +02:00
parent cd1d22d58b
commit a42bc7502b

View File

@ -212,23 +212,38 @@ __curl_err_handler() {
return 1 return 1
} }
# ------------------------------------------------------------------------------ ### dev spinner ###
# Spinner trap: ensures spinner is stopped on termination signals. SPINNER_ACTIVE=0
# ------------------------------------------------------------------------------ SPINNER_PID=""
trap 'stop_spinner' EXIT INT TERM HUP 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() { start_spinner() {
local msg="$1" local msg="${1:-Processing...}"
local frames=(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏) local frames=(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏)
local spin_i=0 local spin_i=0
local interval=0.1 local interval=0.1
# Set message and clear current line
SPINNER_MSG="$msg" SPINNER_MSG="$msg"
printf "\r\e[2K" >&2 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 while [[ "$SPINNER_ACTIVE" -eq 1 ]]; do
printf "\r\e[2K%s %b" "${TAB}${frames[spin_i]}${TAB}" "${YW}${SPINNER_MSG}${CL}" >&2 printf "\r\e[2K%s %b" "${TAB}${frames[spin_i]}${TAB}" "${YW}${SPINNER_MSG}${CL}" >&2
@ -238,63 +253,161 @@ start_spinner() {
} & } &
SPINNER_PID=$! 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() { stop_spinner() {
if [[ -n "${SPINNER_PID:-}" ]] && kill -0 "$SPINNER_PID" 2>/dev/null; then # Check if spinner is active and PID exists
kill "$SPINNER_PID" 2>/dev/null if [[ "$SPINNER_ACTIVE" -eq 1 ]] && [[ -n "${SPINNER_PID}" ]]; then
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 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 fi
} }
# ------------------------------------------------------------------------------
# Displays an informational spinner once per message.
# ------------------------------------------------------------------------------
msg_info() { msg_info() {
local msg="$1" local msg="${1:-Information message}"
[[ -n "${MSG_INFO_SHOWN["$msg"]+x}" ]] && return
# Only show each message once unless reset
if [[ -n "${MSG_INFO_SHOWN["$msg"]+x}" ]]; then
return
fi
MSG_INFO_SHOWN["$msg"]=1 MSG_INFO_SHOWN["$msg"]=1
spinner_guard spinner_guard
SPINNER_ACTIVE=1
start_spinner "$msg" start_spinner "$msg"
} }
# ------------------------------------------------------------------------------
# Displays a success message and stops spinner.
# ------------------------------------------------------------------------------
msg_ok() { msg_ok() {
local msg="$1" local msg="${1:-Operation completed successfully}"
stop_spinner stop_spinner
printf "\r\e[2K%s %b\n" "${CM}" "${GN}${msg}${CL}" >&2 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"] unset MSG_INFO_SHOWN["$msg"]
} }
# ------------------------------------------------------------------------------
# Displays an error message and stops spinner.
# ------------------------------------------------------------------------------
msg_error() { msg_error() {
local msg="${1:-An error occurred}"
stop_spinner stop_spinner
local msg="$1"
printf "\r\e[2K%s %b\n" "${CROSS}" "${RD}${msg}${CL}" >&2 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
# }