🚀

Claude CodeのStatus lineにモデル名・ユーザー・Git 情報を表示

に公開

仕様

表示例

Opus 4.1 userName@hostName ~/path/to [main ↑3 +2 ~1 ?4]

各要素の説明

Opus 4.1 (シアン色)

  • Claude Codeのモデル名
  • JSONのmodel.display_nameから取得

hoge@mac (緑色)

  • userName = ユーザー名(whoamiコマンド)
  • hostName = ホスト名(hostname -sコマンド)

~/path/to (青色)

  • 現在のディレクトリパス
  • ホームディレクトリは~に短縮表示

[main ↑3 +2 ~1 ?4] (色なし)

  • main = Gitブランチ名
  • ↑3 = リモートより3コミット進んでいる
  • ↓2 = リモートより2コミット遅れている
  • +2 = 2ファイルがステージング済み
  • ~1 = 1ファイルが未ステージ
  • ?4 = 4ファイルが未追跡

特徴

  • Git情報は値がある場合のみ表示 - 例えば変更がなければ[main]のみ
  • Gitリポジトリ外では表示なし - Git情報部分が丸ごと非表示

実装方法

~/.claude/settings.json
{
  "statusLine": {
    "type": "command",
    "command": "bash ~/.claude/statusline-command.sh"
  }
}
~/.claude/statusline-command.sh
#!/bin/bash

# Read JSON input from stdin
input=$(cat)

# Extract data from JSON using pure bash
# Function to extract JSON value by key
extract_json_value() {
    local json="$1"
    local key="$2"
    echo "$json" | sed -n "s/.*\"$key\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" | head -1
}

# Extract nested values
# For workspace.current_dir
current_dir=$(echo "$input" | sed -n 's/.*"workspace"[[:space:]]*:[[:space:]]*{[^}]*"current_dir"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1)

# For model.display_name
model_name=$(echo "$input" | sed -n 's/.*"model"[[:space:]]*:[[:space:]]*{[^}]*"display_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1)

# For session_id (top level)
session_id=$(extract_json_value "$input" "session_id")

# Get username and hostname
username=$(whoami)
hostname=$(hostname -s)

# Get current directory (abbreviated like ~)
if [[ "$current_dir" == "$HOME"* ]]; then
    display_dir="~${current_dir#$HOME}"
else
    display_dir="$current_dir"
fi

# Git information with enhanced status
git_info=""
if git -C "$current_dir" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
    branch=$(git -C "$current_dir" branch --show-current 2>/dev/null)
    if [[ -n "$branch" ]]; then
        # Get detailed git status
        status_info=""
        
        # Check remote sync status (ahead/behind)
        upstream=$(git -C "$current_dir" rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null)
        if [[ -n "$upstream" ]]; then
            # Get ahead/behind counts
            ahead_behind=$(git -C "$current_dir" rev-list --left-right --count HEAD..."$upstream" 2>/dev/null)
            if [[ -n "$ahead_behind" ]]; then
                ahead=$(echo "$ahead_behind" | cut -f1)
                behind=$(echo "$ahead_behind" | cut -f2)
                
                if [[ "$ahead" -gt 0 ]]; then
                    status_info="${status_info}${ahead}"
                fi
                if [[ "$behind" -gt 0 ]]; then
                    status_info="${status_info}${behind}"
                fi
            fi
        fi
        
        # Get local changes status
        git_status=$(git -C "$current_dir" status --porcelain 2>/dev/null)
        if [[ -n "$git_status" ]]; then
            # Count staged changes (index modified)
            staged=$(echo "$git_status" | grep -c "^[MADRC]" 2>/dev/null || echo 0)
            # Count unstaged changes (working tree modified)
            unstaged=$(echo "$git_status" | grep -c "^.[MD]" 2>/dev/null || echo 0)
            # Count untracked files
            untracked=$(echo "$git_status" | grep -c "^??" 2>/dev/null || echo 0)
            
            # Add status indicators
            if [[ "$staged" -gt 0 ]]; then
                status_info="${status_info} +${staged}"
            fi
            if [[ "$unstaged" -gt 0 ]]; then
                status_info="${status_info} ~${unstaged}"
            fi
            if [[ "$untracked" -gt 0 ]]; then
                status_info="${status_info} ?${untracked}"
            fi
        fi
        
        # Format git info without colors (git info always uncolored now)
        if [[ -n "$status_info" ]]; then
            git_info=" [${branch}${status_info}]"
        else
            git_info=" [${branch}]"
        fi
    fi
fi


# Create enhanced status line with colors: cyan for model, green for user@host, blue for directory
printf "\033[36m%s\033[0m \033[32m%s@%s\033[0m \033[34m%s\033[0m%s" \
    "$model_name" \
    "$username" \
    "$hostname" \
    "$display_dir" \
    "$git_info"

参考

https://docs.anthropic.com/ja/docs/claude-code/statusline

Discussion