WindowsでClaude Codeのstatuslineを設定する

WindowsでClaude Codeのstatuslineを設定する

2026.04.15

参照元


はじめに

Claude Codeのstatuslineは、ターミナルの下部にモデル名・コンテキスト使用率・コストなどをリアルタイムで表示できる機能です。

Pythonスクリプトで自由にカスタマイズでき、使用状況を一目で確認できます。Windows環境では追加の対応が必要なため、この記事ではWindows 11・PowerShell 7での設定手順をまとめます。

注意: 本記事はWindows 11を想定しています。


前提

  • Python 3(python --version で確認)

未インストールの場合は以下のいずれかで導入できます。

# winget(推奨)
winget install Python.Python.3

または Python 公式サイト からインストーラーをダウンロードして実行。


完成イメージ

 claude-sonnet-4-6  │ my_documents  │ ctx ▓▓▓▓░░░░░░ 39%  │ cost $0.012  │ elapsed 3m42s

モデル名・ディレクトリ名・コンテキスト使用率(ctx)・コスト・経過時間を表示します。ctxバーは使用率に応じて色が変わります(0〜30%: 緑・31〜60%: 黄・61〜100%: 赤)。

Enterpriseプランではレート制限データが提供されないため、Pro/Maxプランの場合は5時間・7日のレート制限バー(5h・7d)も追加で表示されます。


手順

1. Pythonスクリプトを作成する

C:\Users\<ユーザー名>\.claude\statusline_custom.py を作成し、以下の内容を貼り付けます。

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

try:
    if sys.platform == 'win32':
        sys.stdin.reconfigure(encoding='utf-8')   # Windows必須
        sys.stdout.reconfigure(encoding='utf-8')  # Windows必須

    data = json.load(sys.stdin)

    R      = '\033[0m'
    DIM    = '\033[2m'
    GREEN  = '\033[92m'
    YELLOW = '\033[33m'
    RED    = '\033[31m'

    def color(pct):
        if pct <= 30:
            return GREEN
        elif pct <= 60:
            return YELLOW
        else:
            return RED

    def fmt_bar(label, pct):
        p = round(pct)
        filled = p * 10 // 100
        bar = '▓' * filled + '░' * (10 - filled)
        return f'{label} {color(p)}{bar} {p}%{R}'

    model = data.get('model', {}).get('display_name', 'Claude')
    parts = [model]

    # ディレクトリ名
    cwd = data.get('workspace', {}).get('current_dir', '')
    if cwd:
        parts.append(os.path.basename(cwd))

    # コンテキスト
    pct = int(data.get('context_window', {}).get('used_percentage', 0) or 0)
    parts.append(fmt_bar('ctx', pct))

    # レート制限(Pro/Maxプランのみ表示)
    five = data.get('rate_limits', {}).get('five_hour', {}).get('used_percentage')
    if five is not None:
        parts.append(fmt_bar('5h', five))
    week = data.get('rate_limits', {}).get('seven_day', {}).get('used_percentage')
    if week is not None:
        parts.append(fmt_bar('7d', week))

    # コスト
    cost = data.get('cost', {}).get('total_cost_usd', 0) or 0
    parts.append(f'cost ${cost:.3f}')

    # 経過時間
    dur_ms = data.get('cost', {}).get('total_duration_ms', 0) or 0
    mins = int(dur_ms // 60000)
    secs = int((dur_ms % 60000) // 1000)
    parts.append(f'elapsed {mins}m{secs}s')

    print(f'{DIM}{R}'.join(f' {p} ' for p in parts), end='')

except Exception:
    sys.exit(0)

Windows向けのポイント:

  • sys.stdin.reconfiguresys.stdout.reconfigure両方を設定する。stdout だけでは json.load(sys.stdin) が失敗する
  • 全体を try/except で囲み sys.exit(0) で静かに終了させることで、エラー時にstatuslineが壊れた表示になるのを防ぐ

2. settings.json にstatuslineを設定する

以下のコマンドで設定ファイルをメモ帳で開きます。

notepad "$env:USERPROFILE\.claude\settings.json"

statusLine の設定を追加します(既存の設定がある場合はマージしてください)。

{
  "statusLine": {
    "type": "command",
    "command": "python \"C:/Users/<ユーザー名>/.claude/statusline_custom.py\""
  }
}

パスの区切り文字は スラッシュ(/ を使います(バックスラッシュはエスケープが必要なため)。


3. 動作確認

Claude Codeを起動して、ターミナル下部にstatuslineが表示されれば完了です。

表示されない場合は、スクリプトを単体で実行してエラーを確認します。

echo '{}' | python "$env:USERPROFILE\.claude\statusline_custom.py"

まとめ

項目 内容
スクリプトの配置場所 C:\Users\<ユーザー名>\.claude\statusline_custom.py
設定ファイル C:\Users\<ユーザー名>\.claude\settings.json
Windows特有の対応 sys.stdin.reconfiguresys.stdout.reconfigure の両方を設定
エラー対策 全体を try/except で囲み sys.exit(0) で静かに終了
ctxバーの色 0〜30%: 緑 / 31〜60%: 黄 / 61〜100%: 赤

この記事をシェアする

関連記事