ai-battery 0.1.19 → 0.1.21
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/LICENSE +21 -21
- package/README.md +369 -365
- package/bin/ai-battery-hud +0 -0
- package/bin/ai-battery-hud.js +0 -0
- package/bin/ai-battery-hud.ps1 +2 -2
- package/bin/ai-battery-run +80 -16
- package/bin/ai-battery-run-win.js +0 -0
- package/bin/ai-battery.js +0 -0
- package/bin/codex-wrapper-win.cjs +167 -167
- package/docs/RELEASING.md +64 -60
- package/docs/claude-statusline-preview.svg +37 -37
- package/docs/terminal-preview.svg +44 -44
- package/lib/cli.js +22 -10
- package/lib/platforms/process-scanner.js +7 -4
- package/lib/platforms/windows/codex-runner.js +62 -8
- package/lib/platforms/windows/hud/controls.ps1 +403 -403
- package/lib/platforms/windows/hud/dock.ps1 +731 -731
- package/lib/platforms/windows/hud/main.ps1 +1277 -1277
- package/lib/platforms/windows/hud/native.ps1 +169 -169
- package/lib/platforms/windows/hud/rendering.ps1 +382 -382
- package/lib/platforms/windows/hud/snapshot.ps1 +358 -358
- package/lib/platforms/windows/hud/windowing.ps1 +331 -331
- package/lib/platforms/windows/path-integration.js +14 -10
- package/package.json +75 -75
- package/scripts/windows-rowpty-smoke.ps1 +766 -766
- package/scripts/wsl-windows-rowpty-smoke.sh +0 -0
- package/vendor/rowpty/RowPty.cs +2668 -2668
- package/lib/README.md +0 -20
|
@@ -1,358 +1,358 @@
|
|
|
1
|
-
function Invoke-AiBatteryJson {
|
|
2
|
-
if ($UseWsl) {
|
|
3
|
-
$output = & wsl.exe bash -lc "$BatteryCommand 2>/dev/null"
|
|
4
|
-
} else {
|
|
5
|
-
$output = Invoke-LocalBatteryCommandOutput $BatteryCommand
|
|
6
|
-
}
|
|
7
|
-
$text = ($output -join "`n").Trim()
|
|
8
|
-
if (-not $text) { throw "ai-battery produced no output" }
|
|
9
|
-
return $text | ConvertFrom-Json
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function Invoke-LocalBatteryCommandOutput([string]$Command) {
|
|
13
|
-
try {
|
|
14
|
-
return Invoke-Expression "$Command 2>`$null"
|
|
15
|
-
} catch {
|
|
16
|
-
$trimmed = $Command.TrimStart()
|
|
17
|
-
if ($trimmed.StartsWith("&")) { throw }
|
|
18
|
-
return Invoke-Expression "& $Command 2>`$null"
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function Get-DurationText([Nullable[int]]$Seconds) {
|
|
23
|
-
if ($null -eq $Seconds) { return "?" }
|
|
24
|
-
if ($Seconds -le 0) { return "now" }
|
|
25
|
-
$minutes = [math]::Floor($Seconds / 60)
|
|
26
|
-
if ($minutes -lt 60) { return "${minutes}m" }
|
|
27
|
-
$hours = [math]::Floor($minutes / 60)
|
|
28
|
-
$restMinutes = $minutes % 60
|
|
29
|
-
if ($hours -lt 48) {
|
|
30
|
-
if ($restMinutes -gt 0) { return "${hours}h${restMinutes}m" }
|
|
31
|
-
return "${hours}h"
|
|
32
|
-
}
|
|
33
|
-
$days = [math]::Floor($hours / 24)
|
|
34
|
-
$restHours = $hours % 24
|
|
35
|
-
if ($restHours -gt 0) { return "${days}d${restHours}h" }
|
|
36
|
-
return "${days}d"
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function Get-ResetClock($Limit) {
|
|
40
|
-
if (-not $Limit -or -not $Limit.resetsAt) { return "--:--" }
|
|
41
|
-
if ($Limit.resetPassed) { return "--:--" }
|
|
42
|
-
try {
|
|
43
|
-
return ([datetime]$Limit.resetsAt).ToLocalTime().ToString("HH:mm")
|
|
44
|
-
} catch {
|
|
45
|
-
return "--:--"
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function Get-WindowText($Minutes) {
|
|
50
|
-
if ($Minutes -eq 300) { return "5h" }
|
|
51
|
-
if ($Minutes -eq 10080) { return "7d" }
|
|
52
|
-
if (-not $Minutes) { return "?" }
|
|
53
|
-
if (($Minutes % 1440) -eq 0) { return "$($Minutes / 1440)d" }
|
|
54
|
-
if (($Minutes % 60) -eq 0) { return "$($Minutes / 60)h" }
|
|
55
|
-
return "${Minutes}m"
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function Get-Bar([Nullable[int]]$Percent, [int]$Width = 10) {
|
|
59
|
-
$unknown = [string][char]0x2500
|
|
60
|
-
$fullBlock = [string][char]0x2588
|
|
61
|
-
$emptyBlock = [string][char]0x2591
|
|
62
|
-
if ($null -eq $Percent) { return $unknown * $Width }
|
|
63
|
-
# Whole cells only, matching bar() in ai-battery.js: partial blocks read as
|
|
64
|
-
# a hole in the bar and render inconsistently across fonts.
|
|
65
|
-
$clamped = [math]::Max(0, [math]::Min(100, $Percent))
|
|
66
|
-
$full = [int][math]::Round(($clamped / 100.0) * $Width)
|
|
67
|
-
if ($clamped -gt 0 -and $full -eq 0) { $full = 1 }
|
|
68
|
-
return ($fullBlock * $full) + ($emptyBlock * ($Width - $full))
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function Get-BatteryIcon([Nullable[int]]$Percent) {
|
|
72
|
-
return ""
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function Get-PercentColor([Nullable[int]]$Percent) {
|
|
76
|
-
if ($null -eq $Percent) { return [System.Drawing.Color]::FromArgb(150, 150, 150) }
|
|
77
|
-
if ($Percent -le 20) { return [System.Drawing.Color]::FromArgb(255, 92, 92) }
|
|
78
|
-
if ($Percent -le 40) { return [System.Drawing.Color]::FromArgb(255, 158, 67) }
|
|
79
|
-
return [System.Drawing.Color]::FromArgb(80, 220, 120)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function Test-HudDarkText {
|
|
83
|
-
$normalized = ([string]$Text).Trim().ToLowerInvariant()
|
|
84
|
-
return @("dark", "black", "ink", "light-taskbar", "lightbar") -contains $normalized
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function Get-HudActiveTextColor {
|
|
88
|
-
if ($script:hudDarkText) { return [System.Drawing.Color]::FromArgb(28, 28, 28) }
|
|
89
|
-
return [System.Drawing.Color]::FromArgb(235, 235, 235)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function Get-HudMutedTextColor {
|
|
93
|
-
if ($script:hudDarkText) { return [System.Drawing.Color]::FromArgb(88, 88, 88) }
|
|
94
|
-
return [System.Drawing.Color]::FromArgb(145, 145, 145)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function Get-HudDividerColor {
|
|
98
|
-
if ($script:hudDarkText) { return [System.Drawing.Color]::FromArgb(100, 100, 100) }
|
|
99
|
-
return [System.Drawing.Color]::FromArgb(132, 132, 132)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function Get-HudBatteryTextColor([bool]$Running) {
|
|
103
|
-
if ($script:hudDarkText) {
|
|
104
|
-
if ($Running) { return [System.Drawing.Color]::FromArgb(20, 20, 20) }
|
|
105
|
-
return [System.Drawing.Color]::FromArgb(78, 78, 78)
|
|
106
|
-
}
|
|
107
|
-
if ($Running) { return [System.Drawing.Color]::FromArgb(255, 255, 255) }
|
|
108
|
-
return [System.Drawing.Color]::FromArgb(235, 235, 235)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function Get-ActivityColor($Result) {
|
|
112
|
-
if ($Result -and $Result.running) { return Get-HudActiveTextColor }
|
|
113
|
-
return Get-HudMutedTextColor
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function Get-DividerColor {
|
|
117
|
-
return Get-HudDividerColor
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function Format-Parts($Result, [string]$Name) {
|
|
121
|
-
$isRunning = [bool]($Result -and $Result.running)
|
|
122
|
-
$textColor = Get-ActivityColor $Result
|
|
123
|
-
$displayName = $Name.PadRight(6)
|
|
124
|
-
$resetText = ""
|
|
125
|
-
$resetValue = ""
|
|
126
|
-
$weekText = ""
|
|
127
|
-
$weekValue = ""
|
|
128
|
-
$extraText = ""
|
|
129
|
-
|
|
130
|
-
if (-not $Result -or -not $Result.ok) {
|
|
131
|
-
return @{
|
|
132
|
-
Prefix = "$displayName "
|
|
133
|
-
Icon = Get-BatteryIcon $null
|
|
134
|
-
Percent = $null
|
|
135
|
-
PercentText = ""
|
|
136
|
-
ResetText = ""
|
|
137
|
-
ResetValue = ""
|
|
138
|
-
WeekText = ""
|
|
139
|
-
WeekValue = ""
|
|
140
|
-
ExtraText = "?"
|
|
141
|
-
Suffix = " ?"
|
|
142
|
-
Running = $isRunning
|
|
143
|
-
TextColor = $textColor
|
|
144
|
-
IconColor = Get-PercentColor $null
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if ($null -eq $Result.percentRemaining) {
|
|
149
|
-
$resetValue = "5h --:--"
|
|
150
|
-
$weekValue = "7d ---%"
|
|
151
|
-
$divider = [string][char]0x2502
|
|
152
|
-
return @{
|
|
153
|
-
Prefix = "$displayName "
|
|
154
|
-
Icon = Get-BatteryIcon $null
|
|
155
|
-
Percent = $null
|
|
156
|
-
PercentText = "--"
|
|
157
|
-
ResetText = $resetValue
|
|
158
|
-
ResetValue = $resetValue
|
|
159
|
-
WeekText = $weekValue
|
|
160
|
-
WeekValue = $weekValue
|
|
161
|
-
ExtraText = ""
|
|
162
|
-
Suffix = " $divider $resetValue $divider $weekValue"
|
|
163
|
-
Running = $isRunning
|
|
164
|
-
TextColor = $textColor
|
|
165
|
-
IconColor = Get-PercentColor $null
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
$divider = [string][char]0x2502
|
|
170
|
-
if ($Result.primary) {
|
|
171
|
-
$resetWindow = Get-WindowText $Result.primary.windowMinutes
|
|
172
|
-
$resetValue = "$resetWindow $(Get-ResetClock $Result.primary)".PadRight(8)
|
|
173
|
-
$resetText = $resetValue
|
|
174
|
-
}
|
|
175
|
-
if ($Result.secondary) {
|
|
176
|
-
$weekWindow = Get-WindowText $Result.secondary.windowMinutes
|
|
177
|
-
# Space plus a thin space (U+2009): a single space reads slightly tighter
|
|
178
|
-
# here than the "5h <time>" gap because the digits hug their left edge,
|
|
179
|
-
# while a full double space looks padded. The label keeps a fixed width,
|
|
180
|
-
# so the HUD does not resize when the digit count changes.
|
|
181
|
-
$weekValue = "$weekWindow $([char]0x2009)$([int]$Result.secondary.remainingPercent)%"
|
|
182
|
-
$weekText = $weekValue
|
|
183
|
-
}
|
|
184
|
-
$percentText = "$($Result.percentRemaining)"
|
|
185
|
-
$suffix = ""
|
|
186
|
-
if ($resetText) {
|
|
187
|
-
$suffix += " $divider " + $resetText
|
|
188
|
-
}
|
|
189
|
-
if ($weekText) {
|
|
190
|
-
$suffix += " $divider " + $weekText
|
|
191
|
-
}
|
|
192
|
-
return @{
|
|
193
|
-
Prefix = "$displayName "
|
|
194
|
-
Icon = Get-BatteryIcon $Result.percentRemaining
|
|
195
|
-
Percent = $Result.percentRemaining
|
|
196
|
-
PercentText = $percentText
|
|
197
|
-
ResetText = $resetText
|
|
198
|
-
ResetValue = $resetValue
|
|
199
|
-
WeekText = $weekText
|
|
200
|
-
WeekValue = $weekValue
|
|
201
|
-
ExtraText = ""
|
|
202
|
-
Suffix = $suffix
|
|
203
|
-
Running = $isRunning
|
|
204
|
-
TextColor = $textColor
|
|
205
|
-
IconColor = Get-PercentColor $Result.percentRemaining
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function Get-Provider($Snapshot, [string]$Name) {
|
|
210
|
-
return @($Snapshot.results | Where-Object { $_.provider -eq $Name })[0]
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function Copy-MissingProperty($Target, $Source, [string]$Name) {
|
|
214
|
-
if (-not $Target -or -not $Source) { return }
|
|
215
|
-
if ($null -eq $Target.$Name -and $null -ne $Source.$Name) {
|
|
216
|
-
$Target | Add-Member -NotePropertyName $Name -NotePropertyValue $Source.$Name -Force
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function Merge-HudSnapshot($Snapshot, $CachedSnapshot) {
|
|
221
|
-
if (-not $Snapshot -or -not $Snapshot.results) { return $CachedSnapshot }
|
|
222
|
-
$Snapshot.results = @($Snapshot.results)
|
|
223
|
-
$nowUtc = [datetime]::UtcNow
|
|
224
|
-
|
|
225
|
-
for ($i = 0; $i -lt $Snapshot.results.Count; $i += 1) {
|
|
226
|
-
$current = $Snapshot.results[$i]
|
|
227
|
-
if (-not $current -or -not $current.provider) { continue }
|
|
228
|
-
|
|
229
|
-
$usable = [bool]$current.ok -and $null -ne $current.percentRemaining
|
|
230
|
-
if ($usable) {
|
|
231
|
-
$current | Add-Member -NotePropertyName "hudCachedAt" -NotePropertyValue $nowUtc.ToString("o") -Force
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
$cached = $null
|
|
235
|
-
if ($CachedSnapshot) { $cached = Get-Provider $CachedSnapshot $current.provider }
|
|
236
|
-
if (-not $cached -or -not $cached.ok -or $null -eq $cached.percentRemaining) { continue }
|
|
237
|
-
|
|
238
|
-
if ($usable) {
|
|
239
|
-
Copy-MissingProperty $current $cached "secondary"
|
|
240
|
-
continue
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
# A transient read failure or a fallback row would flash "?"; keep the
|
|
244
|
-
# last good reading instead, as long as it is reasonably recent.
|
|
245
|
-
$cachedAt = $cached.hudCachedAt
|
|
246
|
-
if (-not $cachedAt) { $cachedAt = $CachedSnapshot.generatedAt }
|
|
247
|
-
$ageSeconds = $null
|
|
248
|
-
try {
|
|
249
|
-
$ageSeconds = ($nowUtc - ([datetime]$cachedAt).ToUniversalTime()).TotalSeconds
|
|
250
|
-
} catch {
|
|
251
|
-
$ageSeconds = $null
|
|
252
|
-
}
|
|
253
|
-
if ($null -eq $ageSeconds -or $ageSeconds -lt -60 -or $ageSeconds -gt 1800) { continue }
|
|
254
|
-
|
|
255
|
-
$replacement = $cached | Select-Object *
|
|
256
|
-
if ($null -ne $current.running) {
|
|
257
|
-
$replacement | Add-Member -NotePropertyName "running" -NotePropertyValue $current.running -Force
|
|
258
|
-
}
|
|
259
|
-
$Snapshot.results[$i] = $replacement
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return $Snapshot
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function ConvertTo-HudTexts($Snapshot) {
|
|
266
|
-
$codex = Get-Provider $Snapshot "codex"
|
|
267
|
-
$claude = Get-Provider $Snapshot "claude"
|
|
268
|
-
$codexVisible = $null -ne $codex
|
|
269
|
-
$claudeVisible = $null -ne $claude
|
|
270
|
-
return @{
|
|
271
|
-
Codex = $(if ($codexVisible) { Format-Parts $codex "Codex" } else { $null })
|
|
272
|
-
Claude = $(if ($claudeVisible) { Format-Parts $claude "Claude" } else { $null })
|
|
273
|
-
CodexResult = $codex
|
|
274
|
-
ClaudeResult = $claude
|
|
275
|
-
CodexVisible = $codexVisible
|
|
276
|
-
ClaudeVisible = $claudeVisible
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
function Get-Texts {
|
|
281
|
-
$cachedSnapshot = Read-HudSnapshot
|
|
282
|
-
if ($script:initialHudSnapshot) {
|
|
283
|
-
$snapshot = Merge-HudSnapshot $script:initialHudSnapshot $cachedSnapshot
|
|
284
|
-
$script:initialHudSnapshot = $null
|
|
285
|
-
} else {
|
|
286
|
-
try {
|
|
287
|
-
$snapshot = Merge-HudSnapshot (Invoke-AiBatteryJson) $cachedSnapshot
|
|
288
|
-
} catch {
|
|
289
|
-
if ($cachedSnapshot) {
|
|
290
|
-
$snapshot = $cachedSnapshot
|
|
291
|
-
} else {
|
|
292
|
-
throw
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
Write-HudSnapshot $snapshot
|
|
297
|
-
return ConvertTo-HudTexts $snapshot
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
$script:latestSnapshot = $null
|
|
301
|
-
$script:fetchPowerShell = $null
|
|
302
|
-
$script:fetchHandle = $null
|
|
303
|
-
$script:lastFetchStartUtc = [datetime]::MinValue
|
|
304
|
-
$script:weeklyRetryCount = 0
|
|
305
|
-
|
|
306
|
-
function Start-SnapshotFetch {
|
|
307
|
-
if ($script:fetchPowerShell) { return }
|
|
308
|
-
$ps = [powershell]::Create()
|
|
309
|
-
$null = $ps.AddScript({
|
|
310
|
-
param($Command, $UseWslShell)
|
|
311
|
-
try {
|
|
312
|
-
if ($UseWslShell) {
|
|
313
|
-
$output = & wsl.exe bash -lc "$Command 2>/dev/null"
|
|
314
|
-
} else {
|
|
315
|
-
try {
|
|
316
|
-
$output = Invoke-Expression "$Command 2>`$null"
|
|
317
|
-
} catch {
|
|
318
|
-
$trimmed = $Command.TrimStart()
|
|
319
|
-
if ($trimmed.StartsWith("&")) { throw }
|
|
320
|
-
$output = Invoke-Expression "& $Command 2>`$null"
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
($output -join "`n").Trim()
|
|
324
|
-
} catch {
|
|
325
|
-
""
|
|
326
|
-
}
|
|
327
|
-
}).AddArgument($BatteryCommand).AddArgument([bool]$UseWsl)
|
|
328
|
-
$script:fetchHandle = $ps.BeginInvoke()
|
|
329
|
-
$script:fetchPowerShell = $ps
|
|
330
|
-
$script:lastFetchStartUtc = [datetime]::UtcNow
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
function Stop-SnapshotFetch {
|
|
334
|
-
if (-not $script:fetchPowerShell) { return }
|
|
335
|
-
try { $script:fetchPowerShell.Stop() } catch { }
|
|
336
|
-
try { $script:fetchPowerShell.Dispose() } catch { }
|
|
337
|
-
$script:fetchPowerShell = $null
|
|
338
|
-
$script:fetchHandle = $null
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
function Complete-SnapshotFetch {
|
|
342
|
-
if (-not $script:fetchPowerShell -or -not $script:fetchHandle.IsCompleted) { return $null }
|
|
343
|
-
$text = ""
|
|
344
|
-
try {
|
|
345
|
-
$text = (($script:fetchPowerShell.EndInvoke($script:fetchHandle)) -join "`n").Trim()
|
|
346
|
-
} catch {
|
|
347
|
-
$text = ""
|
|
348
|
-
}
|
|
349
|
-
try { $script:fetchPowerShell.Dispose() } catch { }
|
|
350
|
-
$script:fetchPowerShell = $null
|
|
351
|
-
$script:fetchHandle = $null
|
|
352
|
-
if (-not $text) { return $null }
|
|
353
|
-
try {
|
|
354
|
-
return $text | ConvertFrom-Json
|
|
355
|
-
} catch {
|
|
356
|
-
return $null
|
|
357
|
-
}
|
|
358
|
-
}
|
|
1
|
+
function Invoke-AiBatteryJson {
|
|
2
|
+
if ($UseWsl) {
|
|
3
|
+
$output = & wsl.exe bash -lc "$BatteryCommand 2>/dev/null"
|
|
4
|
+
} else {
|
|
5
|
+
$output = Invoke-LocalBatteryCommandOutput $BatteryCommand
|
|
6
|
+
}
|
|
7
|
+
$text = ($output -join "`n").Trim()
|
|
8
|
+
if (-not $text) { throw "ai-battery produced no output" }
|
|
9
|
+
return $text | ConvertFrom-Json
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function Invoke-LocalBatteryCommandOutput([string]$Command) {
|
|
13
|
+
try {
|
|
14
|
+
return Invoke-Expression "$Command 2>`$null"
|
|
15
|
+
} catch {
|
|
16
|
+
$trimmed = $Command.TrimStart()
|
|
17
|
+
if ($trimmed.StartsWith("&")) { throw }
|
|
18
|
+
return Invoke-Expression "& $Command 2>`$null"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function Get-DurationText([Nullable[int]]$Seconds) {
|
|
23
|
+
if ($null -eq $Seconds) { return "?" }
|
|
24
|
+
if ($Seconds -le 0) { return "now" }
|
|
25
|
+
$minutes = [math]::Floor($Seconds / 60)
|
|
26
|
+
if ($minutes -lt 60) { return "${minutes}m" }
|
|
27
|
+
$hours = [math]::Floor($minutes / 60)
|
|
28
|
+
$restMinutes = $minutes % 60
|
|
29
|
+
if ($hours -lt 48) {
|
|
30
|
+
if ($restMinutes -gt 0) { return "${hours}h${restMinutes}m" }
|
|
31
|
+
return "${hours}h"
|
|
32
|
+
}
|
|
33
|
+
$days = [math]::Floor($hours / 24)
|
|
34
|
+
$restHours = $hours % 24
|
|
35
|
+
if ($restHours -gt 0) { return "${days}d${restHours}h" }
|
|
36
|
+
return "${days}d"
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function Get-ResetClock($Limit) {
|
|
40
|
+
if (-not $Limit -or -not $Limit.resetsAt) { return "--:--" }
|
|
41
|
+
if ($Limit.resetPassed) { return "--:--" }
|
|
42
|
+
try {
|
|
43
|
+
return ([datetime]$Limit.resetsAt).ToLocalTime().ToString("HH:mm")
|
|
44
|
+
} catch {
|
|
45
|
+
return "--:--"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function Get-WindowText($Minutes) {
|
|
50
|
+
if ($Minutes -eq 300) { return "5h" }
|
|
51
|
+
if ($Minutes -eq 10080) { return "7d" }
|
|
52
|
+
if (-not $Minutes) { return "?" }
|
|
53
|
+
if (($Minutes % 1440) -eq 0) { return "$($Minutes / 1440)d" }
|
|
54
|
+
if (($Minutes % 60) -eq 0) { return "$($Minutes / 60)h" }
|
|
55
|
+
return "${Minutes}m"
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function Get-Bar([Nullable[int]]$Percent, [int]$Width = 10) {
|
|
59
|
+
$unknown = [string][char]0x2500
|
|
60
|
+
$fullBlock = [string][char]0x2588
|
|
61
|
+
$emptyBlock = [string][char]0x2591
|
|
62
|
+
if ($null -eq $Percent) { return $unknown * $Width }
|
|
63
|
+
# Whole cells only, matching bar() in ai-battery.js: partial blocks read as
|
|
64
|
+
# a hole in the bar and render inconsistently across fonts.
|
|
65
|
+
$clamped = [math]::Max(0, [math]::Min(100, $Percent))
|
|
66
|
+
$full = [int][math]::Round(($clamped / 100.0) * $Width)
|
|
67
|
+
if ($clamped -gt 0 -and $full -eq 0) { $full = 1 }
|
|
68
|
+
return ($fullBlock * $full) + ($emptyBlock * ($Width - $full))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function Get-BatteryIcon([Nullable[int]]$Percent) {
|
|
72
|
+
return ""
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function Get-PercentColor([Nullable[int]]$Percent) {
|
|
76
|
+
if ($null -eq $Percent) { return [System.Drawing.Color]::FromArgb(150, 150, 150) }
|
|
77
|
+
if ($Percent -le 20) { return [System.Drawing.Color]::FromArgb(255, 92, 92) }
|
|
78
|
+
if ($Percent -le 40) { return [System.Drawing.Color]::FromArgb(255, 158, 67) }
|
|
79
|
+
return [System.Drawing.Color]::FromArgb(80, 220, 120)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function Test-HudDarkText {
|
|
83
|
+
$normalized = ([string]$Text).Trim().ToLowerInvariant()
|
|
84
|
+
return @("dark", "black", "ink", "light-taskbar", "lightbar") -contains $normalized
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function Get-HudActiveTextColor {
|
|
88
|
+
if ($script:hudDarkText) { return [System.Drawing.Color]::FromArgb(28, 28, 28) }
|
|
89
|
+
return [System.Drawing.Color]::FromArgb(235, 235, 235)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function Get-HudMutedTextColor {
|
|
93
|
+
if ($script:hudDarkText) { return [System.Drawing.Color]::FromArgb(88, 88, 88) }
|
|
94
|
+
return [System.Drawing.Color]::FromArgb(145, 145, 145)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function Get-HudDividerColor {
|
|
98
|
+
if ($script:hudDarkText) { return [System.Drawing.Color]::FromArgb(100, 100, 100) }
|
|
99
|
+
return [System.Drawing.Color]::FromArgb(132, 132, 132)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function Get-HudBatteryTextColor([bool]$Running) {
|
|
103
|
+
if ($script:hudDarkText) {
|
|
104
|
+
if ($Running) { return [System.Drawing.Color]::FromArgb(20, 20, 20) }
|
|
105
|
+
return [System.Drawing.Color]::FromArgb(78, 78, 78)
|
|
106
|
+
}
|
|
107
|
+
if ($Running) { return [System.Drawing.Color]::FromArgb(255, 255, 255) }
|
|
108
|
+
return [System.Drawing.Color]::FromArgb(235, 235, 235)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function Get-ActivityColor($Result) {
|
|
112
|
+
if ($Result -and $Result.running) { return Get-HudActiveTextColor }
|
|
113
|
+
return Get-HudMutedTextColor
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function Get-DividerColor {
|
|
117
|
+
return Get-HudDividerColor
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function Format-Parts($Result, [string]$Name) {
|
|
121
|
+
$isRunning = [bool]($Result -and $Result.running)
|
|
122
|
+
$textColor = Get-ActivityColor $Result
|
|
123
|
+
$displayName = $Name.PadRight(6)
|
|
124
|
+
$resetText = ""
|
|
125
|
+
$resetValue = ""
|
|
126
|
+
$weekText = ""
|
|
127
|
+
$weekValue = ""
|
|
128
|
+
$extraText = ""
|
|
129
|
+
|
|
130
|
+
if (-not $Result -or -not $Result.ok) {
|
|
131
|
+
return @{
|
|
132
|
+
Prefix = "$displayName "
|
|
133
|
+
Icon = Get-BatteryIcon $null
|
|
134
|
+
Percent = $null
|
|
135
|
+
PercentText = ""
|
|
136
|
+
ResetText = ""
|
|
137
|
+
ResetValue = ""
|
|
138
|
+
WeekText = ""
|
|
139
|
+
WeekValue = ""
|
|
140
|
+
ExtraText = "?"
|
|
141
|
+
Suffix = " ?"
|
|
142
|
+
Running = $isRunning
|
|
143
|
+
TextColor = $textColor
|
|
144
|
+
IconColor = Get-PercentColor $null
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if ($null -eq $Result.percentRemaining) {
|
|
149
|
+
$resetValue = "5h --:--"
|
|
150
|
+
$weekValue = "7d ---%"
|
|
151
|
+
$divider = [string][char]0x2502
|
|
152
|
+
return @{
|
|
153
|
+
Prefix = "$displayName "
|
|
154
|
+
Icon = Get-BatteryIcon $null
|
|
155
|
+
Percent = $null
|
|
156
|
+
PercentText = "--"
|
|
157
|
+
ResetText = $resetValue
|
|
158
|
+
ResetValue = $resetValue
|
|
159
|
+
WeekText = $weekValue
|
|
160
|
+
WeekValue = $weekValue
|
|
161
|
+
ExtraText = ""
|
|
162
|
+
Suffix = " $divider $resetValue $divider $weekValue"
|
|
163
|
+
Running = $isRunning
|
|
164
|
+
TextColor = $textColor
|
|
165
|
+
IconColor = Get-PercentColor $null
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
$divider = [string][char]0x2502
|
|
170
|
+
if ($Result.primary) {
|
|
171
|
+
$resetWindow = Get-WindowText $Result.primary.windowMinutes
|
|
172
|
+
$resetValue = "$resetWindow $(Get-ResetClock $Result.primary)".PadRight(8)
|
|
173
|
+
$resetText = $resetValue
|
|
174
|
+
}
|
|
175
|
+
if ($Result.secondary) {
|
|
176
|
+
$weekWindow = Get-WindowText $Result.secondary.windowMinutes
|
|
177
|
+
# Space plus a thin space (U+2009): a single space reads slightly tighter
|
|
178
|
+
# here than the "5h <time>" gap because the digits hug their left edge,
|
|
179
|
+
# while a full double space looks padded. The label keeps a fixed width,
|
|
180
|
+
# so the HUD does not resize when the digit count changes.
|
|
181
|
+
$weekValue = "$weekWindow $([char]0x2009)$([int]$Result.secondary.remainingPercent)%"
|
|
182
|
+
$weekText = $weekValue
|
|
183
|
+
}
|
|
184
|
+
$percentText = "$($Result.percentRemaining)"
|
|
185
|
+
$suffix = ""
|
|
186
|
+
if ($resetText) {
|
|
187
|
+
$suffix += " $divider " + $resetText
|
|
188
|
+
}
|
|
189
|
+
if ($weekText) {
|
|
190
|
+
$suffix += " $divider " + $weekText
|
|
191
|
+
}
|
|
192
|
+
return @{
|
|
193
|
+
Prefix = "$displayName "
|
|
194
|
+
Icon = Get-BatteryIcon $Result.percentRemaining
|
|
195
|
+
Percent = $Result.percentRemaining
|
|
196
|
+
PercentText = $percentText
|
|
197
|
+
ResetText = $resetText
|
|
198
|
+
ResetValue = $resetValue
|
|
199
|
+
WeekText = $weekText
|
|
200
|
+
WeekValue = $weekValue
|
|
201
|
+
ExtraText = ""
|
|
202
|
+
Suffix = $suffix
|
|
203
|
+
Running = $isRunning
|
|
204
|
+
TextColor = $textColor
|
|
205
|
+
IconColor = Get-PercentColor $Result.percentRemaining
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function Get-Provider($Snapshot, [string]$Name) {
|
|
210
|
+
return @($Snapshot.results | Where-Object { $_.provider -eq $Name })[0]
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function Copy-MissingProperty($Target, $Source, [string]$Name) {
|
|
214
|
+
if (-not $Target -or -not $Source) { return }
|
|
215
|
+
if ($null -eq $Target.$Name -and $null -ne $Source.$Name) {
|
|
216
|
+
$Target | Add-Member -NotePropertyName $Name -NotePropertyValue $Source.$Name -Force
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function Merge-HudSnapshot($Snapshot, $CachedSnapshot) {
|
|
221
|
+
if (-not $Snapshot -or -not $Snapshot.results) { return $CachedSnapshot }
|
|
222
|
+
$Snapshot.results = @($Snapshot.results)
|
|
223
|
+
$nowUtc = [datetime]::UtcNow
|
|
224
|
+
|
|
225
|
+
for ($i = 0; $i -lt $Snapshot.results.Count; $i += 1) {
|
|
226
|
+
$current = $Snapshot.results[$i]
|
|
227
|
+
if (-not $current -or -not $current.provider) { continue }
|
|
228
|
+
|
|
229
|
+
$usable = [bool]$current.ok -and $null -ne $current.percentRemaining
|
|
230
|
+
if ($usable) {
|
|
231
|
+
$current | Add-Member -NotePropertyName "hudCachedAt" -NotePropertyValue $nowUtc.ToString("o") -Force
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
$cached = $null
|
|
235
|
+
if ($CachedSnapshot) { $cached = Get-Provider $CachedSnapshot $current.provider }
|
|
236
|
+
if (-not $cached -or -not $cached.ok -or $null -eq $cached.percentRemaining) { continue }
|
|
237
|
+
|
|
238
|
+
if ($usable) {
|
|
239
|
+
Copy-MissingProperty $current $cached "secondary"
|
|
240
|
+
continue
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
# A transient read failure or a fallback row would flash "?"; keep the
|
|
244
|
+
# last good reading instead, as long as it is reasonably recent.
|
|
245
|
+
$cachedAt = $cached.hudCachedAt
|
|
246
|
+
if (-not $cachedAt) { $cachedAt = $CachedSnapshot.generatedAt }
|
|
247
|
+
$ageSeconds = $null
|
|
248
|
+
try {
|
|
249
|
+
$ageSeconds = ($nowUtc - ([datetime]$cachedAt).ToUniversalTime()).TotalSeconds
|
|
250
|
+
} catch {
|
|
251
|
+
$ageSeconds = $null
|
|
252
|
+
}
|
|
253
|
+
if ($null -eq $ageSeconds -or $ageSeconds -lt -60 -or $ageSeconds -gt 1800) { continue }
|
|
254
|
+
|
|
255
|
+
$replacement = $cached | Select-Object *
|
|
256
|
+
if ($null -ne $current.running) {
|
|
257
|
+
$replacement | Add-Member -NotePropertyName "running" -NotePropertyValue $current.running -Force
|
|
258
|
+
}
|
|
259
|
+
$Snapshot.results[$i] = $replacement
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return $Snapshot
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function ConvertTo-HudTexts($Snapshot) {
|
|
266
|
+
$codex = Get-Provider $Snapshot "codex"
|
|
267
|
+
$claude = Get-Provider $Snapshot "claude"
|
|
268
|
+
$codexVisible = $null -ne $codex
|
|
269
|
+
$claudeVisible = $null -ne $claude
|
|
270
|
+
return @{
|
|
271
|
+
Codex = $(if ($codexVisible) { Format-Parts $codex "Codex" } else { $null })
|
|
272
|
+
Claude = $(if ($claudeVisible) { Format-Parts $claude "Claude" } else { $null })
|
|
273
|
+
CodexResult = $codex
|
|
274
|
+
ClaudeResult = $claude
|
|
275
|
+
CodexVisible = $codexVisible
|
|
276
|
+
ClaudeVisible = $claudeVisible
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function Get-Texts {
|
|
281
|
+
$cachedSnapshot = Read-HudSnapshot
|
|
282
|
+
if ($script:initialHudSnapshot) {
|
|
283
|
+
$snapshot = Merge-HudSnapshot $script:initialHudSnapshot $cachedSnapshot
|
|
284
|
+
$script:initialHudSnapshot = $null
|
|
285
|
+
} else {
|
|
286
|
+
try {
|
|
287
|
+
$snapshot = Merge-HudSnapshot (Invoke-AiBatteryJson) $cachedSnapshot
|
|
288
|
+
} catch {
|
|
289
|
+
if ($cachedSnapshot) {
|
|
290
|
+
$snapshot = $cachedSnapshot
|
|
291
|
+
} else {
|
|
292
|
+
throw
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
Write-HudSnapshot $snapshot
|
|
297
|
+
return ConvertTo-HudTexts $snapshot
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
$script:latestSnapshot = $null
|
|
301
|
+
$script:fetchPowerShell = $null
|
|
302
|
+
$script:fetchHandle = $null
|
|
303
|
+
$script:lastFetchStartUtc = [datetime]::MinValue
|
|
304
|
+
$script:weeklyRetryCount = 0
|
|
305
|
+
|
|
306
|
+
function Start-SnapshotFetch {
|
|
307
|
+
if ($script:fetchPowerShell) { return }
|
|
308
|
+
$ps = [powershell]::Create()
|
|
309
|
+
$null = $ps.AddScript({
|
|
310
|
+
param($Command, $UseWslShell)
|
|
311
|
+
try {
|
|
312
|
+
if ($UseWslShell) {
|
|
313
|
+
$output = & wsl.exe bash -lc "$Command 2>/dev/null"
|
|
314
|
+
} else {
|
|
315
|
+
try {
|
|
316
|
+
$output = Invoke-Expression "$Command 2>`$null"
|
|
317
|
+
} catch {
|
|
318
|
+
$trimmed = $Command.TrimStart()
|
|
319
|
+
if ($trimmed.StartsWith("&")) { throw }
|
|
320
|
+
$output = Invoke-Expression "& $Command 2>`$null"
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
($output -join "`n").Trim()
|
|
324
|
+
} catch {
|
|
325
|
+
""
|
|
326
|
+
}
|
|
327
|
+
}).AddArgument($BatteryCommand).AddArgument([bool]$UseWsl)
|
|
328
|
+
$script:fetchHandle = $ps.BeginInvoke()
|
|
329
|
+
$script:fetchPowerShell = $ps
|
|
330
|
+
$script:lastFetchStartUtc = [datetime]::UtcNow
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function Stop-SnapshotFetch {
|
|
334
|
+
if (-not $script:fetchPowerShell) { return }
|
|
335
|
+
try { $script:fetchPowerShell.Stop() } catch { }
|
|
336
|
+
try { $script:fetchPowerShell.Dispose() } catch { }
|
|
337
|
+
$script:fetchPowerShell = $null
|
|
338
|
+
$script:fetchHandle = $null
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function Complete-SnapshotFetch {
|
|
342
|
+
if (-not $script:fetchPowerShell -or -not $script:fetchHandle.IsCompleted) { return $null }
|
|
343
|
+
$text = ""
|
|
344
|
+
try {
|
|
345
|
+
$text = (($script:fetchPowerShell.EndInvoke($script:fetchHandle)) -join "`n").Trim()
|
|
346
|
+
} catch {
|
|
347
|
+
$text = ""
|
|
348
|
+
}
|
|
349
|
+
try { $script:fetchPowerShell.Dispose() } catch { }
|
|
350
|
+
$script:fetchPowerShell = $null
|
|
351
|
+
$script:fetchHandle = $null
|
|
352
|
+
if (-not $text) { return $null }
|
|
353
|
+
try {
|
|
354
|
+
return $text | ConvertFrom-Json
|
|
355
|
+
} catch {
|
|
356
|
+
return $null
|
|
357
|
+
}
|
|
358
|
+
}
|