#!/bin/sh
# Native status-right modules - no plugins, macOS, Nerd Font icons.
# Emits tmux style sequences (fg only, so the caller's bg carries through).

DIM='#565f89'; CYAN='#7dcfff'; GREEN='#9ece6a'; YELLOW='#e0af68'; RED='#f7768e'

heat() { # value warn crit -> colour
  if   [ "$1" -ge "$3" ]; then printf '%s' "$RED"
  elif [ "$1" -ge "$2" ]; then printf '%s' "$YELLOW"
  else printf '%s' "$GREEN"; fi
}

# CPU + RAM in a single ps pass
eval "$(ps -A -o %cpu,%mem | awk -v n="$(sysctl -n hw.ncpu)" \
  'NR>1{c+=$1; m+=$2} END {printf "CPU=%.0f RAM=%.0f", c/n, m}')"

# Uptime, trimmed to the duration only
UP=$(uptime | sed -E 's/^.*up +//; s/,? *[0-9]+ users?.*$//; s/,$//; s/ *$//')

# Battery: level, charge state, level-appropriate icon
BATT_RAW=$(pmset -g batt)
BATT=$(printf '%s' "$BATT_RAW" | grep -Eo '[0-9]+%' | head -1 | tr -d '%')
printf '%s' "$BATT_RAW" | grep -q 'AC Power' && PLUG=' ' || PLUG=''
B=${BATT:-100}
if   [ "$B" -ge 90 ]; then BICON=''
elif [ "$B" -ge 65 ]; then BICON=''
elif [ "$B" -ge 40 ]; then BICON=''
elif [ "$B" -ge 15 ]; then BICON=''
else                       BICON=''; fi
if   [ "$B" -le 15 ]; then BCOL="$RED"
elif [ "$B" -le 30 ]; then BCOL="$YELLOW"
else                       BCOL="$GREEN"; fi

printf '#[fg=%s]#[fg=%s] %s%%  ' "$DIM" "$(heat "$CPU" 60 85)" "$CPU"
printf '#[fg=%s]#[fg=%s] %s%%  ' "$DIM" "$(heat "$RAM" 70 90)" "$RAM"
printf '#[fg=%s]#[fg=%s] %s  ' "$DIM" "$CYAN" "$UP"
[ -n "$BATT" ] && printf '#[fg=%s]%s%s#[fg=%s] %s%%' "$DIM" "$BICON" "$PLUG" "$BCOL" "$BATT"
