Skip to content

Instantly share code, notes, and snippets.

@grundmanise
Last active August 16, 2026 21:25
Show Gist options
  • Select an option

  • Save grundmanise/e3417808630e97d4972b75b6c7cefcb7 to your computer and use it in GitHub Desktop.

Select an option

Save grundmanise/e3417808630e97d4972b75b6c7cefcb7 to your computer and use it in GitHub Desktop.
Claude Code custom statusline: model [effort] + cwd (git branch) + context bar with token usage and color thresholds
#!/bin/bash
input=$(cat)
model_raw=$(echo "$input" | jq -r '.model.display_name // .model.id // "claude"')
cwd=$(echo "$input" | jq -r '.workspace.current_dir // .cwd // ""')
effort=$(echo "$input" | jq -r '.effort_level // .effortLevel // empty')
output_style=$(echo "$input" | jq -r '.output_style.name // ""')
# Fall back to settings.json — Claude Code doesn't pass effortLevel via stdin
if [ -z "$effort" ]; then
effort=$(jq -r '.effortLevel // empty' "$HOME/.claude/settings.json" 2>/dev/null)
fi
total_tokens=$(echo "$input" | jq -r '(.context_window.total_input_tokens // 0) + (.context_window.total_output_tokens // 0)')
context_size=$(echo "$input" | jq -r '.context_window.context_window_size // 0')
used_pct=$(echo "$input" | jq -r '.context_window.used_percentage // empty')
# Strip "(1M context)" — redundant with progress bar
model=$(echo "$model_raw" | sed -E 's/ *\([^)]*\)//g')
format_tokens() {
local n=$1
if [ "$n" -ge 1000000 ]; then
printf "%s" "$(echo "scale=1; $n/1000000" | bc -l | sed 's/\.0$//')M"
elif [ "$n" -ge 1000 ]; then
printf "%dK" $((n / 1000))
else
printf "%d" "$n"
fi
}
RED=$'\033[1;31m'
YELLOW=$'\033[1;33m'
CYAN=$'\033[1;36m'
RESET=$'\033[0m'
ESC=$'\033'
BEL=$'\007'
# Linked worktree only: its .git dir lives under the main repo's common dir
worktree_name=""
worktree_path=""
if [ -n "$cwd" ] && git -C "$cwd" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
git_dir=$(git -C "$cwd" rev-parse --absolute-git-dir 2>/dev/null)
common_dir=$(git -C "$cwd" rev-parse --path-format=absolute --git-common-dir 2>/dev/null)
if [ -n "$git_dir" ] && [ "$git_dir" != "$common_dir" ]; then
worktree_path=$(git -C "$cwd" rev-parse --show-toplevel 2>/dev/null)
worktree_name=$(basename "$worktree_path")
fi
fi
# OSC 8 hyperlink — terminal right-click offers "copy link address"
osc8_link() {
local url=$1 text=$2
printf '%s]8;;%s%s%s%s]8;;%s' "$ESC" "$url" "$BEL" "$text" "$ESC" "$BEL"
}
url_encode_path() {
local s=$1 out="" c
while [ -n "$s" ]; do
c=${s:0:1}
s=${s:1}
case "$c" in
[a-zA-Z0-9/._~-]) out="$out$c" ;;
*) out="$out$(printf '%%%02X' "'$c")" ;;
esac
done
printf '%s' "$out"
}
# Build segments
left="$model"
[ -n "$effort" ] && [ "$effort" != "null" ] && left="$left [$effort]"
[ -n "$output_style" ] && [ "$output_style" != "null" ] && [ "$output_style" != "default" ] && left="$left {$output_style}"
if [ -n "$used_pct" ] && [ "$used_pct" != "null" ]; then
bw=10
f=$(printf "%.0f" "$(echo "$used_pct * $bw / 100" | bc -l)")
[ "$f" -lt 0 ] && f=0
[ "$f" -gt "$bw" ] && f=$bw
e=$((bw - f))
bar=""
for ((i=0; i<f; i++)); do bar="${bar}"; done
for ((i=0; i<e; i++)); do bar="${bar}"; done
tokens_fmt=$(format_tokens "$total_tokens")
context_fmt=$(format_tokens "$context_size")
ctx="$bar $(printf "%.0f" "$used_pct")% ($tokens_fmt/$context_fmt)"
color=""
[ "$total_tokens" -ge 150000 ] && color="$YELLOW"
[ "$total_tokens" -ge 300000 ] && color="$RED"
if [ -n "$color" ]; then
left="$left · ${color}${ctx}${RESET}"
else
left="$left · $ctx"
fi
fi
if [ -n "$worktree_name" ]; then
wt_url="file://$(url_encode_path "$worktree_path")"
left="$left · ${CYAN}$(osc8_link "$wt_url" "$worktree_name")${RESET}"
fi
printf "%s\n" "$left"
@grundmanise

Copy link
Copy Markdown
Author

How to reuse

Install on a new machine:

curl -fsSL https://gist.githubusercontent.com/grundmanise/e3417808630e97d4972b75b6c7cefcb7/raw/statusline-command.sh -o ~/.claude/statusline-command.sh
chmod +x ~/.claude/statusline-command.sh

Wire it up in ~/.claude/settings.json:

{
  "statusLine": {
    "type": "command",
    "command": "~/.claude/statusline-command.sh"
  }
}

Requires jq and bc on PATH (preinstalled on macOS; brew install jq bc if missing).

Update the gist after local edits:

gh gist edit e3417808630e97d4972b75b6c7cefcb7 ~/.claude/statusline-command.sh

Pull latest changes down to another machine:

curl -fsSL https://gist.githubusercontent.com/grundmanise/e3417808630e97d4972b75b6c7cefcb7/raw/statusline-command.sh -o ~/.claude/statusline-command.sh

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