# 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:-}" ]; then if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then PE_COLOR=1; else PE_COLOR=0; fi fi # pe_fg '#rrggbb' -> 24-bit foreground escape (empty when color disabled). pe_fg() { [ "$PE_COLOR" = 1 ] || return 0 _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 $1 in (''|*[!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 "$@"; do command -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=$1 pe__number_value=${2:-} case $pe__number_value in ''|*[!0-9]*) pe_die "$pe__number_label must be an unsigned integer" ;; esac } pe_require_bool() { pe__bool_label=$1 pe__bool_value=${2:-} case $pe__bool_value in 0|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=0 if [ "${#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)" >&2 read -r _ans require user to type WORD exactly. # Used for irreversible operations. Warning printed plainly (no caveman). pe_confirm_destructive() { _word=$1; shift printf '%s[!] %s%s\n' "$(pe_fg "$PE_RP_LOVE")" "$*" "$(pe_reset)" >&2 printf 'Type %s to confirm: ' "$_word" >&2 read -r _ans /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; }