Skip to content

Instantly share code, notes, and snippets.

@ivan-loh
Created December 26, 2025 15:37
Show Gist options
  • Select an option

  • Save ivan-loh/ccae9676b4cfe07df4f8db73a048750d to your computer and use it in GitHub Desktop.

Select an option

Save ivan-loh/ccae9676b4cfe07df4f8db73a048750d to your computer and use it in GitHub Desktop.
Status line script for claude code - Example Output: ▓ 73% │ Opus │ 54k/200k │ 12m │ ~/Projects/myapp
#!/bin/bash
#
# Claude Code statusline - optimized single-pass JSON parsing ( ~/.claude/statusline.sh )
#
input=$(cat)
# Parse all values in a single jq call (6x fewer process spawns)
# Use NUL separator to handle empty fields and paths with spaces correctly
{
IFS= read -r -d '' MODEL
IFS= read -r -d '' CONTEXT_SIZE
IFS= read -r -d '' DURATION_MS
IFS= read -r -d '' CWD
IFS= read -r -d '' INPUT_TOKENS
IFS= read -r -d '' CACHE_CREATE
IFS= read -r -d '' CACHE_READ
} < <(echo "$input" | jq -j '
(.model.display_name // "unknown"), "\u0000",
(.context_window.context_window_size // 0), "\u0000",
(.cost.total_duration_ms // 0), "\u0000",
(.cwd // .workspace.current_dir // ""), "\u0000",
(.context_window.current_usage.input_tokens // 0), "\u0000",
(.context_window.current_usage.cache_creation_input_tokens // 0), "\u0000",
(.context_window.current_usage.cache_read_input_tokens // 0), "\u0000"
')
# Default empty values to prevent errors
MODEL=${MODEL:-unknown}
CONTEXT_SIZE=${CONTEXT_SIZE:-0}
DURATION_MS=${DURATION_MS:-0}
INPUT_TOKENS=${INPUT_TOKENS:-0}
CACHE_CREATE=${CACHE_CREATE:-0}
CACHE_READ=${CACHE_READ:-0}
# ANSI codes using $'...' syntax for immediate interpretation
DIM=$'\033[2m' RESET=$'\033[0m'
GREEN=$'\033[32m' YELLOW=$'\033[33m' RED=$'\033[31m'
SEP="${DIM} │ ${RESET}"
# Format tokens as human-readable (pure bash, no bc dependency)
format_tokens() {
local t=${1:-0}
if ((t >= 1000000)); then
printf "%d.%dM" "$((t / 1000000))" "$(((t % 1000000) / 100000))"
elif ((t >= 1000)); then
printf "%dk" "$((t / 1000))"
else
printf "%d" "$t"
fi
}
# Format duration from milliseconds (pure bash)
format_duration() {
local total_secs=$((${1:-0} / 1000))
local hours=$((total_secs / 3600))
local mins=$(((total_secs % 3600) / 60))
if ((hours > 0)); then
printf "%dh %dm" "$hours" "$mins"
elif ((mins > 0)); then
printf "%dm" "$mins"
else
printf "<1m"
fi
}
# Calculate token usage
CURRENT=$((INPUT_TOKENS + CACHE_CREATE + CACHE_READ))
# Guard against division by zero
if ((CONTEXT_SIZE > 0)); then
PERCENT=$((CURRENT * 100 / CONTEXT_SIZE))
else
PERCENT=0
fi
REMAINING=$((100 - PERCENT))
# Color based on remaining context
if ((REMAINING > 50)); then
COLOR=$GREEN
elif ((REMAINING > 20)); then
COLOR=$YELLOW
else
COLOR=$RED
fi
# Format and output
printf '%b▓ %3d%%%b%b%-6s%b%s/%s%b%6s%b%s\n' \
"$COLOR" "$REMAINING" "$RESET" \
"$SEP" "$MODEL" \
"$SEP" "$(format_tokens "$CURRENT")" "$(format_tokens "$CONTEXT_SIZE")" \
"$SEP" "$(format_duration "$DURATION_MS")" \
"$SEP" "${CWD/#$HOME/\~}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment