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 +17 -9
- package/bin/ai-battery-hud.js +41 -30
- package/bin/ai-battery-hud.ps1 +268 -25
- package/bin/ai-battery-run +18 -0
- package/bin/ai-battery-run-win.js +395 -37
- package/bin/ai-battery.js +674 -21
- package/package.json +3 -1
- package/vendor/rowpty/RowPty.cs +1918 -0
package/bin/ai-battery-run
CHANGED
|
@@ -251,6 +251,18 @@ class StatusLine:
|
|
|
251
251
|
os.write(sys.stdout.fileno(), payload.encode("ascii"))
|
|
252
252
|
|
|
253
253
|
|
|
254
|
+
def tmux_status_bar_active():
|
|
255
|
+
if not os.environ.get("TMUX"):
|
|
256
|
+
return False
|
|
257
|
+
override = (os.environ.get("AI_BATTERY_TMUX") or os.environ.get("CLAUDEX_BATTERY_TMUX") or "").lower()
|
|
258
|
+
if override == "row":
|
|
259
|
+
return False
|
|
260
|
+
if override in ("status", "plain", "off"):
|
|
261
|
+
return True
|
|
262
|
+
flag = os.environ.get("AI_BATTERY_TMUX_STATUS") or os.environ.get("CLAUDEX_BATTERY_TMUX_STATUS")
|
|
263
|
+
return flag == "1"
|
|
264
|
+
|
|
265
|
+
|
|
254
266
|
def main():
|
|
255
267
|
interval, bar_width, provider, layout, left_padding, command = parse_args(sys.argv[1:])
|
|
256
268
|
command_provider = infer_provider(command)
|
|
@@ -258,6 +270,12 @@ def main():
|
|
|
258
270
|
provider = command_provider
|
|
259
271
|
layout = resolve_layout(layout, command_provider)
|
|
260
272
|
|
|
273
|
+
# Inside tmux with the status-bar integration, the battery already shows
|
|
274
|
+
# once per session in tmux's own bar; skip the per-pane reserved row and
|
|
275
|
+
# hand the pane to the child untouched.
|
|
276
|
+
if tmux_status_bar_active():
|
|
277
|
+
os.execvp(command[0], command)
|
|
278
|
+
|
|
261
279
|
if not os.isatty(sys.stdin.fileno()) or not os.isatty(sys.stdout.fileno()):
|
|
262
280
|
print(
|
|
263
281
|
"ai-battery-run: stdin/stdout is not a real terminal.\n"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import { spawn
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
4
|
import fs from "node:fs";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
@@ -10,14 +10,32 @@ const BATTERY_BIN = process.env.AI_BATTERY_BIN
|
|
|
10
10
|
|| process.env.CLAUDEX_BATTERY_BIN
|
|
11
11
|
|| path.join(SCRIPT_DIR, "ai-battery.js");
|
|
12
12
|
const ANSI_RE = /\u001b\[[0-?]*[ -/]*[@-~]/g;
|
|
13
|
+
const DEBUG_LOG = process.env.AI_BATTERY_DEBUG_LOG || process.env.CLAUDEX_BATTERY_DEBUG_LOG || "";
|
|
13
14
|
const DEFAULT_COLUMN_GUARD = 4;
|
|
14
15
|
const DEFAULT_LEFT_PADDING = 2;
|
|
16
|
+
const DEFAULT_CODEX_OVERLAY_BOTTOM_OFFSET = 1;
|
|
15
17
|
|
|
16
18
|
function usage() {
|
|
17
|
-
console.log(`Usage: ai-battery-run-win [--interval SECONDS] [--bar-width N] [--provider auto|all|codex|claude] [--left-padding N] -- COMMAND [ARGS...]
|
|
19
|
+
console.log(`Usage: ai-battery-run-win [--interval SECONDS] [--bar-width N] [--provider auto|all|codex|claude] [--layout auto|reserve|overlay] [--left-padding N] -- COMMAND [ARGS...]
|
|
18
20
|
|
|
19
21
|
Runs COMMAND and keeps AI Battery on the terminal bottom row.
|
|
20
|
-
On Windows,
|
|
22
|
+
On Windows, the rowpty host (a native ConPTY host with a reserved bottom row) is preferred when its executable
|
|
23
|
+
is available; otherwise AI Battery falls back to a same-console overlay row.
|
|
24
|
+
--layout reserve additionally allows the legacy node-pty/ConPTY path when rowpty is missing.`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function debugLog(event, details = {}) {
|
|
28
|
+
if (!DEBUG_LOG) return;
|
|
29
|
+
try {
|
|
30
|
+
fs.mkdirSync(path.dirname(DEBUG_LOG), { recursive: true });
|
|
31
|
+
fs.appendFileSync(DEBUG_LOG, `${JSON.stringify({
|
|
32
|
+
at: new Date().toISOString(),
|
|
33
|
+
event,
|
|
34
|
+
...details
|
|
35
|
+
})}\n`);
|
|
36
|
+
} catch {
|
|
37
|
+
// Debug logging must never interfere with launching Codex.
|
|
38
|
+
}
|
|
21
39
|
}
|
|
22
40
|
|
|
23
41
|
function parseArgs(argv) {
|
|
@@ -25,6 +43,7 @@ function parseArgs(argv) {
|
|
|
25
43
|
interval: Number(process.env.AI_BATTERY_INTERVAL || process.env.CLAUDEX_BATTERY_INTERVAL || 10),
|
|
26
44
|
barWidth: process.env.AI_BATTERY_BAR_WIDTH || process.env.CLAUDEX_BATTERY_BAR_WIDTH || "10",
|
|
27
45
|
provider: process.env.AI_BATTERY_PROVIDER || process.env.CLAUDEX_BATTERY_PROVIDER || "auto",
|
|
46
|
+
layout: process.env.AI_BATTERY_WIN_LAYOUT || process.env.CLAUDEX_BATTERY_WIN_LAYOUT || process.env.AI_BATTERY_LAYOUT || "auto",
|
|
28
47
|
leftPadding: Number(process.env.AI_BATTERY_LEFT_PADDING || process.env.CLAUDEX_BATTERY_LEFT_PADDING || DEFAULT_LEFT_PADDING),
|
|
29
48
|
command: []
|
|
30
49
|
};
|
|
@@ -43,8 +62,16 @@ function parseArgs(argv) {
|
|
|
43
62
|
if (!["auto", "all", "codex", "claude"].includes(args.provider)) {
|
|
44
63
|
throw new Error("--provider must be one of: auto, all, codex, claude");
|
|
45
64
|
}
|
|
65
|
+
} else if (arg === "--layout") {
|
|
66
|
+
args.layout = argv[++i] || args.layout;
|
|
67
|
+
if (!["auto", "reserve", "overlay"].includes(args.layout)) {
|
|
68
|
+
throw new Error("--layout must be one of: auto, reserve, overlay");
|
|
69
|
+
}
|
|
46
70
|
} else if (arg === "--left-padding") {
|
|
47
|
-
|
|
71
|
+
const value = Number(argv[++i]);
|
|
72
|
+
args.leftPadding = Number.isFinite(value)
|
|
73
|
+
? Math.max(0, Math.min(20, value))
|
|
74
|
+
: args.leftPadding;
|
|
48
75
|
} else if (arg === "--") {
|
|
49
76
|
args.command = argv.slice(i + 1);
|
|
50
77
|
break;
|
|
@@ -59,6 +86,7 @@ function parseArgs(argv) {
|
|
|
59
86
|
process.exit(2);
|
|
60
87
|
}
|
|
61
88
|
if (!Number.isFinite(args.interval)) args.interval = 10;
|
|
89
|
+
if (!["auto", "reserve", "overlay"].includes(args.layout)) args.layout = "auto";
|
|
62
90
|
return args;
|
|
63
91
|
}
|
|
64
92
|
|
|
@@ -71,9 +99,12 @@ function inferProvider(command) {
|
|
|
71
99
|
}
|
|
72
100
|
|
|
73
101
|
function termSize() {
|
|
102
|
+
const windowSize = process.stdout.getWindowSize?.();
|
|
103
|
+
const cols = process.stdout.columns || windowSize?.[0] || 80;
|
|
104
|
+
const rows = process.stdout.rows || windowSize?.[1] || 24;
|
|
74
105
|
return {
|
|
75
|
-
cols: Math.max(20,
|
|
76
|
-
rows: Math.max(1,
|
|
106
|
+
cols: Math.max(20, cols),
|
|
107
|
+
rows: Math.max(1, rows)
|
|
77
108
|
};
|
|
78
109
|
}
|
|
79
110
|
|
|
@@ -83,6 +114,18 @@ function columnGuard() {
|
|
|
83
114
|
return Number.isFinite(value) ? Math.max(0, Math.min(20, Math.floor(value))) : DEFAULT_COLUMN_GUARD;
|
|
84
115
|
}
|
|
85
116
|
|
|
117
|
+
function overlayBottomOffset(activeProvider) {
|
|
118
|
+
const raw = Number(process.env.AI_BATTERY_OVERLAY_BOTTOM_OFFSET || process.env.CLAUDEX_BATTERY_OVERLAY_BOTTOM_OFFSET);
|
|
119
|
+
if (Number.isFinite(raw)) return Math.max(0, Math.min(5, Math.floor(raw)));
|
|
120
|
+
return activeProvider === "codex" ? DEFAULT_CODEX_OVERLAY_BOTTOM_OFFSET : 0;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function statusRow(rows, bottomOffset = 0) {
|
|
124
|
+
const safeRows = Math.max(1, Number(rows) || 1);
|
|
125
|
+
const safeOffset = Math.max(0, Math.min(5, Math.floor(Number(bottomOffset) || 0)));
|
|
126
|
+
return Math.max(1, safeRows - safeOffset);
|
|
127
|
+
}
|
|
128
|
+
|
|
86
129
|
function stripAnsi(text) {
|
|
87
130
|
return String(text).replace(ANSI_RE, "");
|
|
88
131
|
}
|
|
@@ -115,14 +158,133 @@ function batteryCommand(args, activeProvider, maxWidth) {
|
|
|
115
158
|
return command;
|
|
116
159
|
}
|
|
117
160
|
|
|
161
|
+
function statusOutputText(stdout) {
|
|
162
|
+
return String(stdout || "").replace(/[\r\n]+$/g, "");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function rowptyExePath() {
|
|
166
|
+
const explicit = process.env.AI_BATTERY_ROWPTY || process.env.CLAUDEX_BATTERY_ROWPTY;
|
|
167
|
+
if (explicit) return fs.existsSync(explicit) ? explicit : null;
|
|
168
|
+
const candidates = [];
|
|
169
|
+
if (process.env.LOCALAPPDATA) {
|
|
170
|
+
candidates.push(path.join(process.env.LOCALAPPDATA, "ai-battery", "bin", "rowpty.exe"));
|
|
171
|
+
}
|
|
172
|
+
candidates.push(path.join(SCRIPT_DIR, "..", "..", "rowpty", "bin", "rowpty.exe"));
|
|
173
|
+
for (const candidate of candidates) {
|
|
174
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function quoteWindowsCommandLineArg(value) {
|
|
180
|
+
const text = String(value ?? "");
|
|
181
|
+
if (text.length && !/[ \t"]/.test(text)) return text;
|
|
182
|
+
let quoted = "\"";
|
|
183
|
+
let backslashes = 0;
|
|
184
|
+
for (const ch of text) {
|
|
185
|
+
if (ch === "\\") {
|
|
186
|
+
backslashes += 1;
|
|
187
|
+
} else if (ch === "\"") {
|
|
188
|
+
quoted += "\\".repeat(backslashes * 2 + 1) + "\"";
|
|
189
|
+
backslashes = 0;
|
|
190
|
+
} else {
|
|
191
|
+
quoted += "\\".repeat(backslashes) + ch;
|
|
192
|
+
backslashes = 0;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return quoted + "\\".repeat(backslashes * 2) + "\"";
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function rowptyStatusCommand(args, activeProvider) {
|
|
199
|
+
// rowpty substitutes {MAXWIDTH} with (cols - 4) before each status run.
|
|
200
|
+
return batteryCommand(args, activeProvider, "{MAXWIDTH}")
|
|
201
|
+
.map(quoteWindowsCommandLineArg)
|
|
202
|
+
.join(" ");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function runRowPty(args, activeProvider) {
|
|
206
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) return Promise.resolve(null);
|
|
207
|
+
const exe = rowptyExePath();
|
|
208
|
+
if (!exe) {
|
|
209
|
+
debugLog("rowpty:missing");
|
|
210
|
+
return Promise.resolve(null);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const command = windowsCommand(args.command);
|
|
214
|
+
const hostArgs = [
|
|
215
|
+
"--interval", String(Math.max(0.5, args.interval)),
|
|
216
|
+
"--reserve", "1",
|
|
217
|
+
"--status-cmd", rowptyStatusCommand(args, activeProvider),
|
|
218
|
+
"--", command.file, ...command.args
|
|
219
|
+
];
|
|
220
|
+
debugLog("rowpty:start", { exe, hostArgs, size: termSize() });
|
|
221
|
+
const child = spawn(exe, hostArgs, {
|
|
222
|
+
stdio: "inherit",
|
|
223
|
+
cwd: process.cwd(),
|
|
224
|
+
env: process.env,
|
|
225
|
+
windowsHide: false
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
return new Promise((resolve) => {
|
|
229
|
+
child.on("exit", (code, signal) => {
|
|
230
|
+
debugLog("rowpty:exit", { code, signal });
|
|
231
|
+
resolve(code ?? (signal ? 1 : 0));
|
|
232
|
+
});
|
|
233
|
+
child.on("error", (error) => {
|
|
234
|
+
debugLog("rowpty:error", { error: error.message });
|
|
235
|
+
resolve(null);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function conPtyBackspaceMode() {
|
|
241
|
+
// Windows Terminal sends 0x7f for Backspace and 0x08 for Ctrl+Backspace.
|
|
242
|
+
// Rewriting DEL to BS makes TUIs (Codex, Claude Code) treat every
|
|
243
|
+
// Backspace as delete-word, so keys pass through untouched by default.
|
|
244
|
+
const mode = String(process.env.AI_BATTERY_CONPTY_BACKSPACE || process.env.CLAUDEX_BATTERY_CONPTY_BACKSPACE || "passthrough").toLowerCase();
|
|
245
|
+
return ["bs", "del", "passthrough"].includes(mode) ? mode : "passthrough";
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function normalizeConPtyInput(data, mode = conPtyBackspaceMode()) {
|
|
249
|
+
const text = Buffer.isBuffer(data) ? data.toString("utf8") : String(data || "");
|
|
250
|
+
if (mode === "passthrough" || mode === "del") return text;
|
|
251
|
+
return text.replace(/\x7f/g, "\x08");
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function conPtyRepaintIntervalMs() {
|
|
255
|
+
const raw = Number(process.env.AI_BATTERY_CONPTY_REPAINT_MS || process.env.CLAUDEX_BATTERY_CONPTY_REPAINT_MS);
|
|
256
|
+
if (Number.isFinite(raw)) return Math.max(250, Math.min(5000, Math.floor(raw)));
|
|
257
|
+
return 1000;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function outputMayClearDisplay(data) {
|
|
261
|
+
const text = Buffer.isBuffer(data) ? data.toString("utf8") : String(data || "");
|
|
262
|
+
return /\x1bc|\x1b\[[0-?]*[23]J|\x1b\[\?1049[hl]/.test(text);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function scheduleRepaintBurst(status, timers) {
|
|
266
|
+
for (const delayMs of [0, 80, 250, 700, 1400]) {
|
|
267
|
+
const timer = setTimeout(() => {
|
|
268
|
+
timers.delete(timer);
|
|
269
|
+
status.paint(true);
|
|
270
|
+
}, delayMs);
|
|
271
|
+
timers.add(timer);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
118
275
|
class StatusLine {
|
|
119
|
-
constructor(args, activeProvider) {
|
|
276
|
+
constructor(args, activeProvider, options = {}) {
|
|
120
277
|
this.args = args;
|
|
121
278
|
this.activeProvider = activeProvider;
|
|
279
|
+
this.bottomOffset = options.bottomOffset ?? 0;
|
|
122
280
|
this.text = "AI Battery starting...";
|
|
123
281
|
this.nextFetch = 0;
|
|
124
282
|
this.lastLine = "";
|
|
125
283
|
this.lastDraw = 0;
|
|
284
|
+
this.refreshInFlight = false;
|
|
285
|
+
this.refreshQueued = false;
|
|
286
|
+
this.disposed = false;
|
|
287
|
+
this.onRefresh = null;
|
|
126
288
|
this.resize();
|
|
127
289
|
}
|
|
128
290
|
|
|
@@ -132,41 +294,82 @@ class StatusLine {
|
|
|
132
294
|
this.rows = size.rows;
|
|
133
295
|
}
|
|
134
296
|
|
|
135
|
-
|
|
297
|
+
kickRefresh(force = false) {
|
|
298
|
+
if (this.disposed) return;
|
|
136
299
|
const now = Date.now();
|
|
137
300
|
if (!force && now < this.nextFetch) return;
|
|
301
|
+
if (this.refreshInFlight) {
|
|
302
|
+
this.refreshQueued = this.refreshQueued || force;
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
this.refreshInFlight = true;
|
|
138
307
|
this.nextFetch = now + (this.args.interval * 1000);
|
|
139
308
|
const maxWidth = Math.max(20, this.cols - columnGuard());
|
|
140
309
|
const command = batteryCommand(this.args, this.activeProvider, maxWidth);
|
|
141
|
-
const
|
|
142
|
-
encoding: "utf8",
|
|
310
|
+
const child = spawn(command[0], command.slice(1), {
|
|
143
311
|
stdio: ["ignore", "pipe", "ignore"],
|
|
144
|
-
timeout: 1500,
|
|
145
312
|
windowsHide: true
|
|
146
313
|
});
|
|
147
|
-
|
|
148
|
-
|
|
314
|
+
|
|
315
|
+
let stdout = "";
|
|
316
|
+
const timeout = setTimeout(() => child.kill(), 1500);
|
|
317
|
+
child.stdout?.setEncoding("utf8");
|
|
318
|
+
child.stdout?.on("data", (chunk) => {
|
|
319
|
+
stdout += chunk;
|
|
320
|
+
if (stdout.length > 64 * 1024) child.kill();
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
let settled = false;
|
|
324
|
+
const finish = (status) => {
|
|
325
|
+
if (settled) return;
|
|
326
|
+
settled = true;
|
|
327
|
+
clearTimeout(timeout);
|
|
328
|
+
this.refreshInFlight = false;
|
|
329
|
+
if (this.disposed) return;
|
|
330
|
+
const text = statusOutputText(stdout);
|
|
331
|
+
const changed = status === 0 && text && text !== this.text;
|
|
332
|
+
if (changed) {
|
|
333
|
+
this.text = text;
|
|
334
|
+
if (typeof this.onRefresh === "function") {
|
|
335
|
+
this.onRefresh();
|
|
336
|
+
} else {
|
|
337
|
+
this.paint(true);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (this.refreshQueued) {
|
|
341
|
+
this.refreshQueued = false;
|
|
342
|
+
this.kickRefresh(true);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
child.on("close", finish);
|
|
347
|
+
child.on("error", () => finish(1));
|
|
149
348
|
}
|
|
150
349
|
|
|
151
|
-
|
|
350
|
+
paint(force = false) {
|
|
351
|
+
if (this.disposed) return;
|
|
152
352
|
const now = Date.now();
|
|
153
353
|
if (!force && now - this.lastDraw < 150) return;
|
|
154
|
-
this.refresh(force);
|
|
155
354
|
const width = Math.max(1, this.cols - 1);
|
|
355
|
+
const row = statusRow(this.rows, this.bottomOffset);
|
|
156
356
|
const line = fitAnsi(this.text, width, true);
|
|
157
357
|
if (!force && line === this.lastLine) return;
|
|
158
358
|
this.lastLine = line;
|
|
159
359
|
this.lastDraw = now;
|
|
160
|
-
process.stdout.write(`\x1b7\x1b[0m\x1b[${
|
|
360
|
+
process.stdout.write(`\x1b7\x1b[0m\x1b[${row};1H\r\x1b[1G${line}\x1b[K\x1b[0m\x1b8`);
|
|
161
361
|
}
|
|
162
362
|
|
|
163
|
-
|
|
164
|
-
|
|
363
|
+
draw(force = false) {
|
|
364
|
+
this.kickRefresh(force);
|
|
365
|
+
this.paint(force);
|
|
165
366
|
}
|
|
166
|
-
}
|
|
167
367
|
|
|
168
|
-
|
|
169
|
-
|
|
368
|
+
clear() {
|
|
369
|
+
this.disposed = true;
|
|
370
|
+
const row = statusRow(this.rows, this.bottomOffset);
|
|
371
|
+
process.stdout.write(`\x1b7\x1b[0m\x1b[${row};1H\r\x1b[1G\x1b[2K\x1b8`);
|
|
372
|
+
}
|
|
170
373
|
}
|
|
171
374
|
|
|
172
375
|
function normalizeWindowsCommandPath(value) {
|
|
@@ -201,7 +404,7 @@ function windowsCommand(command) {
|
|
|
201
404
|
if (/\.(cmd|bat)$/i.test(exe)) {
|
|
202
405
|
return {
|
|
203
406
|
file: "cmd.exe",
|
|
204
|
-
args: ["/d", "/s", "/c",
|
|
407
|
+
args: ["/d", "/s", "/c", "call", exe, ...rest]
|
|
205
408
|
};
|
|
206
409
|
}
|
|
207
410
|
if (/\.ps1$/i.test(exe)) {
|
|
@@ -222,20 +425,47 @@ function windowsCommand(command) {
|
|
|
222
425
|
async function loadNodePty() {
|
|
223
426
|
try {
|
|
224
427
|
const mod = await import("node-pty");
|
|
428
|
+
debugLog("node-pty:loaded");
|
|
225
429
|
return mod.default ?? mod;
|
|
226
|
-
} catch {
|
|
430
|
+
} catch (error) {
|
|
431
|
+
debugLog("node-pty:missing", { error: error.message });
|
|
227
432
|
return null;
|
|
228
433
|
}
|
|
229
434
|
}
|
|
230
435
|
|
|
231
436
|
async function runConPty(args, activeProvider) {
|
|
437
|
+
// Nested ConPTY (node-pty) breaks host-terminal scrollback and can stall
|
|
438
|
+
// child output until the next keypress, so it is opt-in via --layout reserve.
|
|
439
|
+
if (args.layout !== "reserve") {
|
|
440
|
+
debugLog("conpty:skipped", { reason: `layout ${args.layout}` });
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
|
|
232
444
|
const pty = await loadNodePty();
|
|
233
|
-
if (!pty || !process.stdin.isTTY || !process.stdout.isTTY)
|
|
445
|
+
if (!pty || !process.stdin.isTTY || !process.stdout.isTTY) {
|
|
446
|
+
debugLog("conpty:unavailable", {
|
|
447
|
+
hasPty: Boolean(pty),
|
|
448
|
+
stdinTty: Boolean(process.stdin.isTTY),
|
|
449
|
+
stdoutTty: Boolean(process.stdout.isTTY)
|
|
450
|
+
});
|
|
451
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) return null;
|
|
452
|
+
console.error("ai-battery-run-win: ConPTY reserve layout is unavailable in this terminal.");
|
|
453
|
+
return 2;
|
|
454
|
+
}
|
|
234
455
|
|
|
235
456
|
const status = new StatusLine(args, activeProvider);
|
|
236
457
|
const size = termSize();
|
|
237
458
|
const childRows = Math.max(1, size.rows - 1);
|
|
238
459
|
const command = windowsCommand(args.command);
|
|
460
|
+
debugLog("conpty:start", {
|
|
461
|
+
size,
|
|
462
|
+
childRows,
|
|
463
|
+
command,
|
|
464
|
+
stdoutColumns: process.stdout.columns,
|
|
465
|
+
stdoutRows: process.stdout.rows,
|
|
466
|
+
windowSize: process.stdout.getWindowSize?.() || null,
|
|
467
|
+
wtSession: Boolean(process.env.WT_SESSION)
|
|
468
|
+
});
|
|
239
469
|
const term = pty.spawn(command.file, command.args, {
|
|
240
470
|
name: "xterm-256color",
|
|
241
471
|
cols: size.cols,
|
|
@@ -247,42 +477,102 @@ async function runConPty(args, activeProvider) {
|
|
|
247
477
|
const oldRaw = process.stdin.isRaw;
|
|
248
478
|
process.stdin.setRawMode?.(true);
|
|
249
479
|
process.stdin.resume();
|
|
250
|
-
const onInput = (data) => term.write(data
|
|
480
|
+
const onInput = (data) => term.write(normalizeConPtyInput(data));
|
|
251
481
|
process.stdin.on("data", onInput);
|
|
252
482
|
|
|
253
|
-
|
|
483
|
+
let drawTimer = null;
|
|
484
|
+
let drawForce = false;
|
|
485
|
+
const scheduleDraw = (force = false, delayMs = 45) => {
|
|
486
|
+
drawForce = drawForce || force;
|
|
487
|
+
if (drawTimer) return;
|
|
488
|
+
drawTimer = setTimeout(() => {
|
|
489
|
+
drawTimer = null;
|
|
490
|
+
const forceNow = drawForce;
|
|
491
|
+
drawForce = false;
|
|
492
|
+
status.draw(forceNow);
|
|
493
|
+
}, delayMs);
|
|
494
|
+
};
|
|
495
|
+
status.onRefresh = () => scheduleDraw(true, 45);
|
|
496
|
+
|
|
497
|
+
const timers = new Set();
|
|
498
|
+
const timer = setInterval(() => scheduleDraw(false), Math.max(500, args.interval * 1000));
|
|
499
|
+
const repaintTimer = setInterval(() => status.paint(true), conPtyRepaintIntervalMs());
|
|
254
500
|
const onResize = () => {
|
|
255
501
|
status.resize();
|
|
256
502
|
term.resize(status.cols, Math.max(1, status.rows - 1));
|
|
257
|
-
|
|
503
|
+
scheduleDraw(true, 0);
|
|
504
|
+
scheduleRepaintBurst(status, timers);
|
|
258
505
|
};
|
|
259
506
|
process.stdout.on("resize", onResize);
|
|
260
507
|
|
|
508
|
+
const clearConPtyTimers = () => {
|
|
509
|
+
clearInterval(timer);
|
|
510
|
+
clearInterval(repaintTimer);
|
|
511
|
+
if (drawTimer) clearTimeout(drawTimer);
|
|
512
|
+
for (const timer of timers) clearTimeout(timer);
|
|
513
|
+
timers.clear();
|
|
514
|
+
};
|
|
515
|
+
|
|
261
516
|
return await new Promise((resolve) => {
|
|
262
517
|
term.onData((data) => {
|
|
263
518
|
process.stdout.write(data);
|
|
264
|
-
|
|
519
|
+
if (outputMayClearDisplay(data)) scheduleRepaintBurst(status, timers);
|
|
520
|
+
scheduleDraw(false);
|
|
265
521
|
});
|
|
266
522
|
term.onExit(({ exitCode }) => {
|
|
267
|
-
|
|
523
|
+
debugLog("conpty:exit", { exitCode });
|
|
524
|
+
clearConPtyTimers();
|
|
268
525
|
process.stdout.off("resize", onResize);
|
|
269
526
|
process.stdin.off("data", onInput);
|
|
270
527
|
process.stdin.setRawMode?.(oldRaw);
|
|
271
528
|
status.clear();
|
|
272
529
|
resolve(exitCode ?? 0);
|
|
273
530
|
});
|
|
274
|
-
|
|
531
|
+
scheduleDraw(true, 0);
|
|
532
|
+
scheduleRepaintBurst(status, timers);
|
|
275
533
|
});
|
|
276
534
|
}
|
|
277
535
|
|
|
536
|
+
function overlayInitialDelayMs() {
|
|
537
|
+
const raw = Number(process.env.AI_BATTERY_OVERLAY_INITIAL_DELAY_MS || process.env.CLAUDEX_BATTERY_OVERLAY_INITIAL_DELAY_MS);
|
|
538
|
+
if (Number.isFinite(raw)) return Math.max(0, Math.min(5000, Math.floor(raw)));
|
|
539
|
+
return 1200;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function overlayRepaintIntervalMs() {
|
|
543
|
+
const raw = Number(process.env.AI_BATTERY_OVERLAY_REPAINT_MS || process.env.CLAUDEX_BATTERY_OVERLAY_REPAINT_MS);
|
|
544
|
+
if (Number.isFinite(raw)) return Math.max(250, Math.min(5000, Math.floor(raw)));
|
|
545
|
+
return 1000;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function scheduleOverlayRepaintBurst(status, timers) {
|
|
549
|
+
scheduleRepaintBurst(status, timers);
|
|
550
|
+
}
|
|
551
|
+
|
|
278
552
|
function runOverlay(args, activeProvider) {
|
|
279
553
|
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
554
|
+
debugLog("overlay:unavailable", {
|
|
555
|
+
stdinTty: Boolean(process.stdin.isTTY),
|
|
556
|
+
stdoutTty: Boolean(process.stdout.isTTY)
|
|
557
|
+
});
|
|
280
558
|
console.error("ai-battery-run-win: stdin/stdout is not a real terminal.");
|
|
281
559
|
return 2;
|
|
282
560
|
}
|
|
283
561
|
|
|
284
|
-
const status = new StatusLine(args, activeProvider
|
|
562
|
+
const status = new StatusLine(args, activeProvider, {
|
|
563
|
+
bottomOffset: overlayBottomOffset(activeProvider)
|
|
564
|
+
});
|
|
285
565
|
const command = windowsCommand(args.command);
|
|
566
|
+
debugLog("overlay:start", {
|
|
567
|
+
size: termSize(),
|
|
568
|
+
command,
|
|
569
|
+
bottomOffset: status.bottomOffset,
|
|
570
|
+
initialDelayMs: overlayInitialDelayMs(),
|
|
571
|
+
stdoutColumns: process.stdout.columns,
|
|
572
|
+
stdoutRows: process.stdout.rows,
|
|
573
|
+
windowSize: process.stdout.getWindowSize?.() || null,
|
|
574
|
+
wtSession: Boolean(process.env.WT_SESSION)
|
|
575
|
+
});
|
|
286
576
|
const child = spawn(command.file, command.args, {
|
|
287
577
|
stdio: "inherit",
|
|
288
578
|
cwd: process.cwd(),
|
|
@@ -290,23 +580,39 @@ function runOverlay(args, activeProvider) {
|
|
|
290
580
|
windowsHide: false
|
|
291
581
|
});
|
|
292
582
|
|
|
293
|
-
const
|
|
583
|
+
const timers = new Set();
|
|
584
|
+
const refreshTimer = setInterval(() => status.draw(false), Math.max(1000, args.interval * 1000));
|
|
585
|
+
const repaintTimer = setInterval(() => status.paint(true), overlayRepaintIntervalMs());
|
|
294
586
|
const onResize = () => {
|
|
295
587
|
status.resize();
|
|
296
588
|
status.draw(true);
|
|
589
|
+
scheduleOverlayRepaintBurst(status, timers);
|
|
297
590
|
};
|
|
298
591
|
process.stdout.on("resize", onResize);
|
|
299
|
-
|
|
592
|
+
const firstDraw = setTimeout(() => {
|
|
593
|
+
status.draw(true);
|
|
594
|
+
scheduleOverlayRepaintBurst(status, timers);
|
|
595
|
+
}, overlayInitialDelayMs());
|
|
596
|
+
timers.add(firstDraw);
|
|
597
|
+
|
|
598
|
+
const clearOverlayTimers = () => {
|
|
599
|
+
clearInterval(refreshTimer);
|
|
600
|
+
clearInterval(repaintTimer);
|
|
601
|
+
for (const timer of timers) clearTimeout(timer);
|
|
602
|
+
timers.clear();
|
|
603
|
+
};
|
|
300
604
|
|
|
301
605
|
return new Promise((resolve) => {
|
|
302
606
|
child.on("exit", (code, signal) => {
|
|
303
|
-
|
|
607
|
+
debugLog("overlay:exit", { code, signal });
|
|
608
|
+
clearOverlayTimers();
|
|
304
609
|
process.stdout.off("resize", onResize);
|
|
305
610
|
status.clear();
|
|
306
611
|
resolve(code ?? (signal ? 1 : 0));
|
|
307
612
|
});
|
|
308
613
|
child.on("error", (error) => {
|
|
309
|
-
|
|
614
|
+
debugLog("overlay:error", { error: error.message });
|
|
615
|
+
clearOverlayTimers();
|
|
310
616
|
process.stdout.off("resize", onResize);
|
|
311
617
|
status.clear();
|
|
312
618
|
console.error(`ai-battery-run-win: ${error.message}`);
|
|
@@ -315,6 +621,29 @@ function runOverlay(args, activeProvider) {
|
|
|
315
621
|
});
|
|
316
622
|
}
|
|
317
623
|
|
|
624
|
+
function runPlain(args) {
|
|
625
|
+
const command = windowsCommand(args.command);
|
|
626
|
+
debugLog("plain:start", { command });
|
|
627
|
+
const child = spawn(command.file, command.args, {
|
|
628
|
+
stdio: "inherit",
|
|
629
|
+
cwd: process.cwd(),
|
|
630
|
+
env: process.env,
|
|
631
|
+
windowsHide: false
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
return new Promise((resolve) => {
|
|
635
|
+
child.on("exit", (code, signal) => {
|
|
636
|
+
debugLog("plain:exit", { code, signal });
|
|
637
|
+
resolve(code ?? (signal ? 1 : 0));
|
|
638
|
+
});
|
|
639
|
+
child.on("error", (error) => {
|
|
640
|
+
debugLog("plain:error", { error: error.message });
|
|
641
|
+
console.error(`ai-battery-run-win: ${error.message}`);
|
|
642
|
+
resolve(1);
|
|
643
|
+
});
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
|
|
318
647
|
async function main() {
|
|
319
648
|
if (process.platform !== "win32") {
|
|
320
649
|
console.error("ai-battery-run-win is only for native Windows.");
|
|
@@ -325,14 +654,43 @@ async function main() {
|
|
|
325
654
|
const commandProvider = inferProvider(args.command);
|
|
326
655
|
if (args.provider === "auto") args.provider = commandProvider;
|
|
327
656
|
const activeProvider = ["codex", "claude"].includes(commandProvider) ? commandProvider : null;
|
|
657
|
+
debugLog("main:start", {
|
|
658
|
+
argv: process.argv.slice(2),
|
|
659
|
+
provider: args.provider,
|
|
660
|
+
layout: args.layout,
|
|
661
|
+
commandProvider,
|
|
662
|
+
activeProvider,
|
|
663
|
+
stdinTty: Boolean(process.stdin.isTTY),
|
|
664
|
+
stdoutTty: Boolean(process.stdout.isTTY)
|
|
665
|
+
});
|
|
328
666
|
|
|
329
|
-
|
|
330
|
-
|
|
667
|
+
let exitCode = null;
|
|
668
|
+
if (args.layout !== "overlay") {
|
|
669
|
+
exitCode = await runRowPty(args, activeProvider);
|
|
670
|
+
if (exitCode === null) exitCode = await runConPty(args, activeProvider);
|
|
671
|
+
}
|
|
672
|
+
if (exitCode === null) {
|
|
673
|
+
exitCode = process.stdin.isTTY && process.stdout.isTTY
|
|
674
|
+
? await runOverlay(args, activeProvider)
|
|
675
|
+
: await runPlain(args);
|
|
676
|
+
}
|
|
331
677
|
process.exit(exitCode);
|
|
332
678
|
}
|
|
333
679
|
|
|
334
680
|
export {
|
|
681
|
+
conPtyBackspaceMode,
|
|
682
|
+
conPtyRepaintIntervalMs,
|
|
683
|
+
normalizeConPtyInput,
|
|
684
|
+
outputMayClearDisplay,
|
|
685
|
+
overlayBottomOffset,
|
|
686
|
+
overlayRepaintIntervalMs,
|
|
687
|
+
parseArgs,
|
|
688
|
+
quoteWindowsCommandLineArg,
|
|
335
689
|
resolveWindowsCommandFile,
|
|
690
|
+
rowptyExePath,
|
|
691
|
+
rowptyStatusCommand,
|
|
692
|
+
statusRow,
|
|
693
|
+
statusOutputText,
|
|
336
694
|
windowsCommand
|
|
337
695
|
};
|
|
338
696
|
|