@yanzc/claude-pro 0.10.0 → 0.12.0
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 +66 -35
- package/package.json +1 -1
- package/src/agent.js +16 -6
- package/src/browser/session.js +32 -2
- package/src/commands.js +160 -11
- package/src/config.js +42 -10
- package/src/live.js +148 -0
- package/src/main.js +5 -3
- package/src/peers.js +64 -10
- package/src/providers.js +10 -1
- package/src/remote.js +90 -243
- package/src/runtime.js +36 -11
- package/src/selfcheck.js +3 -3
- package/src/session.js +116 -7
- package/src/supervisor.js +1 -1
- package/src/tools/bash.js +23 -20
- package/src/tools/browser.js +14 -8
- package/src/tools/code.js +18 -11
- package/src/tools/peers.js +6 -5
- package/src/tools/restart.js +1 -1
- package/src/ui/dialogs.js +23 -30
- package/src/ui/editor.js +65 -169
- package/src/ui/repl.js +470 -449
- package/src/ui/screen.js +47 -674
- package/src/updater.js +5 -4
- package/src/utils/shell.js +4 -5
- package/src/web/api.js +787 -0
- package/src/web/app.css +412 -0
- package/src/web/app.js +1850 -0
- package/src/web/data.js +424 -0
- package/src/web/host.js +334 -0
- package/src/web/login.html +66 -0
- package/src/web/page.html +41 -0
- package/src/web/remote-access.js +232 -0
- package/src/web/server.js +459 -0
package/src/ui/screen.js
CHANGED
|
@@ -14,12 +14,6 @@
|
|
|
14
14
|
// Nothing is drawn while the size is still changing: a redraw made between two re-flows works from a
|
|
15
15
|
// screen that is about to change again.
|
|
16
16
|
//
|
|
17
|
-
// Fullscreen (setFull), as Claude Code's fullscreen rendering: the program draws on the terminal's alternate
|
|
18
|
-
// screen like vim, the input box fixed on the last rows, the conversation in the rows above it. The terminal's own
|
|
19
|
-
// scrollback is not used: the conversation is kept here (history) and scrolled here, with the mouse wheel (mouse
|
|
20
|
-
// reporting on, see mouseFilter), PgUp/PgDn and ctrl+Home/End. Scrolled up, new output does not move the view and a
|
|
21
|
-
// "Jump to bottom" bar counts what arrived. Each draw writes only the rows that changed since the last one.
|
|
22
|
-
//
|
|
23
17
|
// Pinned to the bottom. As in Claude Code the input box sits on the last rows of the window, also when the
|
|
24
18
|
// output so far does not fill it: blank rows at the top of the live region fill the space between the last
|
|
25
19
|
// output and the box. New output takes their place first and only then scrolls. This needs to know where on the
|
|
@@ -28,8 +22,7 @@
|
|
|
28
22
|
// known, and nothing is pinned until it is). Closing removes the blank rows, so that the shell (or the process
|
|
29
23
|
// of a restart) goes on right below the output.
|
|
30
24
|
|
|
31
|
-
import {
|
|
32
|
-
import { ESC, wrapLine, termWidth, truncate, stringWidth, stripAnsi, graphemes, graphemeWidth, setTerminalWidths, ambiguousGroups } from './ansi.js';
|
|
25
|
+
import { ESC, wrapLine, termWidth, truncate, setTerminalWidths, ambiguousGroups } from './ansi.js';
|
|
33
26
|
|
|
34
27
|
const SYNC_START = '\x1b[?2026h';
|
|
35
28
|
const SYNC_END = '\x1b[?2026l';
|
|
@@ -40,8 +33,8 @@ const HISTORY_KEEP = 600; // static lines remembered for repainting (a screen sh
|
|
|
40
33
|
const HISTORY_LINE_CHARS = 20_000;
|
|
41
34
|
|
|
42
35
|
// A line as the terminal would show it, so that what is drawn is what was measured: a tab becomes the 4 spaces
|
|
43
|
-
// stringWidth counts it as (the terminal moved to its next 8-column stop instead, past what was measured,
|
|
44
|
-
//
|
|
36
|
+
// stringWidth counts it as (the terminal moved to its next 8-column stop instead, past what was measured), only what
|
|
37
|
+
// follows the last carriage return
|
|
45
38
|
// stays (a progress bar's "40%\r80%" shows 80%, and no longer overwrites the prefix drawn before it), and other
|
|
46
39
|
// control characters (BEL, backspace...) go. ANSI escapes stay.
|
|
47
40
|
const CONTROLS = /[\x00-\x08\x09\x0b-\x1a\x1c-\x1f\x7f\r]/;
|
|
@@ -52,90 +45,10 @@ export function cleanLine(line) {
|
|
|
52
45
|
if (cr >= 0) s = s.slice(cr + 1);
|
|
53
46
|
return s.replace(/\t/g, ' ').replace(/[\x00-\x08\x0b-\x1a\x1c-\x1f\x7f]/g, '');
|
|
54
47
|
}
|
|
55
|
-
const FULL_KEEP = 20_000; // fullscreen: lines of the conversation kept to scroll back through
|
|
56
|
-
// The alternate screen, and the mouse reported (SGR): buttons, the wheel, and moves while a button is held (a drag).
|
|
57
|
-
const ALT_ON = '\x1b[?1049h\x1b[?1002h\x1b[?1006h';
|
|
58
|
-
const ALT_OFF = '\x1b[?1002l\x1b[?1006l\x1b[?1049l';
|
|
59
|
-
const MULTI_CLICK_MS = 450;
|
|
60
|
-
// A search (ctrl+f): the matches, the current one, and how many are looked for at most.
|
|
61
|
-
const FIND_MATCH = '\x1b[30;43m';
|
|
62
|
-
const FIND_CURRENT = '\x1b[30;48;5;208m';
|
|
63
|
-
const FIND_MAX = 10_000;
|
|
64
|
-
|
|
65
|
-
// The text of a line's wrapped rows without colours, all of it and where each row starts in it (see scanFind). It is
|
|
66
|
-
// what paintRow walks through, so offsets in it are the offsets it counts.
|
|
67
|
-
function plainRows(rows) {
|
|
68
|
-
const plains = rows.map((r) => stripAnsi(r));
|
|
69
|
-
const starts = [];
|
|
70
|
-
for (let k = 0, at = 0; k < plains.length; at += plains[k].length, k++) starts.push(at);
|
|
71
|
-
return { text: plains.join(''), starts };
|
|
72
|
-
}
|
|
73
|
-
// A line that starts a part of the conversation: a tool call or a reply ("⏺ …"), a message of the user ("> …"), a
|
|
74
|
-
// command of bash mode ("! …"), a message of another session ("✉ …"). What a quote is from (see selectionQuote).
|
|
75
|
-
const HEADING = /^(?:⏺|>|!|✉) /;
|
|
76
|
-
const SOURCE_REACH = 5000; // lines looked back for it
|
|
77
48
|
// A row that fills the rest of its width with its background (the user's messages, see renderUserMessage) ends in an
|
|
78
|
-
// erase to the end of the line made while that background is on
|
|
79
|
-
// the cursor, so a second one, made after the colours are reset, would wipe the band out again from the same column.
|
|
49
|
+
// erase to the end of the line made while that background is on: nothing may erase after it.
|
|
80
50
|
const FILL = '\x1b[K';
|
|
81
|
-
const LEAD = /^(?:\x1b\[[0-9;]*m)+/; // the colours a row starts in
|
|
82
51
|
const endRow = (row) => row + (row.includes(FILL) ? '\x1b[0m' : '\x1b[0m\x1b[K');
|
|
83
|
-
|
|
84
|
-
// The part of `plain` (no escape codes) between columns `from` (included) and `to` (not).
|
|
85
|
-
function sliceCols(plain, from, to) {
|
|
86
|
-
let out = '';
|
|
87
|
-
let col = 0;
|
|
88
|
-
for (const g of graphemes(plain)) {
|
|
89
|
-
if (col >= to) break;
|
|
90
|
-
if (col >= from) out += g;
|
|
91
|
-
col += graphemeWidth(g);
|
|
92
|
-
}
|
|
93
|
-
return out;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// The text of a selection without the indentation the conversation is drawn with: the first line from where the
|
|
97
|
-
// selection starts, the others less the indentation they all have (a reply's, a tool result's, whose "⎿" counts as
|
|
98
|
-
// a space). Blank lines at either end go.
|
|
99
|
-
export function dedent(text) {
|
|
100
|
-
const lines = String(text).split('\n').map((l) => l.replace(/^(\s*)⎿(?=\s)/, '$1 ').trimEnd());
|
|
101
|
-
let cut = Infinity;
|
|
102
|
-
for (const l of lines.slice(1)) if (l) cut = Math.min(cut, /^ */.exec(l)[0].length);
|
|
103
|
-
if (cut === Infinity) cut = 0;
|
|
104
|
-
return [lines[0].trimStart(), ...lines.slice(1).map((l) => l.slice(cut))].join('\n').replace(/^\n+|\n+$/g, '');
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const before = (a, b) => a.line < b.line || (a.line === b.line && (a.sub < b.sub || (a.sub === b.sub && a.col <= b.col)));
|
|
108
|
-
const MOUSE = /\x1b\[<(\d+);(\d+);(\d+)([Mm])/g;
|
|
109
|
-
|
|
110
|
-
// The keys of `stdin` without the mouse reports in it (SGR: ESC [ < b ; x ; y M/m), which go to
|
|
111
|
-
// `onMouse({button, x, y, release})` instead: a stream to read keys from. A report split between two chunks is
|
|
112
|
-
// put together; the rest passes through as it came.
|
|
113
|
-
export function mouseFilter(stdin, onMouse) {
|
|
114
|
-
const out = new PassThrough();
|
|
115
|
-
let pending = '';
|
|
116
|
-
let timer = null;
|
|
117
|
-
const pass = (text) => { if (text) out.write(text); };
|
|
118
|
-
stdin.on('data', (d) => {
|
|
119
|
-
clearTimeout(timer);
|
|
120
|
-
let text = pending + (typeof d === 'string' ? d : d.toString('utf8'));
|
|
121
|
-
pending = '';
|
|
122
|
-
text = text.replace(MOUSE, (_, b, x, y, kind) => {
|
|
123
|
-
try { onMouse({ button: Number(b), x: Number(x), y: Number(y), release: kind === 'm' }); } catch { /* the listener's problem */ }
|
|
124
|
-
return '';
|
|
125
|
-
});
|
|
126
|
-
// the start of a report cut off at the end of the chunk, as far as ESC or ESC [ alone: passed on, those two let
|
|
127
|
-
// the rest of the report ("<0;35;12M") through as typed text. A chunk that is a lone ESC is the Esc key, passed
|
|
128
|
-
// on at once (readline holds it 500 ms already).
|
|
129
|
-
const partial = text === '\x1b' ? null : /\x1b(?:\[(?:<[\d;]*)?)?$/.exec(text);
|
|
130
|
-
if (partial) {
|
|
131
|
-
pending = partial[0];
|
|
132
|
-
text = text.slice(0, partial.index);
|
|
133
|
-
timer = setTimeout(() => { const p = pending; pending = ''; pass(p); }, 50); // not a report after all
|
|
134
|
-
}
|
|
135
|
-
pass(text);
|
|
136
|
-
});
|
|
137
|
-
return out;
|
|
138
|
-
}
|
|
139
52
|
const CPR = /\x1b\[(\d+);(\d+)R/g; // the terminal's answer to DSR 6: row;column
|
|
140
53
|
// Windows: the symbols the interface is drawn with are measured at start-up (see queryCursorRow), as its terminals
|
|
141
54
|
// do not agree on their widths. The first FONT_SIZED of them are East Asian Ambiguous characters: a terminal that
|
|
@@ -258,24 +171,6 @@ export class Screen {
|
|
|
258
171
|
this.dimensions = this.readSize();
|
|
259
172
|
this.pin = pin;
|
|
260
173
|
this.pinned = null; // rows from the top of the live region to the bottom of the screen; null: not known
|
|
261
|
-
this.full = false; // fullscreen: see the top of this file
|
|
262
|
-
this.scroll = 0; // fullscreen: rows the view is scrolled up from the end of the conversation (0: following it)
|
|
263
|
-
this.unseen = 0; // fullscreen: rows that arrived while scrolled up
|
|
264
|
-
this.frame = []; // fullscreen: the rows on screen, as last written
|
|
265
|
-
this.wrapped = []; // fullscreen: history[i] wrapped to wrapCols - 1, made when first needed
|
|
266
|
-
this.wrapCols = 0;
|
|
267
|
-
this.lastArea = 0; // fullscreen: rows the conversation had on screen at the last draw
|
|
268
|
-
this.scrollLiveRows = null; // fullscreen: keep the live region's height fixed while the user is scrolled up
|
|
269
|
-
this.viewMap = []; // fullscreen: what each row of the conversation area shows, {line, sub} (null: blank)
|
|
270
|
-
this.barRow = null; // fullscreen: the row of the "Jump to bottom" bar, when it is shown
|
|
271
|
-
this.finding = null; // fullscreen: a search in the conversation (ctrl+f), see openFind
|
|
272
|
-
this.dropped = 0; // fullscreen: lines dropped from the top of the history so far (the first one's number)
|
|
273
|
-
this.selection = null; // fullscreen: {anchor, focus}, points {line, sub, col} of the conversation (history line, wrapped row, column)
|
|
274
|
-
this.dragging = false;
|
|
275
|
-
this.dragFrom = null; // fullscreen: where a drag in the conversation started, {line, sub, col}
|
|
276
|
-
this.lastClick = { at: 0, x: 0, y: 0, count: 0 };
|
|
277
|
-
this.inputBox = null; // fullscreen: where the input box's text is on screen, {top, rows, col} (see inputAt)
|
|
278
|
-
this.inputDrag = null; // fullscreen: the mouse button held down on the input box's text: the cell it is over, {x, y, moved}
|
|
279
174
|
this.cursorRow = 0; // row of the terminal cursor, relative to the top of the live region
|
|
280
175
|
this.staticQueue = [];
|
|
281
176
|
this.history = []; // the most recent static lines, for repainting after a resize
|
|
@@ -345,496 +240,6 @@ export class Screen {
|
|
|
345
240
|
|
|
346
241
|
setLive(fn) { this.renderLive = fn; this.schedule(); }
|
|
347
242
|
|
|
348
|
-
// ------------------------------------------------------------ fullscreen
|
|
349
|
-
|
|
350
|
-
// Go to (true) or leave the alternate screen. Leaving puts the newest output back on the normal screen, so that
|
|
351
|
-
// the classic renderer goes on below it.
|
|
352
|
-
setFull(on) {
|
|
353
|
-
if (on === this.full || this.closed) return;
|
|
354
|
-
if (this.scheduled) { clearTimeout(this.scheduled); this.scheduled = null; }
|
|
355
|
-
this.takeStatic();
|
|
356
|
-
if (on) {
|
|
357
|
-
this.out.write('\r' + ESC.up(this.cursorRow) + ESC.clearDown); // the live region off the normal screen
|
|
358
|
-
this.cursorRow = 0;
|
|
359
|
-
this.full = true;
|
|
360
|
-
this.scroll = 0;
|
|
361
|
-
this.unseen = 0;
|
|
362
|
-
this.frame = [];
|
|
363
|
-
this.out.write(ALT_ON + '\x1b[2J');
|
|
364
|
-
this.draw();
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
this.full = false;
|
|
368
|
-
this.frame = [];
|
|
369
|
-
this.out.write(ALT_OFF + ESC.showCursor);
|
|
370
|
-
const tail = this.history.slice(-Math.max(1, this.height - 1));
|
|
371
|
-
if (tail.length) this.out.write(tail.map((l) => l + '\x1b[0m').join('\r\n') + '\r\n');
|
|
372
|
-
this.cursorRow = 0;
|
|
373
|
-
this.pinned = null;
|
|
374
|
-
this.flush();
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
wrappedAt(i, cols) {
|
|
378
|
-
if (this.wrapCols !== cols) { this.wrapped = []; this.wrapCols = cols; this.selection = null; this.dragFrom = null; } // its rows are other rows now
|
|
379
|
-
if (this.wrapped[i]) return this.wrapped[i];
|
|
380
|
-
let rows = wrapLine(this.history[i], Math.max(1, cols - 1));
|
|
381
|
-
// a band now wider than the window: each of its rows starts in its colours and fills the rest of the row
|
|
382
|
-
if (rows.length > 1 && this.history[i].includes(FILL)) {
|
|
383
|
-
const lead = LEAD.exec(this.history[i])?.[0] || '';
|
|
384
|
-
rows = rows.map((r, k) => (k ? lead : '') + r.replaceAll(FILL, '') + lead + FILL);
|
|
385
|
-
}
|
|
386
|
-
return (this.wrapped[i] = rows);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// Rows of the whole conversation at this width (for the scrolling to stop at its top).
|
|
390
|
-
totalRows(cols) {
|
|
391
|
-
let n = 0;
|
|
392
|
-
for (let i = 0; i < this.history.length; i++) n += this.wrappedAt(i, cols).length;
|
|
393
|
-
return n;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
// `rows` rows of the conversation, ending `this.scroll` rows above its end, as {text, line, sub}; padded below
|
|
397
|
-
// with null when it is shorter.
|
|
398
|
-
view(rows, cols) {
|
|
399
|
-
const need = rows + this.scroll;
|
|
400
|
-
const chunks = [];
|
|
401
|
-
let count = 0;
|
|
402
|
-
for (let i = this.history.length - 1; i >= 0 && count < need; i--) {
|
|
403
|
-
const w = this.wrappedAt(i, cols);
|
|
404
|
-
chunks.push(w.map((text, sub) => ({ text, line: i, sub })));
|
|
405
|
-
count += w.length;
|
|
406
|
-
}
|
|
407
|
-
const got = chunks.reverse().flat();
|
|
408
|
-
const end = Math.max(0, got.length - this.scroll);
|
|
409
|
-
const shown = got.slice(Math.max(0, end - rows), end);
|
|
410
|
-
while (shown.length < rows) shown.push(null);
|
|
411
|
-
return shown;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
// ------------------------------------------------------------ selecting with the mouse (fullscreen)
|
|
415
|
-
|
|
416
|
-
// The selection in order: [start, end].
|
|
417
|
-
ordered() {
|
|
418
|
-
const { anchor, focus } = this.selection;
|
|
419
|
-
return before(anchor, focus) ? [anchor, focus] : [focus, anchor];
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
// The columns of row {line, sub} that are selected: [from, to), or null.
|
|
423
|
-
selectedCols(e) {
|
|
424
|
-
if (!this.selection) return null;
|
|
425
|
-
const [a, b] = this.ordered();
|
|
426
|
-
const row = { line: e.line, sub: e.sub, col: 0 };
|
|
427
|
-
if (before(row, { ...a, col: -1 }) || !before({ ...row, col: 0 }, { ...b, col: Infinity })) return null;
|
|
428
|
-
const from = e.line === a.line && e.sub === a.sub ? a.col : 0;
|
|
429
|
-
const to = e.line === b.line && e.sub === b.sub ? b.col + 1 : Infinity;
|
|
430
|
-
return to > from ? [from, to] : null;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
// A row of the conversation as drawn: the selection in reverse video, the matches of a search highlighted (the
|
|
434
|
-
// current one apart). Such a row loses its own colours, except the band of a row that fills its width.
|
|
435
|
-
paintRow(e) {
|
|
436
|
-
const sel = this.selectedCols(e);
|
|
437
|
-
const hits = this.finding?.byRow.get(`${e.line}:${e.sub}`);
|
|
438
|
-
if (!sel && !hits) return e.text;
|
|
439
|
-
const fill = e.text.includes(FILL);
|
|
440
|
-
const base = fill ? LEAD.exec(e.text)?.[0] || '' : '';
|
|
441
|
-
const style = (col, off) => {
|
|
442
|
-
if (sel && col >= sel[0] && col < sel[1]) return '\x1b[7m';
|
|
443
|
-
const hit = hits?.find((h) => off >= h.from && off < h.to); // a match's place in the row's text (see scanFind)
|
|
444
|
-
return !hit ? base : hit.match === this.finding.matches[this.finding.index] ? FIND_CURRENT : FIND_MATCH;
|
|
445
|
-
};
|
|
446
|
-
let out = '';
|
|
447
|
-
let open = '';
|
|
448
|
-
let col = 0;
|
|
449
|
-
let off = 0;
|
|
450
|
-
for (const g of graphemes(stripAnsi(e.text))) {
|
|
451
|
-
const s = style(col, off);
|
|
452
|
-
if (s !== open) { out += '\x1b[0m' + s; open = s; }
|
|
453
|
-
out += g;
|
|
454
|
-
col += graphemeWidth(g);
|
|
455
|
-
off += g.length;
|
|
456
|
-
}
|
|
457
|
-
if (fill) return out + '\x1b[0m' + base + FILL;
|
|
458
|
-
return out + (open ? '\x1b[0m' : '');
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
// ------------------------------------------------------------ finding text (fullscreen, ctrl+f)
|
|
462
|
-
//
|
|
463
|
-
// `finding`: {query, matches, byRow, index, anchor, key}. Matches are newest first (from the bottom of the
|
|
464
|
-
// conversation up): {abs, offset, line, sub, col, fromEnd}, `abs` the line counted from the first line ever
|
|
465
|
-
// printed (so that it survives lines dropped from the top), `offset` where it starts in the line's text, and
|
|
466
|
-
// `fromEnd` the rows from its first row to the end of the conversation. `byRow` maps "line:sub" to the column
|
|
467
|
-
// ranges to highlight, {from, to, match}. A match may go on over the rows a long line was wrapped onto.
|
|
468
|
-
|
|
469
|
-
// Start finding: the first match shown is the nearest one above the bottom of the view as it is now.
|
|
470
|
-
openFind() {
|
|
471
|
-
this.takeStatic();
|
|
472
|
-
this.finding = { query: '', matches: [], byRow: new Map(), index: -1, anchor: this.scroll, cols: 0, dropped: -1, scanned: -1 };
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
closeFind() {
|
|
476
|
-
if (!this.finding) return;
|
|
477
|
-
this.finding = null;
|
|
478
|
-
this.draw();
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
// Look for `query` as typed, in any case; shows the first match (see openFind). {index, total}: index is 0 for
|
|
482
|
-
// the newest match.
|
|
483
|
-
setFind(query) {
|
|
484
|
-
const f = this.finding;
|
|
485
|
-
if (!f) return null;
|
|
486
|
-
f.query = query;
|
|
487
|
-
f.cols = 0; // all of it again
|
|
488
|
-
f.index = -1;
|
|
489
|
-
this.refreshFind();
|
|
490
|
-
if (f.matches.length) {
|
|
491
|
-
const at = f.matches.findIndex((m) => m.fromEnd >= f.anchor);
|
|
492
|
-
f.index = at >= 0 ? at : f.matches.length - 1;
|
|
493
|
-
this.revealMatch();
|
|
494
|
-
}
|
|
495
|
-
this.draw();
|
|
496
|
-
return this.findStatus();
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
// The next match up (`dir` 1: older) or down (-1: newer), round from either end.
|
|
500
|
-
findStep(dir) {
|
|
501
|
-
const f = this.finding;
|
|
502
|
-
if (!f) return null;
|
|
503
|
-
this.refreshFind();
|
|
504
|
-
const n = f.matches.length;
|
|
505
|
-
if (n) {
|
|
506
|
-
f.index = ((f.index < 0 ? 0 : f.index + dir) % n + n) % n;
|
|
507
|
-
this.revealMatch();
|
|
508
|
-
}
|
|
509
|
-
this.draw();
|
|
510
|
-
return this.findStatus();
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
findStatus() {
|
|
514
|
-
const f = this.finding;
|
|
515
|
-
if (!f) return null;
|
|
516
|
-
this.refreshFind();
|
|
517
|
-
return { query: f.query, index: f.index, total: f.matches.length };
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
// The matches again when the conversation or the width changed since they were found; the current one stays.
|
|
521
|
-
// Lines added at the end (output while the search is open) are searched on their own.
|
|
522
|
-
refreshFind() {
|
|
523
|
-
const f = this.finding;
|
|
524
|
-
const cols = this.width;
|
|
525
|
-
if (!f || (f.cols === cols && f.dropped === this.dropped && f.scanned === this.history.length)) return;
|
|
526
|
-
if (!f.query) {
|
|
527
|
-
Object.assign(f, { matches: [], byRow: new Map(), index: -1, cols, dropped: this.dropped, scanned: this.history.length });
|
|
528
|
-
return;
|
|
529
|
-
}
|
|
530
|
-
if (f.cols === cols && f.dropped === this.dropped && f.scanned < this.history.length) {
|
|
531
|
-
const added = this.scanFind(f, f.scanned, this.history.length, cols);
|
|
532
|
-
for (const m of f.matches) m.fromEnd += added.rows;
|
|
533
|
-
f.matches = [...added.matches, ...f.matches];
|
|
534
|
-
if (f.index >= 0) f.index += added.matches.length;
|
|
535
|
-
f.scanned = this.history.length;
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
const current = f.matches[f.index];
|
|
539
|
-
f.byRow = new Map();
|
|
540
|
-
f.matches = this.scanFind(f, 0, this.history.length, cols).matches;
|
|
541
|
-
Object.assign(f, { cols, dropped: this.dropped, scanned: this.history.length });
|
|
542
|
-
const same = current ? f.matches.findIndex((m) => m.abs === current.abs && m.offset === current.offset) : -1;
|
|
543
|
-
f.index = same >= 0 ? same : Math.min(f.index, f.matches.length - 1);
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
// The matches of f.query in history lines [from, to), newest first, entered in f.byRow; `rows`: the rows those
|
|
547
|
-
// lines take (fromEnd counts from the end of line to - 1). A match's place on a row is kept as offsets in the row's
|
|
548
|
-
// text, which paintRow counts as it draws: columns (stringWidth for every match) took most of the time of a key
|
|
549
|
-
// typed in the search, 130 ms over 20,000 lines. The rows' text without colours is kept with them (plainRows).
|
|
550
|
-
scanFind(f, from, to, cols) {
|
|
551
|
-
const re = new RegExp(f.query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'giu');
|
|
552
|
-
const matches = [];
|
|
553
|
-
let rowsAfter = 0;
|
|
554
|
-
for (let line = to - 1; line >= from; line--) {
|
|
555
|
-
const rows = this.wrappedAt(line, cols);
|
|
556
|
-
if (matches.length < FIND_MAX) {
|
|
557
|
-
rows.plain ??= plainRows(rows);
|
|
558
|
-
const { text, starts } = rows.plain;
|
|
559
|
-
// offset -> the row it falls on (`end`: an end offset belongs to the row before)
|
|
560
|
-
const rowOf = (offset, end = false) => {
|
|
561
|
-
let sub = starts.length - 1;
|
|
562
|
-
while (sub > 0 && (end ? starts[sub] >= offset : starts[sub] > offset)) sub--;
|
|
563
|
-
return sub;
|
|
564
|
-
};
|
|
565
|
-
const found = [];
|
|
566
|
-
for (const m of text.matchAll(re)) {
|
|
567
|
-
if (!m[0]) continue;
|
|
568
|
-
const end = m.index + m[0].length;
|
|
569
|
-
const a = rowOf(m.index);
|
|
570
|
-
const b = rowOf(end, true);
|
|
571
|
-
const match = { abs: this.dropped + line, offset: m.index, line, sub: a, fromEnd: rowsAfter + rows.length - 1 - a };
|
|
572
|
-
for (let sub = a; sub <= b; sub++) {
|
|
573
|
-
const k = `${line}:${sub}`;
|
|
574
|
-
if (!f.byRow.has(k)) f.byRow.set(k, []);
|
|
575
|
-
f.byRow.get(k).push({ from: sub === a ? m.index - starts[sub] : 0, to: sub === b ? end - starts[sub] : Infinity, match });
|
|
576
|
-
}
|
|
577
|
-
found.push(match);
|
|
578
|
-
}
|
|
579
|
-
matches.push(...found.reverse());
|
|
580
|
-
}
|
|
581
|
-
rowsAfter += rows.length;
|
|
582
|
-
}
|
|
583
|
-
return { matches, rows: rowsAfter };
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
// Scroll so that the current match is on screen: where it is if it is already in view, else in the middle.
|
|
587
|
-
revealMatch() {
|
|
588
|
-
const m = this.finding?.matches[this.finding.index];
|
|
589
|
-
if (!m || !this.full) return;
|
|
590
|
-
const area = Math.max(1, this.lastArea || this.height - 6);
|
|
591
|
-
const lowest = this.scroll + (this.scroll > 0 ? 1 : 0); // the "Jump to bottom" bar covers the last row
|
|
592
|
-
if (m.fromEnd >= lowest && m.fromEnd < this.scroll + area) return;
|
|
593
|
-
const max = Math.max(0, this.totalRows(this.width) - area);
|
|
594
|
-
this.scroll = Math.max(0, Math.min(max, m.fromEnd - Math.floor(area / 2)));
|
|
595
|
-
if (!this.scroll) this.unseen = 0;
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
// The text selected, as it was printed: rows a long line was wrapped onto are joined again.
|
|
599
|
-
selectedText() {
|
|
600
|
-
if (!this.selection) return '';
|
|
601
|
-
const cols = this.width;
|
|
602
|
-
const [a, b] = this.ordered();
|
|
603
|
-
const lines = [];
|
|
604
|
-
for (let line = a.line; line <= b.line && line < this.history.length; line++) {
|
|
605
|
-
const rows = this.wrappedAt(line, cols);
|
|
606
|
-
let text = '';
|
|
607
|
-
rows.forEach((row, sub) => {
|
|
608
|
-
const range = this.selectedCols({ line, sub });
|
|
609
|
-
if (range) text += sliceCols(stripAnsi(row), range[0], range[1]);
|
|
610
|
-
});
|
|
611
|
-
lines.push(text.replace(/\s+$/, ''));
|
|
612
|
-
}
|
|
613
|
-
return lines.join('\n');
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
// What is selected, to quote in a message (see App.quoteSelection): {text, source}. `text` without the indentation
|
|
617
|
-
// the conversation is drawn with (see dedent); `source` the line that starts the part of the conversation where the
|
|
618
|
-
// selection begins, a tool call, a reply or a message of the user (see HEADING), so that the model knows where the
|
|
619
|
-
// quote is from: '' when there is none, or when the quote starts with that line itself.
|
|
620
|
-
selectionQuote() {
|
|
621
|
-
if (!this.selection) return null;
|
|
622
|
-
const text = dedent(this.selectedText());
|
|
623
|
-
const [a] = this.ordered();
|
|
624
|
-
let source = '';
|
|
625
|
-
for (let i = Math.min(a.line, this.history.length - 1), n = 0; i >= 0 && n < SOURCE_REACH; i--, n++) {
|
|
626
|
-
const plain = stripAnsi(this.history[i]).trim();
|
|
627
|
-
if (HEADING.test(plain)) { source = plain; break; }
|
|
628
|
-
}
|
|
629
|
-
if (source && text.split('\n', 1)[0] === source) source = '';
|
|
630
|
-
if (source.length > 300) source = source.slice(0, 299) + '…';
|
|
631
|
-
return { text, source };
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
clearSelection() {
|
|
635
|
-
if (!this.selection) return false;
|
|
636
|
-
this.selection = null;
|
|
637
|
-
this.dragging = false;
|
|
638
|
-
this.draw();
|
|
639
|
-
return true;
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
// The cell of the input box's text at screen cell (x, y), 1-based: {row, col}, the row among the rows of the text
|
|
643
|
-
// and the column in it (negative left of it). null when (x, y) is not on a row of the text shown; `clamp` (a drag
|
|
644
|
-
// that left the box): the start of its first row on screen above it, the end of the text below it.
|
|
645
|
-
inputAt(x, y, clamp = false) {
|
|
646
|
-
const b = this.inputBox;
|
|
647
|
-
if (!b) return null;
|
|
648
|
-
const first = Math.max(0, this.lastArea - b.top); // rows the conversation covers (the box taller than the screen)
|
|
649
|
-
const row = y - 1 - b.top;
|
|
650
|
-
if (row >= first && row < b.rows) return { row, col: x - 1 - b.col };
|
|
651
|
-
if (!clamp) return null;
|
|
652
|
-
return row < first ? { row: first, col: -1 } : { row: b.rows - 1, col: Infinity };
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
// The point of the conversation at screen cell (x, y), 1-based: the nearest row that shows some of it.
|
|
656
|
-
pointAt(x, y) {
|
|
657
|
-
let i = Math.max(0, Math.min(this.lastArea - 1, y - 1));
|
|
658
|
-
while (i > 0 && !this.viewMap[i]) i--;
|
|
659
|
-
const e = this.viewMap[i];
|
|
660
|
-
return e ? { line: e.line, sub: e.sub, col: Math.max(0, x - 1) } : null;
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
// Word (`whole`: the whole line) around point `p`.
|
|
664
|
-
expand(p, whole) {
|
|
665
|
-
const rows = this.wrappedAt(p.line, this.width);
|
|
666
|
-
if (whole) return { anchor: { line: p.line, sub: 0, col: 0 }, focus: { line: p.line, sub: rows.length - 1, col: Infinity } };
|
|
667
|
-
const cells = [];
|
|
668
|
-
for (const g of graphemes(stripAnsi(rows[p.sub] || ''))) {
|
|
669
|
-
for (let k = 0; k < Math.max(1, graphemeWidth(g)); k++) cells.push(g);
|
|
670
|
-
}
|
|
671
|
-
const word = (g) => g !== undefined && !/\s/.test(g);
|
|
672
|
-
if (!word(cells[p.col])) return null;
|
|
673
|
-
let from = p.col;
|
|
674
|
-
let to = p.col;
|
|
675
|
-
while (word(cells[from - 1])) from--;
|
|
676
|
-
while (word(cells[to + 1])) to++;
|
|
677
|
-
return { anchor: { ...p, col: from }, focus: { ...p, col: to } };
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
// A mouse report (fullscreen). Mouse selection only highlights text; Ctrl+C copies it. Drag to select (the view
|
|
681
|
-
// scrolls at its edges), double-click a word, triple-click a line; a click clears the selection; a click on the
|
|
682
|
-
// "Jump to bottom" bar goes there. On the input box's text it returns {input: {kind, row, col, count}}, for the
|
|
683
|
-
// editor (Editor.mouse): a press there, then a drag (once for each cell the mouse moves to) and the release, from
|
|
684
|
-
// wherever the mouse is by then.
|
|
685
|
-
mouse({ button, x, y, release }) {
|
|
686
|
-
if (!this.full || this.closed) return null;
|
|
687
|
-
if (button & 64) return null; // the wheel: the caller scrolls
|
|
688
|
-
const left = (button & 3) === 0;
|
|
689
|
-
if (!left) return null;
|
|
690
|
-
const motion = Boolean(button & 32);
|
|
691
|
-
if (!motion && !release) { // a press
|
|
692
|
-
const now = Date.now();
|
|
693
|
-
const c = this.lastClick;
|
|
694
|
-
const count = now - c.at < MULTI_CLICK_MS && Math.abs(c.x - x) <= 1 && c.y === y ? c.count + 1 : 1;
|
|
695
|
-
this.lastClick = { at: now, x, y, count };
|
|
696
|
-
this.inputDrag = null;
|
|
697
|
-
this.dragFrom = null;
|
|
698
|
-
if (this.barRow !== null && y - 1 === this.barRow) { this.selection = null; this.scrollBy('bottom'); return null; }
|
|
699
|
-
const cell = this.inputAt(x, y);
|
|
700
|
-
if (cell) {
|
|
701
|
-
this.clearSelection();
|
|
702
|
-
this.dragging = false;
|
|
703
|
-
if (count === 1) this.inputDrag = { x, y, moved: false };
|
|
704
|
-
return { input: { kind: 'press', ...cell, count } };
|
|
705
|
-
}
|
|
706
|
-
if (y - 1 >= this.lastArea) { this.clearSelection(); return null; } // the input box's border, the footer
|
|
707
|
-
const p = this.pointAt(x, y);
|
|
708
|
-
if (!p) { this.clearSelection(); return null; }
|
|
709
|
-
if (count >= 2) {
|
|
710
|
-
this.selection = this.expand(p, count >= 3);
|
|
711
|
-
this.dragging = false;
|
|
712
|
-
this.draw();
|
|
713
|
-
return null;
|
|
714
|
-
}
|
|
715
|
-
// where a drag starts: the selection is made once the mouse moves, so that a click selects nothing (it showed
|
|
716
|
-
// a cell in reverse video while the button was down, and cleared the footer's message)
|
|
717
|
-
this.clearSelection();
|
|
718
|
-
this.dragFrom = p;
|
|
719
|
-
this.dragging = true;
|
|
720
|
-
return null;
|
|
721
|
-
}
|
|
722
|
-
const d = this.inputDrag;
|
|
723
|
-
if (d) {
|
|
724
|
-
if (motion && d.x === x && d.y === y) return null; // still the same cell
|
|
725
|
-
const cell = this.inputAt(x, y, true);
|
|
726
|
-
if (release) {
|
|
727
|
-
this.inputDrag = null;
|
|
728
|
-
if (d.moved) this.lastClick.count = 0; // a drag: the next click starts a count of its own
|
|
729
|
-
return cell && { input: { kind: 'release', ...cell } };
|
|
730
|
-
}
|
|
731
|
-
if (!motion) return null;
|
|
732
|
-
Object.assign(d, { x, y, moved: true });
|
|
733
|
-
return cell && { input: { kind: 'drag', ...cell } };
|
|
734
|
-
}
|
|
735
|
-
if (!this.dragging || !this.dragFrom) return null;
|
|
736
|
-
if (motion) {
|
|
737
|
-
// past the top or the bottom of the conversation: it scrolls, and the selection goes on with it
|
|
738
|
-
if (y <= 1) this.scrollBy(1);
|
|
739
|
-
else if (y - 1 >= this.lastArea - 1 && this.scroll > 0) this.scrollBy(-1);
|
|
740
|
-
const p = this.pointAt(x, y);
|
|
741
|
-
if (p) {
|
|
742
|
-
if (this.selection) this.selection.focus = p;
|
|
743
|
-
else this.selection = { anchor: { ...this.dragFrom }, focus: p };
|
|
744
|
-
}
|
|
745
|
-
this.draw();
|
|
746
|
-
return null;
|
|
747
|
-
}
|
|
748
|
-
// released
|
|
749
|
-
this.dragging = false;
|
|
750
|
-
this.dragFrom = null;
|
|
751
|
-
if (this.selection) this.lastClick.count = 0; // a drag: the next click starts a count of its own
|
|
752
|
-
return null;
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
// Scroll the conversation up (`rows` > 0) or down. 'top' and 'bottom' go to its ends.
|
|
756
|
-
scrollBy(rows) {
|
|
757
|
-
if (!this.full) return;
|
|
758
|
-
this.takeStatic();
|
|
759
|
-
const cols = this.width;
|
|
760
|
-
const max = Math.max(0, this.totalRows(cols) - this.lastArea);
|
|
761
|
-
const to = rows === 'top' ? max : rows === 'bottom' ? 0 : Math.max(0, Math.min(max, this.scroll + rows));
|
|
762
|
-
if (to === this.scroll) return;
|
|
763
|
-
this.scroll = to;
|
|
764
|
-
if (!to) this.unseen = 0;
|
|
765
|
-
this.draw();
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
get scrolledUp() { return this.full && this.scroll > 0; }
|
|
769
|
-
|
|
770
|
-
// The bar over the last row of the conversation while it is scrolled up.
|
|
771
|
-
jumpBar(cols) {
|
|
772
|
-
const label = ` Jump to bottom (ctrl+End) ↓${this.unseen ? ` · ${this.unseen} new line${this.unseen === 1 ? '' : 's'}` : ''} `;
|
|
773
|
-
const pad = Math.max(0, Math.floor((cols - 1 - stringWidth(label)) / 2));
|
|
774
|
-
return ' '.repeat(pad) + '\x1b[7m' + label + '\x1b[27m';
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
// While the user is scrolled up, keep the live region at the height it had when scrolling started. Streaming
|
|
778
|
-
// text otherwise grows and shrinks that region, moving every visible conversation row on every token. That is
|
|
779
|
-
// especially conspicuous through PowerShell/ConPTY, where the cursor moves in the terminal before the whole
|
|
780
|
-
// frame has been painted. The new output remains available behind the jump bar; returning to the bottom releases
|
|
781
|
-
// the lock and lays out the complete live region again.
|
|
782
|
-
keepScrolledLiveHeight(layout, rows) {
|
|
783
|
-
const current = layout.rows.length;
|
|
784
|
-
if (current < rows) {
|
|
785
|
-
const gap = rows - current;
|
|
786
|
-
layout.rows.unshift(...Array(gap).fill(''));
|
|
787
|
-
if (layout.cursorRow !== null) layout.cursorRow += gap;
|
|
788
|
-
if (layout.box) layout.box.top += gap;
|
|
789
|
-
return layout;
|
|
790
|
-
}
|
|
791
|
-
if (current > rows) {
|
|
792
|
-
const dropped = current - rows;
|
|
793
|
-
layout.rows.splice(0, dropped);
|
|
794
|
-
if (layout.box) layout.box.top -= dropped;
|
|
795
|
-
if (layout.cursorRow !== null) {
|
|
796
|
-
layout.cursorRow -= dropped;
|
|
797
|
-
if (layout.cursorRow < 0) layout.cursorRow = null;
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
return layout;
|
|
801
|
-
}
|
|
802
|
-
|
|
803
|
-
// Draw the screen: the conversation above, the live region on the last rows, the cursor in the input box.
|
|
804
|
-
draw({ full = false } = {}) {
|
|
805
|
-
if (this.closed || !this.full) return;
|
|
806
|
-
if (this.checkSize() || this.resizing) return;
|
|
807
|
-
if (this.scheduled) { clearTimeout(this.scheduled); this.scheduled = null; }
|
|
808
|
-
const cols = this.width;
|
|
809
|
-
const screenRows = Math.max(1, this.dimensions[1]);
|
|
810
|
-
if (!this.scroll) this.scrollLiveRows = null;
|
|
811
|
-
else if (full) this.scrollLiveRows = null; // a resize changes wrapping; capture the new live layout below
|
|
812
|
-
let layout = this.layoutLive(cols);
|
|
813
|
-
if (this.scroll > 0) {
|
|
814
|
-
if (this.scrollLiveRows === null) this.scrollLiveRows = Math.max(1, layout.rows.length);
|
|
815
|
-
layout = this.keepScrolledLiveHeight(layout, this.scrollLiveRows);
|
|
816
|
-
}
|
|
817
|
-
const area = Math.max(0, screenRows - layout.rows.length);
|
|
818
|
-
this.lastArea = area;
|
|
819
|
-
this.inputBox = layout.box && { ...layout.box, top: area + layout.box.top };
|
|
820
|
-
if (this.finding) this.refreshFind();
|
|
821
|
-
const shown = this.view(area, cols);
|
|
822
|
-
this.viewMap = shown;
|
|
823
|
-
const top = shown.map((e) => (e ? this.paintRow(e) : ''));
|
|
824
|
-
this.barRow = this.scroll > 0 && area > 0 ? area - 1 : null;
|
|
825
|
-
if (this.barRow !== null) top[this.barRow] = this.jumpBar(cols);
|
|
826
|
-
const frame = [...top, ...layout.rows];
|
|
827
|
-
let buf = SYNC_START + ESC.hideCursor + this.titleUpdate();
|
|
828
|
-
if (full) { buf += '\x1b[2J'; this.frame = []; }
|
|
829
|
-
frame.forEach((row, i) => {
|
|
830
|
-
if (this.frame[i] !== row) buf += `\x1b[${i + 1};1H` + endRow(row);
|
|
831
|
-
});
|
|
832
|
-
if (frame.length < this.frame.length) buf += `\x1b[${frame.length + 1};1H` + ESC.clearDown;
|
|
833
|
-
this.frame = frame;
|
|
834
|
-
if (layout.cursorRow !== null) buf += `\x1b[${area + layout.cursorRow + 1};${layout.cursorCol + 1}H` + ESC.showCursor;
|
|
835
|
-
buf += SYNC_END;
|
|
836
|
-
this.out.write(buf);
|
|
837
|
-
}
|
|
838
243
|
|
|
839
244
|
// The live region starts on screen row `row` (1-based, as the terminal reports it): from now on it fills the
|
|
840
245
|
// screen down to its last row.
|
|
@@ -858,7 +263,7 @@ export class Screen {
|
|
|
858
263
|
}
|
|
859
264
|
|
|
860
265
|
// As log(), drawn with the next frame (within 16 ms) instead of at once: for streamed text, where a line comes
|
|
861
|
-
// with nearly every delta
|
|
266
|
+
// with nearly every delta.
|
|
862
267
|
logSoon(...items) {
|
|
863
268
|
this.queueLog(items);
|
|
864
269
|
this.schedule();
|
|
@@ -882,36 +287,12 @@ export class Screen {
|
|
|
882
287
|
if (!this.staticQueue.length) return [];
|
|
883
288
|
const lines = this.staticQueue.join('\n').split(/\r?\n/).map(cleanLine);
|
|
884
289
|
this.staticQueue = [];
|
|
885
|
-
const first = this.history.length;
|
|
886
290
|
for (const line of lines) this.history.push(line.length > HISTORY_LINE_CHARS ? line.slice(0, HISTORY_LINE_CHARS) : line);
|
|
887
|
-
if (this.
|
|
888
|
-
// keep showing what is shown: the view moves up with the conversation
|
|
889
|
-
let rows = 0;
|
|
890
|
-
for (let i = first; i < this.history.length; i++) rows += this.wrappedAt(i, this.width).length;
|
|
891
|
-
this.scroll += rows;
|
|
892
|
-
this.unseen += rows;
|
|
893
|
-
}
|
|
894
|
-
const keep = this.full ? FULL_KEEP : HISTORY_KEEP;
|
|
895
|
-
if (this.history.length > keep * (this.full ? 1.25 : 2)) {
|
|
896
|
-
const drop = this.history.length - keep;
|
|
897
|
-
this.history.splice(0, drop);
|
|
898
|
-
this.wrapped.splice(0, drop);
|
|
899
|
-
this.dropped += drop;
|
|
900
|
-
if (this.selection) {
|
|
901
|
-
for (const p of [this.selection.anchor, this.selection.focus]) p.line -= drop;
|
|
902
|
-
if (this.selection.anchor.line < 0 || this.selection.focus.line < 0) this.selection = null;
|
|
903
|
-
}
|
|
904
|
-
if (this.dragFrom) {
|
|
905
|
-
this.dragFrom.line -= drop;
|
|
906
|
-
if (this.dragFrom.line < 0) this.dragFrom = null;
|
|
907
|
-
}
|
|
908
|
-
}
|
|
291
|
+
if (this.history.length > HISTORY_KEEP * 2) this.history.splice(0, this.history.length - HISTORY_KEEP);
|
|
909
292
|
return lines;
|
|
910
293
|
}
|
|
911
294
|
|
|
912
|
-
// The live region as rows (pre-wrapped to cols-1, capped to fit the viewport) and where the cursor goes.
|
|
913
|
-
// where the input box's text is among the rows, {top, rows, col}, from what renderLive says (`input`: its first
|
|
914
|
-
// line, how many, the column the text starts at); null when its rows did not stay one row each.
|
|
295
|
+
// The live region as rows (pre-wrapped to cols-1, capped to fit the viewport) and where the cursor goes.
|
|
915
296
|
layoutLive(cols) {
|
|
916
297
|
let live;
|
|
917
298
|
try {
|
|
@@ -919,12 +300,10 @@ export class Screen {
|
|
|
919
300
|
} catch (e) {
|
|
920
301
|
live = { lines: [`\x1b[31mrender error: ${e.message}\x1b[0m`] };
|
|
921
302
|
}
|
|
922
|
-
const { lines = [], cursor = null
|
|
303
|
+
const { lines = [], cursor = null } = live;
|
|
923
304
|
const rows = [];
|
|
924
|
-
const starts = []; // the row each line starts on
|
|
925
305
|
let cursorRow = null, cursorCol = 0;
|
|
926
306
|
lines.forEach((line, i) => {
|
|
927
|
-
starts.push(rows.length);
|
|
928
307
|
const wrapped = wrapLine(cleanLine(line), cols - 1);
|
|
929
308
|
if (cursor && cursor.row === i) {
|
|
930
309
|
const col = cursor.col;
|
|
@@ -934,18 +313,14 @@ export class Screen {
|
|
|
934
313
|
}
|
|
935
314
|
rows.push(...wrapped);
|
|
936
315
|
});
|
|
937
|
-
starts.push(rows.length);
|
|
938
|
-
const end = input ? starts[input.row + input.rows] : undefined;
|
|
939
|
-
const box = end !== undefined && end - starts[input.row] === input.rows ? { top: starts[input.row], rows: input.rows, col: input.col } : null;
|
|
940
316
|
// Cap to the terminal height so there is always a way back to the top.
|
|
941
317
|
const maxRows = Math.max(1, this.dimensions[1] - 1);
|
|
942
318
|
if (rows.length > maxRows) {
|
|
943
319
|
const dropped = rows.length - maxRows;
|
|
944
320
|
rows.splice(0, dropped);
|
|
945
321
|
if (cursorRow !== null) cursorRow = Math.max(0, cursorRow - dropped);
|
|
946
|
-
if (box) box.top -= dropped;
|
|
947
322
|
}
|
|
948
|
-
return { rows, cursorRow, cursorCol
|
|
323
|
+
return { rows, cursorRow, cursorCol };
|
|
949
324
|
}
|
|
950
325
|
|
|
951
326
|
// Escape sequence that leaves the cursor where the user types, and the bookkeeping for the next redraw.
|
|
@@ -987,43 +362,37 @@ export class Screen {
|
|
|
987
362
|
repaint() {
|
|
988
363
|
if (this.closed) return;
|
|
989
364
|
this.dimensions = this.readSize();
|
|
990
|
-
if (this.full) {
|
|
991
|
-
clearTimeout(this.resizeTimer);
|
|
992
|
-
this.resizeTimer = null;
|
|
993
|
-
this.resizeSince = 0;
|
|
994
|
-
this.takeStatic();
|
|
995
|
-
this.draw({ full: true });
|
|
996
|
-
return;
|
|
997
|
-
}
|
|
998
365
|
if (this.scheduled) { clearTimeout(this.scheduled); this.scheduled = null; }
|
|
999
366
|
clearTimeout(this.resizeTimer);
|
|
1000
367
|
this.resizeTimer = null;
|
|
1001
368
|
this.resizeSince = 0;
|
|
1002
369
|
const cols = this.width;
|
|
1003
370
|
const screenRows = Math.max(1, this.dimensions[1]);
|
|
1004
|
-
this.takeStatic();
|
|
1005
371
|
const layout = this.layoutLive(cols);
|
|
372
|
+
if (!layout.rows.length) layout.rows.push(''); // leave the cursor below output even if nothing live is drawn
|
|
1006
373
|
const room = Math.max(0, screenRows - layout.rows.length);
|
|
1007
374
|
const tail = [];
|
|
1008
|
-
|
|
375
|
+
let used = 0;
|
|
376
|
+
for (let i = this.history.length - 1; i >= 0; i--) {
|
|
1009
377
|
const n = rowsOf(this.history[i], cols);
|
|
1010
378
|
if (used + n > room) break;
|
|
1011
379
|
tail.unshift(this.history[i]);
|
|
1012
380
|
used += n;
|
|
1013
381
|
}
|
|
1014
|
-
//
|
|
1015
|
-
//
|
|
382
|
+
// The history tail has already reached the terminal. New output hasn't: write every queued line, letting it
|
|
383
|
+
// scroll off the viewport normally if there is more than fits, and keep the untruncated text for this write.
|
|
384
|
+
const pending = this.takeStatic();
|
|
1016
385
|
if (this.pin) {
|
|
1017
|
-
|
|
386
|
+
used += pending.reduce((n, line) => n + rowsOf(line, cols), 0);
|
|
1018
387
|
this.pinned = screenRows;
|
|
1019
388
|
const gap = this.gapFor(used, layout.rows.length);
|
|
1020
389
|
layout.rows.unshift(...Array(gap).fill(''));
|
|
1021
390
|
if (layout.cursorRow !== null) layout.cursorRow += gap;
|
|
1022
391
|
}
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
buf
|
|
1026
|
-
buf +=
|
|
392
|
+
// Erase the reflowed viewport before writing, without clearing scrollback or erasing the new text afterward.
|
|
393
|
+
const all = [...tail.map(endRow), ...pending.map((line) => line + '\x1b[0m'), ...layout.rows.map(endRow)];
|
|
394
|
+
let buf = SYNC_START + ESC.hideCursor + this.titleUpdate() + '\x1b[H' + ESC.clearDown;
|
|
395
|
+
buf += all.join('\r\n');
|
|
1027
396
|
buf += this.settleCursor(layout);
|
|
1028
397
|
buf += SYNC_END;
|
|
1029
398
|
this.out.write(buf);
|
|
@@ -1033,7 +402,6 @@ export class Screen {
|
|
|
1033
402
|
flush() {
|
|
1034
403
|
if (this.closed) return;
|
|
1035
404
|
if (this.checkSize() || this.resizing) return; // the repaint after the resize writes everything that is waiting
|
|
1036
|
-
if (this.full) { this.takeStatic(); this.draw(); return; }
|
|
1037
405
|
if (this.scheduled) { clearTimeout(this.scheduled); this.scheduled = null; }
|
|
1038
406
|
const cols = this.width;
|
|
1039
407
|
let buf = SYNC_START + ESC.hideCursor + this.titleUpdate() + '\r' + ESC.up(this.cursorRow) + ESC.clearDown;
|
|
@@ -1066,17 +434,6 @@ export class Screen {
|
|
|
1066
434
|
}
|
|
1067
435
|
|
|
1068
436
|
clearScreen() {
|
|
1069
|
-
if (this.full) {
|
|
1070
|
-
this.dropped += this.history.length;
|
|
1071
|
-
this.history = [];
|
|
1072
|
-
this.wrapped = [];
|
|
1073
|
-
this.selection = null;
|
|
1074
|
-
this.scroll = 0;
|
|
1075
|
-
this.unseen = 0;
|
|
1076
|
-
this.staticQueue = [];
|
|
1077
|
-
this.draw({ full: true });
|
|
1078
|
-
return;
|
|
1079
|
-
}
|
|
1080
437
|
this.out.write(ESC.clearScreen);
|
|
1081
438
|
this.cursorRow = 0;
|
|
1082
439
|
this.history = [];
|
|
@@ -1086,22 +443,14 @@ export class Screen {
|
|
|
1086
443
|
|
|
1087
444
|
// Print any pending output, then remove the live region (input box etc.) for good.
|
|
1088
445
|
// `keepTitle`: another process takes the terminal over at once (a restart), so the tab keeps its title.
|
|
1089
|
-
// `after`: lines to leave on the terminal (the exit's usage and resume hint).
|
|
1090
|
-
// the alternate screen is gone: logged before, they were drawn on it and vanished with it.
|
|
446
|
+
// `after`: lines to leave on the terminal (the exit's usage and resume hint).
|
|
1091
447
|
close({ keepTitle = false, after = [] } = {}) {
|
|
1092
448
|
if (this.closed) return;
|
|
1093
449
|
if (this.resizing) this.repaint(); // the cursor is not where the last draw left it: put things in order first
|
|
1094
450
|
this.renderLive = () => ({ lines: [] });
|
|
1095
451
|
this.pinned = null; // no blank rows left behind: the shell, or the next process, goes on right below the output
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
this.full = false;
|
|
1099
|
-
this.out.write(ALT_OFF); // the shell's screen comes back as it was
|
|
1100
|
-
if (after.length) this.out.write(`${after.map((l) => `${l}\x1b[0m`).join('\r\n')}\r\n`);
|
|
1101
|
-
} else {
|
|
1102
|
-
this.staticQueue.push(...after);
|
|
1103
|
-
this.flush();
|
|
1104
|
-
}
|
|
452
|
+
this.staticQueue.push(...after);
|
|
453
|
+
this.flush();
|
|
1105
454
|
this.closed = true;
|
|
1106
455
|
clearInterval(this.sizeTimer);
|
|
1107
456
|
clearTimeout(this.resizeTimer);
|
|
@@ -1115,3 +464,27 @@ export class Screen {
|
|
|
1115
464
|
this.out.write(restore + ESC.showCursor);
|
|
1116
465
|
}
|
|
1117
466
|
}
|
|
467
|
+
|
|
468
|
+
// A session with no terminal (one the web console runs, see web/host.js): what it logs is only kept (the history,
|
|
469
|
+
// carried over by a restart) and passed to onLog (the live view); nothing is drawn.
|
|
470
|
+
export class HeadlessScreen extends Screen {
|
|
471
|
+
constructor(out) {
|
|
472
|
+
super(out, { pin: false });
|
|
473
|
+
this.dimensions = [out.columns || 100, out.rows || 40];
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
readSize() { return this.dimensions || [this.out.columns || 100, this.out.rows || 40]; }
|
|
477
|
+
checkSize() { return false; }
|
|
478
|
+
onResize() {}
|
|
479
|
+
setTitle(provider) { this.titleProvider = provider; }
|
|
480
|
+
schedule() { this.flush(); }
|
|
481
|
+
flush() { if (!this.closed && this.staticQueue.length) this.takeStatic(); }
|
|
482
|
+
repaint() { this.flush(); }
|
|
483
|
+
clearLive() {}
|
|
484
|
+
clearScreen() { this.history = []; this.staticQueue = []; }
|
|
485
|
+
close() {
|
|
486
|
+
if (this.closed) return;
|
|
487
|
+
this.takeStatic();
|
|
488
|
+
this.closed = true;
|
|
489
|
+
}
|
|
490
|
+
}
|