60 lines
1.7 KiB
Bash
60 lines
1.7 KiB
Bash
# shellcheck shell=bash
|
|
# Claude Code status line (statusLine.command in claude-code.nix): one line,
|
|
# "<model> · <directory> · <git branch> · ctx <used>%", from the session JSON on
|
|
# stdin. No network and no ccusage: it is re-run after every assistant message,
|
|
# so it only reads the JSON and `.git/HEAD`. The fields are documented at
|
|
# https://code.claude.com/docs/en/statusline.md ("Available data");
|
|
# `context_window.used_percentage` is null early in a session and the branch is
|
|
# empty outside a git repository, so every field has a fallback.
|
|
# The Nix wrapper (writeShellApplication) adds `set -euo pipefail` and PATH
|
|
# (jq, git).
|
|
|
|
input=$(cat)
|
|
|
|
# One value per line (empty lines kept), so an empty field cannot shift the others.
|
|
mapfile -t field < <(
|
|
jq -r '
|
|
(.model.display_name // "Claude"),
|
|
(.workspace.current_dir // .cwd // ""),
|
|
(.context_window.used_percentage // null
|
|
| if . == null then "" else (round | tostring) end)
|
|
' <<<"$input" 2>/dev/null
|
|
) || true
|
|
|
|
model=${field[0]:-Claude}
|
|
dir=${field[1]:-}
|
|
pct=${field[2]:-}
|
|
|
|
name=${dir##*/}
|
|
if [ -z "$name" ]; then
|
|
name=${dir:-?}
|
|
fi
|
|
|
|
branch=""
|
|
if [ -n "$dir" ] && [ -d "$dir" ]; then
|
|
# symbolic-ref only reads HEAD; a detached HEAD falls back to the short hash.
|
|
branch=$(git -C "$dir" symbolic-ref --short -q HEAD 2>/dev/null \
|
|
|| git -C "$dir" rev-parse --short HEAD 2>/dev/null \
|
|
|| true)
|
|
fi
|
|
|
|
out="$model · $name"
|
|
if [ -n "$branch" ]; then
|
|
out="$out · $branch"
|
|
fi
|
|
|
|
if [ -n "$pct" ]; then
|
|
# Green below 50 %, yellow below 80 %, red from there on.
|
|
if [ "$pct" -ge 80 ]; then
|
|
colour=31
|
|
elif [ "$pct" -ge 50 ]; then
|
|
colour=33
|
|
else
|
|
colour=32
|
|
fi
|
|
esc=$'\033'
|
|
out="$out · ${esc}[${colour}mctx ${pct}%${esc}[0m"
|
|
fi
|
|
|
|
printf '%s\n' "$out"
|