# shellcheck shell=sh# lib/ui.sh — terminal output helpers (color, status lines, tables, prompts).# Pure presentation: knows nothing about the database. Depends only on config.sh.# Color is on when stdout is a TTY and NO_COLOR is unset; override with PE_COLOR.if[ -z "${PE_COLOR:-}"]; thenif[ -t 1]&&[ -z "${NO_COLOR:-}"]; thenPE_COLOR=1; elsePE_COLOR=0; fifi# pe_fg '#rrggbb' -> 24-bit foreground escape (empty when color disabled).pe_fg(){["$PE_COLOR"=1]||return0_h=${1#\#}_r=${_h%????}; _t=${_h#??}; _g=${_t%??}; _b=${_h#????}printf'\033[38;2;%d;%d;%dm'"0x$_r""0x$_g""0x$_b"}pe_bold(){["$PE_COLOR"=1]&&printf'\033[1m'; }pe_dim(){["$PE_COLOR"=1]&&printf'\033[2m'; }pe_reset(){["$PE_COLOR"=1]&&printf'\033[0m'; }# Status lines (all to stderr so they never pollute machine-readable stdout).pe_ok(){printf'%s%s%s %s\n'"$(pe_fg "$PE_RP_FOAM")"'[+]'"$(pe_reset)""$*" >&2; }pe_info(){printf'%s%s%s %s\n'"$(pe_fg "$PE_RP_IRIS")"'[*]'"$(pe_reset)""$*" >&2; }pe_warn(){printf'%s%s%s %s\n'"$(pe_fg "$PE_RP_GOLD")"'[!]'"$(pe_reset)""$*" >&2; }pe_err(){printf'%s%s%s %s\n'"$(pe_fg "$PE_RP_LOVE")"'[-]'"$(pe_reset)""$*" >&2; }# pe_die [code] message... -> print error and exit (default code 1).pe_die(){case$1in(''|*[!0-9]*)_code=1 ;; (*)_code=$1; shift ;; esac pe_err "$@"exit"$_code"}# pe_need cmd [cmd...] -> die if any command is missing from PATH.pe_need(){for _c in"$@"; docommand -v "$_c" >/dev/null 2>&1|| pe_die "required command not found: $_c"done}# Parser / SQL-boundary validators. Call pe_require_value with the caller's# remaining argv (for example: `pe_require_value "$@"`) so an explicitly empty# value remains valid while a missing second argument is rejected.pe_require_value(){["$#" -ge 2]|| pe_die "option $1 requires a value"}pe_require_uint(){pe__number_label=$1pe__number_value=${2:-}case$pe__number_valuein''|*[!0-9]*) pe_die "$pe__number_label must be an unsigned integer" ;;
esac}pe_require_bool(){pe__bool_label=$1pe__bool_value=${2:-}case$pe__bool_valuein0|1) : ;;
*) pe_die "$pe__bool_label must be 0 or 1" ;;
esac}pe_require_port(){pe__port_value=${1:-} pe_require_uint port "$pe__port_value"# Strip leading zeroes before the shell's integer comparison so values such# as 0080 are decimal and very large input is rejected without overflow.pe__port_decimal=$(printf'%s'"$pe__port_value" | sed 's/^0*//')[ -n "$pe__port_decimal"]||pe__port_decimal=0if["${#pe__port_decimal}" -gt 5]\||["$pe__port_decimal" -lt 1]\||["$pe__port_decimal" -gt 65535]; then pe_die "port must be between 1 and 65535"fi}# pe_confirm "question" -> 0 on y/Y, 1 otherwise. Defaults to NO.pe_confirm(){printf'%s%s%s [y/N] '"$(pe_fg "$PE_RP_GOLD")""$1""$(pe_reset)" >&2read -r _ans </dev/tty ||return1case$_ansin(y|Y|yes|YES)return0 ;; (*)return1 ;; esac}# pe_confirm_destructive WORD "warning" -> require user to type WORD exactly.# Used for irreversible operations. Warning printed plainly (no caveman).pe_confirm_destructive(){_word=$1; shiftprintf'%s[!] %s%s\n'"$(pe_fg "$PE_RP_LOVE")""$*""$(pe_reset)" >&2printf'Type %s to confirm: '"$_word" >&2read -r _ans </dev/tty ||return1["$_ans"="$_word"]}# pe_table "Col1|Col2|..." < pipe-delimited-rows# Aligns columns and colorizes the header row. Empty fields are rendered as '-'# first, because `column -t` silently drops empty fields and shifts columns.pe_table(){_hdr=$1{printf'%s\n'"$_hdr"; cat; }\ | awk -F'|''BEGIN { OFS="|" } { for (i=1; i<=NF; i++) if ($i=="") $i="-"; print }'\ | {ifcommand -v column >/dev/null 2>&1; then column -t -s '|'else sed 's/|/ | /g'fi}\ | awk -v h="$(pe_fg "$PE_RP_IRIS")$(pe_bold)" -v r="$(pe_reset)"\'NR==1 { print h $0 r; next } { print }'}# pe_empty "message" -> friendly note for empty result sets.pe_empty(){printf'%s%s%s\n'"$(pe_dim)""$1""$(pe_reset)" >&2; }