多視窗時一眼看出哪個 Claude Code 在跑。採用 OSC 9;4 工作列進度條,inline 在 settings.jsonhooks,不需外部腳本。

設定

Windows 版直接把 hook 指定成 PowerShell。command 用 PowerShell 組出 OSC 9;4 escape sequence,再用 ConvertTo-Json 回傳 terminalSequence;Claude Code 會代為輸出控制序列,不需要 /dev/tty,也不依賴 Git Bash 的 printf

"hooks": {
  "SessionStart": [{
    "hooks": [{
      "type": "command",
      "shell": "powershell",
      "command": "$seq=[char]27+']9;4;0;0'+[char]7; @{terminalSequence=$seq}|ConvertTo-Json -Compress"
    }]
  }],
  "UserPromptSubmit": [{
    "hooks": [{
      "type": "command",
      "shell": "powershell",
      "command": "$seq=[char]27+']9;4;3;0'+[char]7; @{terminalSequence=$seq}|ConvertTo-Json -Compress"
    }]
  }],
  "PostToolBatch": [{
    "hooks": [{
      "type": "command",
      "shell": "powershell",
      "command": "$seq=[char]27+']9;4;3;0'+[char]7; @{terminalSequence=$seq}|ConvertTo-Json -Compress"
    }]
  }],
  "Notification": [
    {
      "matcher": "permission_prompt",
      "hooks": [{
        "type": "command",
        "shell": "powershell",
        "command": "$seq=[char]27+']9;4;4;100'+[char]7; @{terminalSequence=$seq}|ConvertTo-Json -Compress"
      }]
    },
    {
      "matcher": "elicitation_dialog",
      "hooks": [{
        "type": "command",
        "shell": "powershell",
        "command": "$seq=[char]27+']9;4;4;100'+[char]7; @{terminalSequence=$seq}|ConvertTo-Json -Compress"
      }]
    }
  ],
  "Stop": [{
    "hooks": [{
      "type": "command",
      "shell": "powershell",
      "command": "$seq=[char]27+']9;4;0;0'+[char]7; @{terminalSequence=$seq}|ConvertTo-Json -Compress"
    }]
  }],
  "StopFailure": [{
    "hooks": [{
      "type": "command",
      "shell": "powershell",
      "command": "$seq=[char]27+']9;4;0;0'+[char]7; @{terminalSequence=$seq}|ConvertTo-Json -Compress"
    }]
  }]
}
eventmatcherstate;progress視覺效果
SessionStart0;0清除
UserPromptSubmit3;0旋轉動畫(開始跑)
PostToolBatch3;0每批工具後重申旋轉,避免被覆蓋
Notificationpermission_prompt / elicitation_dialog4;100黃色滿格暫停(等你介入)
Stop0;0清除(跑完)
StopFailure0;0清除(API error 等失敗結束)

Notification 用 matcher 精準篩選Notification 也含 idle_prompt(閒置自動觸發),不篩會造成工作列莫名變黃。permission_prompt 對應權限確認;elicitation_dialog 對應 Claude Code 主動詢問使用者。兩者都算「需要你介入」。

OSC 9;4 state 速查:0 清除、1 綠色、2 紅色、3 旋轉、4 黃色暫停。格式:ESC]9;4;<state>;<progress>BEL

需求

  • CC ≥ 2.1.141
  • Windows Terminal
  • hook command 支援 "shell": "powershell"

備選方案

Git Bash printf:可用 printf '%s' '{"terminalSequence":"\u001b]9;4;3;0\u0007"}' 直接吐 JSON 字面字串。優點是短;缺點是綁 Git Bash,且和 Windows PowerShell-first 的設定方向相反。

分頁標題 emoji(OSC 2):double-click rename 後鎖死標題;emoji 不可 inline(cp950 亂碼);$Host.UI.RawUI.WindowTitle 在 hook 子進程讀到 PS 自身路徑而非分頁標題。

桌面 toast 通知(OSC 9):用 ESC]9;訊息BEL 彈出系統通知,切到其他視窗時也看得到。Windows Terminal 原生支援,可與 OSC 9;4 並用。缺點:每次完成都彈出、較吵。

相關