feat(statusline): rate-limit burndown colors + reset times

- Show 5h reset as HH:MM and 7d reset as "Day HH:MM" from rate_limits.*.resets_at
- Color 5h/7d pill+arrow by burndown vs expected rate (5h: 20%/h, 7d: 14%/day)
- Green if available% >= expected, yellow if >= expected/2, red otherwise
- Add separator between 5h and 7d blocks
This commit is contained in:
2026-05-08 20:42:48 +02:00
parent e2a131a6dc
commit bf8651020e
+50 -16
View File
@@ -49,6 +49,14 @@ LINES_REMOVED=$(echo "$INPUT" | jq -r '.cost.total_lines_removed // 0')
# Rate Limits # Rate Limits
RATE_5H=$(echo "$INPUT" | jq -r '.rate_limits.five_hour.used_percentage // 0' | xargs printf "%.0f") RATE_5H=$(echo "$INPUT" | jq -r '.rate_limits.five_hour.used_percentage // 0' | xargs printf "%.0f")
RATE_7D=$(echo "$INPUT" | jq -r '.rate_limits.seven_day.used_percentage // 0' | xargs printf "%.0f") RATE_7D=$(echo "$INPUT" | jq -r '.rate_limits.seven_day.used_percentage // 0' | xargs printf "%.0f")
RESET_5H_EPOCH=$(echo "$INPUT" | jq -r '.rate_limits.five_hour.resets_at // 0')
RESET_7D_EPOCH=$(echo "$INPUT" | jq -r '.rate_limits.seven_day.resets_at // 0')
# Formatear resets (vacio si epoch=0)
RESET_5H=""
RESET_7D=""
[ "$RESET_5H_EPOCH" -gt 0 ] && RESET_5H=$(date -d "@$RESET_5H_EPOCH" +"%H:%M" 2>/dev/null)
[ "$RESET_7D_EPOCH" -gt 0 ] && RESET_7D=$(date -d "@$RESET_7D_EPOCH" +"%a %H:%M" 2>/dev/null)
# Git info (si estamos en un repo) # Git info (si estamos en un repo)
GIT_BRANCH="" GIT_BRANCH=""
@@ -190,23 +198,49 @@ LINE2="${LINE2} ${GRAY}│${RESET} ${DIM}Total:${RESET} ${CYAN}↓${TOTAL_IN_FMT
# 4. Rate Limits (siempre mostrar) # 4. Rate Limits (siempre mostrar)
LINE2="${LINE2} ${GRAY}${RESET} ${BOLD}Limits:${RESET}" LINE2="${LINE2} ${GRAY}${RESET} ${BOLD}Limits:${RESET}"
# 5h limit # Color por burndown vs tasa esperada
if [ "$RATE_5H" -ge 80 ]; then # Tasa: % consumible permitido por unidad de tiempo (5h: 20%/h, 7d: 14%/dia)
LINE2="${LINE2} ${RED}5h:${RATE_5H}%${RESET}" # expected = tasa * unidades_restantes_hasta_reset
elif [ "$RATE_5H" -ge 50 ]; then # available = 100 - used%
LINE2="${LINE2} ${YELLOW}5h:${RATE_5H}%${RESET}" # verde: available >= expected (consumo bajo control)
else # amarillo: available >= expected/2 (consumo agresivo)
LINE2="${LINE2} ${GREEN}5h:${RATE_5H}%${RESET}" # rojo: available < expected/2 (riesgo de agotar antes del reset)
fi NOW_EPOCH=$(date +%s)
burndown_color() {
local used_pct=$1
local secs_left=$2
local rate=$3
local secs_per_unit=$4
local available=$((100 - used_pct))
if [ "$secs_left" -le 0 ]; then
printf "%s" "$GREEN"; return
fi
local expected
expected=$(echo "scale=2; $rate * $secs_left / $secs_per_unit" | bc)
if (( $(echo "$available >= $expected" | bc -l) )); then
printf "%s" "$GREEN"
elif (( $(echo "$available >= $expected / 2" | bc -l) )); then
printf "%s" "$YELLOW"
else
printf "%s" "$RED"
fi
}
# 7d limit # 5h limit (tasa 20%/h)
if [ "$RATE_7D" -ge 80 ]; then SECS_5H=0
LINE2="${LINE2} ${RED}7d:${RATE_7D}%${RESET}" [ "$RESET_5H_EPOCH" -gt "$NOW_EPOCH" ] && SECS_5H=$((RESET_5H_EPOCH - NOW_EPOCH))
elif [ "$RATE_7D" -ge 50 ]; then C5=$(burndown_color $RATE_5H $SECS_5H 20 3600)
LINE2="${LINE2} ${YELLOW}7d:${RATE_7D}%${RESET}" RESET_5H_STR=""
else [ -n "$RESET_5H" ] && RESET_5H_STR=" ${C5}${RESET_5H}${RESET}"
LINE2="${LINE2} ${GREEN}7d:${RATE_7D}%${RESET}" LINE2="${LINE2} ${C5}5h:${RATE_5H}%${RESET}${RESET_5H_STR} ${GRAY}${RESET}"
fi
# 7d limit (tasa 14%/dia)
SECS_7D=0
[ "$RESET_7D_EPOCH" -gt "$NOW_EPOCH" ] && SECS_7D=$((RESET_7D_EPOCH - NOW_EPOCH))
C7=$(burndown_color $RATE_7D $SECS_7D 14 86400)
RESET_7D_STR=""
[ -n "$RESET_7D" ] && RESET_7D_STR=" ${C7}${RESET_7D}${RESET}"
LINE2="${LINE2} ${C7}7d:${RATE_7D}%${RESET}${RESET_7D_STR}"
# 5. Duración sesión # 5. Duración sesión
if [ "$SESSION_DURATION" -gt 0 ]; then if [ "$SESSION_DURATION" -gt 0 ]; then