#!/usr/bin/env bash # ===================================================================== # ZION TERMINAL - Biblioteca de interface (UI) # Cyberdeck criado por Pablo Murad em 2026 # --------------------------------------------------------------------- # Fonte unica de cabecalhos, secoes, caixas, estados rotulados e # formatacao. Estados SEMPRE tem rotulo textual (acessivel sem cor). # Simbolo oficial: >>Z>> (Uplink Chevrons) # ===================================================================== ZION_DIR="/opt/zion-terminal" [ -z "${ZR+x}" ] && . "$ZION_DIR/config/tema.sh" 2>/dev/null # --- Conjunto de caracteres (UTF-8 x ASCII para TERM=dumb) --------- if [ "${TERM:-dumb}" = dumb ] || [ -n "${ZION_ASCII:-}" ]; then UIH='-'; UIV='|'; UITL='+'; UITR='+'; UIBL='+'; UIBR='+'; ZSYM='>>Z>>' else UIH='─'; UIV='│'; UITL='┌'; UITR='┐'; UIBL='└'; UIBR='┘'; ZSYM='»Z»' fi # --- Largura ------------------------------------------------------- ui_cols() { local c="${COLUMNS:-}"; [ -z "$c" ] && c=$(tput cols 2>/dev/null); echo "${c:-80}"; } ui_w() { local c max; c=$(ui_cols); max="${1:-60}"; [ "$c" -lt "$((max+4))" ] && echo "$((c-4))" || echo "$max"; } ui_strip() { printf '%s' "$1" | sed 's/\x1b\[[0-9;]*m//g'; } ui_vlen() { local s; s=$(ui_strip "$1"); echo "${#s}"; } ui_repeat() { local ch="$1" n="$2" i; for ((i=0;i imprime rotulo colorido (largura fixa 8) case "$1" in ok) printf '%b%-8s%b' "$ZOK" "OK" "$ZR" ;; warn) printf '%b%-8s%b' "$ZWARN" "ATENCAO" "$ZR" ;; crit) printf '%b%-8s%b' "$ZCRIT" "CRITICO" "$ZR" ;; off) printf '%b%-8s%b' "$ZMUT" "OFFLINE" "$ZR" ;; nd) printf '%b%-8s%b' "$ZGRY" "N/D" "$ZR" ;; *) printf '%-8s' "$1" ;; esac } ui_ok() { printf ' %b[ OK ]%b %s\n' "$ZOK" "$ZR" "$1"; } ui_warn() { printf ' %b[ ! ]%b %s\n' "$ZWARN" "$ZR" "$1"; } ui_err() { printf ' %b[FALHA]%b %s\n' "$ZCRIT" "$ZR" "$1"; } ui_info() { printf ' %b•%b %s\n' "$ZDISC" "$ZR" "$1"; } # --- Caixas com bordas (alinhamento consciente de ANSI) ----------- ui_box_top() { # $1 titulo $2 largura-interna local t="$1" w="${2:-40}" fill; fill=$(( w - ${#t} - 1 )) [ "$fill" -lt 0 ] && fill=0 printf ' %b%s%s %b%s%b%b ' "$ZBRD" "$UITL" "$UIH" "$ZS$ZB" "$t" "$ZR" "$ZBRD"; ui_repeat "$UIH" "$fill"; printf '%s%b\n' "$UITR" "$ZR" } ui_box_line() { # $1 conteudo(com cor) $2 largura-interna (trunca se exceder) local c="$1" w="${2:-40}" vis pad; vis=$(ui_vlen "$c"); pad=$(( w - vis )) if [ "$pad" -lt 0 ]; then c=$(ui_strip "$c"); c="${c:0:$((w-1))}~"; pad=$(( w - ${#c} )); fi [ "$pad" -lt 0 ] && pad=0 printf ' %b%s%b %s%*s %b%s%b\n' "$ZBRD" "$UIV" "$ZR" "$c" "$pad" "" "$ZBRD" "$UIV" "$ZR" } ui_box_kv() { # $1 rotulo $2 valor $3 largura-interna [$4 cor-valor] local r="$1" v="$2" w="${3:-40}" vc="${4:-$ZWHT}" ui_box_line "$(printf '%b%-11s%b %b%s%b' "$ZDISC" "$r" "$ZR" "$vc" "$v" "$ZR")" "$w" } ui_box_bot() { local w="${1:-40}"; printf ' %b%s' "$ZBRD" "$UIBL"; ui_repeat "$UIH" "$((w+2))"; printf '%s%b\n' "$UIBR" "$ZR"; } # --- Confirmacao / dependencia / timeout / log -------------------- ui_confirm() { local r; read -r -p "$(printf ' %b%s [s/N]:%b ' "$ZWARN" "$1" "$ZR")" r; [ "$r" = s ] || [ "$r" = S ]; } ui_dep() { command -v "$1" >/dev/null 2>&1 && return 0; ui_err "dependencia ausente: $1"; return 1; } ui_timeout() { local t="$1"; shift; timeout "$t" "$@"; } ui_log() { local f="$ZION_DIR/logs/${ZION_LOGNAME:-zion}.log"; printf '[%s] %s\n' "$(date '+%F %T')" "$*" >> "$f" 2>/dev/null; } # --- Formatacao ---------------------------------------------------- ui_bytes() { numfmt --to=iec --suffix=B "${1:-0}" 2>/dev/null | sed 's/\.0//' || printf '%sB' "${1:-0}"; } ui_dur() { # segundos -> "2d 3h 4m" local s="${1:-0}" d h m; d=$((s/86400)); h=$(((s%86400)/3600)); m=$(((s%3600)/60)) if [ "$d" -gt 0 ]; then printf '%dd %dh %dm' "$d" "$h" "$m" elif [ "$h" -gt 0 ]; then printf '%dh %dm' "$h" "$m"; else printf '%dm' "$m"; fi }