Skip to content

Instantly share code, notes, and snippets.

@jspiro
Last active January 16, 2026 23:31
Show Gist options
  • Select an option

  • Save jspiro/139f06b5c078f71981e81d12ab4259ef to your computer and use it in GitHub Desktop.

Select an option

Save jspiro/139f06b5c078f71981e81d12ab4259ef to your computer and use it in GitHub Desktop.
Claude Code custom statusline - context, duration, cost, model, path, git branch
#!/bin/bash
# Read JSON input from stdin
input=$(cat)
# Extract data from JSON using correct field names
cwd=$(echo "$input" | jq -r '.cwd // empty')
model=$(echo "$input" | jq -r '.model.display_name // empty')
context_max=$(echo "$input" | jq -r '.context_window.context_window_size // empty')
cost=$(echo "$input" | jq -r '.cost.total_cost_usd // empty')
duration_ms=$(echo "$input" | jq -r '.cost.total_duration_ms // empty')
# Calculate current context tokens from current_usage fields
input_tokens=$(echo "$input" | jq -r '.context_window.current_usage.input_tokens // 0')
cache_creation=$(echo "$input" | jq -r '.context_window.current_usage.cache_creation_input_tokens // 0')
cache_read=$(echo "$input" | jq -r '.context_window.current_usage.cache_read_input_tokens // 0')
current_tokens=$((input_tokens + cache_creation + cache_read))
# Colors
reset="\033[0m"
cyan="\033[36m"
yellow="\033[33m"
orange="\033[38;5;208m"
red="\033[31m"
magenta="\033[35m"
bold="\033[1m"
dim="\033[2m"
green="\033[32m"
# Build context window display with percentage
context_display=""
if [ "$current_tokens" -gt 0 ] && [ -n "$context_max" ]; then
# Format large numbers with K suffix
if [ "$current_tokens" -ge 1000 ] 2>/dev/null; then
tokens_k=$(awk "BEGIN {printf \"%.1fK\", $current_tokens/1000}")
else
tokens_k="$current_tokens"
fi
if [ "$context_max" -ge 1000 ] 2>/dev/null; then
max_k=$(awk "BEGIN {printf \"%.0fK\", $context_max/1000}")
else
max_k="$context_max"
fi
percent=$(awk "BEGIN {printf \"%.0f\", ($current_tokens/$context_max)*100}")
# Color based on usage percentage (bold when >50%)
if [ "$percent" -ge 90 ]; then
context_color="${bold}${red}"
elif [ "$percent" -ge 75 ]; then
context_color="${bold}${orange}"
elif [ "$percent" -ge 50 ]; then
context_color="${bold}${yellow}"
else
context_color="$cyan"
fi
context_display="${context_color}${tokens_k}${dim} / ${reset}${context_color}${max_k} ${dim}(${percent}%)${reset}"
fi
# Build session duration display (HH:MM format, same as cost)
duration_display=""
if [ -n "$duration_ms" ]; then
total_seconds=$((duration_ms / 1000))
total_hours=$((total_seconds / 3600))
minutes=$(((total_seconds % 3600) / 60))
if [ "$total_hours" -ge 24 ]; then
days=$((total_hours / 24))
hours=$((total_hours % 24))
if [ "$days" -eq 1 ]; then
day_label="day"
else
day_label="days"
fi
duration_display="${dim}${magenta}${days} ${day_label} $(printf "%02d:%02d" "$hours" "$minutes")${reset}"
else
duration_display="${dim}${magenta}$(printf "%02d:%02d" "$total_hours" "$minutes")${reset}"
fi
fi
# Build cost display (green, dimmed)
cost_display=""
if [ -n "$cost" ]; then
cost_display="${dim}${green}\$$(awk "BEGIN {printf \"%.2f\", $cost}")${reset}"
fi
# Build model display (bold magenta)
model_display=""
if [ -n "$model" ]; then
model_display="${bold}${magenta}${model}${reset}"
fi
# Build cwd display (bold white, relative to ~)
cwd_display=""
if [ -n "$cwd" ]; then
# Replace home directory with ~
short_cwd="${cwd/#$HOME/~}"
cwd_display="${bold}${short_cwd}${reset}"
fi
# Get git branch and sha if in a git repository
branch=""
sha=""
if [ -n "$cwd" ] && git -C "$cwd" rev-parse --git-dir >/dev/null 2>&1; then
branch=$(git -C "$cwd" branch --show-current 2>/dev/null)
sha=$(git -C "$cwd" rev-parse --short HEAD 2>/dev/null)
if [ -z "$branch" ]; then
branch="detached"
fi
fi
# Build branch display with icon and sha in parens
branch_display=""
if [ -n "$branch" ]; then
branch_display="${yellow} ${branch}${reset}"
if [ -n "$sha" ]; then
branch_display="${branch_display} ${dim}(${sha})${reset}"
fi
fi
# Build output: context | duration | cost | model | pwd | branch
parts=()
[ -n "$context_display" ] && parts+=("$context_display")
[ -n "$duration_display" ] && parts+=("$duration_display")
[ -n "$cost_display" ] && parts+=("$cost_display")
[ -n "$model_display" ] && parts+=("$model_display")
[ -n "$cwd_display" ] && parts+=("$cwd_display")
[ -n "$branch_display" ] && parts+=("$branch_display")
# Join with " | "
output=""
for part in "${parts[@]}"; do
if [ -n "$output" ]; then
output="${output} ${dim}|${reset} ${part}"
else
output="$part"
fi
done
printf '%b' "$output"

Claude Code Custom Statusline

Screenshot

Features

  • Context usage - color-coded: cyan → bold yellow (50%) → bold orange (75%) → bold red (90%)
  • Session duration - HH:MM format, "N days" prefix for long sessions
  • Cost - green
  • Model - bold magenta
  • Working directory - bold, uses ~ shorthand
  • Git branch - icon with short SHA

Installation

  1. Save statusline-command.sh to ~/.claude/statusline-command.sh
  2. chmod +x ~/.claude/statusline-command.sh
  3. Add to ~/.claude/settings.json:
    {
      "statusLine": {
        "type": "command",
        "command": "~/.claude/statusline-command.sh"
      }
    }

Requires jq and git.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment