b3b8099ba5
dir | git branch | model | tokens-in-context status line, symlinked from ~/.claude/statusline.sh per the portability protocol.
64 lines
1.8 KiB
Bash
Executable File
64 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# ~/.claude/statusline.sh
|
|
# Claude Code status line: dir | git branch | model | token count
|
|
|
|
input=$(cat)
|
|
|
|
# 1. Current directory (abbreviate $HOME to ~)
|
|
cwd=$(printf '%s' "$input" | jq -r '.workspace.current_dir // .cwd // empty' 2>/dev/null)
|
|
if [ -z "$cwd" ]; then
|
|
cwd="$PWD"
|
|
fi
|
|
cwd="${cwd/#$HOME/\~}"
|
|
|
|
# 2. Git branch (if in a git repo)
|
|
branch=""
|
|
if command -v git >/dev/null 2>&1; then
|
|
branch=$(git -C "${cwd/#\~/$HOME}" rev-parse --abbrev-ref HEAD 2>/dev/null)
|
|
[ "$branch" = "HEAD" ] && branch=$(git -C "${cwd/#\~/$HOME}" rev-parse --short HEAD 2>/dev/null)
|
|
fi
|
|
|
|
# 3. Model display name
|
|
model=$(printf '%s' "$input" | jq -r '.model.display_name // empty' 2>/dev/null)
|
|
|
|
# 4. Token count from transcript JSONL
|
|
ctx=""
|
|
transcript=$(printf '%s' "$input" | jq -r '.transcript_path // empty' 2>/dev/null)
|
|
if [ -n "$transcript" ] && [ -r "$transcript" ] && command -v jq >/dev/null 2>&1; then
|
|
raw=$(grep -o '"usage":{[^}]*}' "$transcript" 2>/dev/null | tail -1)
|
|
if [ -n "$raw" ]; then
|
|
total=$(printf '%s' "{$raw}" | jq -r '
|
|
.usage |
|
|
((.input_tokens // 0)
|
|
+ (.cache_read_input_tokens // 0)
|
|
+ (.cache_creation_input_tokens // 0)
|
|
+ (.output_tokens // 0))
|
|
' 2>/dev/null)
|
|
if [ -n "$total" ] && [ "$total" -gt 0 ] 2>/dev/null; then
|
|
if [ "$total" -ge 1000 ]; then
|
|
ctx=$(awk "BEGIN { printf \"%.1fk ctx\", $total/1000 }")
|
|
else
|
|
ctx="${total} ctx"
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
# Assemble the line, separating non-empty segments with " | "
|
|
parts=()
|
|
[ -n "$cwd" ] && parts+=("$cwd")
|
|
[ -n "$branch" ] && parts+=("$branch")
|
|
[ -n "$model" ] && parts+=("$model")
|
|
[ -n "$ctx" ] && parts+=("$ctx")
|
|
|
|
joined=""
|
|
for part in "${parts[@]}"; do
|
|
if [ -z "$joined" ]; then
|
|
joined="$part"
|
|
else
|
|
joined="$joined | $part"
|
|
fi
|
|
done
|
|
|
|
printf '%s' "$joined"
|