/statusline 让 CodeBuddy Code 帮助您设置自定义状态行。默认情况下,它会尝试复制您终端的提示符,但您可以向 CodeBuddy Code 提供有关所需行为的额外说明,例如 /statusline 用橙色显示模型名称.codebuddy/settings.json 中添加 statusLine 命令:{"statusLine": {"type": "command","command": "~/.codebuddy/statusline.sh","padding": 0 // 可选: 设置为 0 让状态行延伸到边缘}}
{"hook_event_name": "Status","session_id": "abc123...","transcript_path": "/path/to/transcript.json","cwd": "/current/working/directory","model": {"id": "gpt-5","display_name": "GPT-5"},"workspace": {"current_dir": "/current/working/directory","project_dir": "/original/project/directory"},"version": "2.9.0","output_style": {"name": "default"},"cost": {"total_cost_usd": 0.01234,"total_duration_ms": 45000,"total_api_duration_ms": 2300,"total_lines_added": 156,"total_lines_removed": 23}}
#!/bin/bash# 从 stdin 读取 JSON 输入input=$(cat)# 使用 jq 提取值MODEL_DISPLAY=$(echo "$input" | jq -r '.model.display_name')CURRENT_DIR=$(echo "$input" | jq -r '.workspace.current_dir')echo "[$MODEL_DISPLAY] 📁 ${CURRENT_DIR##*/}"
#!/bin/bash# 从 stdin 读取 JSON 输入input=$(cat)# 使用 jq 提取值MODEL_DISPLAY=$(echo "$input" | jq -r '.model.display_name')CURRENT_DIR=$(echo "$input" | jq -r '.workspace.current_dir')# 如果在 git 仓库中显示 git 分支GIT_BRANCH=""if git rev-parse --git-dir > /dev/null 2>&1; thenBRANCH=$(git branch --show-current 2>/dev/null)if [ -n "$BRANCH" ]; thenGIT_BRANCH=" | 🌿 $BRANCH"fifiecho "[$MODEL_DISPLAY] 📁 ${CURRENT_DIR##*/}$GIT_BRANCH"
#!/bin/bash# 从 stdin 读取 JSON 输入input=$(cat)# 使用 jq 提取值MODEL_DISPLAY=$(echo "$input" | jq -r '.model.display_name')CURRENT_DIR=$(echo "$input" | jq -r '.workspace.current_dir')SESSION_ID=$(echo "$input" | jq -r '.session_id' | cut -c1-8)# ANSI 颜色代码GREEN='\\033[0;32m'BLUE='\\033[0;34m'YELLOW='\\033[1;33m'NC='\\033[0m' # 无颜色# 获取 git 分支GIT_BRANCH=""if git rev-parse --git-dir > /dev/null 2>&1; thenBRANCH=$(git branch --show-current 2>/dev/null)if [ -n "$BRANCH" ]; thenGIT_BRANCH=" ${GREEN}on${NC} ${YELLOW}$BRANCH${NC}"fifiecho -e "${BLUE}[$MODEL_DISPLAY]${NC} 📁 ${GREEN}${CURRENT_DIR##*/}${NC}$GIT_BRANCH ${BLUE}(${SESSION_ID})${NC}"
#!/bin/bash# 从 stdin 读取 JSON 输入input=$(cat)# 提取值MODEL_DISPLAY=$(echo "$input" | jq -r '.model.display_name')CURRENT_DIR=$(echo "$input" | jq -r '.workspace.current_dir')TOTAL_COST=$(echo "$input" | jq -r '.cost.total_cost_usd')LINES_ADDED=$(echo "$input" | jq -r '.cost.total_lines_added')LINES_REMOVED=$(echo "$input" | jq -r '.cost.total_lines_removed')# 格式化成本COST_STR=""if [ "$TOTAL_COST" != "null" ] && [ "$TOTAL_COST" != "0" ]; thenCOST_STR=$(printf " | 💰 \\$%.4f" "$TOTAL_COST")fi# 格式化代码统计STATS=""if [ "$LINES_ADDED" != "null" ] && [ "$LINES_ADDED" != "0" ]; thenSTATS=" | +$LINES_ADDED -$LINES_REMOVED"fiecho "[$MODEL_DISPLAY] 📁 ${CURRENT_DIR##*/}$COST_STR$STATS"
#!/usr/bin/env python3import jsonimport sysimport osimport subprocess# 从 stdin 读取 JSONdata = json.load(sys.stdin)# 提取值model = data['model']['display_name']current_dir = os.path.basename(data['workspace']['current_dir'])# 检查 git 分支git_branch = ""try:branch = subprocess.check_output(['git', 'branch', '--show-current'],stderr=subprocess.DEVNULL).decode('utf-8').strip()if branch:git_branch = f" | 🌿 {branch}"except:pass# ANSI 颜色代码BLUE = '\\033[0;34m'GREEN = '\\033[0;32m'NC = '\\033[0m'print(f"{BLUE}[{model}]{NC} 📁 {GREEN}{current_dir}{NC}{git_branch}")
#!/usr/bin/env nodeconst fs = require('fs');const path = require('path');const { execSync } = require('child_process');// 从 stdin 读取 JSONlet input = '';process.stdin.on('data', chunk => input += chunk);process.stdin.on('end', () => {const data = JSON.parse(input);// 提取值const model = data.model.display_name;const currentDir = path.basename(data.workspace.current_dir);// 检查 git 分支let gitBranch = '';try {const branch = execSync('git branch --show-current', {encoding: 'utf8',stdio: ['pipe', 'pipe', 'ignore']}).trim();if (branch) {gitBranch = ` | 🌿 ${branch}`;}} catch (e) {// 不是 git 仓库或无法读取分支}// ANSI 颜色const BLUE = '\\x1b[0;34m';const GREEN = '\\x1b[0;32m';const NC = '\\x1b[0m';console.log(`${BLUE}[${model}]${NC} 📁 ${GREEN}${currentDir}${NC}${gitBranch}`);});
#!/bin/bash# 读取一次 JSON 输入input=$(cat)# 常见提取的助手函数get_model_name() { echo "$input" | jq -r '.model.display_name'; }get_model_id() { echo "$input" | jq -r '.model.id'; }get_current_dir() { echo "$input" | jq -r '.workspace.current_dir'; }get_project_dir() { echo "$input" | jq -r '.workspace.project_dir'; }get_version() { echo "$input" | jq -r '.version'; }get_cost() { echo "$input" | jq -r '.cost.total_cost_usd'; }get_duration() { echo "$input" | jq -r '.cost.total_duration_ms'; }get_lines_added() { echo "$input" | jq -r '.cost.total_lines_added'; }get_lines_removed() { echo "$input" | jq -r '.cost.total_lines_removed'; }get_session_id() { echo "$input" | jq -r '.session_id'; }# 使用助手函数MODEL=$(get_model_name)DIR=$(get_current_dir)COST=$(get_cost)# 格式化成本if [ "$COST" != "null" ] && [ "$COST" != "0" ]; thenCOST_DISPLAY=$(printf " | 💰 \\$%.4f" "$COST")elseCOST_DISPLAY=""fiecho "[$MODEL] 📁 ${DIR##*/}$COST_DISPLAY"
#!/bin/bash# ~/.codebuddy/statusline.sh# CodeBuddy Code 中文状态行示例# 从 stdin 读取 JSON 输入input=$(cat)# 提取基本信息MODEL=$(echo "$input" | jq -r '.model.display_name')CURRENT_DIR=$(echo "$input" | jq -r '.workspace.current_dir')DIR_NAME=${CURRENT_DIR##*/}# ANSI 颜色BLUE='\\033[0;34m'GREEN='\\033[0;32m'YELLOW='\\033[1;33m'CYAN='\\033[0;36m'NC='\\033[0m'# Git 信息GIT_INFO=""if git rev-parse --git-dir > /dev/null 2>&1; thenBRANCH=$(git branch --show-current 2>/dev/null)if [ -n "$BRANCH" ]; then# 检查是否有未提交的更改if ! git diff-index --quiet HEAD -- 2>/dev/null; thenSTATUS="*"elseSTATUS=""fiGIT_INFO=" ${GREEN}分支${NC} ${YELLOW}$BRANCH$STATUS${NC}"fifi# 成本信息COST=$(echo "$input" | jq -r '.cost.total_cost_usd')COST_INFO=""if [ "$COST" != "null" ] && [ "$COST" != "0" ]; thenCOST_INFO=$(printf " ${CYAN}成本${NC} \\$%.4f" "$COST")fi# 输出状态行echo -e "${BLUE}[$MODEL]${NC} 📁 ${GREEN}$DIR_NAME${NC}$GIT_INFO$COST_INFO"
#!/bin/bashinput=$(cat)MODEL=$(echo "$input" | jq -r '.model.display_name')DIR=$(echo "$input" | jq -r '.workspace.current_dir')DURATION_MS=$(echo "$input" | jq -r '.cost.total_duration_ms')# 格式化时长if [ "$DURATION_MS" != "null" ] && [ "$DURATION_MS" != "0" ]; thenDURATION_SEC=$((DURATION_MS / 1000))DURATION_MIN=$((DURATION_SEC / 60))DURATION_SEC=$((DURATION_SEC % 60))TIME_INFO=$(printf " | ⏱️ %dm%ds" "$DURATION_MIN" "$DURATION_SEC")elseTIME_INFO=""fi# 当前时间CURRENT_TIME=$(date +"%H:%M:%S")echo "[$MODEL] 📁 ${DIR##*/}$TIME_INFO | 🕐 $CURRENT_TIME"
#!/bin/bashinput=$(cat)MODEL=$(echo "$input" | jq -r '.model.display_name')DIR=$(echo "$input" | jq -r '.workspace.current_dir')COST=$(echo "$input" | jq -r '.cost.total_cost_usd')# 颜色定义GREEN='\\033[0;32m'YELLOW='\\033[1;33m'RED='\\033[0;31m'NC='\\033[0m'# 根据成本选择颜色if [ "$COST" != "null" ]; thenif (( $(echo "$COST < 0.01" | bc -l) )); thenCOST_COLOR=$GREENelif (( $(echo "$COST < 0.1" | bc -l) )); thenCOST_COLOR=$YELLOWelseCOST_COLOR=$REDfiCOST_DISPLAY=$(printf "${COST_COLOR}\\$%.4f${NC}" "$COST")elseCOST_DISPLAY=""fiecho -e "[$MODEL] 📁 ${DIR##*/} | 💰 $COST_DISPLAY"
jq 进行 JSON 解析(参见上面的示例)echo '{"model":{"display_name":"测试"},"workspace":{"current_dir":"/test"}}' | ./statusline.sh
# 文本颜色BLACK='\\033[0;30m'RED='\\033[0;31m'GREEN='\\033[0;32m'YELLOW='\\033[0;33m'BLUE='\\033[0;34m'PURPLE='\\033[0;35m'CYAN='\\033[0;36m'WHITE='\\033[0;37m'# 粗体颜色BOLD_BLACK='\\033[1;30m'BOLD_RED='\\033[1;31m'BOLD_GREEN='\\033[1;32m'BOLD_YELLOW='\\033[1;33m'BOLD_BLUE='\\033[1;34m'BOLD_PURPLE='\\033[1;35m'BOLD_CYAN='\\033[1;36m'BOLD_WHITE='\\033[1;37m'# 背景颜色BG_BLACK='\\033[40m'BG_RED='\\033[41m'BG_GREEN='\\033[42m'BG_YELLOW='\\033[43m'BG_BLUE='\\033[44m'BG_PURPLE='\\033[45m'BG_CYAN='\\033[46m'BG_WHITE='\\033[47m'# 重置NC='\\033[0m' # No Color
chmod +x ~/.codebuddy/statusline.shecho '{}' | ~/.codebuddy/statusline.shexport LANG=zh_CN.UTF-8echo -e 来解释转义序列.git 目录~/.codebuddy/settings.json 中:{"statusLine": {"type": "command","command": "~/.codebuddy/statusline.sh","padding": 0}}
.codebuddy/settings.json 中:{"statusLine": {"type": "command","command": "./.codebuddy/statusline.sh"}}
# macOSbrew install jq bc# Ubuntu/Debiansudo apt-get install jq bc# CentOS/RHELsudo yum install jq bc
文档反馈