Skip to content

Instantly share code, notes, and snippets.

@mathematicalmichael
Last active January 25, 2026 19:59
Show Gist options
  • Select an option

  • Save mathematicalmichael/46339f37c050b73cb3873513c50d20e4 to your computer and use it in GitHub Desktop.

Select an option

Save mathematicalmichael/46339f37c050b73cb3873513c50d20e4 to your computer and use it in GitHub Desktop.
claude-code-status-line

personal settings for claude code.

statusline.sh shows git branch and context utilization.

requires bc to be installed (apt install bc)

{
"statusLine": {
"type": "command",
"command": "~/.claude/statusline.sh"
},
"promptSuggestionEnabled": false
}
#!/bin/bash
input=$(cat)
# Get model name
MODEL=$(echo "$input" | jq -r '.model.display_name // "Unknown"')
# Get current working directory name
CWD=$(echo "$input" | jq -r '.workspace.current_dir // ""')
CWD_NAME=$(basename "$CWD" 2>/dev/null || echo "")
# Get git branch
BRANCH=$(git branch --show-current 2>/dev/null)
if [ -n "$BRANCH" ]; then
GIT_PART="${BRANCH} | "
else
GIT_PART=""
fi
# Build left part: model | cwd |
LEFT_PART="${MODEL} | ${CWD_NAME} | "
# Extract context window info
# NOTE: total_input_tokens and total_output_tokens are cumulative session totals
# and don't reset on /clear or after auto-compact. We use used_percentage as the
# source of truth and derive the actual context usage from it.
# See: https://github.com/anthropics/claude-code/issues/13783
CONTEXT_SIZE=$(echo "$input" | jq -r '.context_window.context_window_size // 200000')
PERCENT=$(echo "$input" | jq -r '.context_window.used_percentage // 0')
# Calculate total tokens used from percentage (this is the accurate method)
# TOTAL_USED = CONTEXT_SIZE * PERCENT / 100
TOTAL_USED=$(echo "$CONTEXT_SIZE * $PERCENT / 100" | bc 2>/dev/null | cut -d. -f1)
TOTAL_USED=${TOTAL_USED:-0}
# Round percentage to integer (use bc for reliable float->int, fallback to 0)
PERCENT_INT=$(echo "$PERCENT" | LC_NUMERIC=C awk '{printf "%.0f", $1}' 2>/dev/null)
PERCENT_INT=${PERCENT_INT:-0}
# Color coding: green <50%, yellow 50-75% (bold), red >75% (bold)
if [ "$PERCENT_INT" -ge 75 ]; then
COLOR="\033[1;31m" # Bold Red
elif [ "$PERCENT_INT" -ge 50 ]; then
COLOR="\033[1;93m" # Bold Bright Yellow
else
COLOR="\033[32m" # Green
fi
RESET="\033[0m"
# Format numbers with k suffix for readability
format_tokens() {
local num=$1
if [ "$num" -ge 1000 ]; then
printf "%.1fk" "$(echo "scale=1; $num/1000" | bc)"
else
echo "$num"
fi
}
USED_FMT=$(format_tokens $TOTAL_USED)
SIZE_FMT=$(format_tokens $CONTEXT_SIZE)
echo -e "${LEFT_PART}${GIT_PART}${COLOR}${USED_FMT}/${SIZE_FMT} (${PERCENT_INT}%)${RESET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment