aegiscode 7.0.26 → 7.0.27
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/SOURCE-OFFER.txt +9 -0
- package/package.json +4 -7
- package/scripts/check.mjs +1 -52
- package/scripts/demo.mjs +1 -94
- package/scripts/predist.mjs +1 -75
- package/scripts/runtests.mjs +1 -56
- package/scripts/seal-gate.mjs +1 -74
- package/src/agents.js +1 -139
- package/src/app.js +1 -2957
- package/src/art.js +1 -289
- package/src/chatflow.js +1 -3023
- package/src/checkpoint.js +1 -90
- package/src/clipboard.js +1 -424
- package/src/cloudsync.js +1 -528
- package/src/commands.js +1 -4089
- package/src/config.js +1 -581
- package/src/connect.js +1 -349
- package/src/credentials.js +1 -33
- package/src/deps.js +1 -232
- package/src/devrun.js +1 -187
- package/src/diff.js +1 -333
- package/src/diffview.js +1 -296
- package/src/engine.js +1 -297
- package/src/entitlement.js +1 -355
- package/src/events.js +1 -707
- package/src/export.js +1 -67
- package/src/filepreview.js +1 -153
- package/src/fingerprint.js +1 -592
- package/src/format.js +1 -259
- package/src/fuzzy.js +1 -119
- package/src/history.js +1 -423
- package/src/init.js +1 -175
- package/src/input.js +1 -139
- package/src/keys.js +1 -183
- package/src/markdown.js +1 -306
- package/src/models.js +1 -507
- package/src/naming.js +1 -57
- package/src/overlays.js +1 -462
- package/src/panels.js +1 -1418
- package/src/ping.js +1 -226
- package/src/presence.js +1 -238
- package/src/render.js +1 -516
- package/src/screen.js +1 -518
- package/src/screens.js +1 -896
- package/src/secret.js +1 -59
- package/src/securefs.js +1 -359
- package/src/settings.js +1 -155
- package/src/shared.js +1 -52
- package/src/sharedpaths.js +1 -53
- package/src/signup.js +1 -156
- package/src/summarize.js +1 -95
- package/src/sysdiag.js +1 -585
- package/src/system.js +1 -40
- package/src/theme.js +1 -570
- package/src/title.js +1 -220
- package/src/todos.js +1 -115
- package/src/tokens.js +1 -263
- package/src/topup.js +1 -210
- package/src/update.js +1 -191
- package/vendor/client/aegis.js +1 -2441
- package/vendor/client/attest.js +1 -518
- package/vendor/client/authorship-key.js +1 -410
- package/vendor/client/auto-provision.js +1 -134
- package/vendor/client/brain-catalog.js +1 -268
- package/vendor/client/byok-catalog.js +1 -199
- package/vendor/client/catalog-client.js +1 -487
- package/vendor/client/conversation-export.js +1 -177
- package/vendor/client/credentials.js +1 -494
- package/vendor/client/device-fingerprint.js +1 -141
- package/vendor/client/drive-connect.js +1 -110
- package/vendor/client/drive-upload.js +1 -234
- package/vendor/client/env-file.js +1 -1273
- package/vendor/client/fingerprint.js +1 -1064
- package/vendor/client/foreign-memory.js +1 -669
- package/vendor/client/google-signin.js +1 -133
- package/vendor/client/hooks.js +1 -430
- package/vendor/client/model-discovery.js +1 -373
- package/vendor/client/naming.js +1 -283
- package/vendor/client/platform.js +1 -937
- package/vendor/client/prefs.js +1 -351
- package/vendor/client/presence-constants.js +1 -107
- package/vendor/client/presence.js +1 -438
- package/vendor/client/record-vault.js +3 -1055
- package/vendor/client/session-store.js +1 -575
- package/vendor/client/topup.js +1 -297
- package/vendor/client/touch.js +1 -458
- package/vendor/client/update.js +1 -763
- package/vendor/client/user-models.js +1 -381
- package/vendor/desktop/lib/local/agents.js +1 -105
- package/vendor/desktop/lib/local/attachments.js +1 -276
- package/vendor/desktop/lib/local/autonomous.js +1 -1045
- package/vendor/desktop/lib/local/claude-pro.js +1 -1028
- package/vendor/desktop/lib/local/context.js +1 -125
- package/vendor/desktop/lib/local/engine.js +1 -3666
- package/vendor/desktop/lib/local/entitlement.js +1 -660
- package/vendor/desktop/lib/local/exec-plan.js +1 -269
- package/vendor/desktop/lib/local/git-scope.js +1 -228
- package/vendor/desktop/lib/local/integrity.js +1 -163
- package/vendor/desktop/lib/local/local.js +1 -2145
- package/vendor/desktop/lib/local/prompt.js +1 -280
- package/vendor/desktop/lib/local/queue.js +1 -560
- package/vendor/desktop/lib/local/session-rounds.js +1 -238
- package/vendor/desktop/lib/local/shell.js +1 -712
- package/vendor/desktop/lib/local/spill.js +1 -75
- package/vendor/desktop/lib/local/tools.js +1 -1328
- package/vendor/desktop/lib/local/turn-guard.js +1 -300
- package/vendor/desktop/lib/local/worktree-lock.js +1 -235
- package/vendor/desktop/lib/settings.js +1 -890
- package/vendor/desktop/main.js +3 -4890
- package/vendor/desktop/renderer/usage.js +1 -1239
- package/vendor/hosts/install.js +1 -638
- package/vendor/hosts/openai-shim.js +1 -515
- package/vendor/hosts/spec.js +1 -164
- package/vendor/hosts/targets.js +1 -346
- package/vendor/mcp/server.js +1 -221
- package/vendor/mcp/tools.js +1 -408
- package/vendor/one/src/child.js +1 -285
- package/vendor/one/src/permissions.js +1 -383
package/src/screen.js
CHANGED
|
@@ -1,520 +1,3 @@
|
|
|
1
1
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
2
|
// Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
|
|
3
|
-
|
|
4
|
-
// Conveying a modified copy, or running one as a network service, owes the
|
|
5
|
-
// corresponding source under this licence (§5/§13) — see LICENSING.md.
|
|
6
|
-
'use strict';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Terminal plumbing: cell-accurate width maths, the span/line render model,
|
|
10
|
-
* wrapping, and the ephemeral "live" region the CLI redraws while a call is in
|
|
11
|
-
* flight.
|
|
12
|
-
*
|
|
13
|
-
* The render model is the upstream engine's: a line is an array of spans
|
|
14
|
-
* `{ t: text, s: style-prefix, w: cell-width }`, built with `span()`, measured
|
|
15
|
-
* with `lineWidth()`, padded/truncated with `padLine()`, and painted with
|
|
16
|
-
* `paint()`. Two width functions exist because they have different callers:
|
|
17
|
-
* `w()` returns the display width of a *plain string* (the linear transcript
|
|
18
|
-
* and the string renderers in render.js use it), while `padLine()` does the
|
|
19
|
-
* per-codepoint maths for a span line (the overlays use it).
|
|
20
|
-
*
|
|
21
|
-
* Design note — this CLI is a LINEAR transcript, not a full-screen alt-buffer
|
|
22
|
-
* TUI (which is what Claude Code and the upstream engine are). Finished turns are
|
|
23
|
-
* written once to scrollback, so output stays selectable, pipeable and
|
|
24
|
-
* scrollback-searchable; only the one live status/spinner line is redrawn. The
|
|
25
|
-
* main session loop deliberately does NOT enter the alternate screen — `--print`
|
|
26
|
-
* and piped runs must keep working. The alt-screen / mouse / bracketed-paste
|
|
27
|
-
* helpers below exist only so the overlay renderers (overlays.js) and any future
|
|
28
|
-
* modal can paint and restore cleanly; they are never called on the transcript
|
|
29
|
-
* path. Keeping the two concerns in one module (rather than two) is the same
|
|
30
|
-
* split the reference uses.
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
const ANSI_RE = /\x1b\[[0-9;]*[A-Za-z]/g;
|
|
34
|
-
|
|
35
|
-
const { caps, cellWidth } = require('./caps.js');
|
|
36
|
-
|
|
37
|
-
// Real terminal cell width (wcwidth-style) comes from `caps.js`, which is the
|
|
38
|
-
// one place the table lives: a platform that draws a rune two cells wide that
|
|
39
|
-
// wcwidth does not know about (`AEGIS_WIDE_RUNES=✦`) has to shift *every* width
|
|
40
|
-
// decision at once, and a second copy of the table here is exactly how the
|
|
41
|
-
// transcript pads to one width while the overlay pads to another.
|
|
42
|
-
const cwidth = (ch) => cellWidth(ch);
|
|
43
|
-
|
|
44
|
-
function stripAnsi(s) {
|
|
45
|
-
return String(s).replace(ANSI_RE, '');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** Display width of a string in terminal cells. ANSI escapes are transparent
|
|
49
|
-
* (so a styled line measures the same as its visible text) — kept from the
|
|
50
|
-
* previous revision so callers measuring render output stay correct. */
|
|
51
|
-
function w(s) {
|
|
52
|
-
let n = 0;
|
|
53
|
-
for (const ch of stripAnsi(s)) n += cwidth(ch);
|
|
54
|
-
return n;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Truncate a plain string to `width` cells without cutting a wide/combining
|
|
58
|
-
* codepoint. */
|
|
59
|
-
function clip(s, width) {
|
|
60
|
-
if (w(s) <= width) return s;
|
|
61
|
-
let out = '';
|
|
62
|
-
let used = 0;
|
|
63
|
-
for (const ch of String(s)) {
|
|
64
|
-
const cw = cwidth(ch);
|
|
65
|
-
if (used + cw > width) break;
|
|
66
|
-
out += ch;
|
|
67
|
-
used += cw;
|
|
68
|
-
}
|
|
69
|
-
return out;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Pad (or clip) a plain string to exactly `width` cells. */
|
|
73
|
-
function pad(s, width) {
|
|
74
|
-
const t = clip(s, width);
|
|
75
|
-
return t + ' '.repeat(Math.max(0, width - w(t)));
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Right-align a plain string inside `width`. */
|
|
79
|
-
function padStart(s, width) {
|
|
80
|
-
const t = clip(s, width);
|
|
81
|
-
return ' '.repeat(Math.max(0, width - w(t))) + t;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// ── the span/line render model ──────────────────────────────────────────────
|
|
85
|
-
|
|
86
|
-
/** A styled text run. Shape { t, s, w } is the contract with overlays.js and
|
|
87
|
-
* markdown.js. */
|
|
88
|
-
function span(style, t) {
|
|
89
|
-
return { t, s: style, w: w(t) };
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** Total display width of a span line. */
|
|
93
|
-
const lineWidth = (line) => line.reduce((a, sp) => a + sp.w, 0);
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Pad / truncate a span line to an exact cell width. Truncation is visual only
|
|
97
|
-
* (a trailing style reset may be dropped) — the terminal clips whole glyphs, and
|
|
98
|
-
* a 2-cell glyph is never split mid-width.
|
|
99
|
-
*/
|
|
100
|
-
function padLine(line, width) {
|
|
101
|
-
let cur = 0;
|
|
102
|
-
const out = [];
|
|
103
|
-
for (const sp of line) {
|
|
104
|
-
if (cur >= width) break;
|
|
105
|
-
let t = sp.t;
|
|
106
|
-
let tw = sp.w;
|
|
107
|
-
if (cur + tw > width) {
|
|
108
|
-
// Truncate by whole code points that fit in the remaining cells — a
|
|
109
|
-
// codepoint slice (keep chars) would split a 2-cell glyph mid-width.
|
|
110
|
-
const room = width - cur;
|
|
111
|
-
let piece = '';
|
|
112
|
-
let used = 0;
|
|
113
|
-
for (const ch of [...t]) {
|
|
114
|
-
const cw = w(ch);
|
|
115
|
-
if (used + cw > room) break;
|
|
116
|
-
piece += ch;
|
|
117
|
-
used += cw;
|
|
118
|
-
}
|
|
119
|
-
t = piece;
|
|
120
|
-
tw = used;
|
|
121
|
-
}
|
|
122
|
-
out.push({ t, s: sp.s, w: tw });
|
|
123
|
-
cur += tw;
|
|
124
|
-
}
|
|
125
|
-
if (cur < width) out.push(span('', ' '.repeat(width - cur)));
|
|
126
|
-
return out;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/** Term size, clamped so a zero-column report (CI, redirected stdout) still
|
|
130
|
-
* yields a paintable frame. Resolved through `caps.js` so `AEGIS_WIDTH` and
|
|
131
|
-
* `--width` reach every caller — the overlays used to read
|
|
132
|
-
* `process.stdout.columns` directly, which is 0 on a redirected run and left
|
|
133
|
-
* the frame 80 columns wide while the transcript had already been told
|
|
134
|
-
* otherwise. */
|
|
135
|
-
const getSize = () => {
|
|
136
|
-
const c = caps();
|
|
137
|
-
return { cols: c.cols, rows: c.rows };
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
// ── wrapping (plain strings; the string renderers use these) ─────────────────
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Word-wrap one logical plain line to `width` cells, preserving words and never
|
|
144
|
-
* breaking mid-word unless the word alone exceeds the width.
|
|
145
|
-
*/
|
|
146
|
-
function wrapLine(line, width, indent = '') {
|
|
147
|
-
const limit = Math.max(8, width);
|
|
148
|
-
if (w(line) <= limit) return [line];
|
|
149
|
-
const words = line.split(' ');
|
|
150
|
-
const out = [];
|
|
151
|
-
let cur = '';
|
|
152
|
-
for (const word of words) {
|
|
153
|
-
const piece = cur ? `${cur} ${word}` : word;
|
|
154
|
-
if (w(piece) <= limit) {
|
|
155
|
-
cur = piece;
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
if (cur) out.push(cur);
|
|
159
|
-
if (w(word) <= limit) {
|
|
160
|
-
cur = word;
|
|
161
|
-
continue;
|
|
162
|
-
}
|
|
163
|
-
// A single token longer than the line: hard-split on cells.
|
|
164
|
-
let rest = word;
|
|
165
|
-
while (w(rest) > limit) {
|
|
166
|
-
const head = clip(rest, limit);
|
|
167
|
-
out.push(head);
|
|
168
|
-
rest = rest.slice(head.length);
|
|
169
|
-
}
|
|
170
|
-
cur = rest;
|
|
171
|
-
}
|
|
172
|
-
if (cur || !out.length) out.push(cur);
|
|
173
|
-
return out;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/** Wrap a block of plain text (honours existing newlines), applying `indent`. */
|
|
177
|
-
function wrapBlock(text, width, indent = '', hang = false) {
|
|
178
|
-
const lines = [];
|
|
179
|
-
for (const raw of String(text).split('\n')) {
|
|
180
|
-
if (raw === '') {
|
|
181
|
-
lines.push('');
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
const parts = wrapLine(raw, width - w(indent));
|
|
185
|
-
parts.forEach((p) => lines.push(indent + p));
|
|
186
|
-
}
|
|
187
|
-
return lines;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// ── cursor / screen control ─────────────────────────────────────────────────
|
|
191
|
-
|
|
192
|
-
const ESC = {
|
|
193
|
-
hideCursor: '\x1b[?25l',
|
|
194
|
-
showCursor: '\x1b[?25h',
|
|
195
|
-
clearToEnd: '\x1b[0K',
|
|
196
|
-
clearLine: '\x1b[2K',
|
|
197
|
-
clearScreen: '\x1b[2J\x1b[H',
|
|
198
|
-
up: (n) => (n > 0 ? `\x1b[${n}A` : ''),
|
|
199
|
-
down: (n) => (n > 0 ? `\x1b[${n}B` : ''),
|
|
200
|
-
col: (n) => `\x1b[${n}G`,
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
const hideCursor = () => process.stdout.write('\x1b[?25l');
|
|
204
|
-
const showCursor = () => process.stdout.write('\x1b[?25h');
|
|
205
|
-
const moveTo = (row, col) => process.stdout.write(`\x1b[${row};${col}H`);
|
|
206
|
-
|
|
207
|
-
function clearScreen() {
|
|
208
|
-
process.stdout.write('\x1b[2J\x1b[H');
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// Alternate screen buffer. Not used by the transcript (see the module note);
|
|
212
|
-
// available to overlays that need to paint and then restore the pre-launch
|
|
213
|
-
// terminal exactly. Guarded so paired enter/leave stay balanced.
|
|
214
|
-
let inAlt = false;
|
|
215
|
-
function enterAltScreen() {
|
|
216
|
-
if (inAlt) return;
|
|
217
|
-
inAlt = true;
|
|
218
|
-
process.stdout.write('\x1b[?1049h');
|
|
219
|
-
}
|
|
220
|
-
function leaveAltScreen() {
|
|
221
|
-
if (!inAlt) return;
|
|
222
|
-
inAlt = false;
|
|
223
|
-
process.stdout.write('\x1b[?1049l');
|
|
224
|
-
flushExitNotice();
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// ── exit notice: text that must land OUTSIDE the alternate screen ───────────
|
|
228
|
-
// The resume hint is written on the way out, and its *position* is the whole
|
|
229
|
-
// contract: written inside the screen, the restore that follows paints over it
|
|
230
|
-
// and the user is told nothing. Ordering cannot deliver that guarantee here —
|
|
231
|
-
// the teardown is reachable from three places (the session loop's `finally`,
|
|
232
|
-
// the session's `process.once('exit')` handler, and the entrypoint's own exit
|
|
233
|
-
// handler) and which one runs first varies with timing, which showed up as the
|
|
234
|
-
// same 81 bytes in two positions across runs and platforms. So the notice is
|
|
235
|
-
// not *scheduled* next to the restore; it is emitted *by* the restore, cleared
|
|
236
|
-
// as it goes, which makes "outside the screen" true by construction and lets
|
|
237
|
-
// only one of the paths ever produce it.
|
|
238
|
-
let exitNotice = null;
|
|
239
|
-
function queueExitNotice(text) {
|
|
240
|
-
exitNotice = text;
|
|
241
|
-
}
|
|
242
|
-
function flushExitNotice() {
|
|
243
|
-
if (exitNotice === null) return;
|
|
244
|
-
const text = exitNotice;
|
|
245
|
-
exitNotice = null;
|
|
246
|
-
process.stdout.write(text);
|
|
247
|
-
}
|
|
248
|
-
const hasExitNotice = () => exitNotice !== null;
|
|
249
|
-
const isAltScreen = () => inAlt;
|
|
250
|
-
|
|
251
|
-
// Bracketed paste (DEC 2004): a paste arrives as one \x1b[200~ … \x1b[201~
|
|
252
|
-
// chunk instead of a burst of keystrokes.
|
|
253
|
-
const enableBracketedPaste = () => process.stdout.write('\x1b[?2004h');
|
|
254
|
-
const disableBracketedPaste = () => process.stdout.write('\x1b[?2004l');
|
|
255
|
-
|
|
256
|
-
// Mouse tracking + SGR coordinates. DECSET 1000 is the press/wheel report the
|
|
257
|
-
// click-to-toggle path needs; 1002 adds DRAG reports, without which a
|
|
258
|
-
// drag-to-mark gesture is invisible — the terminal reports the press and the
|
|
259
|
-
// release and nothing in between, so a sweep over three lines arrives as a
|
|
260
|
-
// single-cell marking. The terminal's own selection can't substitute for it:
|
|
261
|
-
// the alt screen has no scrollback for the terminal to select from. Disabled on
|
|
262
|
-
// exit so the shell we return to isn't left forwarding raw wheel bytes as input.
|
|
263
|
-
const enableMouseTracking = () => process.stdout.write('\x1b[?1000h\x1b[?1002h\x1b[?1006h');
|
|
264
|
-
const disableMouseTracking = () => process.stdout.write('\x1b[?1000l\x1b[?1002l\x1b[?1006l');
|
|
265
|
-
|
|
266
|
-
// Reverse video, used to paint a mouse marking (text selection) over the
|
|
267
|
-
// already-styled grid: the selected slice keeps its own colors and inverts its
|
|
268
|
-
// background. SELECT_OFF (SGR 27) turns inversion back off for the spans that
|
|
269
|
-
// follow the selection on the same row — paint() only resets attributes at the
|
|
270
|
-
// start of a row, so a leftover inverse would wash out the row's tail.
|
|
271
|
-
const SELECT_ON = '\x1b[7m';
|
|
272
|
-
const SELECT_OFF = '\x1b[27m';
|
|
273
|
-
|
|
274
|
-
// ── mouse marking (text selection) ─────────────────────────────────────────
|
|
275
|
-
// The TUI owns the alternate screen, so the terminal's own drag-to-select is
|
|
276
|
-
// unreachable — a marking has to be built from the SGR drag stream (events.js)
|
|
277
|
-
// against the grid the last frame painted (chatflow.js keeps it as
|
|
278
|
-
// `frameLines`). All of it is pure and cell-based: coordinates are grid row +
|
|
279
|
-
// CELL column (0-based), never string indices, so wide glyphs and padded rows
|
|
280
|
-
// can't shift the selection off the text the reader is pointing at.
|
|
281
|
-
// Ported from the upstream engine reference so both hosts mark identically.
|
|
282
|
-
|
|
283
|
-
/** Two drag endpoints → the same range in reading order (top-left first). */
|
|
284
|
-
function selectionRange(a, b) {
|
|
285
|
-
if (a.row < b.row || (a.row === b.row && a.col <= b.col)) {
|
|
286
|
-
return { startRow: a.row, startCol: a.col, endRow: b.row, endCol: b.col };
|
|
287
|
-
}
|
|
288
|
-
return { startRow: b.row, startCol: b.col, endRow: a.row, endCol: a.col };
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* Clip a span line to the cell range [from, to). Edges are cut on whole code
|
|
293
|
-
* points via w() — slicing by character index would split a 2-cell glyph and
|
|
294
|
-
* shift every later column by one. Spans keep their own style, so a marking
|
|
295
|
-
* across a colored diff block stays colored.
|
|
296
|
-
*/
|
|
297
|
-
function sliceLine(line, from, to) {
|
|
298
|
-
const out = [];
|
|
299
|
-
let col = 0;
|
|
300
|
-
for (const sp of line) {
|
|
301
|
-
const start = col;
|
|
302
|
-
col += sp.w;
|
|
303
|
-
if (col <= from || start >= to) continue;
|
|
304
|
-
let text = sp.t;
|
|
305
|
-
let seen = start; // cell column of text[0]
|
|
306
|
-
if (seen < from) {
|
|
307
|
-
const drop = from - seen;
|
|
308
|
-
let acc = 0;
|
|
309
|
-
let cut = 0;
|
|
310
|
-
for (const ch of text) {
|
|
311
|
-
if (acc >= drop) break;
|
|
312
|
-
acc += w(ch);
|
|
313
|
-
cut += ch.length;
|
|
314
|
-
}
|
|
315
|
-
text = text.slice(cut);
|
|
316
|
-
seen += acc;
|
|
317
|
-
}
|
|
318
|
-
if (seen + w(text) > to) {
|
|
319
|
-
let acc = 0;
|
|
320
|
-
let keep = '';
|
|
321
|
-
for (const ch of text) {
|
|
322
|
-
const cw = w(ch);
|
|
323
|
-
if (seen + acc + cw > to) break;
|
|
324
|
-
keep += ch;
|
|
325
|
-
acc += cw;
|
|
326
|
-
}
|
|
327
|
-
text = keep;
|
|
328
|
-
}
|
|
329
|
-
if (text) out.push({ t: text, s: sp.s, w: w(text) });
|
|
330
|
-
}
|
|
331
|
-
return out;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* The text a marking covers, read as a terminal would copy it: hard newlines
|
|
336
|
-
* between rows, trailing padding trimmed (rows are padded to the full width,
|
|
337
|
-
* and nobody wants 200 spaces per line on the clipboard), leading/trailing
|
|
338
|
-
* blank rows dropped.
|
|
339
|
-
*/
|
|
340
|
-
function selectionText(lines, sel) {
|
|
341
|
-
const rows = [];
|
|
342
|
-
for (let r = sel.startRow; r <= sel.endRow; r++) {
|
|
343
|
-
const line = lines[r] || [];
|
|
344
|
-
const from = r === sel.startRow ? sel.startCol : 0;
|
|
345
|
-
const to = r === sel.endRow ? sel.endCol : Infinity;
|
|
346
|
-
rows.push(sliceLine(line, from, to).map((sp) => sp.t).join('').replace(/[ \t]+$/, ''));
|
|
347
|
-
}
|
|
348
|
-
while (rows.length && rows[0] === '') rows.shift();
|
|
349
|
-
while (rows.length && rows[rows.length - 1] === '') rows.pop();
|
|
350
|
-
return rows.join('\n');
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* A copy of `lines` with the marked cells wrapped in reverse video. Rows
|
|
355
|
-
* outside the range are shared, not cloned — a marking repaints up to ~30x/sec
|
|
356
|
-
* during a sweep and only the rows it touches should allocate. Each is padded
|
|
357
|
-
* first so a short row's selection is still painted over the blank cells the
|
|
358
|
-
* reader dragged across.
|
|
359
|
-
*/
|
|
360
|
-
function applySelection(lines, sel, width) {
|
|
361
|
-
const out = lines.slice();
|
|
362
|
-
for (let r = sel.startRow; r <= sel.endRow; r++) {
|
|
363
|
-
if (r < 0 || r >= out.length) continue;
|
|
364
|
-
const line = padLine(out[r] || [], width);
|
|
365
|
-
const from = r === sel.startRow ? sel.startCol : 0;
|
|
366
|
-
const to = r === sel.endRow ? sel.endCol : width;
|
|
367
|
-
const mid = sliceLine(line, from, to);
|
|
368
|
-
if (!mid.length) continue;
|
|
369
|
-
out[r] = [
|
|
370
|
-
...sliceLine(line, 0, from),
|
|
371
|
-
{ t: '', s: SELECT_ON, w: 0 },
|
|
372
|
-
...mid,
|
|
373
|
-
{ t: '', s: SELECT_OFF, w: 0 },
|
|
374
|
-
...sliceLine(line, to, width),
|
|
375
|
-
];
|
|
376
|
-
}
|
|
377
|
-
return out;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* Paint span lines top-aligned and clear the rest of the frame. Used by the
|
|
382
|
-
* overlay renderers; the transcript path never calls it (it stays linear).
|
|
383
|
-
*/
|
|
384
|
-
function paint(lines) {
|
|
385
|
-
const { rows, cols } = getSize();
|
|
386
|
-
let out = '\x1b[H';
|
|
387
|
-
for (let i = 0; i < rows; i++) {
|
|
388
|
-
// Reset at the top of every row: span styles are colour codes, and a colour
|
|
389
|
-
// code does NOT clear bold/italic/dim. Without this a row that ends inside a
|
|
390
|
-
// BOLD segment whose "off" span got dropped by padding would leak the
|
|
391
|
-
// attribute into every later row of the frame.
|
|
392
|
-
out += '\x1b[0m';
|
|
393
|
-
const line = lines[i];
|
|
394
|
-
if (line) {
|
|
395
|
-
const pl = padLine(line, cols);
|
|
396
|
-
for (const sp of pl) out += sp.s + sp.t;
|
|
397
|
-
out += '\x1b[0K';
|
|
398
|
-
} else {
|
|
399
|
-
out += '\x1b[0K';
|
|
400
|
-
}
|
|
401
|
-
if (i < rows - 1) out += '\n';
|
|
402
|
-
}
|
|
403
|
-
out += '\x1b[0m';
|
|
404
|
-
process.stdout.write(out);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/** Repaint from `startRow` down, leaving rows above it untouched. */
|
|
408
|
-
function paintFrom(lines, startRow) {
|
|
409
|
-
const { rows, cols } = getSize();
|
|
410
|
-
let out = '';
|
|
411
|
-
for (let i = 0; i < rows - startRow; i++) {
|
|
412
|
-
const line = lines[startRow + i];
|
|
413
|
-
if (i > 0) out += '\n';
|
|
414
|
-
out += '\x1b[0K';
|
|
415
|
-
out += '\x1b[0m';
|
|
416
|
-
if (line) {
|
|
417
|
-
const pl = padLine(line, cols);
|
|
418
|
-
for (const sp of pl) out += sp.s + sp.t;
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
out += '\x1b[0m';
|
|
422
|
-
process.stdout.write(out);
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
function termWidth(fallback = 80) {
|
|
426
|
-
const { cols } = getSize();
|
|
427
|
-
return cols > 0 ? cols : Math.max(20, fallback);
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
/**
|
|
431
|
-
* A block of lines at the bottom of the transcript that can be redrawn in
|
|
432
|
-
* place. Every write goes through here, so the escape arithmetic lives in one
|
|
433
|
-
* place: `update()` erases the previous block, prints the new one, and keeps
|
|
434
|
-
* the cursor below it.
|
|
435
|
-
*/
|
|
436
|
-
class LiveRegion {
|
|
437
|
-
constructor(out = process.stdout) {
|
|
438
|
-
this.out = out;
|
|
439
|
-
this.rows = 0;
|
|
440
|
-
this.active = false;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
update(lines) {
|
|
444
|
-
const body = lines.map((l) => l + ESC.clearToEnd).join('\n');
|
|
445
|
-
if (!this.active) {
|
|
446
|
-
this.out.write(body);
|
|
447
|
-
this.active = true;
|
|
448
|
-
} else {
|
|
449
|
-
// Back to the first row of the old block, rewrite, clear the tail.
|
|
450
|
-
this.out.write(ESC.col(1) + ESC.up(this.rows) + body + '\n' + ESC.col(1) + ESC.up(1));
|
|
451
|
-
}
|
|
452
|
-
// The PHYSICAL terminal rows the block just occupies, not the number of
|
|
453
|
-
// logical lines passed in. A working line (render.js's `renderWorking`,
|
|
454
|
-
// the only caller with a long verb + elapsed + token tail) can exceed the
|
|
455
|
-
// terminal width and wrap to two rows on anything narrower than ~65
|
|
456
|
-
// columns; recording `lines.length` (always 1) left the next tick's
|
|
457
|
-
// `up(this.rows)` one row short, so the wrapped second row was never
|
|
458
|
-
// erased. Since the line's content (elapsed time, token count) changes
|
|
459
|
-
// every 90ms tick, that stray row renewed itself every tick — a
|
|
460
|
-
// vertically growing stack of near-duplicate status lines while the
|
|
461
|
-
// model streamed, instead of one line updating in place.
|
|
462
|
-
const cols = Math.max(1, getSize().cols);
|
|
463
|
-
this.rows = lines.reduce((n, l) => n + Math.max(1, Math.ceil(w(l) / cols)), 0);
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
/** Erase the block and return the cursor to its start, so the caller can
|
|
467
|
-
* print the durable version of the same content. */
|
|
468
|
-
clear() {
|
|
469
|
-
if (!this.active) return;
|
|
470
|
-
this.out.write(ESC.col(1) + ESC.clearLine);
|
|
471
|
-
for (let i = 1; i < this.rows; i++) this.out.write(`\n${ESC.clearLine}`);
|
|
472
|
-
this.out.write(ESC.up(this.rows - 1) + ESC.col(1));
|
|
473
|
-
this.rows = 0;
|
|
474
|
-
this.active = false;
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
module.exports = {
|
|
479
|
-
queueExitNotice,
|
|
480
|
-
flushExitNotice,
|
|
481
|
-
hasExitNotice,
|
|
482
|
-
// plain-string width helpers (kept for the string renderers + app/bin)
|
|
483
|
-
w,
|
|
484
|
-
clip,
|
|
485
|
-
pad,
|
|
486
|
-
padStart,
|
|
487
|
-
stripAnsi,
|
|
488
|
-
wrapLine,
|
|
489
|
-
wrapBlock,
|
|
490
|
-
// span/line render model (from the upstream engine)
|
|
491
|
-
span,
|
|
492
|
-
lineWidth,
|
|
493
|
-
padLine,
|
|
494
|
-
paint,
|
|
495
|
-
paintFrom,
|
|
496
|
-
getSize,
|
|
497
|
-
// cursor / screen / input helpers (overlay path)
|
|
498
|
-
EC: ESC,
|
|
499
|
-
hideCursor,
|
|
500
|
-
showCursor,
|
|
501
|
-
moveTo,
|
|
502
|
-
clearScreen,
|
|
503
|
-
enterAltScreen,
|
|
504
|
-
leaveAltScreen,
|
|
505
|
-
isAltScreen,
|
|
506
|
-
enableBracketedPaste,
|
|
507
|
-
disableBracketedPaste,
|
|
508
|
-
enableMouseTracking,
|
|
509
|
-
disableMouseTracking,
|
|
510
|
-
// mouse marking (text selection)
|
|
511
|
-
SELECT_ON,
|
|
512
|
-
SELECT_OFF,
|
|
513
|
-
selectionRange,
|
|
514
|
-
sliceLine,
|
|
515
|
-
selectionText,
|
|
516
|
-
applySelection,
|
|
517
|
-
// the live region + sizing (app/bin)
|
|
518
|
-
LiveRegion,
|
|
519
|
-
termWidth,
|
|
520
|
-
};
|
|
3
|
+
"use strict";const ANSI_RE=/\x1b\[[0-9;]*[A-Za-z]/g,{caps:caps,cellWidth:cellWidth}=require("./caps.js"),cwidth=t=>cellWidth(t);function stripAnsi(t){return String(t).replace(ANSI_RE,"")}function w(t){let e=0;for(const o of stripAnsi(t))e+=cwidth(o);return e}function clip(t,e){if(w(t)<=e)return t;let o="",i=0;for(const n of String(t)){const t=cwidth(n);if(i+t>e)break;o+=n,i+=t}return o}function pad(t,e){const o=clip(t,e);return o+" ".repeat(Math.max(0,e-w(o)))}function padStart(t,e){const o=clip(t,e);return" ".repeat(Math.max(0,e-w(o)))+o}function span(t,e){return{t:e,s:t,w:w(e)}}const lineWidth=t=>t.reduce(((t,e)=>t+e.w),0);function padLine(t,e){let o=0;const i=[];for(const n of t){if(o>=e)break;let t=n.t,s=n.w;if(o+s>e){const i=e-o;let n="",r=0;for(const e of[...t]){const t=w(e);if(r+t>i)break;n+=e,r+=t}t=n,s=r}i.push({t:t,s:n.s,w:s}),o+=s}return o<e&&i.push(span(""," ".repeat(e-o))),i}const getSize=()=>{const t=caps();return{cols:t.cols,rows:t.rows}};function wrapLine(t,e,o=""){const i=Math.max(8,e);if(w(t)<=i)return[t];const n=t.split(" "),s=[];let r="";for(const t of n){const e=r?`${r} ${t}`:t;if(w(e)<=i){r=e;continue}if(r&&s.push(r),w(t)<=i){r=t;continue}let o=t;for(;w(o)>i;){const t=clip(o,i);s.push(t),o=o.slice(t.length)}r=o}return!r&&s.length||s.push(r),s}function wrapBlock(t,e,o="",i=!1){const n=[];for(const i of String(t).split("\n"))""!==i?wrapLine(i,e-w(o)).forEach((t=>n.push(o+t))):n.push("");return n}const ESC={hideCursor:"[?25l",showCursor:"[?25h",clearToEnd:"[0K",clearLine:"[2K",clearScreen:"[2J[H",up:t=>t>0?`[${t}A`:"",down:t=>t>0?`[${t}B`:"",col:t=>`[${t}G`},hideCursor=()=>process.stdout.write("[?25l"),showCursor=()=>process.stdout.write("[?25h"),moveTo=(t,e)=>process.stdout.write(`[${t};${e}H`);function clearScreen(){process.stdout.write("[2J[H")}let inAlt=!1;function enterAltScreen(){inAlt||(inAlt=!0,process.stdout.write("[?1049h"))}function leaveAltScreen(){inAlt&&(inAlt=!1,process.stdout.write("[?1049l"),flushExitNotice())}let exitNotice=null;function queueExitNotice(t){exitNotice=t}function flushExitNotice(){if(null===exitNotice)return;const t=exitNotice;exitNotice=null,process.stdout.write(t)}const hasExitNotice=()=>null!==exitNotice,isAltScreen=()=>inAlt,enableBracketedPaste=()=>process.stdout.write("[?2004h"),disableBracketedPaste=()=>process.stdout.write("[?2004l"),enableMouseTracking=()=>process.stdout.write("[?1000h[?1002h[?1006h"),disableMouseTracking=()=>process.stdout.write("[?1000l[?1002l[?1006l"),SELECT_ON="[7m",SELECT_OFF="[27m";function selectionRange(t,e){return t.row<e.row||t.row===e.row&&t.col<=e.col?{startRow:t.row,startCol:t.col,endRow:e.row,endCol:e.col}:{startRow:e.row,startCol:e.col,endRow:t.row,endCol:t.col}}function sliceLine(t,e,o){const i=[];let n=0;for(const s of t){const t=n;if(n+=s.w,n<=e||t>=o)continue;let r=s.t,c=t;if(c<e){const t=e-c;let o=0,i=0;for(const e of r){if(o>=t)break;o+=w(e),i+=e.length}r=r.slice(i),c+=o}if(c+w(r)>o){let t=0,e="";for(const i of r){const n=w(i);if(c+t+n>o)break;e+=i,t+=n}r=e}r&&i.push({t:r,s:s.s,w:w(r)})}return i}function selectionText(t,e){const o=[];for(let i=e.startRow;i<=e.endRow;i++){const n=t[i]||[],s=i===e.startRow?e.startCol:0,r=i===e.endRow?e.endCol:1/0;o.push(sliceLine(n,s,r).map((t=>t.t)).join("").replace(/[ \t]+$/,""))}for(;o.length&&""===o[0];)o.shift();for(;o.length&&""===o[o.length-1];)o.pop();return o.join("\n")}function applySelection(t,e,o){const i=t.slice();for(let t=e.startRow;t<=e.endRow;t++){if(t<0||t>=i.length)continue;const n=padLine(i[t]||[],o),s=t===e.startRow?e.startCol:0,r=t===e.endRow?e.endCol:o,c=sliceLine(n,s,r);c.length&&(i[t]=[...sliceLine(n,0,s),{t:"",s:"[7m",w:0},...c,{t:"",s:"[27m",w:0},...sliceLine(n,r,o)])}return i}function paint(t){const{rows:e,cols:o}=getSize();let i="[H";for(let n=0;n<e;n++){i+="[0m";const s=t[n];if(s){const t=padLine(s,o);for(const e of t)i+=e.s+e.t;i+="[0K"}else i+="[0K";n<e-1&&(i+="\n")}i+="[0m",process.stdout.write(i)}function paintFrom(t,e){const{rows:o,cols:i}=getSize();let n="";for(let s=0;s<o-e;s++){const o=t[e+s];if(s>0&&(n+="\n"),n+="[0K",n+="[0m",o){const t=padLine(o,i);for(const e of t)n+=e.s+e.t}}n+="[0m",process.stdout.write(n)}function termWidth(t=80){const{cols:e}=getSize();return e>0?e:Math.max(20,t)}class LiveRegion{constructor(t=process.stdout){this.out=t,this.rows=0,this.active=!1}update(t){const e=t.map((t=>t+ESC.clearToEnd)).join("\n");this.active?this.out.write(ESC.col(1)+ESC.up(this.rows)+e+"\n"+ESC.col(1)+ESC.up(1)):(this.out.write(e),this.active=!0);const o=Math.max(1,getSize().cols);this.rows=t.reduce(((t,e)=>t+Math.max(1,Math.ceil(w(e)/o))),0)}clear(){if(this.active){this.out.write(ESC.col(1)+ESC.clearLine);for(let t=1;t<this.rows;t++)this.out.write(`\n${ESC.clearLine}`);this.out.write(ESC.up(this.rows-1)+ESC.col(1)),this.rows=0,this.active=!1}}}module.exports={queueExitNotice:queueExitNotice,flushExitNotice:flushExitNotice,hasExitNotice:hasExitNotice,w:w,clip:clip,pad:pad,padStart:padStart,stripAnsi:stripAnsi,wrapLine:wrapLine,wrapBlock:wrapBlock,span:span,lineWidth:lineWidth,padLine:padLine,paint:paint,paintFrom:paintFrom,getSize:getSize,EC:ESC,hideCursor:hideCursor,showCursor:showCursor,moveTo:moveTo,clearScreen:clearScreen,enterAltScreen:enterAltScreen,leaveAltScreen:leaveAltScreen,isAltScreen:isAltScreen,enableBracketedPaste:enableBracketedPaste,disableBracketedPaste:disableBracketedPaste,enableMouseTracking:enableMouseTracking,disableMouseTracking:disableMouseTracking,SELECT_ON:"[7m",SELECT_OFF:"[27m",selectionRange:selectionRange,sliceLine:sliceLine,selectionText:selectionText,applySelection:applySelection,LiveRegion:LiveRegion,termWidth:termWidth};
|