statusline-generator
Installs, configures, customizes, or troubleshoots the Claude Code statusline (cwd, model, token counts). Use when the user wants to set up or change the statusline, switch minimal vs full layouts, show absolute token counts (e.g. ctx 108K / 1M) instead of a percentage, add cost via ccusage or git status, dump the stdin JSON Claude Code passes the script, or fix a statusline that is blank, silent, stuck, shows "permission denied", or stopped updating after a script edit (often a missing chmod +x). Trigger phrases: "configure statusline", "statusline blank", "status line not showing", "statusline broken", "show token count in statusline", 状态栏, 状态栏不显示, 状态栏空白, 显示工作目录, 显示 token 数.
1How It Works & Mechanism
Single Source of Truth architecture: generate_statusline.sh is the sole statusline script (two layouts are toggled via the environment variable CLAUDE_STATUSLINE_LAYOUT rather than flags, because Claude Code passes JSON over stdin and flags would conflict); both installation and health checks center around this single script.
The script reads layout from environment, not flags (Claude Code passes JSON on stdin, so flags would conflict).
Zero-fork performance discipline: reads Git branch without spawning git processes by traversing upward to locate and read .git/HEAD as a plain file (handling indirect gitdir: pointers in worktrees/submodules and short SHAs for detached HEADs); refresh budget is millisecond-grade with single-digit forks.
# Zero-fork git branch: walk up to .git and read HEAD as a file. Never spawns # git.
JSON parsing dependency ladder: prioritizes jq, falls back to python3, and degrades to bare cwd if neither is available; ctx usage is computed by summing input, cache_read, and cache_create; $HOME shortening uses case rather than parameter substitution for cross-shell safety.
| `jq` | JSON parsing (preferred) | falls back to `python3` |
Closed-loop install = copy + chmod + wire-up + self-verification: install_statusline.sh backs up existing ~/.claude/statusline.sh and settings.json, updates only the statusLine block via jq (preserving other settings), enforces chmod +x, and always terminates by executing health_check, exiting with non-zero on failure—an 'installed' state requires concrete proof.
**Mandatorily runs `health_check.sh` and shows the result** — installation is not "complete" until verification passes.
Four-tier validation in health_check.sh: file existence and executability → settings.json wiring → mock stdin coverage (full payload, zero tokens, missing fields, $HOME shortening, synthetic .git/HEAD zero-fork branch) → replay of real stdin captured via CLAUDE_STATUSLINE_DEBUG=1; each failure provides a one-line remediation command.
It validates four layers: 1. `~/.claude/statusline.sh` exists and is executable. **Missing `chmod +x` is the single most common silent-failure cause**
Full layout cost section uses ccusage with caching and silent degradation: caches to /tmp/claude_cost_cache_<min>.txt, rebuilds after a >2 minute expiration, and silently skips if ccusage is unavailable; context window percentage thresholds color output as green/yellow/red at 50% and 80%.
# Cost via ccusage (cached, async; silent if ccusage unavailable)
2Core Capabilities
3External Dependencies
| Kind | Dependency / Endpoint |
|---|---|
| cli | jq(JSON 解析首选) |
| cli | python3(JSON 解析回退) |
| cli | awk(token K/M 格式化,两布局必需) |
| cli | git(full 布局脏标记;minimal 读 .git/HEAD 不需 git) |
| cli | ccusage(full 布局成本,可选) |
4Recommended Governance & Evidence
- scripts/: Local script and command execution: bash generate_statusline.sh (executed by Claude Code on every refresh; internally forks jq/python3/awk/whoami)
- 本地工作目录: File I/O and artifact generation: install overwrites ~/.claude/statusline.sh via cp and modifies ~/.claude/settings.json via jq (both pre-backed up with .b*)
5Second-Pass Verification
- [ok] install 是否真的强制 chmod +x — install_statusline.sh 显式 'chmod +x "$TARGET_PATH" # critical — silent failure root cause if missed'。
- [ok] settings.json 是否保结构更新 — install 用 'jq --arg cmd … .statusLine = {"type":"command","command":$cmd,"padding":(.statusLine.padding // 0)}' 合并;无 jq 分支拒绝编辑并提示手改。
- [ok] health_check 声称的 mock 用例 — run_mock 五例(完整/0 token/缺字段/$HOME 缩短/合成 .git/HEAD 分支)与 SKILL 描述逐条对应。
- [ok] 无网络外发与凭证读取 — 三脚本无 curl/wget/url;ccusage 以 --offline 调用;env 仅 LAYOUT/DEBUG/HOME。
- [ok] ccusage 成本路径缓存语义 — generate 185-210 行:缓存文件 /tmp/claude_cost_cache_$(date +%Y%m%d_%H%M).txt、mmin +2 清理、缺失时后台重建并回退旧缓存。
- [unlocatable] 运行时行为(真实 Claude Code stdin 形状下渲染) — 静态代码+reference 文档(context-window-schema.md)齐备,但本环境无 Claude Code 宿主,未做实机刷新验证;字段语义以文档为准。
6Verdict & Scenarios
7b68993203365b32…