Update core.func
This commit is contained in:
parent
2ae6d0abc5
commit
e59cd135df
@ -222,6 +222,20 @@ __curl_err_handler() {
|
|||||||
exit 1
|
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 ###
|
### dev spinner ###
|
||||||
# Trap cleanup on various signals
|
# Trap cleanup on various signals
|
||||||
trap 'cleanup_spinner' EXIT INT TERM HUP
|
trap 'cleanup_spinner' EXIT INT TERM HUP
|
||||||
@ -239,6 +253,14 @@ start_spinner() {
|
|||||||
SPINNER_MSG="$msg"
|
SPINNER_MSG="$msg"
|
||||||
SPINNER_ACTIVE=1
|
SPINNER_ACTIVE=1
|
||||||
|
|
||||||
|
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
|
while [[ "$SPINNER_ACTIVE" -eq 1 ]]; do
|
||||||
if [[ -t 2 ]]; then
|
if [[ -t 2 ]]; then
|
||||||
@ -251,9 +273,10 @@ start_spinner() {
|
|||||||
sleep "$interval"
|
sleep "$interval"
|
||||||
done
|
done
|
||||||
} &
|
} &
|
||||||
|
fi
|
||||||
|
|
||||||
local pid=$!
|
local pid=$!
|
||||||
if ps -p "$pid" >/dev/null 2>&1; then
|
if kill -0 "$pid" 2>/dev/null; then
|
||||||
SPINNER_PID="$pid"
|
SPINNER_PID="$pid"
|
||||||
else
|
else
|
||||||
SPINNER_ACTIVE=0
|
SPINNER_ACTIVE=0
|
||||||
@ -261,7 +284,6 @@ start_spinner() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# === Spinner Stop ===
|
|
||||||
stop_spinner() {
|
stop_spinner() {
|
||||||
if [[ "$SPINNER_ACTIVE" -eq 1 && -n "$SPINNER_PID" ]]; then
|
if [[ "$SPINNER_ACTIVE" -eq 1 && -n "$SPINNER_PID" ]]; then
|
||||||
SPINNER_ACTIVE=0
|
SPINNER_ACTIVE=0
|
||||||
@ -274,8 +296,8 @@ stop_spinner() {
|
|||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$SPINNER_PID" =~ ^[0-9]+$ ]]; then
|
if [[ "$SPINNER_PID" =~ ^[0-9]+$ ]] && ps -p "$SPINNER_PID" -o pid= >/dev/null 2>&1; then
|
||||||
ps -p "$SPINNER_PID" -o pid= >/dev/null 2>&1 && wait "$SPINNER_PID" 2>/dev/null || true
|
wait "$SPINNER_PID" 2>/dev/null || true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "\r\e[2K" >&2
|
printf "\r\e[2K" >&2
|
||||||
@ -283,57 +305,46 @@ stop_spinner() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# === Cleanup Spinner on signals ===
|
|
||||||
cleanup_spinner() {
|
cleanup_spinner() {
|
||||||
stop_spinner
|
stop_spinner
|
||||||
}
|
}
|
||||||
|
|
||||||
# === msg_info ===
|
|
||||||
msg_info() {
|
msg_info() {
|
||||||
local msg="$1"
|
local msg="$1"
|
||||||
[ -z "$msg" ] && return
|
[[ -z "$msg" || -n "${MSG_INFO_SHOWN["$msg"]+x}" ]] && return
|
||||||
if [ -n "${MSG_INFO_SHOWN["$msg"]+x}" ]; then return; fi
|
|
||||||
MSG_INFO_SHOWN["$msg"]=1
|
MSG_INFO_SHOWN["$msg"]=1
|
||||||
stop_spinner
|
stop_spinner
|
||||||
start_spinner "$msg"
|
start_spinner "$msg"
|
||||||
}
|
}
|
||||||
|
|
||||||
# === msg_ok ===
|
|
||||||
msg_ok() {
|
msg_ok() {
|
||||||
local msg="$1"
|
local msg="$1"
|
||||||
[ -z "$msg" ] && return
|
[[ -z "$msg" ]] && return
|
||||||
if [ "$SPINNER_ACTIVE" -eq 1 ]; then
|
|
||||||
stop_spinner
|
stop_spinner
|
||||||
else
|
|
||||||
printf "\r\e[2K" >&2
|
|
||||||
fi
|
|
||||||
printf "\r\e[2K%s %b\n" "$CM" "${GN}${msg}${CL}" >&2
|
printf "\r\e[2K%s %b\n" "$CM" "${GN}${msg}${CL}" >&2
|
||||||
unset MSG_INFO_SHOWN["$msg"]
|
unset MSG_INFO_SHOWN["$msg"]
|
||||||
}
|
}
|
||||||
|
|
||||||
# === msg_error ===
|
|
||||||
msg_error() {
|
msg_error() {
|
||||||
local msg="$1"
|
local msg="$1"
|
||||||
[ -z "$msg" ] && return
|
[[ -z "$msg" ]] && return
|
||||||
stop_spinner
|
stop_spinner
|
||||||
printf "\r\e[2K%s %b\n" "$CROSS" "${RD}${msg}${CL}" >&2
|
printf "\r\e[2K%s %b\n" "$CROSS" "${RD}${msg}${CL}" >&2
|
||||||
}
|
}
|
||||||
|
|
||||||
# === msg_warn ===
|
|
||||||
msg_warn() {
|
msg_warn() {
|
||||||
local msg="$1"
|
local msg="$1"
|
||||||
[ -z "$msg" ] && return
|
[[ -z "$msg" ]] && return
|
||||||
stop_spinner
|
stop_spinner
|
||||||
printf "\r\e[2K%s %b\n" "$INFO" "${YWB}${msg}${CL}" >&2
|
printf "\r\e[2K%s %b\n" "$INFO" "${YWB}${msg}${CL}" >&2
|
||||||
unset MSG_INFO_SHOWN["$msg"]
|
unset MSG_INFO_SHOWN["$msg"]
|
||||||
}
|
}
|
||||||
|
|
||||||
# === msg_custom ===
|
|
||||||
msg_custom() {
|
msg_custom() {
|
||||||
local symbol="$1"
|
local symbol="$1"
|
||||||
local color="$2"
|
local color="$2"
|
||||||
local msg="$3"
|
local msg="$3"
|
||||||
[ -z "$msg" ] && return
|
[[ -z "$msg" ]] && return
|
||||||
stop_spinner
|
stop_spinner
|
||||||
printf "\r\e[2K%s %b\n" "$symbol" "${color}${msg}${CL}" >&2
|
printf "\r\e[2K%s %b\n" "$symbol" "${color}${msg}${CL}" >&2
|
||||||
}
|
}
|
||||||
@ -343,15 +354,11 @@ msg_progress() {
|
|||||||
local total="$2"
|
local total="$2"
|
||||||
local label="$3"
|
local label="$3"
|
||||||
local width=40
|
local width=40
|
||||||
local filled
|
local filled percent bar empty
|
||||||
local percent
|
|
||||||
local bar
|
|
||||||
local empty
|
|
||||||
local fill_char="#"
|
local fill_char="#"
|
||||||
local empty_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
|
printf "\r\e[2K%s %b\n" "$CROSS" "${RD}Invalid progress input${CL}" >&2
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
@ -363,11 +370,9 @@ msg_progress() {
|
|||||||
bar=$(printf "%${filled}s" | tr ' ' "$fill_char")
|
bar=$(printf "%${filled}s" | tr ' ' "$fill_char")
|
||||||
bar+=$(printf "%${empty}s" | tr ' ' "$empty_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
|
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
|
printf "\n" >&2
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user