Files
claude-config/statusline.sh
T

117 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Claude Code status line. Input: statusline JSON on stdin.
set -uo pipefail
input=$(cat)
eval "$(printf '%s' "$input" | jq -r '
@sh "dir=\(.workspace.current_dir // .cwd // "")",
@sh "model=\(.model.display_name // "")",
@sh "ctx_pct=\(.context_window.used_percentage // "")",
@sh "ctx_used=\(.context_window.total_input_tokens // "")",
@sh "ctx_size=\(.context_window.context_window_size // "")",
@sh "five=\(.rate_limits.five_hour.used_percentage // "")",
@sh "five_reset=\(.rate_limits.five_hour.resets_at // "")",
@sh "week=\(.rate_limits.seven_day.used_percentage // "")",
@sh "week_reset=\(.rate_limits.seven_day.resets_at // "")"
')"
DIM=$'\033[2m'
RESET=$'\033[0m'
CYAN=$'\033[36m'
GREEN=$'\033[32m'
YELLOW=$'\033[33m'
RED=$'\033[31m'
# Green under 50%, yellow under 80%, red above.
usage_color() {
local pct=${1%%.*}
if [ "$pct" -ge 80 ]; then printf '%s' "$RED"
elif [ "$pct" -ge 50 ]; then printf '%s' "$YELLOW"
else printf '%s' "$GREEN"
fi
}
humanize_tokens() {
local n=$1
if [ "$n" -ge 1000000 ]; then printf '%d.%dM' "$(( n / 1000000 ))" "$(( n % 1000000 / 100000 ))"
elif [ "$n" -ge 1000 ]; then printf '%dk' "$(( n / 1000 ))"
else printf '%d' "$n"
fi
}
# " 2h15m left (01:23)" for a reset epoch. Weekday shown when it lands on another day.
reset_suffix() {
local epoch=$1 secs at fmt
epoch=$(printf '%.0f' "$epoch")
secs=$(( epoch - $(date +%s) ))
[ "$secs" -lt 0 ] && secs=0
if [ "$secs" -ge 86400 ]; then
printf ' %s%dd%dh left' "$DIM" "$(( secs / 86400 ))" "$(( secs % 86400 / 3600 ))"
elif [ "$secs" -ge 3600 ]; then
printf ' %s%dh%dm left' "$DIM" "$(( secs / 3600 ))" "$(( secs % 3600 / 60 ))"
else
printf ' %s%dm left' "$DIM" "$(( secs / 60 ))"
fi
fmt='%H:%M'
[ "$(date -d "@$epoch" +%F 2>/dev/null)" != "$(date +%F)" ] && fmt='%a %H:%M'
at=$(date -d "@$epoch" "+$fmt" 2>/dev/null)
[ -n "$at" ] && printf ' (%s)' "$at"
printf '%s' "$RESET"
}
row1=()
row2=()
home=${HOME:-}
if [ -n "$dir" ]; then
shortdir=$dir
[ -n "$home" ] && shortdir=${dir/#$home/\~}
row1+=("${CYAN}${shortdir}${RESET}")
branch=$(git -C "$dir" branch --show-current 2>/dev/null)
[ -n "$branch" ] && row1+=("${DIM}${branch}${RESET}")
fi
[ -n "$model" ] && row1+=("${DIM}${model}${RESET}")
if [ -n "$ctx_pct" ]; then
pct=$(printf '%.0f' "$ctx_pct")
tokens=""
[ -n "$ctx_used" ] && [ -n "$ctx_size" ] && tokens=" ${DIM}$(humanize_tokens "$ctx_used")/$(humanize_tokens "$ctx_size")${RESET}"
row2+=("${DIM}ctx${RESET} $(usage_color "$pct")${pct}%${RESET}${tokens}")
fi
if [ -n "$five" ]; then
pct=$(printf '%.0f' "$five")
left=""
[ -n "$five_reset" ] && left=$(reset_suffix "$five_reset")
row2+=("${DIM}5h${RESET} $(usage_color "$pct")${pct}%${RESET}${left}")
fi
if [ -n "$week" ]; then
pct=$(printf '%.0f' "$week")
left=""
[ -n "$week_reset" ] && left=$(reset_suffix "$week_reset")
row2+=("${DIM}7d${RESET} $(usage_color "$pct")${pct}%${RESET}${left}")
fi
join_parts() {
local sep="${DIM} | ${RESET}" out="" part
for part in "$@"; do
[ -n "$out" ] && out+="$sep"
out+="$part"
done
printf '%s' "$out"
}
out=""
for line in "$(join_parts ${row1[@]+"${row1[@]}"})" "$(join_parts ${row2[@]+"${row2[@]}"})"; do
[ -z "$line" ] && continue
[ -n "$out" ] && out+=$'\n'
out+="$line"
done
printf '%s' "$out"