Update core.func

This commit is contained in:
CanbiZ 2025-07-04 09:20:33 +02:00
parent cea9ebec37
commit e2865f9e6a

View File

@ -250,79 +250,6 @@ header_info() {
fi fi
} }
# ------------------------------------------------------------------------------
# Performs a curl request with retry logic and inline feedback.
# ------------------------------------------------------------------------------
run_curl() {
if [ "$VERBOSE" = "no" ]; then
$STD curl "$@"
else
curl "$@"
fi
}
curl_handler() {
set +e
trap 'set -e' RETURN
local args=()
local url=""
local max_retries=3
local delay=2
local attempt=1
local exit_code
local has_output_file=false
local result=""
# Parse arguments
for arg in "$@"; do
if [[ "$arg" != -* && -z "$url" ]]; then
url="$arg"
fi
[[ "$arg" == "-o" || "$arg" == --output ]] && has_output_file=true
args+=("$arg")
done
if [[ -z "$url" ]]; then
msg_error "No valid URL or option entered for curl_handler"
return 1
fi
$STD msg_info "Fetching: $url"
while [[ $attempt -le $max_retries ]]; do
if $has_output_file; then
$STD run_curl "${args[@]}"
exit_code=$?
else
result=$(run_curl "${args[@]}")
exit_code=$?
fi
if [[ $exit_code -eq 0 ]]; then
$STD msg_ok "Fetched: $url"
$has_output_file || printf '%s' "$result"
return 0
fi
if ((attempt >= max_retries)); then
# Read error log if it exists
if [ -s /tmp/curl_error.log ]; then
local curl_stderr
curl_stderr=$(</tmp/curl_error.log)
rm -f /tmp/curl_error.log
fi
__curl_err_handler "$exit_code" "$url" "${curl_stderr:-}"
exit
fi
$STD printf "\r\033[K${INFO}${YW}Retry $attempt/$max_retries in ${delay}s...${CL}" >&2
sleep "$delay"
((attempt++))
done
set -e
}
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Handles specific curl error codes and displays descriptive messages. # Handles specific curl error codes and displays descriptive messages.
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -386,32 +313,39 @@ stop_spinner() {
wait "$pid" 2>/dev/null || true wait "$pid" 2>/dev/null || true
fi fi
rm -f /tmp/.spinner.pid rm -f /tmp/.spinner.pid
unset SPINNER_PID
fi fi
stty sane 2>/dev/null unset SPINNER_PID SPINNER_MSG
#printf "\r\033[K\e[?25h\n" stty sane 2>/dev/null || true
} }
msg_info() { msg_info() {
local msg="$1" local msg="$1"
[[ -z "$msg" || -n "${MSG_INFO_SHOWN["$msg"]+x}" ]] && return [[ -z "$msg" ]] && return
if ! declare -p MSG_INFO_SHOWN &>/dev/null || ! declare -A MSG_INFO_SHOWN &>/dev/null; then
declare -gA MSG_INFO_SHOWN=()
fi
if [[ -n "${MSG_INFO_SHOWN["$msg"]+x}" ]]; then
return
fi
MSG_INFO_SHOWN["$msg"]=1 MSG_INFO_SHOWN["$msg"]=1
stop_spinner stop_spinner
local HOURGLASS="${TAB}⏳${TAB}"
if [[ "${VERBOSE:-no}" != "no" || "${var_os:-}" == "alpine" || ! -t 2 ]]; then if [[ "${VERBOSE:-no}" != "no" || "${var_os:-}" == "alpine" || ! -t 2 ]]; then
local HOURGLASS="${TAB}⏳${TAB}"
printf "\r\e[2K%s %b\n" "$HOURGLASS" "${YW}${msg}${CL}" >&2 printf "\r\e[2K%s %b\n" "$HOURGLASS" "${YW}${msg}${CL}" >&2
else return
SPINNER_MSG="$msg"
color_spinner
spinner &
SPINNER_PID=$!
echo "$SPINNER_PID" >/tmp/.spinner.pid
disown "$SPINNER_PID" 2>/dev/null || true
fi fi
SPINNER_MSG="$msg"
color_spinner
spinner &
SPINNER_PID=$!
echo "$SPINNER_PID" >/tmp/.spinner.pid
disown "$SPINNER_PID" 2>/dev/null || true
} }
msg_ok() { msg_ok() {