打造完美的 Claude Code 状态栏
我制作了一个完美的 Claude Code 状态栏,其中包含 Git 信息和上下文使用量。下面是分步指南。
本页目录
我认为自己已经打造出了完美的 Claude Code 状态栏:
matt/course-video-manager | main | S: 0 | U: 1 | A: 0 | 17.3%
首先,它会显示当前仓库相对于仓库根目录(matt/course-video-manager)的路径。如果你和我一样,可能会用一个目录存放所有仓库。
因此,我让状态栏只显示相对于 ~repos/ 目录的路径。
接着是一些 Git 信息:
main | S: 0 | U: 1 | A: 0
它会显示当前分支,以及已暂存、未暂存和新增的更改。这部分是可选的,但当只使用终端而不用 IDE 时,我很喜欢随时看到 Git 状态。
最后,也是最重要的一项:本次会话已经使用的 上下文窗口 百分比:
17.3%
这一直让我保持警惕。我总想尽量少用上下文,因为上下文窗口中放入的内容越多,AI 的表现就越差。
让这个数字始终触手可及,并在看到它时判断“嗯,大约 60% 应该就该停了”,非常有用。
Claude Code 状态栏应该显示什么?
有用的 Claude Code 状态栏应该在你决定下一步行动前,展示所需的事实。
对我而言,包括:
- 当前位于哪个仓库
- 当前所在 Git 分支
- 是否存在已暂存或未暂存更改
- 本次会话使用了多少上下文窗口
仓库和 Git 状态能避免我在错误位置工作;上下文百分比则帮助判断何时继续、压缩或交接给新会话。
具体格式并不重要,关键是直接看到重要信号,而不必让 Claude Code 再花一轮对话检查。
想继续深入: 加入“面向真正工程师的 AI 编码”候补名单
如何设置
设置方式是在 settings.json 目录中使用这个文件: .claude :
{"hooks": {}
可以看到,它会运行 bash ~/.claude/statusline-wrapper.sh。这个状态栏包装器基本上接收一个状态栏命令脚本。
然后还会获取一个名为 ccStatusLine的 CLI 输出,从中读取上下文百分比:
{"version": 3,"lines": [[{"id": "1","type": "context-percentage","color": "yellow","bold": true,"rawValue": true}
最后合并这些文件的输出。
亲自配置
如果想自行配置,请按以下步骤操作。
步骤 1:安装 ccstatusline
首先,全局安装 ccstatusline 包:
npm install -g ccstatusline
步骤 2:配置 ccstatusline
在以下位置创建配置文件: ~/.config/ccstatusline/settings.json:
{"version": 3,"lines": [[{"id": "1","type": "context-percentage","color": "yellow","bold": true,"rawValue": true}],[],[]],"flexMode": "full-minus-40","compactThreshold": 60,"colorLevel": 2,"inheritSeparatorColors": false,"globalBold": false,"powerline": {"enabled": false,"separators": [""],"separatorInvertBackground": [false],"startCaps": [],"endCaps": [],"autoAlign": false}}
这里的关键配置是 "rawValue": true ,它只显示百分比而不显示“Ctx:”标签;以及 "bold": true ,用于匹配 Git 数字的颜色。
步骤 3:创建 Git 状态脚本
创建 ~/.claude/statusline-command.sh:
#!/bin/bash# Read JSON inputinput=$(cat)# Extract values from JSON (without jq)cwd=$(echo "$input" | sed -n 's/.*"current_dir":"\([^"]*\)".*/\1/p')# Git information (skip optional locks for performance)if git -C "$cwd" rev-parse --git-dir > /dev/null 2>&1; then# Get repo name relative to ~/repos/repo_name=$(echo "$cwd" | sed "s|^$HOME/repos/||")# Get branchbranch=$(git -C "$cwd" --no-optional-locks rev-parse --abbrev-ref HEAD 2>/dev/null)# Count staged filesstaged=$(git -C "$cwd" --no-optional-locks diff --cached --name-only 2>/dev/null | wc -l)# Count unstaged files (modified + deleted, not untracked)unstaged=$(git -C "$cwd" --no-optional-locks diff --name-only 2>/dev/null | wc -l)# Count untracked filesuntracked=$(git -C "$cwd" --no-optional-locks ls-files --others --exclude-standard 2>/dev/null | wc -l)printf '\033[01;36m%s\033[00m | \033[01;32m%s\033[00m | S: \033[01;33m%s\033[00m | U: \033[01;33m%s\033[00m | A: \033[01;33m%s\033[00m' \"$repo_name" "$branch" "$staged" "$unstaged" "$untracked"else# Not a git repoprintf '\033[01;36m%s\033[00m' "$cwd"fi
注意: 如果仓库存放在其他目录,请修改 ~/repos/ 第 12 行的值。
步骤 4:创建包装脚本
创建 ~/.claude/statusline-wrapper.sh:
#!/bin/bash# Read JSON input onceinput=$(cat)# Get git info from existing scriptgit_info=$(echo "$input" | bash ~/.claude/statusline-command.sh)# Get context percentage from ccstatuslinecontext_pct=$(echo "$input" | npx ccstatusline)# Combine outputsprintf '%s | %s' "$git_info" "$context_pct"
步骤 5:赋予脚本执行权限
chmod +x ~/.claude/statusline-command.shchmod +x ~/.claude/statusline-wrapper.sh
步骤 6:更新 Claude 设置
把以下内容添加到 ~/.claude/settings.json:
{"statusLine": {"type": "command","command": "bash ~/.claude/statusline-wrapper.sh"}}
步骤 7:重启 Claude Code
重启 Claude Code,即可看到新的状态栏!