Specifies a single-file Python statusline script for Claude Code that reads session JSON from stdin and shows model, directory and git status.
# Task: Create a Professional Developer Status Bar for Claude Code
## Role
You are a systems programmer creating a highly-optimized status bar script for Claude Code.
## Deliverable
A single-file Python script (`~/.claude/statusline.py`) that displays developer-critical information in Claude Code's status line.
## Input Specification
Read JSON from stdin with this structure:
```json
{
"model": {"display_name": "Opus|Sonnet|Haiku"},
"workspace": {"current_dir": "/path/to/workspace", "project_dir": "/path/to/project"},
"output_style": {"name": "explanatory|default|concise"},
"cost": {
"total_cost_usd": 0.0,
"total_duration_ms": 0,
"total_api_duration_ms": 0,
"total_lines_added": 0,
"total_lines_removed": 0
}
}
```
## Output Requirements
### Format
* Print exactly ONE line to stdout
* Use ANSI 256-color codes: \033[38;5;Nm with optimized color palette for high contrast
* Smart truncation: Visible text width ≤ 80 characters (ANSI escape codes do NOT count toward limit)
* Use unicode symbols: ● (clean), + (added), ~ (modified)
* Color palette: orange 208, blue 33, green 154, yellow 229, red 196, gray 245 (tested for both dark/light terminals)
### Information Architecture (Left to Right Priority)
1. Core: Model name (orange)
2. Context: Project directory basename (blue)
3. Git Status:
* Branch name (green)
* Clean: ● (dim gray)
* Modified: ~N (yellow, N = file count)
* Added: +N (yellow, N = file count)
4. Metadata (dim gray):
* Uncommitted files: !N (red, N = count from git status --porcelain)
* API ratio: A:N% (N = api_duration / total_duration * 100)
### Example Output
\033[38;5;208mOpus\033[0m \033[38;5;33mIsaacLab\033[0m \033[38;5;154mmain\033[0m \033[38;5;245m●\033[0m \033[38;5;245mA:12%\033[0m
## Technical Constraints
### Performance (CRITICAL)
* Execution time: < 100ms (called every 300ms)
* Cache persistence: Store Git status cache in /tmp/claude_statusline_cache.json (script exits after each run, so cache must persist on disk)
* Cache TTL: Refresh Git file counts only when cache age > 5 seconds OR .git/index mtime changes
* Git logic optimization:
* Branch name: Read .git/HEAD directly (no subprocess)
* File counts: Call subprocess.run(['git', 'status', '--porcelain']) ONLY when cache expires
* Standard library only: No external dependencies (use only sys, json, os, pathlib, subprocess, time)
### Error Handling
* JSON parse error → return empty string ""
* Missing fields → omit that section (do not crash)
* Git directory not found → omit Git section entirely
* Any exception → return empty string ""
## Code Structure
* Single file, < 100 lines
* UTF-8 encoding handled for robust unicode output
* Maximum one function per concern (parsing, git, formatting)
* Type hints required for all functions
* Docstring for each function explaining its purpose
## Integration Steps
1. Save script to ~/.claude/statusline.py
2. Run chmod +x ~/.claude/statusline.py
3. Add to ~/.claude/settings.json:
```json
{
"statusLine": {
"type": "command",
"command": "~/.claude/statusline.py",
"padding": 0
}
}
```
4. Test manually: echo '{"model":{"display_name":"Test"},"workspace":{"current_dir":"/tmp"}}' | ~/.claude/statusline.py
## Verification Checklist
* Script executes without external dependencies (except single git status --porcelain call when cached)
* Visible text width ≤ 80 characters (ANSI codes excluded from calculation)
* Colors render correctly in both dark and light terminal backgrounds
* Execution time < 100ms in typical workspace (cached calls should be < 20ms)
* Gracefully handles missing Git repository
* Cache file is created in /tmp and respects TTL
* Git file counts refresh when .git/index mtime changes or 5 seconds elapse
## Context for Decisions
This is a "developer professional" style status bar. It prioritizes:
* Detailed Git information for branch switching awareness
* API efficiency monitoring for cost-conscious development
* Visual density for maximum information per character# 任务:为 Claude Code 创建专业的开发者状态栏
## 角色
你是一名系统程序员,正在为 Claude Code 创建一个高度优化的状态栏脚本。
## 交付物
一个单文件的 Python 脚本(`~/.claude/statusline.py`),在 Claude Code 的状态行中显示开发者关键信息。
## 输入规范
从 stdin 读取具有以下结构的 JSON:
```json
{
"model": {"display_name": "Opus|Sonnet|Haiku"},
"workspace": {"current_dir": "/path/to/workspace", "project_dir": "/path/to/project"},
"output_style": {"name": "explanatory|default|concise"},
"cost": {
"total_cost_usd": 0.0,
"total_duration_ms": 0,
"total_api_duration_ms": 0,
"total_lines_added": 0,
"total_lines_removed": 0
}
}
```
## 输出要求
### 格式
* 向 stdout 打印**恰好一行**
* 使用 ANSI 256 色代码:\033[38;5;Nm,采用高对比度优化的调色板
* 智能截断:可见文本宽度 ≤ 80 字符(ANSI 转义码不计入限制)
* 使用 Unicode 符号:●(干净)、+(新增)、~(修改)
* 色彩调色板:橙色 208,蓝色 33,绿色 154,黄色 229,红色 196,灰色 245(已在深/浅终端背景下测试)
### 信息架构(从左到右优先级)
1. 核心:模型名称(橙色)
2. 上下文:项目目录基名(蓝色)
3. Git 状态:
* 分支名称(绿色)
* 干净:●(暗灰色)
* 修改:~N(黄色,N = 文件数)
* 新增:+N(黄色,N = 文件数)
4. 元数据(暗灰色):
* 未提交文件:!N(红色,N = 来自 git status --porcelain 的计数)
* API 比率:A:N%(N = api_duration / total_duration * 100)
### 示例输出
\033[38;5;208mOpus\033[0m \033[38;5;33mIsaacLab\033[0m \033[38;5;154mmain\033[0m \033[38;5;245m●\033[0m \033[38;5;245mA:12%\033[0m
## 技术约束
### 性能(至关重要)
* 执行时间:< 100ms(每 300ms 调用一次)
* 缓存持久化:将 Git 状态缓存存储在 /tmp/claude_statusline_cache.json 中(脚本每次运行后退出,因此缓存必须保留在磁盘上)
* 缓存 TTL:仅当缓存年龄 > 5 秒 或 .git/index 的 mtime 发生变化时才刷新 Git 文件计数
* Git 逻辑优化:
* 分支名称:直接读取 .git/HEAD(不使用子进程)
* 文件计数:仅当缓存过期时才调用 subprocess.run(['git', 'status', '--porcelain'])
* 仅使用标准库:无外部依赖(仅使用 sys、json、os、pathlib、subprocess、time)
### 错误处理
* JSON 解析错误 → 返回空字符串 ""
* 缺少字段 → 忽略该部分(不崩溃)
* 未找到 Git 目录 → 完全忽略 Git 部分
* 任何异常 → 返回空字符串 ""
## 代码结构
* 单个文件,< 100 行
* 处理 UTF-8 编码以确保健壮的 Unicode 输出
* 每个关注点最多一个函数(解析、Git、格式化)
* 所有函数必须包含类型提示
* 每个函数需提供 docstring 说明其用途
## 集成步骤
1. 将脚本保存到 ~/.claude/statusline.py
2. 运行 chmod +x ~/.claude/statusline.py
3. 添加到 ~/.claude/settings.json:
```json
{
"statusLine": {
"type": "command",
"command": "~/.claude/statusline.py",
"padding": 0
}
}
```
4. 手动测试:echo '{"model":{"display_name":"Test"},"workspace":{"current_dir":"/tmp"}}' | ~/.claude/statusline.py
## 验证清单
* 脚本执行无需外部依赖(缓存过期时除外,仅需一次 git status --porcelain 调用)
* 可见文本宽度 ≤ 80 字符(计算时不包括 ANSI 代码)
* 颜色在深色和浅色终端背景中均正确渲染
* 在典型工作区中执行时间 < 100ms(缓存命中调用应 < 20ms)
* 能优雅处理缺少 Git 仓库的情况
* 缓存文件创建于 /tmp 并遵守 TTL
* 当 .git/index mtime 改变或 5 秒过去后,Git 文件计数会刷新
## 决策上下文
这是一个“专业开发者”风格的状态栏。它优先考虑:
* 详细的 Git 信息,用于分支切换感知
* API 效率监控,用于成本敏感的开发
* 视觉密度,以实现每个字符最大信息量