Skip to content

Instantly share code, notes, and snippets.

@scottsauberlt
Last active August 10, 2026 14:22
Show Gist options
  • Select an option

  • Save scottsauberlt/1fb0076c9ee7c59cf58ecdd746cbd83f to your computer and use it in GitHub Desktop.

Select an option

Save scottsauberlt/1fb0076c9ee7c59cf58ecdd746cbd83f to your computer and use it in GitHub Desktop.
statusline.sh
#!/bin/bash
# Claude Code status line: context token usage + usage limits + git working-tree summary.
# Receives session JSON on stdin (see code.claude.com/docs/en/statusline).
input=$(cat)
# --- Context window usage (provided directly, no transcript parsing needed) ---
tokens=$(printf '%s' "$input" | jq -r '.context_window.total_input_tokens // 0')
pct=$(printf '%s' "$input" | jq -r '(.context_window.used_percentage // 0) | floor')
exceeds=$(printf '%s' "$input" | jq -r '.exceeds_200k_tokens // false')
# Human-friendly token count (e.g. 45.2k)
if [ "$tokens" -ge 1000 ] 2>/dev/null; then
tok_label=$(awk "BEGIN{printf \"%.1fk\", $tokens/1000}")
else
tok_label="$tokens"
fi
ctx="🧠 ${tok_label} tokens (${pct}%)"
[ "$exceeds" = "true" ] && ctx="⚠️ ${ctx}"
# --- Usage limits: week / session, colored by how close to maxing out ---
# green < 50%, yellow 50-79%, red >= 80%
color_pct() {
local p=$1
if [ "$p" -ge 80 ]; then printf '\033[31m%s%%\033[0m' "$p"
elif [ "$p" -ge 50 ]; then printf '\033[33m%s%%\033[0m' "$p"
else printf '\033[32m%s%%\033[0m' "$p"
fi
}
week_pct=$(printf '%s' "$input" | jq -r '.rate_limits.seven_day.used_percentage // empty | floor')
sess_pct=$(printf '%s' "$input" | jq -r '.rate_limits.five_hour.used_percentage // empty | floor')
limits=""
[ -n "$week_pct" ] && limits="Wk $(color_pct "$week_pct")"
[ -n "$sess_pct" ] && limits="${limits:+$limits · }5h $(color_pct "$sess_pct")"
[ -n "$limits" ] && ctx="${ctx} | 📊 ${limits}"
# --- Current directory ---
dir=$(printf '%s' "$input" | jq -r '.workspace.current_dir // .cwd // "."')
dir_label=$(basename "$dir")
# --- Model ---
model_name=$(printf '%s' "$input" | jq -r '.model.display_name // empty')
model_label="🤖 ${model_name}"
# --- Git working-tree summary ---
if git -C "$dir" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
branch=$(git -C "$dir" symbolic-ref --short -q HEAD 2>/dev/null || git -C "$dir" rev-parse --short HEAD 2>/dev/null)
porcelain=$(git -C "$dir" status --porcelain 2>/dev/null)
if [ -n "$porcelain" ]; then
changed=$(printf '%s\n' "$porcelain" | grep -vc '^??')
untracked=$(printf '%s\n' "$porcelain" | grep -c '^??')
else
changed=0
untracked=0
fi
# Commits on HEAD that have not reached any remote-tracking branch. Beats
# @{upstream}..HEAD, which fails on never-pushed branches and pruned upstreams.
unpushed=0
if [ -n "$(git -C "$dir" remote 2>/dev/null)" ]; then
unpushed=$(git -C "$dir" rev-list --count HEAD --not --remotes 2>/dev/null)
fi
[ -n "$unpushed" ] || unpushed=0
if [ "$unpushed" -gt 0 ] 2>/dev/null; then
unpushed_label=$(printf '\033[33m⬆ %s\033[0m' "$unpushed")
else
unpushed_label="⬆ 0"
fi
git_label="📁 ${dir_label} (${branch}) | ✏️ ${changed} · ❓ ${untracked} · ${unpushed_label}"
else
git_label="📁 ${dir_label} | (not a git repo)"
fi
printf '%s | %s | %s' "$git_label" "$ctx" "$model_label"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment