Skip to content

Instantly share code, notes, and snippets.

@mojzis
Last active February 24, 2026 07:31
Show Gist options
  • Select an option

  • Save mojzis/ec166e62770f2f77fb5517f4a26ced3a to your computer and use it in GitHub Desktop.

Select an option

Save mojzis/ec166e62770f2f77fb5517f4a26ced3a to your computer and use it in GitHub Desktop.
claude code statusline
#!/bin/bash
# Optimized statusline for Claude Code
# - Single jq call (fast)
# - No node check, no bc dependency
#
# ENV OPTIONS:
# STATUSLINE_SHOW_COST=1 Show $$
# STATUSLINE_TIMING=1 Show script exec time
[ "${STATUSLINE_TIMING:-0}" = "1" ] && _start=$(date +%s%3N)
export TERM=xterm-256color
# Read stdin first (JSON from Claude Code), then process with jq
json=$(cat)
# Single jq call extracts everything (tab-separated)
IFS=$'\t' read -r cwd model used_pct cost < <(
echo "$json" | jq -r '[
(.workspace.current_dir // ""),
(.model.display_name // ""),
(.context_window.used_percentage // 0 | tostring),
(.cost.total_cost_usd // 0 | tostring)
] | @tsv'
)
# Convert to integer
pct=${used_pct%%.*}
pct=${pct:-0}
# Colors
RED=$'\033[31m'; GREEN=$'\033[32m'; YELLOW=$'\033[33m'
BLUE=$'\033[34m'; MAGENTA=$'\033[35m'; CYAN=$'\033[36m'
GRAY=$'\033[90m'; RESET=$'\033[0m'
SEP="${GRAY}β”‚${RESET}"
short_dir="${cwd##*/}"
# Git branch (single command with dirty check)
git_info=""
if cd "$cwd" 2>/dev/null; then
if git_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null); then
git diff --quiet 2>/dev/null && git diff --cached --quiet 2>/dev/null && dirty="" || dirty="*"
[ ${#git_branch} -gt 20 ] && git_branch="${git_branch:0:17}..."
[ -n "$dirty" ] && bc="$YELLOW" || bc="$CYAN"
git_info=" ${SEP} ${bc} ${git_branch}${dirty}${RESET}"
fi
fi
# Python venv (concise, warns if mismatched with project)
venv_info=""
venv_name=""
[ -n "$VIRTUAL_ENV" ] && venv_name="${VIRTUAL_ENV##*/}"
[ -z "$venv_name" ] && [ -n "$CONDA_DEFAULT_ENV" ] && venv_name="$CONDA_DEFAULT_ENV"
if [ -n "$venv_name" ]; then
# Truncate long names (max 12 chars)
[ ${#venv_name} -gt 12 ] && venv_name="${venv_name:0:10}.."
# Warn (yellow) if venv doesn't seem to match project dir
if [[ "$short_dir" == *"$venv_name"* ]] || [[ "$venv_name" == *"$short_dir"* ]] || [[ "$venv_name" == ".venv" ]] || [[ "$venv_name" == "venv" ]]; then
vc="$GREEN"
else
vc="$YELLOW" # Possible mismatch
fi
venv_info=" ${SEP} ${vc}🐍${venv_name}${RESET}"
fi
# Timestamp of this update (for tracking cache expiry - 5 min inactivity)
time_info=" ${SEP} ${CYAN}$(date +%H:%M)${RESET}"
# Token display: progress bar (uses used_percentage from API which includes cache)
token_info=""
if [ "$pct" -gt 0 ]; then
if [ "$pct" -ge 75 ]; then pc="$RED"
elif [ "$pct" -ge 50 ]; then pc="$YELLOW"
else pc="$GREEN"; fi
filled=$((pct * 8 / 100)); [ "$filled" -gt 8 ] && filled=8; [ "$filled" -lt 1 ] && filled=1
bar="${pc}"; for ((i=0;i<filled;i++)); do bar+="β–“"; done
bar+="${GRAY}"; for ((i=filled;i<8;i++)); do bar+="β–‘"; done
token_info=" ${SEP} ${bar}${RESET} ${GRAY}${pct}%${RESET}"
fi
# Cost (hidden by default, set STATUSLINE_SHOW_COST=1 to display)
cost_info=""
if [ "${STATUSLINE_SHOW_COST:-0}" = "1" ] && [ -n "$cost" ] && [ "$cost" != "0" ] && [ "$cost" != "null" ]; then
cost_fmt=$(awk -v c="$cost" 'BEGIN {printf "%.2f", c}')
cost_info=" ${SEP} ${GRAY}\$${cost_fmt}${RESET}"
fi
# Model (compact with version: "O 4.6", "S 4", "H 3.5")
model_info=""
if [ -n "$model" ]; then
# Extract version number (e.g., "4.6" from "Claude Opus 4.6" or "4" from "Claude Sonnet 4")
ver=$(echo "$model" | grep -oE '[0-9]+\.?[0-9]*' | head -1)
case "$model" in
*Opus*) mc="$MAGENTA"; ms="β—†"; mn="O${ver:+ $ver}" ;;
*Sonnet*) mc="$BLUE"; ms="β—‡"; mn="S${ver:+ $ver}" ;;
*Haiku*) mc="$GREEN"; ms="β—‹"; mn="H${ver:+ $ver}" ;;
*) mc="$GRAY"; ms="●"; mn="$model" ;;
esac
model_info=" ${SEP} ${mc}${ms} ${mn}${RESET}"
fi
# Self-timing (if enabled)
timing_info=""
if [ "${STATUSLINE_TIMING:-0}" = "1" ]; then
_end=$(date +%s%3N)
_elapsed=$((_end - _start))
timing_info=" ${SEP} ${GRAY}${_elapsed}ms${RESET}"
fi
printf "${BLUE}%s${RESET}%s%s%s%s%s%s%s" \
"$short_dir" "$git_info" "$venv_info" "$time_info" "$token_info" "$cost_info" "$model_info" "$timing_info"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment