From 212e94de7614bcf760ff512ea87c664273ea38a6 Mon Sep 17 00:00:00 2001 From: "Aaron D. Lee" Date: Tue, 16 Dec 2025 00:18:39 -0500 Subject: [PATCH] Auto-sync from catchthesethighs --- zsh/functions/motd.zsh | 177 ++++++++++++++++------------------------- 1 file changed, 67 insertions(+), 110 deletions(-) diff --git a/zsh/functions/motd.zsh b/zsh/functions/motd.zsh index 30d6ebf..54ac086 100644 --- a/zsh/functions/motd.zsh +++ b/zsh/functions/motd.zsh @@ -1,145 +1,102 @@ #!/usr/bin/env zsh # ============================================================================ -# Dynamic MOTD (zsh) — PARSE-SAFE GRID LAYOUT +# MOTD — Width-Safe, Color-Safe, Reload-Safe # ============================================================================ -# ---------------------------- Configuration --------------------------------- +# ---------------------------- Guards ----------------------------------------- -MOTD_ENABLED="${MOTD_ENABLED:-true}" -MOTD_MAX_WIDTH=80 -MOTD_LABEL_WIDTH=12 -MOTD_ONCE_VAR="__MOTD_SHOWN" +[[ -o interactive ]] || return +[[ -n $__MOTD_SHOWN ]] && return +typeset -g __MOTD_SHOWN=1 + +# ---------------------------- Configuration ---------------------------------- + +BOX_WIDTH=78 # total width INCLUDING borders +INNER_WIDTH=$(( BOX_WIDTH - 2 )) +LABEL_WIDTH=12 # ---------------------------- Colors ----------------------------------------- autoload -Uz colors && colors -C_RESET="%f%k" C_DIM="%F{242}" C_HEAD="%B%F{39}" C_LABEL="%F{51}" C_OK="%F{82}" +C_RESET="%f%b" -# ---------------------------- Utilities -------------------------------------- +# ---------------------------- Low-level helpers ------------------------------ -_strip_colors() { - local s="$1" - s="${s//\%\{/%}" - s="${s//\%\}/%}" - print -r -- "${(S%%)s}" +repeat_char() { + local ch="$1" n="$2" + printf '%*s' "$n" '' | tr ' ' "$ch" } -_term_width() { - print ${COLUMNS:-$(tput cols 2>/dev/null || echo 80)} +pad_right() { + local s="$1" w="$2" + printf '%-*s' "$w" "$s" } -_box_width() { - local w=$(_term_width) - (( w > MOTD_MAX_WIDTH )) && w=$MOTD_MAX_WIDTH - print $w +center_text() { + local s="$1" w="$2" + local len=${#s} + (( len >= w )) && { print "${s[1,w]}"; return } + local pad=$(( (w - len) / 2 )) + printf '%*s%s%*s' "$pad" '' "$s" "$(( w - len - pad ))" '' } -_hr_top() { - local w=$(_box_width) - print -P -- "${C_DIM}┌${(l:$((w-2))::─:)}┐${C_RESET}" +# ---------------------------- Box primitives --------------------------------- + +box_top() { + print "${C_DIM}┌$(repeat_char '─' $INNER_WIDTH)┐${C_RESET}" } -_hr_bottom() { - local w=$(_box_width) - print -P -- "${C_DIM}└${(l:$((w-2))::─:)}┘${C_RESET}" +box_bottom() { + print "${C_DIM}└$(repeat_char '─' $INNER_WIDTH)┘${C_RESET}" } -_header() { +box_blank() { + print "${C_DIM}│$(repeat_char ' ' $INNER_WIDTH)│${C_RESET}" +} + +box_line() { + local content="$1" + content="$(pad_right "$content" "$INNER_WIDTH")" + print "${C_DIM}│${C_RESET}${content}${C_DIM}│${C_RESET}" +} + +# ---------------------------- Content builders ------------------------------- + +header_line() { local text="$1" - local w=$(_box_width) - local inner=$(( w - 2 )) - - # Strip color + force ASCII dash to avoid Unicode width issues - local raw="$(_strip_colors "$text" | sed 's/—/-/')" - (( ${#raw} > inner )) && raw="${raw[1,inner]}" - - local pad=$(( (inner - ${#raw}) / 2 )) - local rpad=$(( inner - ${#raw} - pad )) - - print -P -- \ - "${C_DIM}│${C_RESET}${(l:$pad:: :)}${text}${(l:$rpad:: :)}${C_DIM}│${C_RESET}" + box_line "$(center_text "$text" "$INNER_WIDTH")" } -_message() { - local msg="$1" - local w=$(_box_width) - local inner=$(( w - 2 )) - - local raw="$(_strip_colors "$msg")" - (( ${#raw} > inner )) && msg="${msg[1,inner]}" - - local fill=$(( inner - ${#raw} )) - - print -P -- \ - "${C_DIM}│${C_RESET}${msg}${(l:$fill:: :)}${C_DIM}│${C_RESET}" +row_line() { + local label="$1" value="$2" + local left="$(pad_right "$label" "$LABEL_WIDTH")" + box_line " ${left} ${value}" } -_blank() { - local w=$(_box_width) - print -P -- "${C_DIM}│${C_RESET}$(printf '%*s' $((w-2)) '')${C_DIM}│${C_RESET}" -} +# ---------------------------- Info providers --------------------------------- -_row() { - local label="$1" - local value="$2" +get_os() { uname -sr } +get_uptime() { uptime | sed 's/.*up *//' | cut -d',' -f1 } +get_load() { uptime | awk -F'load average:' '{print $2}' | xargs } +get_mem() { free -h | awk '/Mem:/ {print $3 "/" $2}' } +get_disk() { df -h / | awk 'NR==2 {print $3 "/" $2 " (" $5 ")"}' } - local w=$(_box_width) - local value_width=$(( w - 4 - MOTD_LABEL_WIDTH )) +# ---------------------------- Render ----------------------------------------- - # Truncate value safely - local raw="$(_strip_colors "$value")" - if (( ${#raw} > value_width )); then - value="${value[1,value_width]}" - fi - - # Recalculate after truncation - raw="$(_strip_colors "$value")" - local pad_len=$(( value_width - ${#raw} )) - (( pad_len < 0 )) && pad_len=0 - - printf -v lpad "%-*s" "$MOTD_LABEL_WIDTH" "$label" - printf -v vpad "%*s" "$pad_len" "" - - print -P -- \ - "${C_DIM}│${C_RESET} ${C_LABEL}${lpad}${C_RESET} ${value}${vpad}${C_DIM}│${C_RESET}" -} - - -# ---------------------------- Info Providers --------------------------------- - -_get_os() { uname -sr } -_get_uptime() { uptime | sed 's/.*up *//' | cut -d',' -f1 } -_get_load() { uptime | awk -F'load average:' '{print $2}' | xargs } -_get_mem() { free -h | awk '/Mem:/ {print $3 "/" $2}' } -_get_disk() { - df -h / | awk 'NR==2 {print $3 "/" $2 " (" $5 ")"}' -} - -# ---------------------------- MOTD ------------------------------------------- - -show_motd() { - [[ -o interactive ]] || return - [[ "$MOTD_ENABLED" != true ]] && return - [[ -n ${(P)MOTD_ONCE_VAR} ]] && return - typeset -g ${MOTD_ONCE_VAR}=1 - - _hr_top - _header "${C_HEAD} $(hostname) ${C_RESET}- ${C_DIM}$(_get_os)${C_RESET}" - _blank - _row "Uptime" "$(_get_uptime)" - _row "Load" "$(_get_load)" - _row "Memory" "$(_get_mem)" - _row "Disk" "$(_get_disk)" - _blank - _message "${C_OK}System up to date${C_RESET}" - _hr_bottom - print -} - -show_motd +box_top +header_line "${C_HEAD}$(hostname)${C_RESET} - ${C_DIM}$(get_os)${C_RESET}" +box_blank +row_line "${C_LABEL}Uptime${C_RESET}" "$(get_uptime)" +row_line "${C_LABEL}Load${C_RESET}" "$(get_load)" +row_line "${C_LABEL}Memory${C_RESET}" "$(get_mem)" +row_line "${C_LABEL}Disk${C_RESET}" "$(get_disk)" +box_blank +box_line " ${C_OK}System up to date${C_RESET}" +box_bottom +print