Skip to content

Instantly share code, notes, and snippets.

@Nikkely
Last active February 21, 2026 11:57
Show Gist options
  • Select an option

  • Save Nikkely/83a70ef0af59c2f13aa6fb9dadf99518 to your computer and use it in GitHub Desktop.

Select an option

Save Nikkely/83a70ef0af59c2f13aa6fb9dadf99518 to your computer and use it in GitHub Desktop.
claude status line
#!/bin/bash
input=$(cat)
MODEL=$(echo "$input" | jq -r '.model.display_name')
COST=$(echo "$input" | jq -r '.cost.total_cost_usd // 0' | awk '{printf "%.4f", $1}')
INPUT_TOKENS=$(echo "$input" | jq -r '.context_window.total_input_tokens // 0')
OUTPUT_TOKENS=$(echo "$input" | jq -r '.context_window.total_output_tokens // 0')
PCT=$(echo "$input" | jq -r '.context_window.used_percentage // 0' | cut -d. -f1)
CWD=$(basename "$CLAUDE_PROJECT_DIR")
# --- Cache info ---
CACHE_CREATE=$(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')
CACHE_TOTAL=$(( CACHE_CREATE + CACHE_READ ))
if [ "$CACHE_TOTAL" -gt 0 ]; then
CACHE_HIT_PCT=$(( CACHE_READ * 100 / CACHE_TOTAL ))
else
CACHE_HIT_PCT=0
fi
# --- API duration ---
API_DURATION_MS=$(echo "$input" | jq -r '.cost.total_api_duration_ms // 0' | cut -d. -f1)
API_DURATION_S=$(( API_DURATION_MS / 1000 ))
if [ "$API_DURATION_S" -ge 60 ]; then
API_TIME="$(( API_DURATION_S / 60 ))m$(( API_DURATION_S % 60 ))s"
else
API_TIME="${API_DURATION_S}s"
fi
# --- Git info (cached for 5 seconds) ---
CACHE_FILE="/tmp/claude-statusline-git-cache"
CACHE_MAX_AGE=5
GIT_BRANCH=""
GIT_REPO=""
refresh_git() {
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
local branch repo
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
repo=$(basename "$(git rev-parse --show-toplevel 2>/dev/null)")
echo "${repo}|${branch}" > "$CACHE_FILE"
else
echo "|" > "$CACHE_FILE"
fi
}
if [ -f "$CACHE_FILE" ]; then
cache_age=$(( $(date +%s) - $(stat -f%m "$CACHE_FILE" 2>/dev/null || echo 0) ))
if [ "$cache_age" -ge "$CACHE_MAX_AGE" ]; then
refresh_git
fi
else
refresh_git
fi
if [ -f "$CACHE_FILE" ]; then
IFS='|' read -r GIT_REPO GIT_BRANCH < "$CACHE_FILE"
fi
# --- Colors ---
RST='\033[0m'
BOLD='\033[1m'
DIM='\033[2m'
CYAN='\033[36m'
GREEN='\033[32m'
YELLOW='\033[33m'
RED='\033[31m'
MAGENTA='\033[35m'
WHITE='\033[37m'
BLUE='\033[34m'
# --- Context bar color based on usage ---
if [ "$PCT" -lt 50 ]; then
BAR_COLOR="$GREEN"
elif [ "$PCT" -lt 75 ]; then
BAR_COLOR="$YELLOW"
else
BAR_COLOR="$RED"
fi
# --- Build progress bar (width=20) ---
BAR_WIDTH=10
FILLED=$(( PCT * BAR_WIDTH / 100 ))
EMPTY=$(( BAR_WIDTH - FILLED ))
BAR=""
for ((i=0; i<FILLED; i++)); do BAR+=""; done
for ((i=0; i<EMPTY; i++)); do BAR+=""; done
# --- Format token counts (e.g. 12345 -> 12.3k) ---
fmt_tokens() {
local n=$1
if [ "$n" -ge 1000000 ]; then
printf "%.1fM" "$(echo "$n / 1000000" | bc -l)"
elif [ "$n" -ge 1000 ]; then
printf "%.1fk" "$(echo "$n / 1000" | bc -l)"
else
printf "%d" "$n"
fi
}
IN_FMT=$(fmt_tokens "$INPUT_TOKENS")
OUT_FMT=$(fmt_tokens "$OUTPUT_TOKENS")
# --- Cache hit color ---
if [ "$CACHE_HIT_PCT" -ge 70 ]; then
CACHE_COLOR="$GREEN"
elif [ "$CACHE_HIT_PCT" -ge 30 ]; then
CACHE_COLOR="$YELLOW"
else
CACHE_COLOR="$RED"
fi
# --- Output ---
printf '%b' "${CYAN}${BOLD}${MODEL}${RST}"
printf '%b' " ${DIM}: ${RST} "
printf '%s' "${CWD}\$"
printf '%b' " ${DIM}${RST} "
printf '%b' "${WHITE}${IN_FMT}${OUT_FMT}${RST}"
printf '%b' " ${DIM}${RST} "
printf '%b' "${BAR_COLOR}${BAR}${RST} ${BAR_COLOR}${PCT}%${RST}"
printf '%b' " ${DIM}${RST} "
# Cache hit rate
if [ "$CACHE_TOTAL" -gt 0 ]; then
printf '%b' "${CACHE_COLOR}${CACHE_HIT_PCT}%${RST}"
else
printf '%b' "${DIM}⚡--${RST}"
fi
printf '%b' " ${DIM}${RST} "
# API duration
printf '%b' "${DIM}⏰️${RST}${WHITE}${API_TIME}${RST}"
printf '%b' " ${DIM}${RST} "
printf '%b' "${MAGENTA}${BOLD}\$${COST}${RST}"
printf '%b' " ${DIM}${RST} "
if [ -n "$CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS" ]; then
printf '%b' "${MAGENTA}🤝Teams${RST}"
printf '%b' " ${DIM}${RST} "
fi
# Git info (only if inside a repo)
if [ -n "$GIT_REPO" ] && [ -n "$GIT_BRANCH" ]; then
printf '%b' "${BLUE}${BOLD}${GIT_REPO}${RST}"
printf '%b' "${DIM}:${RST}"
printf '%b' "${GREEN}${GIT_BRANCH}${RST}"
printf '%b' " ${DIM}${RST} "
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment