Created
June 7, 2026 17:10
-
-
Save PeterKnego/dbb02b678e202efd6e20b8e5fdf535e9 to your computer and use it in GitHub Desktop.
Claude Code statusline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Claude Code status line — experienced-developer layout | |
| # Fields: DIR │ GIT │ MODEL <effort> │ CONTEXT │ $COST | |
| set -o nounset | |
| input=$(cat) | |
| # ── ANSI helpers ───────────────────────────────────────────────────────────── | |
| bold_blue() { printf '\033[1;34m%s\033[0m' "$1"; } | |
| green() { printf '\033[0;32m%s\033[0m' "$1"; } | |
| red() { printf '\033[0;31m%s\033[0m' "$1"; } | |
| dim() { printf '\033[2m%s\033[0m' "$1"; } | |
| yellow() { printf '\033[0;33m%s\033[0m' "$1"; } | |
| dim_cyan() { printf '\033[2;36m%s\033[0m' "$1"; } | |
| grey() { printf '\033[0;90m%s\033[0m' "$1"; } | |
| orange() { printf '\033[0;38;5;208m%s\033[0m' "$1"; } | |
| magenta() { printf '\033[0;35m%s\033[0m' "$1"; } | |
| cyan() { printf '\033[0;36m%s\033[0m' "$1"; } | |
| dim_green() { printf '\033[2;32m%s\033[0m' "$1"; } | |
| dim_divider() { dim " │ "; } | |
| # ── 1. DIRECTORY ───────────────────────────────────────────────────────────── | |
| cwd=$(printf '%s' "$input" | jq -r '.cwd // empty' 2>/dev/null) | |
| cwd="${cwd:-$(pwd)}" | |
| dir=$(basename "$cwd") | |
| dir_part=$(bold_blue " $dir") | |
| # ── 2. GIT ─────────────────────────────────────────────────────────────────── | |
| git_part="" | |
| if command -v git >/dev/null 2>&1; then | |
| branch=$(git -C "$cwd" --no-optional-locks branch --show-current 2>/dev/null) | |
| if [ -n "$branch" ]; then | |
| # Staged count | |
| staged_count=$(git -C "$cwd" --no-optional-locks diff --cached --name-only 2>/dev/null | wc -l | tr -d ' ') | |
| staged_count="${staged_count:-0}" | |
| # Modified (unstaged tracked files) count | |
| modified_count=$(git -C "$cwd" --no-optional-locks diff --name-only 2>/dev/null | wc -l | tr -d ' ') | |
| modified_count="${modified_count:-0}" | |
| # Untracked files check (for dirty bullet only) | |
| untracked=$(git -C "$cwd" --no-optional-locks ls-files --others --exclude-standard 2>/dev/null | head -1) | |
| # Colored bullet: red = dirty, green = clean | |
| if [ "${staged_count}" -gt 0 ] 2>/dev/null || \ | |
| [ "${modified_count}" -gt 0 ] 2>/dev/null || \ | |
| [ -n "$untracked" ]; then | |
| bullet=$(red "●") | |
| else | |
| bullet=$(green "●") | |
| fi | |
| # +S staged indicator (green), omit when zero | |
| staged_part="" | |
| [ "${staged_count}" -gt 0 ] 2>/dev/null && staged_part=$(green "+${staged_count}") | |
| # ~M modified indicator (yellow), omit when zero | |
| modified_part="" | |
| [ "${modified_count}" -gt 0 ] 2>/dev/null && modified_part=$(yellow "~${modified_count}") | |
| # Stash count | |
| stash_part="" | |
| stash_count=$(git -C "$cwd" --no-optional-locks stash list 2>/dev/null | wc -l | tr -d ' ') | |
| [ "${stash_count:-0}" -gt 0 ] 2>/dev/null && stash_part=$(dim_cyan "⚑${stash_count}") | |
| # Ahead / behind upstream | |
| ahead_behind="" | |
| upstream=$(git -C "$cwd" --no-optional-locks rev-parse --abbrev-ref '@{u}' 2>/dev/null) | |
| if [ -n "$upstream" ]; then | |
| counts=$(git -C "$cwd" --no-optional-locks rev-list --left-right --count HEAD..."$upstream" 2>/dev/null) | |
| ahead=$(printf '%s' "$counts" | awk '{print $1}') | |
| behind=$(printf '%s' "$counts" | awk '{print $2}') | |
| [ "${ahead:-0}" -gt 0 ] 2>/dev/null && ahead_behind="${ahead_behind}$(green "↑${ahead}")" | |
| [ "${behind:-0}" -gt 0 ] 2>/dev/null && ahead_behind="${ahead_behind}$(dim "↓${behind}")" | |
| fi | |
| # Assemble git field: branch ● +S ~M ⚑n ↑n ↓n | |
| git_part="$(green "$branch") ${bullet}${staged_part:+ ${staged_part}}${modified_part:+ ${modified_part}}${stash_part:+ ${stash_part}}${ahead_behind:+ ${ahead_behind}}" | |
| fi | |
| fi | |
| # ── 3. MODEL ───────────────────────────────────────────────────────────────── | |
| model_name=$(printf '%s' "$input" | jq -r '.model.display_name // empty' 2>/dev/null) | |
| model_part="" | |
| if [ -n "$model_name" ]; then | |
| # Strip leading "Claude " prefix, then collapse "(Xsize [token] context)" → "(Xsize)" | |
| model_short=$(printf '%s' "$model_name" \ | |
| | sed 's/^Claude //' \ | |
| | sed 's/([[:space:]]*\([^[:space:](]*\)[[:space:]]\+token[[:space:]]\+context[[:space:]]*)/(\1)/g' \ | |
| | sed 's/([[:space:]]*\([^[:space:](]*\)[[:space:]]\+context[[:space:]]*)/(\1)/g') | |
| model_part=$(dim "$model_short") | |
| fi | |
| # ── 4. EFFORT ──────────────────────────────────────────────────────────────── | |
| # .effort is optional; only present when the model supports reasoning effort. | |
| # .effort.level values: low | medium | high | xhigh | max (+ "auto" handled gracefully) | |
| effort_level=$(printf '%s' "$input" | jq -r '.effort.level // empty' 2>/dev/null) | |
| effort_glyph="" | |
| case "${effort_level:-}" in | |
| low) effort_glyph=$(grey "○") ;; | |
| medium) effort_glyph=$(cyan "◐") ;; | |
| high) effort_glyph=$(orange "●") ;; | |
| xhigh) effort_glyph=$(orange "◉") ;; | |
| max) effort_glyph=$(red "◉") ;; | |
| auto) effort_glyph=$(magenta "Ⓐ") ;; | |
| *) effort_glyph="" ;; # absent or unknown — show nothing | |
| esac | |
| # ── 5. CONTEXT ─────────────────────────────────────────────────────────────── | |
| used_pct=$(printf '%s' "$input" | jq -r '.context_window.used_percentage // empty' 2>/dev/null) | |
| ctx_str="" | |
| if [ -n "$used_pct" ]; then | |
| pct_int=$(printf '%.0f' "$used_pct") | |
| label="${pct_int}%" | |
| # Color by threshold | |
| if [ "$pct_int" -ge 80 ]; then ctx_str=$(red "$label") | |
| elif [ "$pct_int" -ge 50 ]; then ctx_str=$(yellow "$label") | |
| else ctx_str=$(green "$label") | |
| fi | |
| else | |
| ctx_str=$(dim "--%") | |
| fi | |
| # ── 6. COST ────────────────────────────────────────────────────────────────── | |
| # Shows three figures: S:$1.20 T:$3.40 W:$8.90 | |
| # S = this session (.cost.total_cost_usd — cumulative for the session) | |
| # T = today's total across all sessions | |
| # W = rolling last-7-days total across all sessions | |
| # Today/Week are aggregated via a tiny ledger keyed by session_id. Each row is | |
| # session_id <TAB> first_seen_epoch <TAB> latest_cumulative_cost | |
| # On each render we upsert this session's row (keeping its first-seen date, | |
| # refreshing its cost) under a flock, prune rows older than 30 days, and emit | |
| # the recomputed Today/Week sums in the same awk pass. | |
| cost_part="" | |
| session_cost=$(printf '%s' "$input" | jq -r '(.cost.total_cost_usd | numbers) // empty' 2>/dev/null) | |
| session_id=$(printf '%s' "$input" | jq -r '.session_id // empty' 2>/dev/null) | |
| if [ -n "$session_cost" ]; then | |
| [ -n "$session_id" ] || session_id="_nosession_" | |
| ledger="${HOME}/.claude/cost-ledger.tsv" | |
| now=$(date +%s) | |
| start_today=$(date -d "today 00:00:00" +%s 2>/dev/null || echo 0) | |
| week_ago=$((now - 7 * 86400)) | |
| prune_before=$((now - 30 * 86400)) | |
| touch "$ledger" 2>/dev/null | |
| sums=$( | |
| { | |
| flock -x 9 2>/dev/null | |
| tmp="${ledger}.$$" | |
| awk -F'\t' -v sid="$session_id" -v cost="$session_cost" -v now="$now" \ | |
| -v st="$start_today" -v wa="$week_ago" -v pb="$prune_before" -v out="$tmp" ' | |
| $1 == sid { first = $2 + 0; seen = 1; next } # current session: drop old row, keep first-seen | |
| ($2 + 0) >= pb { # keep other recent rows; prune the rest | |
| print $0 > out | |
| if (($2 + 0) >= st) t += $3 | |
| if (($2 + 0) >= wa) w += $3 | |
| } | |
| END { | |
| if (!seen) first = now | |
| printf "%s\t%d\t%s\n", sid, first, cost > out | |
| if (first >= st) t += cost | |
| if (first >= wa) w += cost | |
| close(out) | |
| printf "%.2f\t%.2f", t + 0, w + 0 | |
| } | |
| ' "$ledger" 2>/dev/null | |
| mv -f "$tmp" "$ledger" 2>/dev/null | |
| } 9>"${ledger}.lock" | |
| ) | |
| s_fmt=$(printf '%.2f' "$session_cost" 2>/dev/null) || s_fmt="0.00" | |
| t_fmt=$(printf '%s' "$sums" | cut -f1) | |
| w_fmt=$(printf '%s' "$sums" | cut -f2) | |
| [ -n "$t_fmt" ] || t_fmt="$s_fmt" | |
| [ -n "$w_fmt" ] || w_fmt="$s_fmt" | |
| cost_part=$(dim_green "S:\$${s_fmt} T:\$${t_fmt} W:\$${w_fmt}") | |
| fi | |
| # ── Assemble ───────────────────────────────────────────────────────────────── | |
| output="$dir_part" | |
| if [ -n "$git_part" ]; then | |
| output="${output}$(dim_divider)${git_part}" | |
| fi | |
| if [ -n "$model_part" ]; then | |
| model_effort_part="${model_part}${effort_glyph:+ ${effort_glyph}}" | |
| output="${output}$(dim_divider)${model_effort_part}" | |
| fi | |
| output="${output}$(dim_divider)${ctx_str}" | |
| if [ -n "$cost_part" ]; then | |
| output="${output}$(dim_divider)${cost_part}" | |
| fi | |
| printf '%s' "$output" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment