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.
Files changed (45) hide show
  1. package/README.md +30 -14
  2. package/bin/ai-battery-macos-status.applescript +5 -12
  3. package/bin/ai-battery-run +701 -31
  4. package/bin/ai-battery-tmux.js +17 -0
  5. package/bin/terminal-compositor.js +111 -0
  6. package/docs/REFERENCE.md +23 -3
  7. package/docs/RELEASING.md +24 -7
  8. package/docs/WINDOWS-TERMINAL-SHARED-BAR.md +74 -0
  9. package/docs/hud-preview.svg +43 -20
  10. package/docs/terminal-vertical-preview.svg +73 -0
  11. package/lib/cli.js +945 -219
  12. package/lib/core/atomic-rename.js +18 -0
  13. package/lib/core/cli-help.js +35 -7
  14. package/lib/core/cli-messages.js +45 -1
  15. package/lib/core/cli-output.js +277 -72
  16. package/lib/core/cli-wrappers.js +410 -0
  17. package/lib/core/config-editor.js +177 -29
  18. package/lib/core/display-config.js +45 -0
  19. package/lib/core/integration-files.js +32 -3
  20. package/lib/core/shared-bar.js +603 -0
  21. package/lib/core/statusline-git.js +179 -15
  22. package/lib/core/terminal.js +25 -7
  23. package/lib/core/tmux-status.js +172 -0
  24. package/lib/core/uninstall.js +20 -7
  25. package/lib/core/usage-store.js +85 -0
  26. package/lib/hud/launcher.js +29 -31
  27. package/lib/platforms/linux/hud.js +1 -1
  28. package/lib/platforms/linux/panel.js +1 -1
  29. package/lib/platforms/macos/hud.js +5 -5
  30. package/lib/platforms/process-scanner.js +77 -29
  31. package/lib/platforms/windows/autostart-cleanup.js +1 -1
  32. package/lib/platforms/windows/codex-runner.js +178 -21
  33. package/lib/platforms/windows/hud/controls.ps1 +44 -10
  34. package/lib/platforms/windows/hud/main.ps1 +350 -43
  35. package/lib/platforms/windows/hud/rendering.ps1 +1227 -1
  36. package/lib/platforms/windows/hud/snapshot.ps1 +26 -46
  37. package/lib/platforms/windows/hud-launcher.js +153 -79
  38. package/lib/platforms/windows/shared-bar-platform.js +972 -0
  39. package/lib/platforms/windows/terminal-compositor.js +210 -25
  40. package/lib/platforms/windows/wt-bar.js +729 -0
  41. package/lib/platforms/windows/wt-fragment.js +665 -0
  42. package/lib/platforms/windows/wt-keybinding.js +300 -0
  43. package/lib/providers/antigravity.js +155 -8
  44. package/package.json +5 -2
  45. package/vendor/PaneContext.cs +875 -0
@@ -1,5 +1,7 @@
1
1
  import { createRequire } from "node:module";
2
+ import { normalizeBarPosition } from "../../core/display-config.js";
2
3
  import { visibleWidth } from "../../core/terminal.js";
4
+ import { formatVerticalStatusLines } from "./wt-bar.js";
3
5
 
4
6
  const require = createRequire(import.meta.url);
5
7
 
@@ -49,6 +51,13 @@ function rgbCode(prefix, packed) {
49
51
  return `${prefix};2;${red};${green};${blue}`;
50
52
  }
51
53
 
54
+ function underlineRgbCode(packed) {
55
+ const red = (packed >> 16) & 0xff;
56
+ const green = (packed >> 8) & 0xff;
57
+ const blue = packed & 0xff;
58
+ return `58:2::${red}:${green}:${blue}`;
59
+ }
60
+
52
61
  function cellStyleCodes(cell) {
53
62
  const codes = [];
54
63
  if (cell.isBold()) codes.push("1");
@@ -76,20 +85,26 @@ function cellStyleCodes(cell) {
76
85
  codes.push(paletteBackgroundCode(cell.getBgColor()));
77
86
  }
78
87
 
79
- if (cell.isUnderlineColorRGB?.()) {
80
- codes.push(rgbCode("58", cell.getUnderlineColor()));
81
- } else if (cell.isUnderlineColorPalette?.()) {
82
- codes.push(`58;5;${cell.getUnderlineColor()}`);
88
+ // @xterm/headless falls back to fg color for isUnderlineColorRGB/Palette when
89
+ // underline is off; also emit ITU T.416 colon subparameters so terminals that
90
+ // ignore SGR 58 never misparse ";2;" as SGR 2 (dim) or ";5;" as SGR 5 (blink).
91
+ if (cell.isUnderline() && !cell.isUnderlineColorDefault?.()) {
92
+ if (cell.isUnderlineColorRGB?.()) {
93
+ codes.push(underlineRgbCode(cell.getUnderlineColor()));
94
+ } else if (cell.isUnderlineColorPalette?.()) {
95
+ codes.push(`58:5:${cell.getUnderlineColor()}`);
96
+ }
83
97
  }
84
98
  return codes;
85
99
  }
86
100
 
87
- function renderBufferLine(line, columns) {
101
+ function renderBufferLineSlice(line, startColumn, columns) {
88
102
  let output = "";
89
103
  let previousStyle = null;
90
104
  let renderedColumns = 0;
105
+ const endColumn = startColumn + columns;
91
106
 
92
- for (let column = 0; column < columns; column += 1) {
107
+ for (let column = startColumn; column < endColumn; column += 1) {
93
108
  const cell = line?.getCell(column);
94
109
  if (!cell) {
95
110
  if (previousStyle !== "default") {
@@ -102,7 +117,26 @@ function renderBufferLine(line, columns) {
102
117
  }
103
118
 
104
119
  const width = cell.getWidth();
105
- if (width === 0) continue;
120
+ if (width === 0) {
121
+ if (column === startColumn) {
122
+ if (previousStyle !== "default") {
123
+ output += `${CSI}0m`;
124
+ previousStyle = "default";
125
+ }
126
+ output += " ";
127
+ renderedColumns += 1;
128
+ }
129
+ continue;
130
+ }
131
+ if (renderedColumns + width > columns) {
132
+ if (previousStyle !== "default") {
133
+ output += `${CSI}0m`;
134
+ previousStyle = "default";
135
+ }
136
+ output += " ".repeat(columns - renderedColumns);
137
+ renderedColumns = columns;
138
+ break;
139
+ }
106
140
 
107
141
  const style = cellStyleCodes(cell).join(";");
108
142
  if (style !== previousStyle) {
@@ -118,6 +152,10 @@ function renderBufferLine(line, columns) {
118
152
  return `${output}${CSI}0m`;
119
153
  }
120
154
 
155
+ function renderBufferLine(line, columns) {
156
+ return renderBufferLineSlice(line, 0, columns);
157
+ }
158
+
121
159
  function modeSequence(parameter, enabled) {
122
160
  return `${CSI}?${parameter}${enabled ? "h" : "l"}`;
123
161
  }
@@ -181,7 +219,16 @@ class HeadlessTerminalCompositor {
181
219
  constructor(options = {}) {
182
220
  this.columns = clampInteger(options.columns, 2, 1000, 80);
183
221
  this.rows = clampInteger(options.rows, 2, 1000, 24);
184
- this.childRows = Math.max(1, this.rows - 1);
222
+ this.statusVisible = options.statusVisible !== false;
223
+ this.statusPosition = normalizeBarPosition(options.statusPosition || "bottom", "bottom");
224
+ this.sidebarWidth = clampInteger(options.sidebarWidth, 5, 5, 5);
225
+ this.statusSliceOffset = options.statusSlice && Number(options.statusSlice.total) > 0
226
+ ? Math.max(0, Math.trunc(Number(options.statusSlice.offset) || 0))
227
+ : 0;
228
+ this.statusSliceTotal = options.statusSlice && Number(options.statusSlice.total) > 0
229
+ ? Math.max(0, Math.trunc(Number(options.statusSlice.total) || 0))
230
+ : 0;
231
+ this.recomputeLayout();
185
232
  this.frameIntervalMs = clampInteger(options.frameIntervalMs, 0, 1000, DEFAULT_FRAME_INTERVAL_MS);
186
233
  const usesDefaultHost = typeof options.writeHost !== "function";
187
234
  this.usesDefaultHost = usesDefaultHost;
@@ -213,8 +260,8 @@ class HeadlessTerminalCompositor {
213
260
  windowsPty: options.windowsPty,
214
261
  windowOptions: options.windowOptions
215
262
  };
216
- this.terminal = makeTerminal(this.columns, this.childRows, this.terminalOptions);
217
- this.statusTerminal = makeTerminal(Math.max(1, this.columns - 1), 1, {
263
+ this.terminal = makeTerminal(this.childColumns, this.childRows, this.terminalOptions);
264
+ this.statusTerminal = makeTerminal(this.horizontalStatusWidth(), 1, {
218
265
  scrollback: 0,
219
266
  windowsMode: false
220
267
  });
@@ -342,6 +389,18 @@ class HeadlessTerminalCompositor {
342
389
 
343
390
  input(data) {
344
391
  if (this.disposed || typeof this.onReply !== "function") return;
392
+ if ((this.childColOffset > 0 || this.childRowOffset > 0) && this.mousePrivateModes.size > 0) {
393
+ const text = Buffer.isBuffer(data) ? data.toString("latin1") : String(data);
394
+ if (text.includes("\x1b[<")) {
395
+ const translated = text.replace(/\x1b\[<(\d+);(\d+);(\d+)([Mm])/g, (_m, btn, col, row, suffix) => {
396
+ const adjCol = Math.max(1, Math.min(this.childColumns, Number(col) - this.childColOffset));
397
+ const adjRow = Math.max(1, Math.min(this.childRows, Number(row) - this.childRowOffset));
398
+ return `\x1b[<${btn};${adjCol};${adjRow}${suffix}`;
399
+ });
400
+ this.onReply(Buffer.isBuffer(data) ? Buffer.from(translated, "latin1") : translated);
401
+ return;
402
+ }
403
+ }
345
404
  this.onReply(data);
346
405
  }
347
406
 
@@ -365,13 +424,103 @@ class HeadlessTerminalCompositor {
365
424
  });
366
425
  }
367
426
 
427
+ setStatusVisible(visible) {
428
+ if (this.statusVisible === Boolean(visible)) return Promise.resolve();
429
+ this.statusVisible = Boolean(visible);
430
+ return this.resize(this.columns, this.rows);
431
+ }
432
+
433
+ horizontalStatusWidth() {
434
+ const isSide = this.effectivePosition === "left" || this.effectivePosition === "right";
435
+ const span = !isSide && this.statusSliceTotal > this.columns ? this.statusSliceTotal : this.columns;
436
+ return Math.max(1, span - 1);
437
+ }
438
+
439
+ recomputeLayout() {
440
+ const wantsSide = this.statusVisible && (this.statusPosition === "left" || this.statusPosition === "right");
441
+ const useSide = wantsSide && this.columns >= this.sidebarWidth + 11;
442
+ this.effectivePosition = !this.statusVisible
443
+ ? "none"
444
+ : (useSide ? this.statusPosition : (this.statusPosition === "top" ? "top" : "bottom"));
445
+ if (this.effectivePosition === "left") {
446
+ this.childColumns = Math.max(10, this.columns - this.sidebarWidth - 1);
447
+ this.childRows = this.rows;
448
+ this.childRowOffset = 0;
449
+ this.childColOffset = this.sidebarWidth + 1;
450
+ } else if (this.effectivePosition === "right") {
451
+ this.childColumns = Math.max(10, this.columns - this.sidebarWidth - 1);
452
+ this.childRows = this.rows;
453
+ this.childRowOffset = 0;
454
+ this.childColOffset = 0;
455
+ } else if (this.effectivePosition === "top") {
456
+ this.childColumns = this.columns;
457
+ this.childRows = Math.max(1, this.rows - 1);
458
+ this.childRowOffset = 1;
459
+ this.childColOffset = 0;
460
+ } else if (this.effectivePosition === "bottom") {
461
+ this.childColumns = this.columns;
462
+ this.childRows = Math.max(1, this.rows - 1);
463
+ this.childRowOffset = 0;
464
+ this.childColOffset = 0;
465
+ } else {
466
+ this.childColumns = this.columns;
467
+ this.childRows = this.rows;
468
+ this.childRowOffset = 0;
469
+ this.childColOffset = 0;
470
+ }
471
+ }
472
+
473
+ setStatusSlice(slice) {
474
+ if (this.disposed) return Promise.resolve();
475
+ const nextTotal = slice && Number(slice.total) > 0 ? Math.max(0, Math.trunc(Number(slice.total) || 0)) : 0;
476
+ const nextOffset = nextTotal > 0 ? Math.max(0, Math.trunc(Number(slice.offset) || 0)) : 0;
477
+ if (nextOffset === this.statusSliceOffset && nextTotal === this.statusSliceTotal) return Promise.resolve();
478
+ this.statusSliceOffset = nextOffset;
479
+ this.statusSliceTotal = nextTotal;
480
+ this.lastStatusRow = null;
481
+ const isSide = this.effectivePosition === "left" || this.effectivePosition === "right";
482
+ if (isSide) {
483
+ this.scheduleRender();
484
+ return Promise.resolve();
485
+ }
486
+ this.statusTerminal.resize(this.horizontalStatusWidth(), 1);
487
+ const status = this.statusText;
488
+ const generation = ++this.statusWriteGeneration;
489
+ this.statusTerminal.reset();
490
+ this.pendingWrites += 1;
491
+ return new Promise((resolve) => {
492
+ this.statusTerminal.write(`${CSI}0m${CSI}2K\r${status}`, () => {
493
+ if (generation === this.statusWriteGeneration) {
494
+ this.lastStatusRow = null;
495
+ this.scheduleRender();
496
+ }
497
+ resolve();
498
+ this.completePendingWrite();
499
+ });
500
+ });
501
+ }
502
+
503
+ setStatusPosition(position) {
504
+ const next = normalizeBarPosition(position, "bottom");
505
+ if (this.statusPosition === next) return Promise.resolve();
506
+ this.statusPosition = next;
507
+ return this.resize(this.columns, this.rows);
508
+ }
509
+
368
510
  resize(columns, rows) {
369
511
  if (this.disposed) return Promise.resolve();
370
512
  this.columns = clampInteger(columns, 2, 1000, this.columns);
371
513
  this.rows = clampInteger(rows, 2, 1000, this.rows);
372
- this.childRows = Math.max(1, this.rows - 1);
373
- this.terminal.resize(this.columns, this.childRows);
374
- this.statusTerminal.resize(Math.max(1, this.columns - 1), 1);
514
+ this.recomputeLayout();
515
+ this.terminal.resize(this.childColumns, this.childRows);
516
+ this.statusTerminal.resize(this.horizontalStatusWidth(), 1);
517
+ this.forceFullRender = true;
518
+ this.lastRows = [];
519
+ this.lastStatusRow = null;
520
+ if (!this.statusVisible) {
521
+ this.scheduleRender(true, 0);
522
+ return Promise.resolve();
523
+ }
375
524
  const status = this.statusText;
376
525
  this.statusWriteGeneration += 1;
377
526
  this.statusTerminal.reset();
@@ -415,9 +564,10 @@ class HeadlessTerminalCompositor {
415
564
  const active = this.terminal.buffer.active;
416
565
  const nextRows = [];
417
566
  const changed = [];
567
+ const isSide = this.effectivePosition === "left" || this.effectivePosition === "right";
418
568
  for (let row = 0; row < this.childRows; row += 1) {
419
569
  const line = active.getLine(active.viewportY + row);
420
- const rendered = renderBufferLine(line, this.columns);
570
+ const rendered = renderBufferLine(line, this.childColumns);
421
571
  nextRows.push(rendered);
422
572
  if (this.forceFullRender || this.lastRows[row] !== rendered) {
423
573
  changed.push({ row: row + 1, rendered });
@@ -431,7 +581,7 @@ class HeadlessTerminalCompositor {
431
581
  let scrollOffset = 0;
432
582
  let bestSavings = 0;
433
583
  const scrollRows = Math.max(1, this.childRows - 2);
434
- if (!this.forceFullRender && changed.length > 3 && this.lastRows.length === nextRows.length) {
584
+ if (!isSide && !this.forceFullRender && changed.length > 3 && this.lastRows.length === nextRows.length) {
435
585
  const limit = Math.min(10, scrollRows - 1);
436
586
  for (let offset = -limit; offset <= limit; offset += 1) {
437
587
  if (!offset) continue;
@@ -460,26 +610,60 @@ class HeadlessTerminalCompositor {
460
610
 
461
611
  const statusLine = this.statusTerminal.buffer.active.getLine(0);
462
612
  // Keep the battery's original SGR and glyphs, as the POSIX row runner
463
- // does. Only use the parsed row when text would exceed the safe width.
464
- const renderedStatus = visibleWidth(this.statusText) <= this.columns - 1
465
- ? `${CSI}0m${this.statusText}${CSI}0m`
466
- : renderBufferLine(statusLine, Math.max(1, this.columns - 1));
467
- const statusChanged = this.forceFullRender || this.lastStatusRow !== renderedStatus;
613
+ // does. Only use the parsed row when text would exceed the safe width or
614
+ // when rendering a horizontal multi-pane slice.
615
+ const isRight = this.effectivePosition === "right";
616
+ let renderedStatus;
617
+ if (isSide) {
618
+ const totalVerticalRows = this.statusSliceTotal > this.rows ? this.statusSliceTotal : this.rows;
619
+ const verticalOffset = this.statusSliceTotal > this.rows ? Math.max(0, this.statusSliceOffset) : 0;
620
+ const allSideLines = formatVerticalStatusLines(this.statusText, this.sidebarWidth, totalVerticalRows, {
621
+ fullWidth: true,
622
+ alignBias: isRight ? "right" : "left"
623
+ });
624
+ renderedStatus = allSideLines.slice(verticalOffset, verticalOffset + this.rows).join("\n");
625
+ } else if (this.statusSliceTotal > this.columns) {
626
+ renderedStatus = renderBufferLineSlice(statusLine, Math.max(0, this.statusSliceOffset), Math.max(1, this.columns));
627
+ } else {
628
+ renderedStatus = visibleWidth(this.statusText) <= this.columns - 1
629
+ ? `${CSI}0m${this.statusText}${CSI}0m`
630
+ : renderBufferLine(statusLine, Math.max(1, this.columns - 1));
631
+ }
632
+ const statusChanged = this.statusVisible && (this.forceFullRender || this.lastStatusRow !== renderedStatus);
468
633
  const nextInputModes = terminalInputModeSnapshot(this.terminal, this.win32InputMode, this.mousePrivateModes);
469
634
  const modes = inputModeSequence(this.lastInputModes, nextInputModes);
470
635
 
471
- const cursorRow = Math.max(1, Math.min(this.childRows, active.cursorY + 1));
472
- const cursorColumn = Math.max(1, Math.min(this.columns, active.cursorX + 1));
636
+ const cursorRow = Math.max(1, Math.min(this.rows, active.cursorY + 1 + this.childRowOffset));
637
+ const cursorColumn = Math.max(1, Math.min(this.columns, active.cursorX + 1 + this.childColOffset));
473
638
  const cursorKey = `${cursorRow}:${cursorColumn}:${this.cursorHidden}`;
474
639
  if (!this.forceFullRender && !changed.length && !statusChanged && !modes && cursorKey === this.lastCursorKey) return;
475
640
 
476
641
  let frame = `${CSI}?2026h${CSI}0m${CSI}?25l${CSI}?7l${modes}`;
477
642
  if (this.forceFullRender) frame += `${CSI}2J`;
478
643
  if (scrollOffset) {
479
- frame += `${CSI}1;${scrollRows}r${CSI}${Math.abs(scrollOffset)}${scrollOffset > 0 ? "S" : "T"}${CSI}r`;
644
+ const srTop = 1 + this.childRowOffset;
645
+ const srBot = scrollRows + this.childRowOffset;
646
+ frame += `${CSI}${srTop};${srBot}r${CSI}${Math.abs(scrollOffset)}${scrollOffset > 0 ? "S" : "T"}${CSI}r`;
647
+ }
648
+ const childCol = 1 + this.childColOffset;
649
+ for (const entry of changed) frame += `${CSI}${entry.row + this.childRowOffset};${childCol}H${entry.rendered}`;
650
+ if (statusChanged) {
651
+ if (isSide) {
652
+ const sideLines = renderedStatus ? renderedStatus.split("\n") : [];
653
+ const dividerCol = isRight ? (this.childColumns + 1) : (this.sidebarWidth + 1);
654
+ const sideStartCol = isRight ? (this.childColumns + 2) : 1;
655
+ const blankSide = " ".repeat(this.sidebarWidth);
656
+ for (let r = 0; r < this.rows; r += 1) {
657
+ const text = sideLines[r] || "";
658
+ const pad = Math.max(0, this.sidebarWidth - visibleWidth(text));
659
+ frame += `${CSI}${r + 1};${dividerCol}H${CSI}90m│${CSI}0m`;
660
+ frame += `${CSI}${r + 1};${sideStartCol}H${CSI}0m${text ? `${text}${CSI}0m${" ".repeat(pad)}` : blankSide}`;
661
+ }
662
+ } else {
663
+ const statusRow = this.effectivePosition === "top" ? 1 : this.rows;
664
+ frame += `${CSI}${statusRow};1H${renderedStatus}${CSI}K`;
665
+ }
480
666
  }
481
- for (const entry of changed) frame += `${CSI}${entry.row};1H${entry.rendered}`;
482
- if (statusChanged) frame += `${CSI}${this.rows};1H${renderedStatus}${CSI}K`;
483
667
 
484
668
  frame += `${CSI}0m${CSI}${cursorRow};${cursorColumn}H`;
485
669
  frame += this.cursorHidden ? `${CSI}?25l` : `${CSI}?25h`;
@@ -523,6 +707,7 @@ class HeadlessTerminalCompositor {
523
707
  columns: this.columns,
524
708
  rows: this.rows,
525
709
  childRows: this.childRows,
710
+ statusVisible: this.statusVisible,
526
711
  bufferType: active.type,
527
712
  cursor: { x: active.cursorX, y: active.cursorY },
528
713
  lines,