使用自定義狀態列讓 Claude Code 成為您專屬的工具,該狀態列顯示在 Claude Code 介面底部,類似於終端提示符(PS1)在 Oh-my-zsh 等 shell 中的工作方式。

創建自定義狀態列

您可以選擇:

  • 執行 /statusline 讓 Claude Code 幫助您設置自定義狀態列。預設情況下,它會嘗試重現您終端的提示符,但您可以向 Claude Code 提供關於所需行為的額外指示,例如 /statusline show the model name in orange

  • 直接在您的 .claude/settings.json 中添加 statusLine 命令:

{
  "statusLine": {
    "type": "command",
    "command": "~/.claude/statusline.sh",
    "padding": 0 // 可選:設為 0 讓狀態列延伸到邊緣
  }
}

工作原理

  • 狀態列在對話訊息更新時更新
  • 更新最多每 300 毫秒執行一次
  • 您命令的 stdout 第一行成為狀態列文字
  • 支援 ANSI 顏色代碼來設計您的狀態列樣式
  • Claude Code 通過 stdin 以 JSON 格式向您的腳本傳遞關於當前會話的上下文資訊(模型、目錄等)

JSON 輸入結構

您的狀態列命令通過 stdin 接收 JSON 格式的結構化資料:

{
  "hook_event_name": "Status",
  "session_id": "abc123...",
  "transcript_path": "/path/to/transcript.json",
  "cwd": "/current/working/directory",
  "model": {
    "id": "claude-opus-4-1",
    "display_name": "Opus"
  },
  "workspace": {
    "current_dir": "/current/working/directory",
    "project_dir": "/original/project/directory"
  }
}

範例腳本

簡單狀態列

#!/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##*/}"

Git 感知狀態列

#!/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; then
    BRANCH=$(git branch --show-current 2>/dev/null)
    if [ -n "$BRANCH" ]; then
        GIT_BRANCH=" | 🌿 $BRANCH"
    fi
fi

echo "[$MODEL_DISPLAY] 📁 ${CURRENT_DIR##*/}$GIT_BRANCH"

Python 範例

#!/usr/bin/env python3
import json
import sys
import os

# 從 stdin 讀取 JSON
data = json.load(sys.stdin)

# 提取值
model = data['model']['display_name']
current_dir = os.path.basename(data['workspace']['current_dir'])

# 檢查 git 分支
git_branch = ""
if os.path.exists('.git'):
    try:
        with open('.git/HEAD', 'r') as f:
            ref = f.read().strip()
            if ref.startswith('ref: refs/heads/'):
                git_branch = f" | 🌿 {ref.replace('ref: refs/heads/', '')}"
    except:
        pass

print(f"[{model}] 📁 {current_dir}{git_branch}")

Node.js 範例

#!/usr/bin/env node

const fs = require('fs');
const path = require('path');

// 從 stdin 讀取 JSON
let 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 headContent = fs.readFileSync('.git/HEAD', 'utf8').trim();
        if (headContent.startsWith('ref: refs/heads/')) {
            gitBranch = ` | 🌿 ${headContent.replace('ref: refs/heads/', '')}`;
        }
    } catch (e) {
        // 不是 git 倉庫或無法讀取 HEAD
    }
    
    console.log(`[${model}] 📁 ${currentDir}${gitBranch}`);
});

輔助函數方法

對於更複雜的 bash 腳本,您可以創建輔助函數:

#!/bin/bash
# 一次性讀取 JSON 輸入
input=$(cat)

# 常見提取的輔助函數
get_model_name() { echo "$input" | jq -r '.model.display_name'; }
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'; }

# 使用輔助函數
MODEL=$(get_model_name)
DIR=$(get_current_dir)
echo "[$MODEL] 📁 ${DIR##*/}"

提示

  • 保持您的狀態列簡潔 - 它應該適合一行
  • 使用表情符號(如果您的終端支援)和顏色使資訊易於掃描
  • 在 Bash 中使用 jq 進行 JSON 解析(見上述範例)
  • 通過使用模擬 JSON 輸入手動執行來測試您的腳本:echo '{"model":{"display_name":"Test"},"workspace":{"current_dir":"/test"}}' | ./statusline.sh
  • 如果需要,考慮快取昂貴的操作(如 git status)

故障排除

  • 如果您的狀態列沒有出現,檢查您的腳本是否可執行(chmod +x
  • 確保您的腳本輸出到 stdout(而不是 stderr)