Auto-sync from catchthesethighs

This commit is contained in:
Aaron D. Lee
2025-12-16 00:13:14 -05:00
parent 383d72e6d9
commit 58fd306ec9
9 changed files with 114 additions and 151 deletions

BIN
zsh/.zshrc.zwc Normal file

Binary file not shown.

BIN
zsh/aliases.zsh.zwc Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -1,182 +1,145 @@
#!/usr/bin/env zsh #!/usr/bin/env zsh
# ============================================================================ # ============================================================================
# Dynamic MOTD - Compact System Info on Shell Start # Dynamic MOTD (zsh) — PARSE-SAFE GRID LAYOUT
# ============================================================================
# A beautiful, informative welcome screen for your terminal
#
# Add to .zshrc:
# source ~/.dotfiles/zsh/functions/motd.zsh
# show_motd # or call automatically
# ============================================================================ # ============================================================================
# ============================================================================ # ---------------------------- Configuration ---------------------------------
# Configuration
# ============================================================================
typeset -g MOTD_ENABLED="${MOTD_ENABLED:-true}" MOTD_ENABLED="${MOTD_ENABLED:-true}"
typeset -g MOTD_SHOW_WEATHER="${MOTD_SHOW_WEATHER:-false}" MOTD_MAX_WIDTH=80
typeset -g MOTD_WEATHER_LOCATION="${MOTD_WEATHER_LOCATION:-}" MOTD_LABEL_WIDTH=12
MOTD_ONCE_VAR="__MOTD_SHOWN"
# Colors # ---------------------------- Colors -----------------------------------------
typeset -g M_RESET=$'\033[0m'
typeset -g M_BOLD=$'\033[1m'
typeset -g M_DIM=$'\033[2m'
typeset -g M_CYAN=$'\033[36m'
typeset -g M_GREEN=$'\033[32m'
typeset -g M_YELLOW=$'\033[33m'
typeset -g M_RED=$'\033[31m'
typeset -g M_BLUE=$'\033[34m'
typeset -g M_MAGENTA=$'\033[35m'
# ============================================================================ autoload -Uz colors && colors
# Data Collection Functions
# ============================================================================
_motd_hostname() { C_RESET="%f%k"
hostname -s 2>/dev/null || echo "${HOST:-unknown}" C_DIM="%F{242}"
C_HEAD="%B%F{39}"
C_LABEL="%F{51}"
C_OK="%F{82}"
# ---------------------------- Utilities --------------------------------------
_strip_colors() {
local s="$1"
s="${s//\%\{/%}"
s="${s//\%\}/%}"
print -r -- "${(S%%)s}"
} }
_motd_user() { _term_width() {
echo "${USER:-$(whoami)}" print ${COLUMNS:-$(tput cols 2>/dev/null || echo 80)}
} }
_motd_uptime() { _box_width() {
if [[ "$OSTYPE" == "darwin"* ]]; then local w=$(_term_width)
local boot=$(sysctl -n kern.boottime | awk '{print $4}' | tr -d ',') (( w > MOTD_MAX_WIDTH )) && w=$MOTD_MAX_WIDTH
local now=$(date +%s) print $w
local diff=$((now - boot)) }
else
local diff=$(cat /proc/uptime 2>/dev/null | cut -d. -f1) _hr_top() {
local w=$(_box_width)
print -P -- "${C_DIM}${(l:$((w-2)):::)}${C_RESET}"
}
_hr_bottom() {
local w=$(_box_width)
print -P -- "${C_DIM}${(l:$((w-2)):::)}${C_RESET}"
}
_header() {
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}"
}
_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}"
}
_blank() {
local w=$(_box_width)
print -P -- "${C_DIM}${C_RESET}$(printf '%*s' $((w-2)) '')${C_DIM}${C_RESET}"
}
_row() {
local label="$1"
local value="$2"
local w=$(_box_width)
local value_width=$(( w - 4 - MOTD_LABEL_WIDTH ))
# Truncate value safely
local raw="$(_strip_colors "$value")"
if (( ${#raw} > value_width )); then
value="${value[1,value_width]}"
fi fi
[[ -z "$diff" ]] && { echo "?"; return; } # Recalculate after truncation
raw="$(_strip_colors "$value")"
local pad_len=$(( value_width - ${#raw} ))
(( pad_len < 0 )) && pad_len=0
local days=$((diff / 86400)) printf -v lpad "%-*s" "$MOTD_LABEL_WIDTH" "$label"
local hours=$(((diff % 86400) / 3600)) printf -v vpad "%*s" "$pad_len" ""
local mins=$(((diff % 3600) / 60))
if [[ $days -gt 0 ]]; then print -P -- \
echo "${days}d ${hours}h" "${C_DIM}${C_RESET} ${C_LABEL}${lpad}${C_RESET} ${value}${vpad}${C_DIM}${C_RESET}"
elif [[ $hours -gt 0 ]]; then
echo "${hours}h ${mins}m"
else
echo "${mins}m"
fi
} }
_motd_cpu() {
if [[ "$OSTYPE" == "darwin"* ]]; then # ---------------------------- Info Providers ---------------------------------
local cpu=$(top -l 1 -n 0 2>/dev/null | grep "CPU usage" | awk '{print int($3)}')
else _get_os() { uname -sr }
local cpu=$(top -bn1 2>/dev/null | grep "Cpu(s)" | awk '{print int($2)}') _get_uptime() { uptime | sed 's/.*up *//' | cut -d',' -f1 }
[[ -z "$cpu" ]] && cpu=$(cat /proc/stat 2>/dev/null | head -1 | awk '{print int(($2+$4)*100/($2+$4+$5))}') _get_load() { uptime | awk -F'load average:' '{print $2}' | xargs }
fi _get_mem() { free -h | awk '/Mem:/ {print $3 "/" $2}' }
echo "${cpu:-?}%" _get_disk() {
df -h / | awk 'NR==2 {print $3 "/" $2 " (" $5 ")"}'
} }
_motd_memory() { # ---------------------------- MOTD -------------------------------------------
if [[ "$OSTYPE" == "darwin"* ]]; then
local total=$(sysctl -n hw.memsize 2>/dev/null)
local used=$(vm_stat 2>/dev/null | awk '/Pages active/ {print $3}' | tr -d '.')
if [[ -n "$total" && -n "$used" ]]; then
local total_gb=$((total / 1073741824))
local used_gb=$((used * 4096 / 1073741824))
echo "${used_gb}/${total_gb}G"
else
echo "?G"
fi
else
local mem=$(free -h 2>/dev/null | awk '/^Mem:/ {print $3"/"$2}')
echo "${mem:-?G}" | sed 's/i//g'
fi
}
_motd_disk() {
local disk=$(df -h ~ 2>/dev/null | awk 'NR==2 {print $4}')
echo "${disk:-?}free"
}
_motd_load() {
local load=$(uptime 2>/dev/null | awk -F'load average:' '{print $2}' | awk -F',' '{print $1}' | tr -d ' ')
echo "${load:-?}"
}
_motd_date() {
date '+%a %b %d'
}
_motd_time() {
date '+%H:%M'
}
# ============================================================================
# Display Function
# ============================================================================
show_motd() { show_motd() {
[[ "$MOTD_ENABLED" != "true" ]] && return [[ -o interactive ]] || return
[[ "$MOTD_ENABLED" != true ]] && return
[[ -n ${(P)MOTD_ONCE_VAR} ]] && return
typeset -g ${MOTD_ONCE_VAR}=1
# Collect data _hr_top
local user=$(_motd_user) _header "${C_HEAD} $(hostname) ${C_RESET}- ${C_DIM}$(_get_os)${C_RESET}"
local host=$(_motd_hostname) _blank
local uptime=$(_motd_uptime) _row "Uptime" "$(_get_uptime)"
local cpu=$(_motd_cpu) _row "Load" "$(_get_load)"
local mem=$(_motd_memory) _row "Memory" "$(_get_mem)"
local disk=$(_motd_disk) _row "Disk" "$(_get_disk)"
local load=$(_motd_load) _blank
local dotfiles=$(_motd_dotfiles_status) _message "${C_OK}System up to date${C_RESET}"
local dt=$(_motd_date) _hr_bottom
local tm=$(_motd_time) print
# Build status indicators
local status_line=""
# Print compact MOTD
echo
echo "${M_DIM}┌──────────────────────────────────────────────────────────────┐${M_RESET}"
printf "${M_DIM}${M_RESET} ${M_BOLD}${M_CYAN}${M_RESET} ${M_BOLD}%s${M_RESET}@${M_CYAN}%s${M_RESET}%*s${M_DIM}%s %s${M_RESET} ${M_DIM}${M_RESET}\n" \
"$user" "$host" $((40 - ${#user} - ${#host})) "" "$dt" "$tm"
echo "${M_DIM}├──────────────────────────────────────────────────────────────┤${M_RESET}"
printf "${M_DIM}${M_RESET} ▲ %-8s ◆ %-10s ◇ %-10s ⊡ %-8s ${M_DIM}${M_RESET}\n" \
"up:$uptime" "cpu:$cpu" "mem:$mem" "$disk"
if [[ -n "$status_line" ]]; then
echo "${M_DIM}├──────────────────────────────────────────────────────────────┤${M_RESET}"
printf "${M_DIM}${M_RESET} %s%*s${M_DIM}${M_RESET}\n" "$status_line" $((62 - ${#status_line} + 30)) ""
fi
echo "${M_DIM}└──────────────────────────────────────────────────────────────┘${M_RESET}"
echo
} }
# Alias show_motd
motd() { show_motd; }
# ============================================================================
# Minimal Version (single line)
# ============================================================================
show_motd_mini() {
[[ "$MOTD_ENABLED" != "true" ]] && return
local user=$(_motd_user)
local host=$(_motd_hostname)
local uptime=$(_motd_uptime)
local mem=$(_motd_memory)
local updates=$(_motd_updates)
local info="${M_CYAN}${user}@${host}${M_RESET}"
info+=" ${M_DIM}${M_RESET}${uptime}"
info+=" ${M_DIM}${M_RESET}${mem}"
[[ "$updates" -gt 0 ]] && info+=" ${M_DIM}${M_RESET} ${M_GREEN}${updates}${M_RESET}"
echo "$info"
echo
}
# ============================================================================
# Auto-show on source (optional)
# ============================================================================
# Uncomment to show automatically:
# show_motd

BIN
zsh/functions/motd.zsh.zwc Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.