开发工具开发状态,一眼掌握
文本 · 通用对话模型
Claude Code 开发者状态栏脚本设计
指导生成一个单文件 Python 状态栏脚本,展示模型、项目目录、Git 状态、API 时长比例等开发信息,并包含缓存、性能和错误处理要求。
Prompt
# 任务:为 Claude Code 创建专业开发者状态栏
## 角色
你是一名系统程序员,负责为 Claude Code 创建高度优化的状态栏脚本。
## 交付内容
一个单文件 Python 脚本(`~/.claude/statusline.py`),用于在 Claude Code 的状态栏中显示开发者最关心的信息。
## 输入规范
从标准输入读取 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
}
}
```
## 输出要求
### 格式
* 向标准输出恰好打印一行
* 使用 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、格式化)
* 所有函数都必须包含类型提示
* 每个函数都要有文档字符串,说明其用途
## 集成步骤
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 call 外)
* 可见文本宽度 ≤ 80 个字符(计算时排除 ANSI 代码)
* 颜色在深色和浅色终端背景下都能正确显示
* 执行时间 < 100ms,在典型工作区中(缓存调用应 < 20ms)
* 能优雅地处理缺少 Git 仓库的情况
* 缓存文件会在 /tmp 中创建,并遵守 TTL
* 当 .git/index 的 mtime 发生变化或经过 5 秒时,刷新 Git 文件数量
## 决策背景
这是一个“专业开发者”风格的状态栏。它优先考虑:
* 详细的 Git 信息,以便了解分支切换
* API 效率监控,以支持注重成本的开发
* 视觉信息密度,以实现每个字符承载最多信息# 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,并根据实际输入字段调整显示内容。