Update core.func

This commit is contained in:
CanbiZ 2025-05-07 16:10:25 +02:00
parent be9eb883ef
commit b217f9f909

View File

@ -7,6 +7,10 @@ SPINNER_ACTIVE=0
SPINNER_MSG="" SPINNER_MSG=""
declare -A MSG_INFO_SHOWN declare -A MSG_INFO_SHOWN
# ------------------------------------------------------------------------------
# Loads core utility groups once (colors, formatting, icons, defaults).
# ------------------------------------------------------------------------------
[[ -n "${_CORE_FUNC_LOADED:-}" ]] && return [[ -n "${_CORE_FUNC_LOADED:-}" ]] && return
_CORE_FUNC_LOADED=1 _CORE_FUNC_LOADED=1
@ -20,7 +24,9 @@ load_functions() {
# add more # add more
} }
# This function sets various color variables using ANSI escape codes for formatting text in the terminal. # ------------------------------------------------------------------------------
# Sets ANSI color codes used for styled terminal output.
# ------------------------------------------------------------------------------
color() { color() {
YW=$(echo "\033[33m") YW=$(echo "\033[33m")
YWB=$(echo "\033[93m") YWB=$(echo "\033[93m")
@ -32,6 +38,9 @@ color() {
CL=$(echo "\033[m") CL=$(echo "\033[m")
} }
# ------------------------------------------------------------------------------
# Defines formatting helpers like tab, bold, and line reset sequences.
# ------------------------------------------------------------------------------
formatting() { formatting() {
BFR="\\r\\033[K" BFR="\\r\\033[K"
BOLD=$(echo "\033[1m") BOLD=$(echo "\033[1m")
@ -39,6 +48,9 @@ formatting() {
TAB=" " TAB=" "
} }
# ------------------------------------------------------------------------------
# Sets symbolic icons used throughout user feedback and prompts.
# ------------------------------------------------------------------------------
icons() { icons() {
CM="${TAB}✔️${TAB}" CM="${TAB}✔️${TAB}"
CROSS="${TAB}✖️${TAB}" CROSS="${TAB}✖️${TAB}"
@ -66,13 +78,18 @@ icons() {
ADVANCED="${TAB}🧩${TAB}${CL}" ADVANCED="${TAB}🧩${TAB}${CL}"
} }
# ------------------------------------------------------------------------------
# Sets default retry and wait variables used for system actions.
# ------------------------------------------------------------------------------
default_vars() { default_vars() {
# System
RETRY_NUM=10 RETRY_NUM=10
RETRY_EVERY=3 RETRY_EVERY=3
i=$RETRY_NUM i=$RETRY_NUM
} }
# ------------------------------------------------------------------------------
# Performs a curl request with retry logic and inline feedback.
# ------------------------------------------------------------------------------
community_curl() { community_curl() {
local url="$1" local url="$1"
shift shift
@ -100,13 +117,15 @@ community_curl() {
return 1 return 1
fi fi
# Print retry inline
printf "\r\033[K${INFO}${YW}Retry $attempt/$max_retries in ${delay}s...${CL}" >&2 printf "\r\033[K${INFO}${YW}Retry $attempt/$max_retries in ${delay}s...${CL}" >&2
sleep "$delay" sleep "$delay"
((attempt++)) ((attempt++))
done done
} }
# ------------------------------------------------------------------------------
# Handles specific curl error codes and displays descriptive messages.
# ------------------------------------------------------------------------------
__curl_err_handler() { __curl_err_handler() {
local exit_code="$1" local exit_code="$1"
local cmd="${BASH_COMMAND:-unknown}" local cmd="${BASH_COMMAND:-unknown}"
@ -143,11 +162,14 @@ __curl_err_handler() {
exit exit
} }
# ---------------------------------------------------------------------------------- # ------------------------------------------------------------------------------
# This function displays an informational message with logging support. # Spinner trap: ensures spinner is stopped on termination signals.
# ---------------------------------------------------------------------------------- # ------------------------------------------------------------------------------
trap 'stop_spinner' EXIT INT TERM HUP trap 'stop_spinner' EXIT INT TERM HUP
# ------------------------------------------------------------------------------
# Starts a spinner animation for ongoing async operations.
# ------------------------------------------------------------------------------
start_spinner() { start_spinner() {
local msg="$1" local msg="$1"
local frames=(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏) local frames=(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏)
@ -169,6 +191,9 @@ start_spinner() {
disown "$SPINNER_PID" disown "$SPINNER_PID"
} }
# ------------------------------------------------------------------------------
# Stops the spinner animation and resets its state.
# ------------------------------------------------------------------------------
stop_spinner() { stop_spinner() {
if [[ -n "${SPINNER_PID:-}" ]] && kill -0 "$SPINNER_PID" 2>/dev/null; then if [[ -n "${SPINNER_PID:-}" ]] && kill -0 "$SPINNER_PID" 2>/dev/null; then
kill "$SPINNER_PID" 2>/dev/null kill "$SPINNER_PID" 2>/dev/null
@ -180,6 +205,9 @@ stop_spinner() {
unset SPINNER_PID unset SPINNER_PID
} }
# ------------------------------------------------------------------------------
# Stops the spinner if active, used to avoid multiple spinners.
# ------------------------------------------------------------------------------
spinner_guard() { spinner_guard() {
if [[ "$SPINNER_ACTIVE" -eq 1 ]] && [[ -n "${SPINNER_PID:-}" ]]; then if [[ "$SPINNER_ACTIVE" -eq 1 ]] && [[ -n "${SPINNER_PID:-}" ]]; then
kill "$SPINNER_PID" 2>/dev/null kill "$SPINNER_PID" 2>/dev/null
@ -189,6 +217,9 @@ spinner_guard() {
fi fi
} }
# ------------------------------------------------------------------------------
# Displays an informational spinner once per message.
# ------------------------------------------------------------------------------
msg_info() { msg_info() {
local msg="$1" local msg="$1"
[[ -n "${MSG_INFO_SHOWN["$msg"]+x}" ]] && return [[ -n "${MSG_INFO_SHOWN["$msg"]+x}" ]] && return
@ -199,6 +230,9 @@ msg_info() {
start_spinner "$msg" start_spinner "$msg"
} }
# ------------------------------------------------------------------------------
# Displays a success message and stops spinner.
# ------------------------------------------------------------------------------
msg_ok() { msg_ok() {
local msg="$1" local msg="$1"
stop_spinner stop_spinner
@ -206,6 +240,9 @@ msg_ok() {
unset MSG_INFO_SHOWN["$msg"] unset MSG_INFO_SHOWN["$msg"]
} }
# ------------------------------------------------------------------------------
# Displays an error message and stops spinner.
# ------------------------------------------------------------------------------
msg_error() { msg_error() {
stop_spinner stop_spinner
local msg="$1" local msg="$1"