gendesign/scripts/claude-hooks/statusline.py

47 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""
Claude Code statusLine — `⎇ <branch> · <model> · <dir>`.
Wired in `.claude/settings.json` statusLine. Reads session JSON from stdin
(model.display_name, workspace.current_dir). Branch via a 1s git probe in cwd.
Why: 3-5 parallel bg-windows on shared worktrees → branch/persona confusion is a
documented fail-class (orphan commits). A persistent branch+model line кат this risk.
Always exits 0; never blocks.
"""
from __future__ import annotations
import json
import os
import subprocess
import sys
def main() -> int:
try:
data = json.load(sys.stdin)
except Exception:
data = {}
model = (data.get("model") or {}).get("display_name") or "?"
cwd = (data.get("workspace") or {}).get("current_dir") or data.get("cwd") or os.getcwd()
base = os.path.basename(str(cwd).rstrip("/\\")) or str(cwd)
branch = "?"
try:
out = subprocess.run(
["git", "-C", str(cwd), "rev-parse", "--abbrev-ref", "HEAD"],
capture_output=True,
text=True,
timeout=1,
)
branch = out.stdout.strip() or "?"
except Exception:
pass
print(f"{branch} · {model} · {base}")
return 0
if __name__ == "__main__":
sys.exit(main())