diff --git a/misc/core.func b/misc/core.func index 44adce5..bcc4ac1 100644 --- a/misc/core.func +++ b/misc/core.func @@ -222,6 +222,20 @@ __curl_err_handler() { exit 1 } +detect_os_type() { + if [[ -f /etc/os-release ]]; then + local os_id + os_id=$(awk -F= '/^ID=/{print $2}' /etc/os-release | tr -d '"') + case "$os_id" in + alpine) VAR_OS="alpine" ;; + debian | ubuntu) VAR_OS="debian" ;; + *) VAR_OS="other" ;; + esac + else + VAR_OS="unknown" + fi +} + ### dev spinner ### # Trap cleanup on various signals trap 'cleanup_spinner' EXIT INT TERM HUP @@ -239,21 +253,30 @@ start_spinner() { SPINNER_MSG="$msg" SPINNER_ACTIVE=1 - { - while [[ "$SPINNER_ACTIVE" -eq 1 ]]; do - if [[ -t 2 ]]; then - printf "\r\e[2K%s %b" "${TAB}${spinner_frames[spin_i]}${TAB}" "${YW}${SPINNER_MSG}${CL}" >&2 - else - printf "%s...\n" "$SPINNER_MSG" >&2 - break - fi - spin_i=$(((spin_i + 1) % ${#spinner_frames[@]})) - sleep "$interval" - done - } & + if [[ "$VAR_OS" == "alpine" ]]; then + { + while [[ "$SPINNER_ACTIVE" -eq 1 ]]; do + printf "\r\e[2K⠋ %b" "$SPINNER_MSG" >&2 + sleep "$interval" + done + } & + else + { + while [[ "$SPINNER_ACTIVE" -eq 1 ]]; do + if [[ -t 2 ]]; then + printf "\r\e[2K%s %b" "${TAB}${spinner_frames[spin_i]}${TAB}" "${YW}${SPINNER_MSG}${CL}" >&2 + else + printf "%s...\n" "$SPINNER_MSG" >&2 + break + fi + spin_i=$(((spin_i + 1) % ${#spinner_frames[@]})) + sleep "$interval" + done + } & + fi local pid=$! - if ps -p "$pid" >/dev/null 2>&1; then + if kill -0 "$pid" 2>/dev/null; then SPINNER_PID="$pid" else SPINNER_ACTIVE=0 @@ -261,7 +284,6 @@ start_spinner() { fi } -# === Spinner Stop === stop_spinner() { if [[ "$SPINNER_ACTIVE" -eq 1 && -n "$SPINNER_PID" ]]; then SPINNER_ACTIVE=0 @@ -274,8 +296,8 @@ stop_spinner() { done fi - if [[ "$SPINNER_PID" =~ ^[0-9]+$ ]]; then - ps -p "$SPINNER_PID" -o pid= >/dev/null 2>&1 && wait "$SPINNER_PID" 2>/dev/null || true + if [[ "$SPINNER_PID" =~ ^[0-9]+$ ]] && ps -p "$SPINNER_PID" -o pid= >/dev/null 2>&1; then + wait "$SPINNER_PID" 2>/dev/null || true fi printf "\r\e[2K" >&2 @@ -283,57 +305,46 @@ stop_spinner() { fi } -# === Cleanup Spinner on signals === cleanup_spinner() { stop_spinner } -# === msg_info === msg_info() { local msg="$1" - [ -z "$msg" ] && return - if [ -n "${MSG_INFO_SHOWN["$msg"]+x}" ]; then return; fi + [[ -z "$msg" || -n "${MSG_INFO_SHOWN["$msg"]+x}" ]] && return MSG_INFO_SHOWN["$msg"]=1 stop_spinner start_spinner "$msg" } -# === msg_ok === msg_ok() { local msg="$1" - [ -z "$msg" ] && return - if [ "$SPINNER_ACTIVE" -eq 1 ]; then - stop_spinner - else - printf "\r\e[2K" >&2 - fi + [[ -z "$msg" ]] && return + stop_spinner printf "\r\e[2K%s %b\n" "$CM" "${GN}${msg}${CL}" >&2 unset MSG_INFO_SHOWN["$msg"] } -# === msg_error === msg_error() { local msg="$1" - [ -z "$msg" ] && return + [[ -z "$msg" ]] && return stop_spinner printf "\r\e[2K%s %b\n" "$CROSS" "${RD}${msg}${CL}" >&2 } -# === msg_warn === msg_warn() { local msg="$1" - [ -z "$msg" ] && return + [[ -z "$msg" ]] && return stop_spinner printf "\r\e[2K%s %b\n" "$INFO" "${YWB}${msg}${CL}" >&2 unset MSG_INFO_SHOWN["$msg"] } -# === msg_custom === msg_custom() { local symbol="$1" local color="$2" local msg="$3" - [ -z "$msg" ] && return + [[ -z "$msg" ]] && return stop_spinner printf "\r\e[2K%s %b\n" "$symbol" "${color}${msg}${CL}" >&2 } @@ -343,15 +354,11 @@ msg_progress() { local total="$2" local label="$3" local width=40 - local filled - local percent - local bar - local empty + local filled percent bar empty local fill_char="#" local empty_char="-" - # Sanitize and validate input - if ! [[ "$current" =~ ^[0-9]+$ ]] || ! [[ "$total" =~ ^[0-9]+$ ]] || [ "$total" -eq 0 ]; then + 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 @@ -363,11 +370,9 @@ msg_progress() { 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 + if [[ "$current" -eq "$total" ]]; then printf "\n" >&2 fi }