ai-battery 0.1.7 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -50,8 +50,8 @@ Codex 86% │ 5h 18:09 │ 7d 82% ┃ Claude 4% │ 5h 18:10 │ 7d 71%
50
50
  | `ai-battery` | 지원 | 지원 | 지원 | 지원 | Node.js 18 이상이 필요합니다. |
51
51
  | `ai-battery --watch` | 지원 | 지원 | 지원 | 지원 | 터미널 안에서 주기적으로 갱신합니다. |
52
52
  | Claude statusLine | 지원 | 지원 | 지원 | 지원 | Claude Code `statusLine`에 `node <script>` 명령을 저장합니다. |
53
- | Codex terminal row | 지원 | 지원 | 지원 | 지원 | Windows는 `node-pty`/ConPTY가 있으면 reserved row, 없으면 overlay fallback을 사용합니다. WSL/Linux/macOS는 POSIX PTY와 `python3`를 사용합니다. |
54
- | `ai-battery setup codex` | 지원 | 지원 | 지원 | 지원 | Windows는 `codex.cmd` wrapper, WSL/Linux/macOS는 POSIX shell wrapper를 설치합니다. |
53
+ | Codex terminal row | 지원 | 지원 | 지원 | 지원 | Windows는 `rowpty.exe`(전용 ConPTY host)가 있으면 하단 row를 예약하고, 없으면 같은 콘솔에 덧그리는 overlay row로 동작합니다. WSL/Linux/macOS는 POSIX PTY와 `python3`를 사용합니다. |
54
+ | `ai-battery setup codex` | 지원 | 지원 | 지원 | 지원 | Codex `[tui].status_line`을 맞추고, Windows는 `codex.cmd` wrapper, WSL/Linux/macOS는 POSIX shell wrapper를 설치합니다. |
55
55
  | `ai-battery hud` | 지원 | 지원 | 미지원 | 지원 | Windows/WSL은 PowerShell/WinForms HUD, macOS는 menu bar status item입니다. |
56
56
 
57
57
  실행 중 감지(흰색/회색 표시)는 Linux/WSL은 `/proc`, macOS는 `ps`, Windows는 PowerShell 프로세스 목록을 사용합니다.
@@ -141,7 +141,7 @@ ai-battery uninstall claude
141
141
  ai-battery uninstall hud
142
142
  ```
143
143
 
144
- 이 명령은 AI Battery가 관리 마커를 넣은 Codex wrapper, Claude `statusLine`, HUD/menu bar autostart와 실행 중인 HUD를 정리합니다. 다른 도구가 만든 `codex` 파일이나 Claude `statusLine`은 건드리지 않습니다. 이전 버전이나 `--force`로 기존 파일을 백업한 경우에는 가능한 한 원래 파일/symlink를 복원합니다. 현재 이미 AI Battery wrapper 안에서 실행 중인 Codex 세션의 terminal row는 그 세션을 종료해야 사라집니다.
144
+ 이 명령은 AI Battery가 관리 마커를 넣은 Codex wrapper, Codex `[tui].status_line`, Claude `statusLine`, HUD/menu bar autostart와 실행 중인 HUD를 정리합니다. 다른 도구가 만든 `codex` 파일이나 Claude `statusLine`은 건드리지 않습니다. Codex config가 setup 이후 사용자가 수정한 상태라면 안전하게 그대로 둡니다. 이전 버전이나 `--force`로 기존 파일을 백업한 경우에는 가능한 한 원래 파일/symlink를 복원합니다. 현재 이미 AI Battery wrapper 안에서 실행 중인 Codex 세션의 terminal row는 그 세션을 종료해야 사라집니다.
145
145
 
146
146
  최신 npm은 패키지의 uninstall lifecycle을 실행하지 않기 때문에 `npm uninstall ai-battery` 또는 `npm uninstall -g ai-battery`만으로는 외부 통합 지점을 자동 정리할 수 없습니다. 완전히 제거하려면 npm 패키지를 지우기 전에 먼저 실행하세요.
147
147
 
@@ -150,11 +150,11 @@ ai-battery uninstall
150
150
  npm uninstall -g ai-battery
151
151
  ```
152
152
 
153
- 이미 npm 패키지를 먼저 지운 뒤라면, 다시 설치한 다음 `ai-battery uninstall`을 실행하거나 아래 항목을 직접 확인해 제거해야 합니다: AI Battery가 만든 Codex wrapper, shell rc의 `# >>> ai-battery setup >>>` block, Claude `statusLine`, HUD/menu bar autostart.
153
+ 이미 npm 패키지를 먼저 지운 뒤라면, 다시 설치한 다음 `ai-battery uninstall`을 실행하거나 아래 항목을 직접 확인해 제거해야 합니다: AI Battery가 만든 Codex wrapper, shell rc의 `# >>> ai-battery setup >>>` block, Codex `~/.codex/config.toml`의 `[tui].status_line`, Claude `statusLine`, HUD/menu bar autostart.
154
154
 
155
155
  ## Setup
156
156
 
157
- `setup`은 한 번만 실행합니다. Claude Code에는 statusLine hook을 설치하고, Codex에는 platform wrapper를 설치해서 이후에는 추가 명령 없이 원래처럼 실행하게 합니다.
157
+ `setup`은 한 번만 실행합니다. Claude Code에는 statusLine hook을 설치하고, Codex에는 기본 status line 구성과 platform wrapper를 설치해서 이후에는 추가 명령 없이 원래처럼 실행하게 합니다.
158
158
 
159
159
  ```bash
160
160
  ai-battery setup
@@ -167,9 +167,17 @@ ai-battery setup claude
167
167
  ai-battery setup codex
168
168
  ```
169
169
 
170
- Codex wrapper는 기존 `codex` 명령을 직접 덮어쓰지 않습니다. `~/.local/bin`이 이미 PATH에서 원본 `codex`보다 앞에 있고 `~/.local/bin/codex`가 비어 있거나 AI Battery 관리 파일이면 그 위치에 wrapper를 둬서 바로 잡히게 합니다. 그렇지 않으면 `~/.local/share/ai-battery/bin/codex`에 관리형 wrapper를 만들고, 필요한 경우 셸 설정에 이 디렉터리를 PATH 앞쪽으로 추가합니다. `~/.local/bin/codex` 같은 공용 위치에 이미 다른 파일이 있으면 덮어쓰지 않습니다. 새 터미널부터 `codex`가 자동으로 AI Battery 하단 행과 함께 실행됩니다. 같은 터미널에서 이미 `codex`를 실행한 적이 있으면 셸 캐시 때문에 `hash -r`이 한 번 필요할 수 있고, PATH 추가가 필요한 경우에는 `setup` 출력에 표시되는 `source ...` 명령을 실행하세요.
170
+ Codex setup은 `~/.codex/config.toml`의 `[tui]`에 `model-with-reasoning`, `current-dir`, `git-branch` status line을 설정합니다. 기존 값이 있으면 uninstall 복구용으로 백업합니다. Codex wrapper는 기존 `codex` 명령을 직접 덮어쓰지 않습니다. `~/.local/bin`이 이미 PATH에서 원본 `codex`보다 앞에 있고 `~/.local/bin/codex`가 비어 있거나 AI Battery 관리 파일이면 그 위치에 wrapper를 둬서 바로 잡히게 합니다. 그렇지 않으면 `~/.local/share/ai-battery/bin/codex`에 관리형 wrapper를 만들고, 필요한 경우 셸 설정에 이 디렉터리를 PATH 앞쪽으로 추가합니다. `~/.local/bin/codex` 같은 공용 위치에 이미 다른 파일이 있으면 덮어쓰지 않습니다. 새 터미널부터 `codex`가 자동으로 AI Battery 하단 행과 함께 실행됩니다. 같은 터미널에서 이미 `codex`를 실행한 적이 있으면 셸 캐시 때문에 `hash -r`이 한 번 필요할 수 있고, PATH 추가가 필요한 경우에는 `setup` 출력에 표시되는 `source ...` 명령을 실행하세요.
171
171
 
172
- Windows native `cmd`/PowerShell에서는 `codex.cmd` wrapper가 Windows runner를 실행합니다. `node-pty`가 설치되어 있으면 ConPTY로 Codex 화면을 한 줄 짧게 띄워 하단 row를 예약하고, `node-pty`가 없거나 로드되지 않으면 같은 콘솔의 하단에 overlay row를 다시 그리는 fallback을 사용합니다. Claude statusLine은 일반 `cmd`/PowerShell 프롬프트가 아니라 Claude Code 안에서만 표시됩니다.
172
+ Windows native `cmd`/PowerShell에서는 `codex.cmd` wrapper가 Windows runner를 실행합니다. runner는 `rowpty.exe`(별도 rowpty 프로젝트의 전용 ConPTY host)가 있으면 WSL과 같은 방식으로 하단 row를 예약합니다: 자식 프로그램은 한 줄 짧은 화면을 쓰고, 상태줄은 출력이 잠잠해진 시점에만 그려져 깜빡임 없이 하단에 고정됩니다. `rowpty.exe`는 바이너리로 배포되지 않습니다 — `ai-battery setup`이 패키지에 동봉된 소스(`vendor/rowpty/RowPty.cs`)를 Windows 내장 .NET Framework `csc.exe`로 사용자 머신에서 직접 컴파일해 `%LOCALAPPDATA%\ai-battery\bin`에 설치하고, 그 옆에 Microsoft 서명된 최신 ConPTY(`conpty.dll`/`OpenConsole.exe`, node-pty 패키지에서 복사)를 배치합니다. 서명 없는 다운로드 바이너리가 없으므로 SmartScreen/Defender류 평판 경고를 원천적으로 피하고, 소스가 텍스트로 열려 있어 감사할 수 있습니다. 직접 빌드한 exe를 쓰려면 `AI_BATTERY_ROWPTY` 환경변수로 지정합니다. rowpty가 없으면 같은 콘솔에 덧그리는 overlay layout으로 동작하며(`AI_BATTERY_WIN_LAYOUT=overlay`로 강제 가능), legacy `node-pty` reserve는 `AI_BATTERY_WIN_LAYOUT=reserve`이면서 rowpty가 없을 때만 사용됩니다. Claude statusLine은 일반 `cmd`/PowerShell 프롬프트가 아니라 Claude Code 안에서만 표시됩니다.
173
+
174
+ tmux에서는 pane마다 하단 행을 예약하면 같은 전역 배터리가 pane 수만큼 중복 표시됩니다. 대신 tmux의 status bar에 세션당 한 번만 표시할 수 있습니다.
175
+
176
+ ```bash
177
+ ai-battery setup tmux
178
+ ```
179
+
180
+ `~/.tmux.conf`에 관리 블록을 추가해 status-right에 배터리를 표시하고(10초 간격 갱신), 이 tmux 안에서 실행되는 `codex`는 pane별 배터리 행을 생략하고 pane 전체를 사용합니다. 적용하려면 `tmux source-file ~/.tmux.conf` 후 새 pane을 여세요. 이 블록은 기존 `status-right` 설정을 덮어쓰므로 `setup all`에 포함되지 않는 opt-in입니다. 해제는 `ai-battery uninstall tmux`, tmux 안에서도 pane별 행을 유지하려면 `AI_BATTERY_TMUX=row`를 설정합니다. Claude statusLine은 Claude Code 내부 UI라 tmux 여부와 무관하게 그대로 표시됩니다.
173
181
 
174
182
  Codex 하단 행이 보이지 않으면 진단을 실행합니다.
175
183
 
@@ -192,7 +200,7 @@ ai-battery on all
192
200
 
193
201
  ## Codex Terminal Row
194
202
 
195
- Codex 자체 status line은 수정하지 않습니다. `ai-battery setup`은 `codex` wrapper를 설치해서 사용자가 원래처럼 `codex`만 입력해도 `ai-battery-run`이 내부에서 Codex를 한 줄 짧은 PTY 안에서 실행하도록 합니다.
203
+ `ai-battery setup`은 Codex 자체 status line을 `모델/추론강도 · 워크스페이스 · git branch` 구성으로 맞춥니다. 사용량 표시는 별도의 `codex` wrapper가 담당하므로, 사용자는 원래처럼 `codex`만 입력하면 `ai-battery-run`이 내부에서 Codex를 한 줄 짧은 PTY 안에서 실행합니다.
196
204
 
197
205
  ```bash
198
206
  codex
@@ -305,7 +313,7 @@ Codex는 최근 세션 로그에서 `rate_limits` 이벤트를 찾습니다. Cla
305
313
 
306
314
  ## Source Environment
307
315
 
308
- 기본 CLI는 Node.js 18 이상이 있으면 Windows native, WSL, Linux, macOS에서 실행됩니다. Windows native의 Codex terminal row는 Node runner를 사용하며, optional `node-pty`가 있으면 ConPTY reserved row를 사용하고 없으면 overlay fallback을 사용합니다. WSL/Linux/macOS의 `ai-battery-run`은 Python 3와 POSIX PTY를 사용합니다. HUD는 Windows/WSL에서는 PowerShell/WinForms, macOS에서는 내장 `osascript`와 AppleScriptObjC를 사용합니다.
316
+ 기본 CLI는 Node.js 18 이상이 있으면 Windows native, WSL, Linux, macOS에서 실행됩니다. Windows native의 Codex terminal row는 `rowpty.exe`(전용 ConPTY host, .NET Framework 4.8 내장 csc.exe로 빌드)가 있으면 reserved row를 사용하고, 없으면 Node runner의 same-console overlay row로 동작합니다. WSL/Linux/macOS의 `ai-battery-run`은 Python 3와 POSIX PTY를 사용합니다. HUD는 Windows/WSL에서는 PowerShell/WinForms, macOS에서는 내장 `osascript`와 AppleScriptObjC를 사용합니다.
309
317
 
310
318
  Codex 데이터는 기본적으로 `~/.codex/sessions`를 읽습니다. 다른 위치를 쓰고 있다면 `CODEX_HOME`을 설정하세요.
311
319
 
@@ -74,6 +74,23 @@ function buildStartProcessCommand(filePath, args) {
74
74
  return `Start-Process -WindowStyle Hidden -FilePath ${psSingleQuote(filePath)} -ArgumentList ${psSingleQuote(commandLine)}`;
75
75
  }
76
76
 
77
+ function psInvocationArg(value) {
78
+ const text = String(value);
79
+ if (/^-[A-Za-z][A-Za-z0-9]*$/.test(text)) return text;
80
+ return psSingleQuote(text);
81
+ }
82
+
83
+ function powerShellCommandArgs(scriptPath, scriptArgs) {
84
+ const invocation = `& ${psSingleQuote(scriptPath)} ${scriptArgs.map(psInvocationArg).join(" ")}`;
85
+ return [
86
+ "-NoProfile",
87
+ "-ExecutionPolicy",
88
+ "Bypass",
89
+ "-Command",
90
+ invocation
91
+ ];
92
+ }
93
+
77
94
  function sleepSync(ms) {
78
95
  Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
79
96
  }
@@ -412,7 +429,8 @@ const configuredCommand = process.env.AI_BATTERY_COMMAND || process.env.CLAUDEX_
412
429
  const envPrefix = useWsl ? wslEnvPrefix() : "";
413
430
  const batteryCommand = configuredCommand || (useWsl
414
431
  ? `${envPrefix ? `${envPrefix} ` : ""}HOME=${shellQuote(os.homedir())} ${shellQuote(nodePath)} ${shellQuote(batteryJs)} --json`
415
- : `${winArgQuote(nodePath)} ${winArgQuote(batteryJs)} --json`);
432
+ : `& ${winArgQuote(nodePath)} ${winArgQuote(batteryJs)} --json`);
433
+ const batteryCommandBase64 = Buffer.from(batteryCommand, "utf8").toString("base64");
416
434
 
417
435
  const AUTOSTART_REG_PATH = "HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Run";
418
436
  const AUTOSTART_REG_NAME = "AiBatteryHud";
@@ -430,7 +448,7 @@ function hudProcessStatus() {
430
448
  + "$_.Name -match '^(powershell|pwsh)' -and "
431
449
  + "$_.CommandLine -like '*ai-battery-hud.ps1*' -and "
432
450
  + "$_.CommandLine -notlike '*Start-Process*' }; "
433
- + "if ($hud) { 'running (PID ' + @($hud)[0].ProcessId + ')' } else { 'stopped' }";
451
+ + "if ($hud) { $p = @($hud)[0]; $source = if ($p.CommandLine -like '*-UseWsl*') { 'WSL' } else { 'Windows' }; 'running (' + $source + ', PID ' + $p.ProcessId + ')' } else { 'stopped' }";
434
452
  const result = runPowerShell(query);
435
453
  return (result.stdout || "").trim() || "unknown";
436
454
  }
@@ -448,26 +466,17 @@ function autostartEnable() {
448
466
  // autostart.ps1 refreshes the local copy of the HUD script when the source
449
467
  // is reachable (the WSL share is not mounted until the distro starts), then
450
468
  // launches the copy so sign-in start never depends on WSL being up. The HUD
451
- // must run as a separate "powershell -File ...ai-battery-hud.ps1" process:
452
- // stop/status and the single-instance cleanup all match that command line.
453
- const hudArgLiterals = [
454
- "'-NoProfile'",
455
- "'-ExecutionPolicy'",
456
- "'Bypass'",
457
- "'-File'",
458
- "('\"' + $hud + '\"')",
459
- "'-BatteryCommand'",
460
- "('\"' + ($battery -replace '\"', '\\\"') + '\"')"
461
- ];
462
- if (useWsl) hudArgLiterals.push("'-UseWsl'");
463
-
469
+ // must run as a separate PowerShell process whose command line contains
470
+ // ai-battery-hud.ps1: stop/status and single-instance cleanup match it.
464
471
  const autostartScript = [
465
472
  "# Generated by: ai-battery hud autostart on",
466
473
  `$src = ${psSingleQuote(hudScript)}`,
467
474
  "$hud = Join-Path $PSScriptRoot 'ai-battery-hud.ps1'",
468
475
  "try { Copy-Item $src $hud -Force -ErrorAction Stop } catch { }",
469
476
  `$battery = ${psSingleQuote(batteryCommand)}`,
470
- `$argList = @(${hudArgLiterals.join(", ")}) -join ' '`,
477
+ "$batteryB64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($battery))",
478
+ "$invoke = '& ' + \"'\" + ($hud -replace \"'\", \"''\") + \"'\" + ' -BatteryCommandBase64 ' + $batteryB64" + (useWsl ? " + ' -UseWsl'" : ""),
479
+ "$argList = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', ('\"' + ($invoke -replace '\"', '\\\"') + '\"')) -join ' '",
471
480
  "Start-Process -WindowStyle Hidden -FilePath 'powershell.exe' -ArgumentList $argList",
472
481
  ""
473
482
  ].join("\r\n");
@@ -527,29 +536,26 @@ const readyPath = (!useWsl && process.platform === "win32" && !foreground && !on
527
536
  ? path.join(os.tmpdir(), `ai-battery-hud-ready-${process.pid}-${Date.now()}.json`)
528
537
  : null;
529
538
 
530
- const psArgs = [
531
- "-NoProfile",
532
- "-ExecutionPolicy",
533
- "Bypass",
534
- "-File",
535
- hudScript,
536
- "-BatteryCommand",
537
- batteryCommand,
539
+ const hudScriptArgs = [
540
+ "-BatteryCommandBase64",
541
+ batteryCommandBase64,
538
542
  ...filteredArgs
539
543
  ];
540
544
 
541
545
  if (readyPath) {
542
- psArgs.push("-ReadyPath", readyPath);
546
+ hudScriptArgs.push("-ReadyPath", readyPath);
543
547
  }
544
548
 
545
549
  if (initialJson) {
546
- psArgs.push("-InitialJsonBase64", Buffer.from(initialJson, "utf8").toString("base64"));
550
+ hudScriptArgs.push("-InitialJsonBase64", Buffer.from(initialJson, "utf8").toString("base64"));
547
551
  }
548
552
 
549
553
  if (useWsl) {
550
- psArgs.push("-UseWsl");
554
+ hudScriptArgs.push("-UseWsl");
551
555
  }
552
556
 
557
+ const psArgs = powerShellCommandArgs(hudScript, hudScriptArgs);
558
+
553
559
  if (foreground || once || stop) {
554
560
  const result = spawnSync(powershell, psArgs, { stdio: "inherit", windowsHide: true });
555
561
  if (stop && (result.status ?? 0) === 0) {
@@ -571,12 +577,17 @@ if (useWsl) {
571
577
  });
572
578
  if (start.status !== 0) process.exit(start.status ?? 1);
573
579
  } else {
574
- const child = spawn(powershell, psArgs, {
575
- detached: true,
580
+ const start = spawnSync(powershell, [
581
+ "-NoProfile",
582
+ "-ExecutionPolicy",
583
+ "Bypass",
584
+ "-Command",
585
+ buildStartProcessCommand("powershell.exe", psArgs)
586
+ ], {
576
587
  stdio: "ignore",
577
588
  windowsHide: true
578
589
  });
579
- child.unref();
590
+ if (start.status !== 0) process.exit(start.status ?? 1);
580
591
  }
581
592
 
582
593
  if (readyPath && !waitForFile(readyPath)) {
@@ -4,6 +4,7 @@ param(
4
4
  [ValidateSet("tray", "statusline", "floating")]
5
5
  [string]$Mode = $(if ($env:AI_BATTERY_HUD_MODE) { $env:AI_BATTERY_HUD_MODE } elseif ($env:CLAUDEX_BATTERY_HUD_MODE) { $env:CLAUDEX_BATTERY_HUD_MODE } else { "floating" }),
6
6
  [string]$BatteryCommand = $(if ($env:AI_BATTERY_COMMAND) { $env:AI_BATTERY_COMMAND } elseif ($env:CLAUDEX_BATTERY_COMMAND) { $env:CLAUDEX_BATTERY_COMMAND } else { "ai-battery --json" }),
7
+ [string]$BatteryCommandBase64 = "",
7
8
  [string]$InitialJsonBase64 = "",
8
9
  [int]$Width = 282,
9
10
  [double]$Opacity = $(if ($env:AI_BATTERY_HUD_OPACITY) { [double]$env:AI_BATTERY_HUD_OPACITY } elseif ($env:CLAUDEX_BATTERY_HUD_OPACITY) { [double]$env:CLAUDEX_BATTERY_HUD_OPACITY } else { 1.0 }),
@@ -20,6 +21,14 @@ $ErrorActionPreference = "Stop"
20
21
  Add-Type -AssemblyName System.Drawing
21
22
  [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
22
23
 
24
+ if (-not [string]::IsNullOrWhiteSpace($BatteryCommandBase64)) {
25
+ try {
26
+ $BatteryCommand = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($BatteryCommandBase64))
27
+ } catch {
28
+ # Fall back to BatteryCommand/env defaults; the HUD can still show an error row.
29
+ }
30
+ }
31
+
23
32
  function Stop-ExistingHudProcesses {
24
33
  $currentPid = $PID
25
34
  Get-CimInstance Win32_Process |
@@ -89,6 +98,11 @@ function Get-HudStatePath {
89
98
  return Join-Path $root "hud-position.json"
90
99
  }
91
100
 
101
+ function Get-HudSourceId {
102
+ if ($UseWsl) { return "wsl" }
103
+ return "windows"
104
+ }
105
+
92
106
  function Get-HudSnapshotPath {
93
107
  $root = if ($env:LOCALAPPDATA) {
94
108
  Join-Path $env:LOCALAPPDATA "ai-battery"
@@ -96,7 +110,7 @@ function Get-HudSnapshotPath {
96
110
  Join-Path $env:TEMP "ai-battery"
97
111
  }
98
112
  New-Item -ItemType Directory -Force -Path $root | Out-Null
99
- return Join-Path $root "hud-snapshot.json"
113
+ return Join-Path $root "hud-snapshot-$(Get-HudSourceId).json"
100
114
  }
101
115
 
102
116
  function Get-LegacyHudStatePath {
@@ -206,6 +220,7 @@ function Read-InitialHudSnapshot {
206
220
  function Write-HudSnapshot($Snapshot) {
207
221
  try {
208
222
  if (-not $Snapshot) { return }
223
+ $Snapshot | Add-Member -NotePropertyName "hudSource" -NotePropertyValue (Get-HudSourceId) -Force
209
224
  $Snapshot | ConvertTo-Json -Depth 20 -Compress | Set-Content -Encoding UTF8 -Path (Get-HudSnapshotPath)
210
225
  } catch {
211
226
  # The HUD can still run without a warm-start cache.
@@ -231,12 +246,36 @@ public struct AiBatteryMonitorInfo {
231
246
  public AiBatteryRect Work;
232
247
  public int Flags;
233
248
  }
249
+ [StructLayout(LayoutKind.Sequential)]
250
+ public struct AiBatteryPowerThrottlingState {
251
+ public uint Version;
252
+ public uint ControlMask;
253
+ public uint StateMask;
254
+ }
255
+ public delegate void AiBatteryWinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
234
256
  public static class AiBatteryNative {
257
+ public const int ProcessPowerThrottling = 4;
258
+ public const uint PROCESS_POWER_THROTTLING_CURRENT_VERSION = 1;
259
+ public const uint PROCESS_POWER_THROTTLING_EXECUTION_SPEED = 0x1;
260
+ public const int OBJID_WINDOW = 0;
235
261
  public const int GWL_EXSTYLE = -20;
236
262
  public const int WS_EX_TRANSPARENT = 0x20;
237
263
  public const int WS_EX_TOOLWINDOW = 0x80;
238
264
  public const int WS_EX_NOACTIVATE = 0x08000000;
265
+ public const int SW_HIDE = 0;
266
+ public const int SW_SHOWNOACTIVATE = 4;
239
267
  public const UInt32 MONITOR_DEFAULTTONEAREST = 2;
268
+ public const UInt32 GW_HWNDNEXT = 2;
269
+ public const int DWMWA_CLOAKED = 14;
270
+ public const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
271
+ public const uint EVENT_OBJECT_SHOW = 0x8002;
272
+ public const uint EVENT_OBJECT_HIDE = 0x8003;
273
+ public const uint EVENT_OBJECT_REORDER = 0x8004;
274
+ public const uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B;
275
+ public const uint EVENT_OBJECT_CLOAKED = 0x8017;
276
+ public const uint EVENT_OBJECT_UNCLOAKED = 0x8018;
277
+ public const uint WINEVENT_OUTOFCONTEXT = 0x0000;
278
+ public const uint WINEVENT_SKIPOWNPROCESS = 0x0002;
240
279
  public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
241
280
  public const UInt32 SWP_NOSIZE = 0x0001;
242
281
  public const UInt32 SWP_NOMOVE = 0x0002;
@@ -250,6 +289,26 @@ public static class AiBatteryNative {
250
289
  public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
251
290
  [DllImport("user32.dll")]
252
291
  public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, UInt32 uFlags);
292
+ [DllImport("user32.dll")]
293
+ public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
294
+ [DllImport("user32.dll")]
295
+ public static extern IntPtr GetWindow(IntPtr hWnd, UInt32 uCmd);
296
+ [DllImport("user32.dll")]
297
+ public static extern IntPtr GetTopWindow(IntPtr hWnd);
298
+ [DllImport("user32.dll")]
299
+ public static extern bool IsWindowVisible(IntPtr hWnd);
300
+ [DllImport("user32.dll", CharSet=CharSet.Auto)]
301
+ public static extern int GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);
302
+ [DllImport("dwmapi.dll")]
303
+ public static extern int DwmGetWindowAttribute(IntPtr hWnd, int dwAttribute, out int pvAttribute, int cbAttribute);
304
+ [DllImport("kernel32.dll")]
305
+ public static extern IntPtr GetCurrentProcess();
306
+ [DllImport("kernel32.dll", SetLastError=true)]
307
+ public static extern bool SetProcessInformation(IntPtr hProcess, int ProcessInformationClass, ref AiBatteryPowerThrottlingState ProcessInformation, int ProcessInformationSize);
308
+ [DllImport("user32.dll")]
309
+ public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, AiBatteryWinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
310
+ [DllImport("user32.dll")]
311
+ public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
253
312
  [DllImport("user32.dll", CharSet=CharSet.Auto)]
254
313
  public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
255
314
  [DllImport("user32.dll", CharSet=CharSet.Auto)]
@@ -266,17 +325,48 @@ public static class AiBatteryNative {
266
325
  "@
267
326
  Add-Type -TypeDefinition $nativeCode
268
327
 
328
+ function Disable-ProcessPowerThrottling {
329
+ # When a fullscreen app is in the foreground, Windows applies EcoQoS power
330
+ # throttling to background processes like this HUD, which stretches the
331
+ # WinForms timer from 250ms out to several seconds. That made the HUD linger
332
+ # over a fullscreen window for 3-5s before hiding (while unhide, which happens
333
+ # after throttling lifts, stayed instant). Opt the process out so the
334
+ # fullscreen check keeps firing on time even while backgrounded.
335
+ try {
336
+ $state = New-Object AiBatteryPowerThrottlingState
337
+ $state.Version = [AiBatteryNative]::PROCESS_POWER_THROTTLING_CURRENT_VERSION
338
+ $state.ControlMask = [AiBatteryNative]::PROCESS_POWER_THROTTLING_EXECUTION_SPEED
339
+ $state.StateMask = 0
340
+ $size = [System.Runtime.InteropServices.Marshal]::SizeOf([type][AiBatteryPowerThrottlingState])
341
+ return [AiBatteryNative]::SetProcessInformation([AiBatteryNative]::GetCurrentProcess(), [AiBatteryNative]::ProcessPowerThrottling, [ref]$state, $size)
342
+ } catch {
343
+ # Older Windows builds lack ProcessPowerThrottling; the HUD still works.
344
+ return $false
345
+ }
346
+ }
347
+ $script:powerThrottlingDisabled = Disable-ProcessPowerThrottling
348
+
269
349
  function Invoke-AiBatteryJson {
270
350
  if ($UseWsl) {
271
351
  $output = & wsl.exe bash -lc "$BatteryCommand 2>/dev/null"
272
352
  } else {
273
- $output = Invoke-Expression "$BatteryCommand 2>`$null"
353
+ $output = Invoke-LocalBatteryCommandOutput $BatteryCommand
274
354
  }
275
355
  $text = ($output -join "`n").Trim()
276
356
  if (-not $text) { throw "ai-battery produced no output" }
277
357
  return $text | ConvertFrom-Json
278
358
  }
279
359
 
360
+ function Invoke-LocalBatteryCommandOutput([string]$Command) {
361
+ try {
362
+ return Invoke-Expression "$Command 2>`$null"
363
+ } catch {
364
+ $trimmed = $Command.TrimStart()
365
+ if ($trimmed.StartsWith("&")) { throw }
366
+ return Invoke-Expression "& $Command 2>`$null"
367
+ }
368
+ }
369
+
280
370
  function Get-DurationText([Nullable[int]]$Seconds) {
281
371
  if ($null -eq $Seconds) { return "?" }
282
372
  if ($Seconds -le 0) { return "now" }
@@ -541,7 +631,13 @@ function Start-SnapshotFetch {
541
631
  if ($UseWslShell) {
542
632
  $output = & wsl.exe bash -lc "$Command 2>/dev/null"
543
633
  } else {
544
- $output = Invoke-Expression "$Command 2>`$null"
634
+ try {
635
+ $output = Invoke-Expression "$Command 2>`$null"
636
+ } catch {
637
+ $trimmed = $Command.TrimStart()
638
+ if ($trimmed.StartsWith("&")) { throw }
639
+ $output = Invoke-Expression "& $Command 2>`$null"
640
+ }
545
641
  }
546
642
  ($output -join "`n").Trim()
547
643
  } catch {
@@ -1154,7 +1250,7 @@ function Show-HitForm {
1154
1250
  $hitForm.Show()
1155
1251
  }
1156
1252
  $style = [AiBatteryNative]::GetWindowLong($hitForm.Handle, [AiBatteryNative]::GWL_EXSTYLE)
1157
- $style = $style -bor [AiBatteryNative]::WS_EX_TOOLWINDOW
1253
+ $style = $style -bor [AiBatteryNative]::WS_EX_TOOLWINDOW -bor [AiBatteryNative]::WS_EX_NOACTIVATE
1158
1254
  [AiBatteryNative]::SetWindowLong($hitForm.Handle, [AiBatteryNative]::GWL_EXSTYLE, $style) | Out-Null
1159
1255
  }
1160
1256
 
@@ -1274,21 +1370,89 @@ function Test-RectCoversMonitor($WindowRect, $MonitorRect) {
1274
1370
  )
1275
1371
  }
1276
1372
 
1277
- function Test-ForegroundFullscreen {
1278
- if (-not $form -or $form.IsDisposed) { return $false }
1279
- $foreground = [AiBatteryNative]::GetForegroundWindow()
1280
- if ($foreground -eq [IntPtr]::Zero) { return $false }
1281
- if ($foreground -eq $form.Handle) { return $false }
1282
- if ($hitForm -and -not $hitForm.IsDisposed -and $foreground -eq $hitForm.Handle) { return $false }
1373
+ function Test-RectIsSubstantialOnMonitor($WindowRect, $MonitorRect) {
1374
+ if (-not $WindowRect -or -not $MonitorRect) { return $false }
1375
+ $left = [math]::Max([int]$WindowRect.Left, [int]$MonitorRect.Left)
1376
+ $top = [math]::Max([int]$WindowRect.Top, [int]$MonitorRect.Top)
1377
+ $right = [math]::Min([int]$WindowRect.Right, [int]$MonitorRect.Right)
1378
+ $bottom = [math]::Min([int]$WindowRect.Bottom, [int]$MonitorRect.Bottom)
1379
+ $width = [math]::Max(0, $right - $left)
1380
+ $height = [math]::Max(0, $bottom - $top)
1381
+ $monitorArea = [math]::Max(1, [int]$MonitorRect.Width * [int]$MonitorRect.Height)
1382
+ $windowArea = $width * $height
1383
+ return (($windowArea / $monitorArea) -ge 0.20)
1384
+ }
1385
+
1386
+ function Test-HudWindowHandle([IntPtr]$Handle) {
1387
+ if ($Handle -eq [IntPtr]::Zero) { return $false }
1388
+ if ($form -and -not $form.IsDisposed -and $Handle -eq $form.Handle) { return $true }
1389
+ if ($hitForm -and -not $hitForm.IsDisposed -and $Handle -eq $hitForm.Handle) { return $true }
1390
+ return $false
1391
+ }
1392
+
1393
+ function Get-WindowClassName([IntPtr]$Handle) {
1394
+ if ($Handle -eq [IntPtr]::Zero) { return "" }
1395
+ $builder = New-Object System.Text.StringBuilder 256
1396
+ [AiBatteryNative]::GetClassName($Handle, $builder, $builder.Capacity) | Out-Null
1397
+ return $builder.ToString()
1398
+ }
1399
+
1400
+ function Test-WindowCloaked([IntPtr]$Handle) {
1401
+ if ($Handle -eq [IntPtr]::Zero) { return $false }
1402
+ $value = 0
1403
+ try {
1404
+ $hr = [AiBatteryNative]::DwmGetWindowAttribute($Handle, [AiBatteryNative]::DWMWA_CLOAKED, [ref]$value, 4)
1405
+ } catch {
1406
+ return $false
1407
+ }
1408
+ if ($hr -ne 0) { return $false }
1409
+ return ($value -ne 0)
1410
+ }
1411
+
1412
+ # Desktop and shell surfaces span the monitor but must never hide the HUD.
1413
+ $script:hudSkipWindowClasses = @(
1414
+ "Shell_TrayWnd",
1415
+ "Shell_SecondaryTrayWnd",
1416
+ "NotifyIconOverflowWindow",
1417
+ "Progman",
1418
+ "WorkerW"
1419
+ )
1283
1420
 
1421
+ function Test-FullscreenOnHudMonitor {
1422
+ if (-not $form -or $form.IsDisposed) { return $false }
1284
1423
  $hudMonitor = [AiBatteryNative]::MonitorFromWindow($form.Handle, [AiBatteryNative]::MONITOR_DEFAULTTONEAREST)
1285
- $foregroundMonitor = [AiBatteryNative]::MonitorFromWindow($foreground, [AiBatteryNative]::MONITOR_DEFAULTTONEAREST)
1286
- if ($hudMonitor -eq [IntPtr]::Zero -or $foregroundMonitor -eq [IntPtr]::Zero) { return $false }
1287
- if ($hudMonitor -ne $foregroundMonitor) { return $false }
1424
+ if ($hudMonitor -eq [IntPtr]::Zero) { return $false }
1425
+ $monitorRect = Get-MonitorRectObject $hudMonitor
1426
+ if (-not $monitorRect) { return $false }
1427
+
1428
+ # Walk the Z-order from the top down. Small transient overlays (volume, IME,
1429
+ # Game Bar, browser controls) are ignored so we can still see the fullscreen
1430
+ # window beneath them. A substantial normal window stops the search: any
1431
+ # fullscreen window below it is not the visible fullscreen surface.
1432
+ $handle = [AiBatteryNative]::GetTopWindow([IntPtr]::Zero)
1433
+ for ($scanned = 0; $scanned -lt 400 -and $handle -ne [IntPtr]::Zero; $scanned += 1) {
1434
+ $next = [AiBatteryNative]::GetWindow($handle, [AiBatteryNative]::GW_HWNDNEXT)
1435
+ # Filter cheapest-first: most windows live on another monitor, so skip the
1436
+ # DWM cloak and class-name lookups until a window is on the HUD monitor.
1437
+ if ((-not (Test-HudWindowHandle $handle)) -and
1438
+ [AiBatteryNative]::IsWindowVisible($handle) -and
1439
+ ([AiBatteryNative]::MonitorFromWindow($handle, [AiBatteryNative]::MONITOR_DEFAULTTONEAREST) -eq $hudMonitor) -and
1440
+ (-not (Test-WindowCloaked $handle)) -and
1441
+ ($script:hudSkipWindowClasses -notcontains (Get-WindowClassName $handle))) {
1442
+ $windowRect = Get-WindowRectObject $handle
1443
+ if ($windowRect -and $windowRect.Width -gt 0 -and $windowRect.Height -gt 0) {
1444
+ if (Test-RectCoversMonitor $windowRect $monitorRect) {
1445
+ return $true
1446
+ }
1447
+ if (Test-RectIsSubstantialOnMonitor $windowRect $monitorRect) {
1448
+ return $false
1449
+ }
1450
+ }
1451
+ }
1452
+ $handle = $next
1453
+ }
1288
1454
 
1289
- $windowRect = Get-WindowRectObject $foreground
1290
- $monitorRect = Get-MonitorRectObject $foregroundMonitor
1291
- return Test-RectCoversMonitor $windowRect $monitorRect
1455
+ return $false
1292
1456
  }
1293
1457
 
1294
1458
  function Set-TaskbarPosition {
@@ -1418,9 +1582,11 @@ function Set-HudHiddenForFullscreen([bool]$Hidden) {
1418
1582
  $script:hudHiddenForFullscreen = $Hidden
1419
1583
  if ($Hidden) {
1420
1584
  if ($hitForm -and -not $hitForm.IsDisposed -and $hitForm.Visible) {
1585
+ [AiBatteryNative]::ShowWindow($hitForm.Handle, [AiBatteryNative]::SW_HIDE) | Out-Null
1421
1586
  $hitForm.Hide()
1422
1587
  }
1423
1588
  if ($form -and -not $form.IsDisposed -and $form.Visible) {
1589
+ [AiBatteryNative]::ShowWindow($form.Handle, [AiBatteryNative]::SW_HIDE) | Out-Null
1424
1590
  $form.Hide()
1425
1591
  }
1426
1592
  return
@@ -1428,21 +1594,95 @@ function Set-HudHiddenForFullscreen([bool]$Hidden) {
1428
1594
 
1429
1595
  if ($form -and -not $form.IsDisposed -and -not $form.Visible) {
1430
1596
  $form.Show()
1597
+ [AiBatteryNative]::ShowWindow($form.Handle, [AiBatteryNative]::SW_SHOWNOACTIVATE) | Out-Null
1431
1598
  }
1432
1599
  Show-HitForm
1433
1600
  Sync-HitFormBounds
1434
1601
  }
1435
1602
 
1603
+ $script:fullscreenClearCount = 0
1604
+
1436
1605
  function Update-HudVisibilityForFullscreen {
1437
1606
  if (-not $form -or $form.IsDisposed) { return }
1438
- if (Test-ForegroundFullscreen) {
1607
+ if (Test-FullscreenOnHudMonitor) {
1608
+ $script:fullscreenClearCount = 0
1439
1609
  Set-HudHiddenForFullscreen $true
1440
1610
  return
1441
1611
  }
1612
+ if ($script:hudHiddenForFullscreen) {
1613
+ $script:fullscreenClearCount += 1
1614
+ if ($script:fullscreenClearCount -lt 2) { return }
1615
+ }
1616
+ $script:fullscreenClearCount = 0
1442
1617
  Set-HudHiddenForFullscreen $false
1443
1618
  Ensure-HudTopMost
1444
1619
  }
1445
1620
 
1621
+ $script:fullscreenCheckPending = $false
1622
+
1623
+ function Request-FullscreenCheck {
1624
+ if (-not $form -or $form.IsDisposed) { return }
1625
+ if ($form.InvokeRequired) {
1626
+ if ($script:fullscreenCheckPending) { return }
1627
+ $script:fullscreenCheckPending = $true
1628
+ try {
1629
+ $form.BeginInvoke([Action]{
1630
+ $script:fullscreenCheckPending = $false
1631
+ Update-HudVisibilityForFullscreen
1632
+ }) | Out-Null
1633
+ } catch {
1634
+ $script:fullscreenCheckPending = $false
1635
+ }
1636
+ return
1637
+ }
1638
+ Update-HudVisibilityForFullscreen
1639
+ }
1640
+
1641
+ $script:fullscreenEventCallback = $null
1642
+ $script:fullscreenEventHooks = @()
1643
+
1644
+ function Start-FullscreenEventHooks {
1645
+ if ($script:fullscreenEventCallback) { return }
1646
+ $script:fullscreenEventCallback = [AiBatteryWinEventDelegate]{
1647
+ param($hook, $eventType, $hwnd, $idObject, $idChild, $eventThread, $eventTime)
1648
+ if ($idObject -ne [AiBatteryNative]::OBJID_WINDOW) { return }
1649
+ Request-FullscreenCheck
1650
+ }
1651
+
1652
+ $events = @(
1653
+ [AiBatteryNative]::EVENT_SYSTEM_FOREGROUND,
1654
+ [AiBatteryNative]::EVENT_OBJECT_SHOW,
1655
+ [AiBatteryNative]::EVENT_OBJECT_HIDE,
1656
+ [AiBatteryNative]::EVENT_OBJECT_REORDER,
1657
+ [AiBatteryNative]::EVENT_OBJECT_LOCATIONCHANGE,
1658
+ [AiBatteryNative]::EVENT_OBJECT_CLOAKED,
1659
+ [AiBatteryNative]::EVENT_OBJECT_UNCLOAKED
1660
+ )
1661
+ $flags = [AiBatteryNative]::WINEVENT_OUTOFCONTEXT -bor [AiBatteryNative]::WINEVENT_SKIPOWNPROCESS
1662
+ foreach ($eventId in $events) {
1663
+ try {
1664
+ $hook = [AiBatteryNative]::SetWinEventHook($eventId, $eventId, [IntPtr]::Zero, $script:fullscreenEventCallback, 0, 0, $flags)
1665
+ if ($hook -ne [IntPtr]::Zero) {
1666
+ $script:fullscreenEventHooks += $hook
1667
+ }
1668
+ } catch {
1669
+ # Polling still covers older or restricted Windows environments.
1670
+ }
1671
+ }
1672
+ }
1673
+
1674
+ function Stop-FullscreenEventHooks {
1675
+ foreach ($hook in $script:fullscreenEventHooks) {
1676
+ try {
1677
+ [AiBatteryNative]::UnhookWinEvent($hook) | Out-Null
1678
+ } catch {
1679
+ # The hook may already be gone during shutdown.
1680
+ }
1681
+ }
1682
+ $script:fullscreenEventHooks = @()
1683
+ $script:fullscreenEventCallback = $null
1684
+ }
1685
+
1446
1686
  function Set-HudBatteryImage($Box, [Nullable[int]]$Percent, [bool]$Running) {
1447
1687
  $oldImage = $Box.Image
1448
1688
  $Box.Image = New-BatteryImage $Percent $Running
@@ -1789,8 +2029,11 @@ function Invoke-HudPump {
1789
2029
  $timer = New-Object System.Windows.Forms.Timer
1790
2030
  $timer.Interval = 1000
1791
2031
  $timer.add_Tick({ Invoke-HudPump })
2032
+ # WinEvent hooks wake fullscreen checks immediately on foreground/Z-order/show
2033
+ # changes. The UI timer is only a fallback for Windows builds or shells that do
2034
+ # not deliver every event we subscribe to.
1792
2035
  $topMostTimer = New-Object System.Windows.Forms.Timer
1793
- $topMostTimer.Interval = 1000
2036
+ $topMostTimer.Interval = 150
1794
2037
  $topMostTimer.add_Tick({ Update-HudVisibilityForFullscreen })
1795
2038
  $form.add_FormClosed({
1796
2039
  if ($canMove) {
@@ -1800,6 +2043,7 @@ $form.add_FormClosed({
1800
2043
  $timer.Dispose()
1801
2044
  $topMostTimer.Stop()
1802
2045
  $topMostTimer.Dispose()
2046
+ Stop-FullscreenEventHooks
1803
2047
  Stop-SnapshotFetch
1804
2048
  foreach ($box in @($codexIconLabel, $claudeIconLabel)) {
1805
2049
  if ($box.Image) {
@@ -1841,14 +2085,13 @@ if ($script:initialHudSnapshot) {
1841
2085
  }
1842
2086
  Update-HudFromSnapshot $script:latestSnapshot
1843
2087
  Start-SnapshotFetch
1844
- if ($Mode -eq "statusline" -or $ClickThrough) {
1845
- $style = [AiBatteryNative]::GetWindowLong($form.Handle, [AiBatteryNative]::GWL_EXSTYLE)
1846
- $style = $style -bor [AiBatteryNative]::WS_EX_TOOLWINDOW -bor [AiBatteryNative]::WS_EX_NOACTIVATE
1847
- if ($ClickThrough) {
1848
- $style = $style -bor [AiBatteryNative]::WS_EX_TRANSPARENT
1849
- }
1850
- [AiBatteryNative]::SetWindowLong($form.Handle, [AiBatteryNative]::GWL_EXSTYLE, $style) | Out-Null
2088
+ $style = [AiBatteryNative]::GetWindowLong($form.Handle, [AiBatteryNative]::GWL_EXSTYLE)
2089
+ $style = $style -bor [AiBatteryNative]::WS_EX_TOOLWINDOW -bor [AiBatteryNative]::WS_EX_NOACTIVATE
2090
+ if ($ClickThrough) {
2091
+ $style = $style -bor [AiBatteryNative]::WS_EX_TRANSPARENT
1851
2092
  }
2093
+ [AiBatteryNative]::SetWindowLong($form.Handle, [AiBatteryNative]::GWL_EXSTYLE, $style) | Out-Null
1852
2094
  $timer.Start()
2095
+ Start-FullscreenEventHooks
1853
2096
  $topMostTimer.Start()
1854
2097
  [System.Windows.Forms.Application]::Run($form)