ai-battery 0.2.0 → 0.2.1
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 +30 -14
- package/bin/ai-battery-macos-status.applescript +5 -12
- package/bin/ai-battery-run +701 -31
- package/bin/ai-battery-tmux.js +17 -0
- package/bin/terminal-compositor.js +111 -0
- package/docs/REFERENCE.md +23 -3
- package/docs/RELEASING.md +24 -7
- package/docs/WINDOWS-TERMINAL-SHARED-BAR.md +74 -0
- package/docs/hud-preview.svg +43 -20
- package/docs/terminal-vertical-preview.svg +73 -0
- package/lib/cli.js +945 -219
- package/lib/core/atomic-rename.js +18 -0
- package/lib/core/cli-help.js +35 -7
- package/lib/core/cli-messages.js +45 -1
- package/lib/core/cli-output.js +277 -72
- package/lib/core/cli-wrappers.js +410 -0
- package/lib/core/config-editor.js +177 -29
- package/lib/core/display-config.js +45 -0
- package/lib/core/integration-files.js +32 -3
- package/lib/core/shared-bar.js +603 -0
- package/lib/core/statusline-git.js +179 -15
- package/lib/core/terminal.js +25 -7
- package/lib/core/tmux-status.js +172 -0
- package/lib/core/uninstall.js +20 -7
- package/lib/core/usage-store.js +85 -0
- package/lib/hud/launcher.js +29 -31
- package/lib/platforms/linux/hud.js +1 -1
- package/lib/platforms/linux/panel.js +1 -1
- package/lib/platforms/macos/hud.js +5 -5
- package/lib/platforms/process-scanner.js +77 -29
- package/lib/platforms/windows/autostart-cleanup.js +1 -1
- package/lib/platforms/windows/codex-runner.js +178 -21
- package/lib/platforms/windows/hud/controls.ps1 +44 -10
- package/lib/platforms/windows/hud/main.ps1 +350 -43
- package/lib/platforms/windows/hud/rendering.ps1 +1227 -1
- package/lib/platforms/windows/hud/snapshot.ps1 +26 -46
- package/lib/platforms/windows/hud-launcher.js +153 -79
- package/lib/platforms/windows/shared-bar-platform.js +972 -0
- package/lib/platforms/windows/terminal-compositor.js +210 -25
- package/lib/platforms/windows/wt-bar.js +729 -0
- package/lib/platforms/windows/wt-fragment.js +665 -0
- package/lib/platforms/windows/wt-keybinding.js +300 -0
- package/lib/providers/antigravity.js +155 -8
- package/package.json +5 -2
- package/vendor/PaneContext.cs +875 -0
package/bin/ai-battery-run
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
+
import json
|
|
2
3
|
import os
|
|
3
4
|
import re
|
|
4
5
|
import shutil
|
|
@@ -130,7 +131,7 @@ def infer_provider(command):
|
|
|
130
131
|
return "codex"
|
|
131
132
|
if any("claude" in name for name in names) or "@anthropic-ai/claude-code" in joined:
|
|
132
133
|
return "claude"
|
|
133
|
-
if any(name in ("agy", "agy.exe", "agy.cmd") for name in names):
|
|
134
|
+
if any(name in ("agy", "agy.exe", "agy.cmd", "agy.js", "agy.mjs", "agy.cjs") for name in names):
|
|
134
135
|
return "antigravity"
|
|
135
136
|
return "all"
|
|
136
137
|
|
|
@@ -199,13 +200,394 @@ def fit_ansi(text, width):
|
|
|
199
200
|
return "".join(output)
|
|
200
201
|
|
|
201
202
|
|
|
203
|
+
def slice_ansi(text, start_col, width):
|
|
204
|
+
if start_col <= 0:
|
|
205
|
+
return fit_ansi(text, width)
|
|
206
|
+
output = []
|
|
207
|
+
pending_sgr = []
|
|
208
|
+
emitted_prefix = False
|
|
209
|
+
col = 0
|
|
210
|
+
cells = 0
|
|
211
|
+
for token in ANSI_RE.split(text):
|
|
212
|
+
if token.startswith("\x1b"):
|
|
213
|
+
if SGR_RE.fullmatch(token):
|
|
214
|
+
if col < start_col:
|
|
215
|
+
if token in ("\x1b[0m", "\x1b[m"):
|
|
216
|
+
pending_sgr.clear()
|
|
217
|
+
else:
|
|
218
|
+
pending_sgr.append(token)
|
|
219
|
+
else:
|
|
220
|
+
output.append(token)
|
|
221
|
+
continue
|
|
222
|
+
for char in token:
|
|
223
|
+
category = unicodedata.category(char)
|
|
224
|
+
if category in ("Cc", "Cs"):
|
|
225
|
+
continue
|
|
226
|
+
char_width = 0 if category in ("Mn", "Me", "Cf") else (
|
|
227
|
+
2 if unicodedata.east_asian_width(char) in ("W", "F") else 1
|
|
228
|
+
)
|
|
229
|
+
if col + char_width <= start_col:
|
|
230
|
+
col += char_width
|
|
231
|
+
continue
|
|
232
|
+
if not emitted_prefix:
|
|
233
|
+
if pending_sgr:
|
|
234
|
+
output.extend(pending_sgr)
|
|
235
|
+
emitted_prefix = True
|
|
236
|
+
if col < start_col and col + char_width > start_col:
|
|
237
|
+
if cells + 1 > width:
|
|
238
|
+
return "".join(output)
|
|
239
|
+
output.append(" ")
|
|
240
|
+
cells += 1
|
|
241
|
+
col += char_width
|
|
242
|
+
continue
|
|
243
|
+
if cells + char_width > width:
|
|
244
|
+
return "".join(output)
|
|
245
|
+
output.append(char)
|
|
246
|
+
cells += char_width
|
|
247
|
+
col += char_width
|
|
248
|
+
return "".join(output)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def normalize_bar_position(pos):
|
|
252
|
+
val = str(pos or "").strip().lower()
|
|
253
|
+
if val in ("top", "up", "t"):
|
|
254
|
+
return "top"
|
|
255
|
+
if val in ("left", "l"):
|
|
256
|
+
return "left"
|
|
257
|
+
if val in ("right", "r"):
|
|
258
|
+
return "right"
|
|
259
|
+
if val in ("bottom", "down", "b"):
|
|
260
|
+
return "bottom"
|
|
261
|
+
return None
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def display_config_candidates():
|
|
265
|
+
candidates = []
|
|
266
|
+
for key in ("AI_BATTERY_SHARED_USAGE_STATE_DIR", "AI_BATTERY_WINDOWS_STATE_DIR", "AI_BATTERY_STATE_DIR"):
|
|
267
|
+
val = (os.environ.get(key) or "").strip()
|
|
268
|
+
if val:
|
|
269
|
+
candidates.append(os.path.join(val, "display-config.json"))
|
|
270
|
+
home = os.path.expanduser("~")
|
|
271
|
+
win_cache = os.path.join(home, ".local", "state", "ai-battery", "windows-state-dir")
|
|
272
|
+
try:
|
|
273
|
+
with open(win_cache, "r", encoding="utf-8") as f:
|
|
274
|
+
win_dir = f.read().strip()
|
|
275
|
+
if win_dir:
|
|
276
|
+
candidates.append(os.path.join(win_dir, "display-config.json"))
|
|
277
|
+
except OSError:
|
|
278
|
+
pass
|
|
279
|
+
candidates.append(os.path.join(home, ".local", "state", "ai-battery", "display-config.json"))
|
|
280
|
+
return candidates
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def read_bar_position():
|
|
284
|
+
override = (os.environ.get("AI_BATTERY_BAR_POSITION") or "").strip()
|
|
285
|
+
if override:
|
|
286
|
+
return normalize_bar_position(override) or "bottom"
|
|
287
|
+
# Use the same config precedence as native Windows and statusline hooks.
|
|
288
|
+
node = os.environ.get("AI_BATTERY_NODE")
|
|
289
|
+
if not node or not os.path.isfile(node) or not os.access(node, os.X_OK):
|
|
290
|
+
node = shutil.which("node")
|
|
291
|
+
reader = os.path.join(SCRIPT_DIR, "terminal-compositor.js")
|
|
292
|
+
if node and os.path.isfile(reader):
|
|
293
|
+
try:
|
|
294
|
+
result = subprocess.run([node, reader, "--bar-position"], capture_output=True, text=True, timeout=5)
|
|
295
|
+
pos = normalize_bar_position(result.stdout) if result.returncode == 0 else None
|
|
296
|
+
if pos:
|
|
297
|
+
return pos
|
|
298
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
299
|
+
pass
|
|
300
|
+
for path in display_config_candidates():
|
|
301
|
+
try:
|
|
302
|
+
with open(path, "r", encoding="utf-8") as f:
|
|
303
|
+
data = json.load(f)
|
|
304
|
+
pos = normalize_bar_position(data.get("barPosition"))
|
|
305
|
+
if pos:
|
|
306
|
+
return pos
|
|
307
|
+
except (OSError, ValueError, TypeError):
|
|
308
|
+
continue
|
|
309
|
+
return "bottom"
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
def read_sidebar_width():
|
|
313
|
+
return 5
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def write_all(fd, data):
|
|
317
|
+
view = memoryview(data)
|
|
318
|
+
written = 0
|
|
319
|
+
total = len(view)
|
|
320
|
+
while written < total:
|
|
321
|
+
try:
|
|
322
|
+
n = os.write(fd, view[written:])
|
|
323
|
+
if n <= 0:
|
|
324
|
+
break
|
|
325
|
+
written += n
|
|
326
|
+
except BlockingIOError:
|
|
327
|
+
select.select([], [fd], [], 0.05)
|
|
328
|
+
except InterruptedError:
|
|
329
|
+
continue
|
|
330
|
+
except OSError:
|
|
331
|
+
break
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
SGR_MOUSE_RE = re.compile(rb"\x1b\[<(\d+);(\d+);(\d+)([Mm])")
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def translate_sgr_mouse(data, col_offset, row_offset, max_cols, max_rows):
|
|
338
|
+
if (col_offset <= 0 and row_offset <= 0) or b"\x1b[<" not in data:
|
|
339
|
+
return data
|
|
340
|
+
|
|
341
|
+
def _replace(match):
|
|
342
|
+
btn = match.group(1)
|
|
343
|
+
col = max(1, min(max_cols, int(match.group(2)) - col_offset))
|
|
344
|
+
row = max(1, min(max_rows, int(match.group(3)) - row_offset))
|
|
345
|
+
suffix = match.group(4)
|
|
346
|
+
return b"\x1b[<" + btn + b";" + str(col).encode("ascii") + b";" + str(row).encode("ascii") + suffix
|
|
347
|
+
|
|
348
|
+
return SGR_MOUSE_RE.sub(_replace, data)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
class PosixCompositorBridge:
|
|
352
|
+
def __init__(self, position, sidebar_width, cols, rows, visible=True):
|
|
353
|
+
self.position = position
|
|
354
|
+
self.sidebar_width = max(5, min(6, int(sidebar_width)))
|
|
355
|
+
self.cols = cols
|
|
356
|
+
self.rows = rows
|
|
357
|
+
self.visible = visible
|
|
358
|
+
self.proc = None
|
|
359
|
+
self.in_buf = bytearray()
|
|
360
|
+
self.out_buf = bytearray()
|
|
361
|
+
self.last_status = None
|
|
362
|
+
self._start()
|
|
363
|
+
|
|
364
|
+
def _start(self):
|
|
365
|
+
compositor_script = os.path.join(SCRIPT_DIR, "terminal-compositor.js")
|
|
366
|
+
if not os.path.isfile(compositor_script):
|
|
367
|
+
return
|
|
368
|
+
node = os.environ.get("AI_BATTERY_NODE")
|
|
369
|
+
if not node or not os.path.isfile(node) or not os.access(node, os.X_OK):
|
|
370
|
+
node = shutil.which("node")
|
|
371
|
+
if not node:
|
|
372
|
+
return
|
|
373
|
+
cmd = [
|
|
374
|
+
node,
|
|
375
|
+
compositor_script,
|
|
376
|
+
"--stdio",
|
|
377
|
+
"--position",
|
|
378
|
+
self.position,
|
|
379
|
+
"--sidebar-width",
|
|
380
|
+
str(self.sidebar_width),
|
|
381
|
+
"--cols",
|
|
382
|
+
str(self.cols),
|
|
383
|
+
"--rows",
|
|
384
|
+
str(self.rows),
|
|
385
|
+
]
|
|
386
|
+
if not self.visible:
|
|
387
|
+
cmd.append("--hidden")
|
|
388
|
+
try:
|
|
389
|
+
self.proc = subprocess.Popen(
|
|
390
|
+
cmd,
|
|
391
|
+
stdin=subprocess.PIPE,
|
|
392
|
+
stdout=subprocess.PIPE,
|
|
393
|
+
stderr=subprocess.DEVNULL,
|
|
394
|
+
)
|
|
395
|
+
os.set_blocking(self.proc.stdout.fileno(), False)
|
|
396
|
+
os.set_blocking(self.proc.stdin.fileno(), False)
|
|
397
|
+
except OSError:
|
|
398
|
+
self.proc = None
|
|
399
|
+
return
|
|
400
|
+
|
|
401
|
+
# Wait briefly for the compositor's initial frame so startup failures
|
|
402
|
+
# (missing @xterm/headless, symlink guard mismatch, runtime crash)
|
|
403
|
+
# fall back cleanly instead of stranding the user in a blank terminal.
|
|
404
|
+
deadline = time.monotonic() + 2.0
|
|
405
|
+
ready = False
|
|
406
|
+
while time.monotonic() < deadline:
|
|
407
|
+
if self.proc.poll() is not None:
|
|
408
|
+
break
|
|
409
|
+
remaining = max(0.01, min(0.05, deadline - time.monotonic()))
|
|
410
|
+
try:
|
|
411
|
+
readable, _, _ = select.select([self.proc.stdout.fileno()], [], [], remaining)
|
|
412
|
+
except InterruptedError:
|
|
413
|
+
continue
|
|
414
|
+
if readable:
|
|
415
|
+
try:
|
|
416
|
+
chunk = os.read(self.proc.stdout.fileno(), 65536)
|
|
417
|
+
if chunk:
|
|
418
|
+
self.in_buf.extend(chunk)
|
|
419
|
+
if len(self.in_buf) >= 5:
|
|
420
|
+
ready = True
|
|
421
|
+
break
|
|
422
|
+
elif self.proc.poll() is not None:
|
|
423
|
+
break
|
|
424
|
+
except BlockingIOError:
|
|
425
|
+
pass
|
|
426
|
+
except OSError:
|
|
427
|
+
break
|
|
428
|
+
if not ready or self.proc.poll() is not None:
|
|
429
|
+
self.close()
|
|
430
|
+
|
|
431
|
+
@property
|
|
432
|
+
def active(self):
|
|
433
|
+
return self.proc is not None and self.proc.poll() is None
|
|
434
|
+
|
|
435
|
+
@property
|
|
436
|
+
def fd(self):
|
|
437
|
+
return self.proc.stdout.fileno() if self.active else None
|
|
438
|
+
|
|
439
|
+
@property
|
|
440
|
+
def write_fd(self):
|
|
441
|
+
return self.proc.stdin.fileno() if (self.active and self.out_buf and self.proc.stdin is not None) else None
|
|
442
|
+
|
|
443
|
+
def child_dims(self, cols, rows, visible):
|
|
444
|
+
if not visible:
|
|
445
|
+
return (max(1, cols), max(1, rows))
|
|
446
|
+
if self.position in ("left", "right") and cols >= self.sidebar_width + 11:
|
|
447
|
+
return (max(10, cols - self.sidebar_width - 1), max(1, rows))
|
|
448
|
+
return (max(1, cols), max(1, rows - 1))
|
|
449
|
+
|
|
450
|
+
def child_offsets(self, cols, visible):
|
|
451
|
+
if not visible:
|
|
452
|
+
return (0, 0)
|
|
453
|
+
if self.position == "left" and cols >= self.sidebar_width + 11:
|
|
454
|
+
return (self.sidebar_width + 1, 0)
|
|
455
|
+
if self.position == "top":
|
|
456
|
+
return (0, 1)
|
|
457
|
+
return (0, 0)
|
|
458
|
+
|
|
459
|
+
def flush_input(self):
|
|
460
|
+
if not self.active or self.proc.stdin is None or not self.out_buf:
|
|
461
|
+
return
|
|
462
|
+
fd = self.proc.stdin.fileno()
|
|
463
|
+
while self.out_buf:
|
|
464
|
+
try:
|
|
465
|
+
written = os.write(fd, self.out_buf)
|
|
466
|
+
if written <= 0:
|
|
467
|
+
break
|
|
468
|
+
del self.out_buf[:written]
|
|
469
|
+
except BlockingIOError:
|
|
470
|
+
break
|
|
471
|
+
except InterruptedError:
|
|
472
|
+
continue
|
|
473
|
+
except OSError:
|
|
474
|
+
self.out_buf.clear()
|
|
475
|
+
break
|
|
476
|
+
|
|
477
|
+
def _send(self, frame_type, payload=b""):
|
|
478
|
+
if not self.active or self.proc.stdin is None:
|
|
479
|
+
return
|
|
480
|
+
header = struct.pack(">BI", frame_type, len(payload))
|
|
481
|
+
self.out_buf.extend(header)
|
|
482
|
+
if payload:
|
|
483
|
+
self.out_buf.extend(payload)
|
|
484
|
+
self.flush_input()
|
|
485
|
+
|
|
486
|
+
def send_child(self, data):
|
|
487
|
+
if data:
|
|
488
|
+
self._send(1, data)
|
|
489
|
+
|
|
490
|
+
def send_status(self, text):
|
|
491
|
+
if text == self.last_status:
|
|
492
|
+
return
|
|
493
|
+
self.last_status = text
|
|
494
|
+
self._send(2, str(text or "").encode("utf-8", errors="replace"))
|
|
495
|
+
|
|
496
|
+
def send_resize(self, cols, rows):
|
|
497
|
+
self.cols = cols
|
|
498
|
+
self.rows = rows
|
|
499
|
+
self._send(3, struct.pack(">HH", max(2, min(65535, cols)), max(2, min(65535, rows))))
|
|
500
|
+
|
|
501
|
+
def send_visible(self, visible):
|
|
502
|
+
self.visible = bool(visible)
|
|
503
|
+
self._send(4, b"\x01" if self.visible else b"\x00")
|
|
504
|
+
|
|
505
|
+
def send_slice(self, bar_slice):
|
|
506
|
+
if isinstance(bar_slice, dict) and int(bar_slice.get("total") or 0) > 0:
|
|
507
|
+
offset = max(0, min(65535, int(bar_slice.get("offset") or 0)))
|
|
508
|
+
total = max(0, min(65535, int(bar_slice.get("total") or 0)))
|
|
509
|
+
else:
|
|
510
|
+
offset = 0
|
|
511
|
+
total = 0
|
|
512
|
+
self._send(7, struct.pack(">HH", offset, total))
|
|
513
|
+
|
|
514
|
+
def drain_output(self, master_fd, allow_exited=False):
|
|
515
|
+
if self.proc is None or self.proc.stdout is None:
|
|
516
|
+
return
|
|
517
|
+
if not allow_exited and not self.active:
|
|
518
|
+
return
|
|
519
|
+
try:
|
|
520
|
+
while True:
|
|
521
|
+
try:
|
|
522
|
+
chunk = os.read(self.proc.stdout.fileno(), 65536)
|
|
523
|
+
except BlockingIOError:
|
|
524
|
+
break
|
|
525
|
+
if not chunk:
|
|
526
|
+
break
|
|
527
|
+
self.in_buf.extend(chunk)
|
|
528
|
+
except OSError:
|
|
529
|
+
return
|
|
530
|
+
while len(self.in_buf) >= 5:
|
|
531
|
+
frame_type, length = struct.unpack_from(">BI", self.in_buf, 0)
|
|
532
|
+
if len(self.in_buf) < 5 + length:
|
|
533
|
+
break
|
|
534
|
+
payload = bytes(self.in_buf[5 : 5 + length])
|
|
535
|
+
del self.in_buf[: 5 + length]
|
|
536
|
+
if frame_type == 1:
|
|
537
|
+
write_all(sys.stdout.fileno(), payload)
|
|
538
|
+
elif frame_type == 2 and master_fd is not None:
|
|
539
|
+
write_all(master_fd, payload)
|
|
540
|
+
|
|
541
|
+
def close(self):
|
|
542
|
+
if self.proc is None:
|
|
543
|
+
return
|
|
544
|
+
proc = self.proc
|
|
545
|
+
try:
|
|
546
|
+
if proc.poll() is None:
|
|
547
|
+
self._send(6)
|
|
548
|
+
deadline = time.monotonic() + 0.6
|
|
549
|
+
while time.monotonic() < deadline:
|
|
550
|
+
self.flush_input()
|
|
551
|
+
self.drain_output(None, allow_exited=True)
|
|
552
|
+
if proc.poll() is not None:
|
|
553
|
+
break
|
|
554
|
+
time.sleep(0.01)
|
|
555
|
+
self.drain_output(None, allow_exited=True)
|
|
556
|
+
except OSError:
|
|
557
|
+
pass
|
|
558
|
+
self.out_buf.clear()
|
|
559
|
+
try:
|
|
560
|
+
if proc.stdin:
|
|
561
|
+
proc.stdin.close()
|
|
562
|
+
except OSError:
|
|
563
|
+
pass
|
|
564
|
+
try:
|
|
565
|
+
if proc.poll() is None:
|
|
566
|
+
proc.terminate()
|
|
567
|
+
proc.wait(timeout=0.5)
|
|
568
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
569
|
+
try:
|
|
570
|
+
proc.kill()
|
|
571
|
+
except OSError:
|
|
572
|
+
pass
|
|
573
|
+
try:
|
|
574
|
+
if proc.stdout:
|
|
575
|
+
proc.stdout.close()
|
|
576
|
+
except OSError:
|
|
577
|
+
pass
|
|
578
|
+
self.proc = None
|
|
579
|
+
|
|
580
|
+
|
|
202
581
|
class StatusLine:
|
|
203
|
-
def __init__(self, interval, bar_width, provider, left_padding, active_provider):
|
|
582
|
+
def __init__(self, interval, bar_width, provider, left_padding, active_provider, position="bottom", sidebar_width=5):
|
|
204
583
|
self.interval = interval
|
|
205
584
|
self.bar_width = bar_width
|
|
206
585
|
self.provider = provider
|
|
207
586
|
self.left_padding = left_padding
|
|
208
587
|
self.active_provider = active_provider
|
|
588
|
+
self.position = position if position in ("top", "bottom", "left", "right") else "bottom"
|
|
589
|
+
self.sidebar_width = max(5, int(sidebar_width or 5))
|
|
590
|
+
self.bar_slice = None
|
|
209
591
|
self.next_fetch = 0.0
|
|
210
592
|
self.last_draw = 0.0
|
|
211
593
|
self.text = "AI Battery starting..."
|
|
@@ -259,7 +641,12 @@ class StatusLine:
|
|
|
259
641
|
if not force and now < self.next_fetch:
|
|
260
642
|
return False
|
|
261
643
|
self.next_fetch = now + self.interval
|
|
262
|
-
|
|
644
|
+
if self.position in ("left", "right") and self.cols >= self.sidebar_width + 11:
|
|
645
|
+
max_width = 240
|
|
646
|
+
elif isinstance(self.bar_slice, dict) and int(self.bar_slice.get("total") or 0) > self.cols:
|
|
647
|
+
max_width = max(20, int(self.bar_slice["total"]) - column_guard())
|
|
648
|
+
else:
|
|
649
|
+
max_width = max(20, self.cols - column_guard())
|
|
263
650
|
try:
|
|
264
651
|
battery_command = battery_command_base() + [
|
|
265
652
|
"--muted",
|
|
@@ -319,11 +706,15 @@ class StatusLine:
|
|
|
319
706
|
return 0.05
|
|
320
707
|
return max(0.0, self.next_fetch - time.monotonic())
|
|
321
708
|
|
|
709
|
+
def _status_row(self):
|
|
710
|
+
return 1 if self.position == "top" else self.rows
|
|
711
|
+
|
|
322
712
|
def _write_row(self, line):
|
|
713
|
+
row = self._status_row()
|
|
323
714
|
payload = (
|
|
324
715
|
"\x1b7"
|
|
325
716
|
"\x1b[0m"
|
|
326
|
-
f"\x1b[{
|
|
717
|
+
f"\x1b[{row};1H"
|
|
327
718
|
"\r\x1b[1G"
|
|
328
719
|
f"{line}"
|
|
329
720
|
"\x1b[K"
|
|
@@ -332,10 +723,16 @@ class StatusLine:
|
|
|
332
723
|
)
|
|
333
724
|
os.write(sys.stdout.fileno(), payload.encode("utf-8", errors="replace"))
|
|
334
725
|
|
|
726
|
+
def _render_raw_line(self):
|
|
727
|
+
paint_width = max(1, self.cols - 1)
|
|
728
|
+
if isinstance(self.bar_slice, dict) and int(self.bar_slice.get("total") or 0) > self.cols:
|
|
729
|
+
offset = max(0, int(self.bar_slice.get("offset") or 0))
|
|
730
|
+
return slice_ansi(self.text, offset, paint_width)
|
|
731
|
+
return fit_ansi(self.text, paint_width)
|
|
732
|
+
|
|
335
733
|
def draw_cached(self):
|
|
336
734
|
"""Repaint cached text at the new terminal size without starting a poll."""
|
|
337
|
-
|
|
338
|
-
line = fit_ansi(self.text, paint_width)
|
|
735
|
+
line = self._render_raw_line()
|
|
339
736
|
self.last_draw = time.monotonic()
|
|
340
737
|
self.last_line = line
|
|
341
738
|
self._write_row(line)
|
|
@@ -345,8 +742,7 @@ class StatusLine:
|
|
|
345
742
|
if not force and now - self.last_draw < 0.15:
|
|
346
743
|
return
|
|
347
744
|
self.refresh(force)
|
|
348
|
-
|
|
349
|
-
line = fit_ansi(self.text, paint_width)
|
|
745
|
+
line = self._render_raw_line()
|
|
350
746
|
if not force and not dirty and line == self.last_line:
|
|
351
747
|
return
|
|
352
748
|
self.last_draw = now
|
|
@@ -354,7 +750,8 @@ class StatusLine:
|
|
|
354
750
|
self._write_row(line)
|
|
355
751
|
|
|
356
752
|
def clear(self):
|
|
357
|
-
|
|
753
|
+
row = self._status_row()
|
|
754
|
+
payload = "\x1b7" "\x1b[0m" f"\x1b[{row};1H" "\r\x1b[1G" "\x1b[2K" "\x1b8"
|
|
358
755
|
os.write(sys.stdout.fileno(), payload.encode("ascii"))
|
|
359
756
|
|
|
360
757
|
|
|
@@ -367,7 +764,152 @@ def tmux_status_bar_active():
|
|
|
367
764
|
if override in ("status", "plain", "off"):
|
|
368
765
|
return True
|
|
369
766
|
flag = os.environ.get("AI_BATTERY_TMUX_STATUS") or os.environ.get("CLAUDEX_BATTERY_TMUX_STATUS")
|
|
370
|
-
|
|
767
|
+
try:
|
|
768
|
+
target = ["-t", os.environ["TMUX_PANE"]] if os.environ.get("TMUX_PANE") else []
|
|
769
|
+
result = subprocess.run(["tmux", "display-message", "-p", *target,
|
|
770
|
+
"#{AI_BATTERY_TMUX_STATUS}\t#{status}\t#{?@ai_battery_tmux_saved,1,0}\t#{?@ai_battery_tmux_paused,1,0}"],
|
|
771
|
+
capture_output=True, text=True, timeout=1)
|
|
772
|
+
if result.returncode == 0:
|
|
773
|
+
enabled, height, managed, paused = result.stdout.strip().split("\t")
|
|
774
|
+
return enabled == "1" and paused != "1" and (height != "5" or managed == "1")
|
|
775
|
+
return flag == "1"
|
|
776
|
+
except (OSError, ValueError, subprocess.TimeoutExpired):
|
|
777
|
+
return flag == "1"
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
def is_wsl_wt_session():
|
|
781
|
+
if os.environ.get("TMUX") or os.environ.get("SSH_TTY") or os.environ.get("SSH_CONNECTION"):
|
|
782
|
+
return False
|
|
783
|
+
if os.environ.get("WSL_DISTRO_NAME") or os.environ.get("WSL_INTEROP"):
|
|
784
|
+
return True
|
|
785
|
+
try:
|
|
786
|
+
with open("/proc/version", "r", encoding="utf-8", errors="ignore") as f:
|
|
787
|
+
return "microsoft" in f.read().lower()
|
|
788
|
+
except OSError:
|
|
789
|
+
return False
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
class SharedBarBridge:
|
|
793
|
+
def __init__(self, provider, owner_pid):
|
|
794
|
+
self.enabled = is_wsl_wt_session() and provider in ("codex", "claude", "antigravity")
|
|
795
|
+
self.provider = provider
|
|
796
|
+
self.owner_pid = owner_pid
|
|
797
|
+
self.show_usage = True
|
|
798
|
+
self.bar_slice = None
|
|
799
|
+
self.bar_lease_path = None
|
|
800
|
+
self.last_lease_present = None
|
|
801
|
+
self.last_lease_check = 0.0
|
|
802
|
+
self.proc = None
|
|
803
|
+
self.output = bytearray()
|
|
804
|
+
self.next_poll = 0.0
|
|
805
|
+
self.deadline = 0.0
|
|
806
|
+
self.poll_count = 0
|
|
807
|
+
|
|
808
|
+
def close(self):
|
|
809
|
+
proc = self.proc
|
|
810
|
+
if proc is None:
|
|
811
|
+
return
|
|
812
|
+
self.proc = None
|
|
813
|
+
if proc.poll() is None:
|
|
814
|
+
try:
|
|
815
|
+
proc.kill()
|
|
816
|
+
except ProcessLookupError:
|
|
817
|
+
pass
|
|
818
|
+
proc.wait()
|
|
819
|
+
proc.stdout.close()
|
|
820
|
+
|
|
821
|
+
def poll(self):
|
|
822
|
+
if not self.enabled:
|
|
823
|
+
return False
|
|
824
|
+
now = time.monotonic()
|
|
825
|
+
if self.proc is not None:
|
|
826
|
+
try:
|
|
827
|
+
while True:
|
|
828
|
+
try:
|
|
829
|
+
chunk = os.read(self.proc.stdout.fileno(), 4096)
|
|
830
|
+
except BlockingIOError:
|
|
831
|
+
break
|
|
832
|
+
if not chunk:
|
|
833
|
+
break
|
|
834
|
+
self.output.extend(chunk)
|
|
835
|
+
code = self.proc.poll()
|
|
836
|
+
if code is not None:
|
|
837
|
+
raw = self.output.decode("utf-8", errors="replace").strip()
|
|
838
|
+
self.close()
|
|
839
|
+
if code == 0 and raw:
|
|
840
|
+
data = json.loads(raw)
|
|
841
|
+
next_show = bool(data.get("showUsage", True))
|
|
842
|
+
lease_path = data.get("barLeasePath")
|
|
843
|
+
if isinstance(lease_path, str) and lease_path:
|
|
844
|
+
self.bar_lease_path = lease_path
|
|
845
|
+
self.last_lease_present = os.path.exists(lease_path)
|
|
846
|
+
raw_slice = data.get("barSlice") if next_show else None
|
|
847
|
+
next_slice = (
|
|
848
|
+
{
|
|
849
|
+
"offset": int(raw_slice.get("offset") or 0),
|
|
850
|
+
"total": int(raw_slice.get("total") or 0),
|
|
851
|
+
}
|
|
852
|
+
if isinstance(raw_slice, dict) and int(raw_slice.get("total") or 0) > 0
|
|
853
|
+
else None
|
|
854
|
+
)
|
|
855
|
+
changed = (next_show != self.show_usage) or (next_slice != self.bar_slice)
|
|
856
|
+
self.show_usage = next_show
|
|
857
|
+
self.bar_slice = next_slice
|
|
858
|
+
if changed:
|
|
859
|
+
return True
|
|
860
|
+
elif now >= self.deadline:
|
|
861
|
+
self.close()
|
|
862
|
+
except (OSError, ValueError):
|
|
863
|
+
self.close()
|
|
864
|
+
return False
|
|
865
|
+
|
|
866
|
+
if self.bar_lease_path and (now - self.last_lease_check) >= 0.15:
|
|
867
|
+
self.last_lease_check = now
|
|
868
|
+
present = os.path.exists(self.bar_lease_path)
|
|
869
|
+
if not present:
|
|
870
|
+
trigger_path = os.path.join(os.path.dirname(self.bar_lease_path), "trigger.json")
|
|
871
|
+
try:
|
|
872
|
+
if (time.time() - os.path.getmtime(trigger_path)) <= 1.8:
|
|
873
|
+
present = True
|
|
874
|
+
except OSError:
|
|
875
|
+
pass
|
|
876
|
+
if self.last_lease_present is not None and present != self.last_lease_present:
|
|
877
|
+
self.last_lease_present = present
|
|
878
|
+
self.next_poll = 0.0
|
|
879
|
+
|
|
880
|
+
if now < self.next_poll:
|
|
881
|
+
return False
|
|
882
|
+
self.poll_count += 1
|
|
883
|
+
self.next_poll = now + (0.5 if self.poll_count <= 3 else 1.5)
|
|
884
|
+
try:
|
|
885
|
+
cols, rows = term_size()
|
|
886
|
+
cmd = battery_command_base() + [
|
|
887
|
+
"--pane-decision",
|
|
888
|
+
self.provider,
|
|
889
|
+
"--owner-pid",
|
|
890
|
+
str(self.owner_pid),
|
|
891
|
+
"--cols",
|
|
892
|
+
str(cols),
|
|
893
|
+
"--rows",
|
|
894
|
+
str(rows),
|
|
895
|
+
]
|
|
896
|
+
self.proc = subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
|
897
|
+
os.set_blocking(self.proc.stdout.fileno(), False)
|
|
898
|
+
self.deadline = now + 3.0
|
|
899
|
+
self.output = bytearray()
|
|
900
|
+
except OSError:
|
|
901
|
+
self.close()
|
|
902
|
+
return False
|
|
903
|
+
|
|
904
|
+
def cleanup(self):
|
|
905
|
+
self.close()
|
|
906
|
+
if not self.enabled:
|
|
907
|
+
return
|
|
908
|
+
try:
|
|
909
|
+
cmd = battery_command_base() + ["--pane-cleanup", self.provider, "--owner-pid", str(self.owner_pid)]
|
|
910
|
+
subprocess.run(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=1.5)
|
|
911
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
912
|
+
pass
|
|
371
913
|
|
|
372
914
|
|
|
373
915
|
def main():
|
|
@@ -392,40 +934,105 @@ def main():
|
|
|
392
934
|
raise SystemExit(2)
|
|
393
935
|
|
|
394
936
|
active_provider = command_provider if command_provider in ("codex", "claude", "antigravity") else None
|
|
395
|
-
|
|
937
|
+
bar_position = read_bar_position()
|
|
938
|
+
sidebar_width = read_sidebar_width()
|
|
939
|
+
shared_bar = SharedBarBridge(active_provider, os.getpid())
|
|
396
940
|
cols, rows = term_size()
|
|
397
|
-
|
|
941
|
+
compositor = None
|
|
942
|
+
if bar_position in ("top", "left", "right"):
|
|
943
|
+
candidate = PosixCompositorBridge(bar_position, sidebar_width, cols, rows, visible=shared_bar.show_usage)
|
|
944
|
+
if candidate.active:
|
|
945
|
+
compositor = candidate
|
|
946
|
+
else:
|
|
947
|
+
candidate.close()
|
|
948
|
+
bar_position = "bottom"
|
|
949
|
+
status = StatusLine(interval, bar_width, provider, left_padding, active_provider, position=bar_position, sidebar_width=sidebar_width)
|
|
950
|
+
if compositor is not None:
|
|
951
|
+
child_cols, child_rows = compositor.child_dims(cols, rows, shared_bar.show_usage)
|
|
952
|
+
else:
|
|
953
|
+
child_cols = cols
|
|
954
|
+
child_rows = child_rows_for(layout, rows) if shared_bar.show_usage else rows
|
|
955
|
+
|
|
956
|
+
def apply_scroll_region(total_rows, show_row):
|
|
957
|
+
if compositor is not None or bar_position != "top":
|
|
958
|
+
return
|
|
959
|
+
if show_row and total_rows > 1:
|
|
960
|
+
os.write(sys.stdout.fileno(), f"\x1b[2;{total_rows}r\x1b[2;1H".encode("ascii"))
|
|
961
|
+
else:
|
|
962
|
+
os.write(sys.stdout.fileno(), b"\x1b[r")
|
|
398
963
|
|
|
399
964
|
pid, master_fd = pty.fork()
|
|
400
965
|
if pid == 0:
|
|
401
|
-
set_winsize(0, child_rows,
|
|
966
|
+
set_winsize(0, child_rows, child_cols)
|
|
967
|
+
if active_provider in ("claude", "antigravity"):
|
|
968
|
+
os.environ["AI_BATTERY_EXTERNAL_BAR"] = "1"
|
|
402
969
|
os.execvp(command[0], command)
|
|
403
970
|
|
|
404
|
-
set_winsize(master_fd, child_rows,
|
|
971
|
+
set_winsize(master_fd, child_rows, child_cols)
|
|
405
972
|
old_term = termios.tcgetattr(sys.stdin.fileno())
|
|
406
973
|
child_exit = None
|
|
407
974
|
|
|
408
|
-
def
|
|
975
|
+
def sync_pty_size():
|
|
409
976
|
status.resize()
|
|
410
977
|
new_cols, new_rows = term_size()
|
|
411
|
-
|
|
978
|
+
if compositor is not None:
|
|
979
|
+
compositor.send_resize(new_cols, new_rows)
|
|
980
|
+
target_cols, target_rows = compositor.child_dims(new_cols, new_rows, shared_bar.show_usage)
|
|
981
|
+
set_winsize(master_fd, target_rows, target_cols)
|
|
982
|
+
else:
|
|
983
|
+
apply_scroll_region(new_rows, shared_bar.show_usage)
|
|
984
|
+
target_rows = child_rows_for(layout, new_rows) if shared_bar.show_usage else new_rows
|
|
985
|
+
set_winsize(master_fd, target_rows, new_cols)
|
|
412
986
|
try:
|
|
413
987
|
os.kill(pid, signal.SIGWINCH)
|
|
414
988
|
except ProcessLookupError:
|
|
415
989
|
pass
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
990
|
+
|
|
991
|
+
resize_pending = False
|
|
992
|
+
|
|
993
|
+
def on_resize(_signum, _frame):
|
|
994
|
+
nonlocal resize_pending
|
|
995
|
+
resize_pending = True
|
|
419
996
|
|
|
420
997
|
signal.signal(signal.SIGWINCH, on_resize)
|
|
421
998
|
|
|
422
999
|
try:
|
|
423
1000
|
tty.setraw(sys.stdin.fileno())
|
|
424
|
-
|
|
1001
|
+
apply_scroll_region(rows, shared_bar.show_usage)
|
|
1002
|
+
if compositor is not None:
|
|
1003
|
+
compositor.drain_output(master_fd)
|
|
1004
|
+
if shared_bar.show_usage:
|
|
1005
|
+
if compositor is not None:
|
|
1006
|
+
status.refresh(True)
|
|
1007
|
+
compositor.send_status(status.text)
|
|
1008
|
+
compositor.drain_output(master_fd)
|
|
1009
|
+
else:
|
|
1010
|
+
status.draw(True)
|
|
425
1011
|
screen_dirty = False
|
|
426
1012
|
last_child_output = 0.0
|
|
427
1013
|
|
|
428
1014
|
while True:
|
|
1015
|
+
if compositor is not None and not compositor.active:
|
|
1016
|
+
compositor.close()
|
|
1017
|
+
compositor = None
|
|
1018
|
+
bar_position = "bottom"
|
|
1019
|
+
status.position = "bottom"
|
|
1020
|
+
write_all(sys.stdout.fileno(), b"\x1b[?1049l\x1b[?25h")
|
|
1021
|
+
sync_pty_size()
|
|
1022
|
+
if shared_bar.show_usage:
|
|
1023
|
+
status.draw(True)
|
|
1024
|
+
|
|
1025
|
+
if resize_pending:
|
|
1026
|
+
resize_pending = False
|
|
1027
|
+
sync_pty_size()
|
|
1028
|
+
shared_bar.next_poll = 0.0
|
|
1029
|
+
if shared_bar.show_usage:
|
|
1030
|
+
if compositor is not None:
|
|
1031
|
+
status.next_fetch = 0.0
|
|
1032
|
+
else:
|
|
1033
|
+
status.draw_cached()
|
|
1034
|
+
status.next_fetch = 0.0
|
|
1035
|
+
|
|
429
1036
|
wait_pid, wait_status = os.waitpid(pid, os.WNOHANG)
|
|
430
1037
|
if wait_pid == pid:
|
|
431
1038
|
if os.WIFEXITED(wait_status):
|
|
@@ -444,11 +1051,32 @@ def main():
|
|
|
444
1051
|
raise
|
|
445
1052
|
if not data:
|
|
446
1053
|
break
|
|
447
|
-
|
|
1054
|
+
if compositor is not None and compositor.active:
|
|
1055
|
+
compositor.send_child(data)
|
|
1056
|
+
compositor.drain_output(master_fd)
|
|
1057
|
+
else:
|
|
1058
|
+
os.write(sys.stdout.fileno(), data)
|
|
448
1059
|
break
|
|
449
1060
|
|
|
450
1061
|
timeout = 0.05 if screen_dirty else min(0.1, max(0.01, status.seconds_until_refresh()))
|
|
451
|
-
|
|
1062
|
+
if shared_bar.enabled and shared_bar.proc is None and shared_bar.next_poll <= time.monotonic():
|
|
1063
|
+
timeout = 0.0
|
|
1064
|
+
watch_fds = [master_fd, sys.stdin.fileno()]
|
|
1065
|
+
write_fds = []
|
|
1066
|
+
comp_fd = compositor.fd if compositor is not None else None
|
|
1067
|
+
comp_wfd = compositor.write_fd if compositor is not None else None
|
|
1068
|
+
if comp_fd is not None:
|
|
1069
|
+
watch_fds.append(comp_fd)
|
|
1070
|
+
if comp_wfd is not None:
|
|
1071
|
+
write_fds.append(comp_wfd)
|
|
1072
|
+
try:
|
|
1073
|
+
readable, writable, _ = select.select(watch_fds, write_fds, [], timeout)
|
|
1074
|
+
except InterruptedError:
|
|
1075
|
+
continue
|
|
1076
|
+
if comp_wfd is not None and comp_wfd in writable:
|
|
1077
|
+
compositor.flush_input()
|
|
1078
|
+
if comp_fd is not None and comp_fd in readable:
|
|
1079
|
+
compositor.drain_output(master_fd)
|
|
452
1080
|
if master_fd in readable:
|
|
453
1081
|
try:
|
|
454
1082
|
data = os.read(master_fd, 8192)
|
|
@@ -458,25 +1086,67 @@ def main():
|
|
|
458
1086
|
raise
|
|
459
1087
|
if not data:
|
|
460
1088
|
break
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
1089
|
+
if compositor is not None and compositor.active:
|
|
1090
|
+
compositor.send_child(data)
|
|
1091
|
+
compositor.drain_output(master_fd)
|
|
1092
|
+
else:
|
|
1093
|
+
os.write(sys.stdout.fileno(), data)
|
|
1094
|
+
screen_dirty = True
|
|
1095
|
+
last_child_output = time.monotonic()
|
|
464
1096
|
|
|
465
1097
|
if sys.stdin.fileno() in readable:
|
|
466
1098
|
data = os.read(sys.stdin.fileno(), 8192)
|
|
467
1099
|
if data:
|
|
1100
|
+
if compositor is not None and shared_bar.show_usage:
|
|
1101
|
+
c_off, r_off = compositor.child_offsets(compositor.cols, True)
|
|
1102
|
+
c_cols, c_rows = compositor.child_dims(compositor.cols, compositor.rows, True)
|
|
1103
|
+
data = translate_sgr_mouse(data, c_off, r_off, c_cols, c_rows)
|
|
468
1104
|
os.write(master_fd, data)
|
|
469
1105
|
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
1106
|
+
if shared_bar.poll():
|
|
1107
|
+
status.bar_slice = shared_bar.bar_slice if shared_bar.show_usage else None
|
|
1108
|
+
if compositor is not None:
|
|
1109
|
+
compositor.send_visible(shared_bar.show_usage)
|
|
1110
|
+
compositor.send_slice(shared_bar.bar_slice if shared_bar.show_usage else None)
|
|
1111
|
+
sync_pty_size()
|
|
1112
|
+
if shared_bar.show_usage:
|
|
1113
|
+
status.refresh(True)
|
|
1114
|
+
compositor.send_status(status.text)
|
|
1115
|
+
else:
|
|
1116
|
+
if not shared_bar.show_usage:
|
|
1117
|
+
status.clear()
|
|
1118
|
+
sync_pty_size()
|
|
1119
|
+
if shared_bar.show_usage:
|
|
1120
|
+
status.draw(True)
|
|
1121
|
+
|
|
1122
|
+
if shared_bar.show_usage:
|
|
1123
|
+
if compositor is not None:
|
|
1124
|
+
if status.refresh_due():
|
|
1125
|
+
status.refresh(False)
|
|
1126
|
+
compositor.send_status(status.text)
|
|
1127
|
+
compositor.drain_output(master_fd)
|
|
1128
|
+
else:
|
|
1129
|
+
now = time.monotonic()
|
|
1130
|
+
if screen_dirty and (now - last_child_output >= 0.05 or now - status.last_draw >= 0.75):
|
|
1131
|
+
status.draw(False, dirty=True)
|
|
1132
|
+
screen_dirty = False
|
|
1133
|
+
elif status.refresh_due():
|
|
1134
|
+
status.draw(False)
|
|
1135
|
+
else:
|
|
473
1136
|
screen_dirty = False
|
|
474
|
-
elif status.refresh_due():
|
|
475
|
-
status.draw(False)
|
|
476
1137
|
finally:
|
|
1138
|
+
shared_bar.cleanup()
|
|
477
1139
|
status.close()
|
|
1140
|
+
if compositor is not None:
|
|
1141
|
+
compositor.close()
|
|
478
1142
|
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old_term)
|
|
479
|
-
|
|
1143
|
+
if compositor is None:
|
|
1144
|
+
if bar_position == "top":
|
|
1145
|
+
try:
|
|
1146
|
+
os.write(sys.stdout.fileno(), b"\x1b[r")
|
|
1147
|
+
except OSError:
|
|
1148
|
+
pass
|
|
1149
|
+
status.clear()
|
|
480
1150
|
|
|
481
1151
|
# PTY EOF/EIO may precede the nonblocking wait above. Preserve the
|
|
482
1152
|
# command's exit code in that case as well (restored from 0.1.24).
|