Auto-sync from catchthesethighs

This commit is contained in:
Aaron D. Lee
2025-12-16 00:18:39 -05:00
parent 58fd306ec9
commit 212e94de76

View File

@@ -1,145 +1,102 @@
#!/usr/bin/env zsh #!/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}" [[ -o interactive ]] || return
MOTD_MAX_WIDTH=80 [[ -n $__MOTD_SHOWN ]] && return
MOTD_LABEL_WIDTH=12 typeset -g __MOTD_SHOWN=1
MOTD_ONCE_VAR="__MOTD_SHOWN"
# ---------------------------- Configuration ----------------------------------
BOX_WIDTH=78 # total width INCLUDING borders
INNER_WIDTH=$(( BOX_WIDTH - 2 ))
LABEL_WIDTH=12
# ---------------------------- Colors ----------------------------------------- # ---------------------------- Colors -----------------------------------------
autoload -Uz colors && colors autoload -Uz colors && colors
C_RESET="%f%k"
C_DIM="%F{242}" C_DIM="%F{242}"
C_HEAD="%B%F{39}" C_HEAD="%B%F{39}"
C_LABEL="%F{51}" C_LABEL="%F{51}"
C_OK="%F{82}" C_OK="%F{82}"
C_RESET="%f%b"
# ---------------------------- Utilities -------------------------------------- # ---------------------------- Low-level helpers ------------------------------
_strip_colors() { repeat_char() {
local s="$1" local ch="$1" n="$2"
s="${s//\%\{/%}" printf '%*s' "$n" '' | tr ' ' "$ch"
s="${s//\%\}/%}"
print -r -- "${(S%%)s}"
} }
_term_width() { pad_right() {
print ${COLUMNS:-$(tput cols 2>/dev/null || echo 80)} local s="$1" w="$2"
printf '%-*s' "$w" "$s"
} }
_box_width() { center_text() {
local w=$(_term_width) local s="$1" w="$2"
(( w > MOTD_MAX_WIDTH )) && w=$MOTD_MAX_WIDTH local len=${#s}
print $w (( len >= w )) && { print "${s[1,w]}"; return }
local pad=$(( (w - len) / 2 ))
printf '%*s%s%*s' "$pad" '' "$s" "$(( w - len - pad ))" ''
} }
_hr_top() { # ---------------------------- Box primitives ---------------------------------
local w=$(_box_width)
print -P -- "${C_DIM}${(l:$((w-2)):::)}${C_RESET}" box_top() {
print "${C_DIM}$(repeat_char '─' $INNER_WIDTH)${C_RESET}"
} }
_hr_bottom() { box_bottom() {
local w=$(_box_width) print "${C_DIM}$(repeat_char '─' $INNER_WIDTH)${C_RESET}"
print -P -- "${C_DIM}${(l:$((w-2)):::)}${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 text="$1"
local w=$(_box_width) box_line "$(center_text "$text" "$INNER_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}"
} }
_message() { row_line() {
local msg="$1" local label="$1" value="$2"
local w=$(_box_width) local left="$(pad_right "$label" "$LABEL_WIDTH")"
local inner=$(( w - 2 )) box_line " ${left} ${value}"
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}"
} }
_blank() { # ---------------------------- Info providers ---------------------------------
local w=$(_box_width)
print -P -- "${C_DIM}${C_RESET}$(printf '%*s' $((w-2)) '')${C_DIM}${C_RESET}"
}
_row() { get_os() { uname -sr }
local label="$1" get_uptime() { uptime | sed 's/.*up *//' | cut -d',' -f1 }
local value="$2" 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) # ---------------------------- Render -----------------------------------------
local value_width=$(( w - 4 - MOTD_LABEL_WIDTH ))
# Truncate value safely box_top
local raw="$(_strip_colors "$value")" header_line "${C_HEAD}$(hostname)${C_RESET} - ${C_DIM}$(get_os)${C_RESET}"
if (( ${#raw} > value_width )); then box_blank
value="${value[1,value_width]}" row_line "${C_LABEL}Uptime${C_RESET}" "$(get_uptime)"
fi row_line "${C_LABEL}Load${C_RESET}" "$(get_load)"
row_line "${C_LABEL}Memory${C_RESET}" "$(get_mem)"
# Recalculate after truncation row_line "${C_LABEL}Disk${C_RESET}" "$(get_disk)"
raw="$(_strip_colors "$value")" box_blank
local pad_len=$(( value_width - ${#raw} )) box_line " ${C_OK}System up to date${C_RESET}"
(( pad_len < 0 )) && pad_len=0 box_bottom
print
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