Skip to content

Instantly share code, notes, and snippets.

@mzelzoghbi
Created April 14, 2026 18:26
Show Gist options
  • Select an option

  • Save mzelzoghbi/c84876263251194d2f0b0d3546f7b088 to your computer and use it in GitHub Desktop.

Select an option

Save mzelzoghbi/c84876263251194d2f0b0d3546f7b088 to your computer and use it in GitHub Desktop.
Claude Code statusline — 3-line status bar with git, context usage, thinking mode, rate limits, dark/light theme
#!/usr/bin/env bash
# ~/.claude/statusline-command.sh
# Two-line Claude Code status bar.
# Requires: jq (brew install jq)
# Optional: ccusage (npm i -g ccusage) for real 5-hour / weekly rate-limit data.
# ── ANSI escape codes (bash $'...' syntax so \033 is interpreted) ─────────────
RST=$'\033[0m'
BOLD=$'\033[1m'
DIM=$'\033[2m'
CYAN=$'\033[36m'
# ── Theme detection: dark (default) vs light ─────────────────────────────────
# Priority: 1) $CLAUDE_STATUSLINE_THEME override, 2) $COLORFGBG env var
# (set by iTerm2/Konsole/rxvt — format "fg;bg", bg digits 0-6 = dark, 7-15 = light),
# 3) macOS system appearance, 4) fall back to dark.
theme="${CLAUDE_STATUSLINE_THEME:-}"
if [ -z "$theme" ] && [ -n "$COLORFGBG" ]; then
bg="${COLORFGBG##*;}"
case "$bg" in 7|15) theme="light" ;; *) theme="dark" ;; esac
fi
if [ -z "$theme" ] && command -v defaults >/dev/null 2>&1; then
if defaults read -g AppleInterfaceStyle >/dev/null 2>&1; then
theme="dark"
else
theme="light"
fi
fi
[ -z "$theme" ] && theme="dark"
if [ "$theme" = "light" ]; then
# Darker, higher-contrast palette for light backgrounds
GREEN=$'\033[38;2;46;139;61m' # #2E8B3D
YELLOW=$'\033[38;2;184;134;11m' # #B8860B (dark goldenrod — readable on white)
RED=$'\033[38;2;192;57;43m' # #C0392B
BLUE_MODEL=$'\033[38;2;10;102;207m' # #0A66CF
ORANGE_CTX=$'\033[38;2;199;122;42m' # #C77A2A
else
# Vivid palette for dark backgrounds (original)
GREEN=$'\033[32m'
YELLOW=$'\033[33m'
RED=$'\033[31m'
BLUE_MODEL=$'\033[38;2;10;153;247m' # #0A99F7
ORANGE_CTX=$'\033[38;2;242;169;93m' # #F2A95D
fi
SEP=" | "
# ── Read stdin JSON ────────────────────────────────────────────────────────────
input=$(cat)
# ── Parse fields from Claude Code JSON ────────────────────────────────────────
model_name=$(printf '%s' "$input" | jq -r '.model.display_name // "Unknown"')
ctx_size=$(printf '%s' "$input" | jq -r '.context_window.context_window_size // 0')
transcript_path=$(printf '%s' "$input" | jq -r '.transcript_path // ""')
rl5_pct=$(printf '%s' "$input" | jq -r '.rate_limits.five_hour.used_percentage // empty')
rl5_reset=$(printf '%s' "$input" | jq -r '.rate_limits.five_hour.resets_at // empty')
rl7_pct=$(printf '%s' "$input" | jq -r '.rate_limits.seven_day.used_percentage // empty')
rl7_reset=$(printf '%s' "$input" | jq -r '.rate_limits.seven_day.resets_at // empty')
workspace_dir=$(printf '%s' "$input" | jq -r '.workspace.current_dir // empty')
[ -z "$workspace_dir" ] && workspace_dir="$(pwd)"
# TODO: verify .mcp_servers field path against Claude Code statusline docs.
mcp_count=$(printf '%s' "$input" | jq -r '(.mcp_servers // []) | length // 0')
# ── Helpers ───────────────────────────────────────────────────────────────────
# Human-readable context window label: 1000000 → "1M context"
ctx_label() {
local sz="$1"
if [ "$sz" -ge 1000000 ]; then echo "$(( sz / 1000000 ))M context"
elif [ "$sz" -ge 1000 ]; then echo "$(( sz / 1000 ))k context"
else echo "${sz} context"; fi
}
# 10-cell sub-cell block bar from integer pct (0-100)
# Filled: █ Fractional last cell: ▏▎▍▌▋▊▉ Empty: ─
make_bar() {
local pct="${1:-0}"
[ "$pct" -gt 100 ] && pct=100
[ "$pct" -lt 0 ] && pct=0
local filled=$(( pct / 10 ))
local remainder=$(( pct % 10 ))
local frac_idx=$(( remainder * 8 / 10 ))
local frac_chars=( '' '▏' '▎' '▍' '▌' '▋' '▊' '▉' )
local bar="" i=0
while [ "$i" -lt "$filled" ]; do bar="${bar}█"; i=$(( i + 1 )); done
if [ "$frac_idx" -gt 0 ] && [ "$filled" -lt 10 ]; then
bar="${bar}${frac_chars[$frac_idx]}"
i=$(( i + 1 ))
fi
while [ "$i" -lt 10 ]; do bar="${bar}─"; i=$(( i + 1 )); done
printf '%s' "$bar"
}
# ANSI color string for a percentage (green < 50, yellow < 80, red ≥ 80)
bar_color() {
local pct="${1:-0}"
if [ "$pct" -lt 50 ]; then printf '%s' "$GREEN"
elif [ "$pct" -lt 80 ]; then printf '%s' "$YELLOW"
else printf '%s' "$RED"; fi
}
# Number with thousands commas (pure awk, locale-safe)
fmt_num() {
printf '%s' "$1" | awk '{
n=int($1); s=""
while(n>=1000){s=","sprintf("%03d",n%1000)s; n=int(n/1000)}
print n s
}'
}
# Unix epoch → "9:00pm" (macOS date -r)
fmt_time_epoch() {
date -r "$1" "+%-I:%M%p" 2>/dev/null | tr '[:upper:]' '[:lower:]'
}
# Seconds until epoch → "4h10m" or "10m"
fmt_remaining() {
local now; now=$(date +%s)
local secs=$(( $1 - now ))
[ "$secs" -lt 0 ] && secs=0
local h=$(( secs / 3600 )) m=$(( (secs % 3600) / 60 ))
[ "$h" -gt 0 ] && printf '%dh%dm' "$h" "$m" || printf '%dm' "$m"
}
# Unix epoch → "Fri, 3:00pm"
fmt_day_time_epoch() {
date -r "$1" "+%a, %-I:%M%p" 2>/dev/null | sed 's/AM/am/;s/PM/pm/'
}
# ── Token usage from transcript JSONL ─────────────────────────────────────────
# Finds the last "usage" object and sums all four token fields.
total_tokens=0
if [ -n "$transcript_path" ] && [ -f "$transcript_path" ]; then
usage_json=$(grep -o '"usage":{[^}]*}' "$transcript_path" 2>/dev/null | tail -1)
if [ -n "$usage_json" ]; then
_get() { printf '%s' "$usage_json" | grep -o "\"$1\":[0-9]*" | grep -o '[0-9]*$'; }
input_tok=$( _get input_tokens ); [ -z "$input_tok" ] && input_tok=0
cache_r=$( _get cache_read_input_tokens ); [ -z "$cache_r" ] && cache_r=0
cache_c=$( _get cache_creation_input_tokens ); [ -z "$cache_c" ] && cache_c=0
output_tok=$( _get output_tokens ); [ -z "$output_tok" ] && output_tok=0
total_tokens=$(( input_tok + cache_r + cache_c + output_tok ))
fi
fi
# ── Context percentages ───────────────────────────────────────────────────────
ctx_sz_int=$(( ctx_size ))
[ "$ctx_sz_int" -le 0 ] && ctx_sz_int=1000000 # fallback to 1M
used_pct=0; free_pct=100; free_tokens=0
if [ "$total_tokens" -gt 0 ]; then
used_pct=$(( total_tokens * 100 / ctx_sz_int ))
[ "$used_pct" -gt 100 ] && used_pct=100
free_pct=$(( 100 - used_pct ))
free_tokens=$(( ctx_sz_int - total_tokens ))
[ "$free_tokens" -lt 0 ] && free_tokens=0
fi
# Short forms: "76k", "1M"
if [ "$total_tokens" -ge 1000 ]; then
tok_used_k="$(( total_tokens / 1000 ))k"
else
tok_used_k="$total_tokens"
fi
if [ "$ctx_sz_int" -ge 1000000 ]; then ctx_display="$(( ctx_sz_int / 1000000 ))M"
elif [ "$ctx_sz_int" -ge 1000 ]; then ctx_display="$(( ctx_sz_int / 1000 ))k"
else ctx_display="$ctx_sz_int"; fi
# ── "thinking:" mode name ────────────────────────────────────────────────────
# Claude Code's thinking budget is set by keywords in the user prompt.
# Scan the most recent user message in the transcript for these triggers:
# ultrathink → ultrathink (~31999 tokens)
# think harder → harder (~31999 tokens)
# think hard / megathink → hard (~10000 tokens)
# think → think (~4000 tokens)
# No match → "off".
thinking_state="off"
if [ -n "$transcript_path" ] && [ -f "$transcript_path" ]; then
last_user=$(grep '"role":"user"' "$transcript_path" 2>/dev/null | tail -1)
if [ -n "$last_user" ]; then
lu_lc=$(printf '%s' "$last_user" | tr '[:upper:]' '[:lower:]')
case "$lu_lc" in
*ultrathink*) thinking_state="ultrathink" ;;
*"think harder"*) thinking_state="harder" ;;
*"think hard"*|*megathink*) thinking_state="hard" ;;
*think*) thinking_state="think" ;;
esac
fi
fi
# ── Rate-limit data: prefer ccusage, fall back to Claude Code built-ins ───────
# TODO: install ccusage (npm i -g ccusage) for accurate 5-hour / 7-day data.
# ccusage exposes more granular per-session cost tracking than Claude Code.
final_5h_pct="$rl5_pct"; final_5h_reset="$rl5_reset"
final_7d_pct="$rl7_pct"; final_7d_reset="$rl7_reset"
if command -v ccusage >/dev/null 2>&1; then
cu_out=$(ccusage statusline --json 2>/dev/null || true)
if [ -n "$cu_out" ]; then
_cu() { printf '%s' "$cu_out" | jq -r "$1 // empty" 2>/dev/null; }
cu5p=$(_cu '.five_hour.used_percentage'); cu5r=$(_cu '.five_hour.resets_at')
cu7p=$(_cu '.seven_day.used_percentage'); cu7r=$(_cu '.seven_day.resets_at')
[ -n "$cu5p" ] && final_5h_pct="$cu5p"
[ -n "$cu5r" ] && final_5h_reset="$cu5r"
[ -n "$cu7p" ] && final_7d_pct="$cu7p"
[ -n "$cu7r" ] && final_7d_reset="$cu7r"
fi
fi
rl5_int=0; rl7_int=0
[ -n "$final_5h_pct" ] && rl5_int=$(printf '%.0f' "$final_5h_pct" 2>/dev/null || echo 0)
[ -n "$final_7d_pct" ] && rl7_int=$(printf '%.0f' "$final_7d_pct" 2>/dev/null || echo 0)
# ── Repo name + git branch segment (colored green/red by dirty state) ────────
repo_seg=""
if git_root=$(git -C "$workspace_dir" rev-parse --show-toplevel 2>/dev/null); then
repo_name=$(basename "$git_root")
git_branch=$(git -C "$workspace_dir" symbolic-ref --short HEAD 2>/dev/null \
|| git -C "$workspace_dir" rev-parse --short HEAD 2>/dev/null)
branch_col="$GREEN"
if ! git -C "$workspace_dir" diff --quiet 2>/dev/null \
|| ! git -C "$workspace_dir" diff --cached --quiet 2>/dev/null \
|| [ -n "$(git -C "$workspace_dir" ls-files --others --exclude-standard 2>/dev/null)" ]; then
branch_col="$RED"
fi
repo_seg=$(printf '%b%s%b git:(%b%s%b)' \
"$BOLD" "$repo_name" "$RST" \
"$branch_col" "$git_branch" "$RST")
fi
# ── MCP server count segment ──────────────────────────────────────────────────
mcp_seg=""
[ "${mcp_count:-0}" -gt 0 ] && mcp_seg="${SEP}mcp: ${mcp_count}"
# ── LINE 1 ────────────────────────────────────────────────────────────────────
ctx_lbl=$(ctx_label "$ctx_sz_int")
if [ -n "$repo_seg" ]; then
printf '%s%s' "$repo_seg" "$SEP"
fi
printf '%b%b%s%b %b(%s)%b' "$BLUE_MODEL" "$BOLD" "$model_name" "$RST" "$ORANGE_CTX" "$ctx_lbl" "$RST"
printf '%s%b%s / %s (%d%%)%b' "$SEP" "$ORANGE_CTX" "$tok_used_k" "$ctx_display" "$used_pct" "$RST"
printf '%sthinking: %b%s%b' "$SEP" "$ORANGE_CTX" "$thinking_state" "$RST"
printf '%s' "$mcp_seg"
printf '\n'
# ── LINE 2 ────────────────────────────────────────────────────────────────────
# 5-hour current session
if [ -n "$final_5h_pct" ]; then
col5=$(bar_color "$rl5_int")
bar5=$(make_bar "$rl5_int")
reset5_str=""
if [ -n "$final_5h_reset" ] && [ "$final_5h_reset" -gt 0 ] 2>/dev/null; then
t5=$(fmt_time_epoch "$final_5h_reset")
r5=$(fmt_remaining "$final_5h_reset")
reset5_str=" resets ${t5} (${r5})"
fi
printf 'current: %b%s%b %d%%%s' "$col5" "$bar5" "$RST" "$rl5_int" "$reset5_str"
else
# TODO: No 5-hour data. Install ccusage (npm i -g ccusage) for real data.
printf 'current: %b██────────%b --%% -- no data --' "$DIM" "$RST"
fi
printf '\n'
# ── LINE 3 ────────────────────────────────────────────────────────────────────
# 7-day weekly
if [ -n "$final_7d_pct" ]; then
col7=$(bar_color "$rl7_int")
bar7=$(make_bar "$rl7_int")
reset7_str=""
if [ -n "$final_7d_reset" ] && [ "$final_7d_reset" -gt 0 ] 2>/dev/null; then
dt7=$(fmt_day_time_epoch "$final_7d_reset")
reset7_str=" resets ${dt7}"
fi
printf 'weekly: %b%s%b %d%%%s' "$col7" "$bar7" "$RST" "$rl7_int" "$reset7_str"
else
# TODO: No 7-day data. Install ccusage (npm i -g ccusage) for real data.
printf 'weekly: %b█─────────%b --%% -- no data --' "$DIM" "$RST"
fi
printf '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment