all repos — zion-terminal @ 66508a0a605214d34c7a2231848d44ee63b394b2

dotfiles

opt/zion-terminal/lib/zion-ui.sh (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
#!/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<n;i++)); do printf '%s' "$ch"; done; }

# --- Simbolo oficial (ZSYM definido no bloco de charset acima) -----
ui_sym() { printf '%b%s%b' "$ZP$ZB" "$ZSYM" "$ZR"; }

# --- Cabecalho / secao / regua ------------------------------------
ui_header() {  # $1 = titulo da tela
  printf '  %b%s%b  %b%s%b\n' "$ZP$ZB" "$ZSYM" "$ZR" "$ZA$ZB" "$1" "$ZR"
  ui_rule
}
ui_section() { printf '\n  %b%s%b\n' "$ZS$ZB" "$1" "$ZR"; }
ui_rule() { local w; w=$(ui_w "${1:-60}"); printf '  %b' "$ZBRD"; ui_repeat "$UIH" "$w"; printf '%b\n' "$ZR"; }

# --- Par rotulo/valor ---------------------------------------------
ui_kv() {  # $1 rotulo  $2 valor  [$3 cor-do-valor]
  printf '  %b%-14s%b %b%s%b\n' "$ZDISC" "$1" "$ZR" "${3:-$ZWHT}" "$2" "$ZR"
}

# --- Estados (rotulo textual + cor) -------------------------------
ui_state() {  # ok|warn|crit|off|nd  -> 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
}