mobux 0.40.3

A touch-friendly tmux web UI for unhinged people who run terminal sessions from their phone while walking the dog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
// The engine's one text buffer (issue #315). Two headless xterm.js terminals,
// each fed from exactly one source and parsed once:
//
//   screen   the tmux client screen, fed by the WebSocket stream. The
//            alternate screen is allowed; tmux's client lives on it, so it
//            keeps no scrollback. Its last row is the tmux status line.
//   history  the pane's tmux history above the visible screen, one entry
//            per logical line, fed by capture-pane (scope=history). Syncs
//            line a captured tail (or, failing that, the whole history) up
//            with what is held and append what is new. Lines tmux drops off
//            its top are kept until the display's row budget is spent.
//
// xterm draws this buffer through terminal-redraw.js, sterk through
// terminal-screen-source.js; the reader reads it through terminal-document.js.

const SCREEN_SCROLLBACK = 1000;

const MIRRORED_MODES = [1, 25, 2004];

// Mouse modes tmux asks of its client (`mouse on`). They are the format a
// wheel event sent to tmux must take; tmux forwards it to the pane in the
// pane app's own format, or scrolls copy-mode.
const MOUSE_TRACKING_MODES = new Set([1000, 1002, 1003]);
const MOUSE_SGR_MODE = 1006;

// Synchronized output: the app asks for nothing to be drawn until it ends.
const SYNC_MODE = 2026;

const ESC_RE = /\x1b(?:\[[0-?]*[ -/]*[@-~]|\][^\x07\x1b]*(?:\x07|\x1b\\)|.)/g;

const DEFAULT_STYLE = {
  bold: false,
  dim: false,
  italic: false,
  underline: false,
  blink: false,
  inverse: false,
  invisible: false,
  strike: false,
  fg: null,
  bg: null,
};

const FLAG_ON = {
  1: "bold",
  2: "dim",
  3: "italic",
  4: "underline",
  5: "blink",
  7: "inverse",
  8: "invisible",
  9: "strike",
};
const FLAG_OFF = {
  22: ["bold", "dim"],
  23: ["italic"],
  24: ["underline"],
  25: ["blink"],
  27: ["inverse"],
  28: ["invisible"],
  29: ["strike"],
};
const FLAG_SGR = [
  ["bold", 1],
  ["dim", 2],
  ["italic", 3],
  ["underline", 4],
  ["blink", 5],
  ["inverse", 7],
  ["invisible", 8],
  ["strike", 9],
];

// Extended colour: `38;5;n` / `38;2;r;g;b` (and 48/58). Returns the colour
// text and how many params it used.
function extendedColour(params, i) {
  if (params[i + 1] === "5") return [`5;${params[i + 2]}`, 3];
  if (params[i + 1] === "2") {
    return [`2;${params[i + 2]};${params[i + 3]};${params[i + 4]}`, 5];
  }
  return [null, 1];
}

function applySgr(style, body) {
  const params = body.replace(/:/g, ";").split(";");
  let next = { ...style };
  for (let i = 0; i < params.length;) {
    const p = params[i] === "" ? 0 : Number(params[i]);
    if (p === 0) next = { ...DEFAULT_STYLE };
    else if (FLAG_ON[p]) next[FLAG_ON[p]] = true;
    else if (FLAG_OFF[p]) for (const k of FLAG_OFF[p]) next[k] = false;
    else if ((p >= 30 && p <= 37) || (p >= 90 && p <= 97)) next.fg = `${p}`;
    else if (p === 39) next.fg = null;
    else if ((p >= 40 && p <= 47) || (p >= 100 && p <= 107)) next.bg = `${p}`;
    else if (p === 49) next.bg = null;
    else if (p === 38 || p === 48 || p === 58) {
      const [colour, used] = extendedColour(params, i);
      if (colour && p === 38) next.fg = `38;${colour}`;
      if (colour && p === 48) next.bg = `48;${colour}`;
      i += used;
      continue;
    }
    i += 1;
  }
  return next;
}

function styleSgr(style) {
  const p = ["0"];
  for (const [k, code] of FLAG_SGR) if (style[k]) p.push(code);
  if (style.fg) p.push(style.fg);
  if (style.bg) p.push(style.bg);
  return `\x1b[${p.join(";")}m`;
}

// capture-pane -e carries SGR state from one line into the next, so a line
// reads differently depending on where a capture starts. Rewrite every line
// to state its own style from the default: equal lines compare equal
// wherever the capture began, and each line parses on its own.
export function normalizeCapture(lines) {
  let style = { ...DEFAULT_STYLE };
  return lines.map((line) => {
    let out = "";
    let emitted = styleSgr(DEFAULT_STYLE);
    let last = 0;
    const text = (s) => {
      if (!s) return;
      const want = styleSgr(style);
      if (want !== emitted) {
        out += want;
        emitted = want;
      }
      out += s;
    };
    for (const m of line.matchAll(ESC_RE)) {
      text(line.slice(last, m.index));
      last = m.index + m[0].length;
      if (m[0].startsWith("\x1b[") && m[0].endsWith("m")) {
        style = applySgr(style, m[0].slice(2, -1));
      } else {
        out += m[0];
      }
    }
    text(line.slice(last));
    return out;
  });
}

export function splitCapture(text) {
  if (!text) return [];
  const lines = text.split("\n");
  if (lines[lines.length - 1] === "") lines.pop();
  return lines;
}

// A held line matches its captured counterpart when equal. The last held
// line also matches the start of it when it straddled the top of the
// screen (`grows`): such a line is captured only up to there, and whole
// once it has scrolled off.
function suffixMatches(held, next, from, count, grows) {
  for (let j = 0; j < count; j++) {
    const a = held[from + j];
    const b = next[j];
    if (a === b) continue;
    if (grows && from + j === held.length - 1 && b.startsWith(a)) continue;
    return false;
  }
  return true;
}

// How a whole captured history lines up with the one held. tmux appends
// lines, drops a tenth of its limit off the top once it reaches it, and
// moves lines back onto the screen when the pane grows. So the capture is
// the held history from line `drop` on — its last line possibly grown —
// followed by new lines; or, after the pane grew, only its next `keep`
// lines. Null when nothing lines up.
export function alignHistory(held, next, grows = false) {
  const L = held.length;
  if (L === 0) return { drop: 0, keep: 0, extended: false };
  for (let drop = 0; drop < L; drop++) {
    const keep = L - drop;
    if (keep > next.length) continue;
    if (suffixMatches(held, next, drop, keep, grows)) {
      return { drop, keep, extended: next[keep - 1] !== held[L - 1] };
    }
  }
  for (let drop = 0; drop < L; drop++) {
    const keep = Math.min(L - drop, next.length);
    if (keep === 0 || keep === L - drop) continue;
    let match = true;
    for (let j = 0; j < keep && match; j++) match = held[drop + j] === next[j];
    if (match) return { drop, keep, extended: false };
  }
  return null;
}

// How a captured tail (the last lines of tmux's history) continues the
// held history: it must overlap the held end on exactly one run of at
// least `minOverlap` lines. Anything else — no overlap, more new lines
// than the tail can show, or repeating output that fits several ways — is
// null, and the caller fetches the whole history instead.
export function alignTail(held, tail, minOverlap, grows = false) {
  const L = held.length;
  if (L === 0) return { overlap: 0, extended: false };
  let found = null;
  for (let o = Math.min(L, tail.length); o > 0; o--) {
    if (!suffixMatches(held, tail, L - o, o, grows)) continue;
    if (found !== null) return null;
    found = o;
  }
  if (found === null || found < Math.min(minOverlap, L)) return null;
  return { overlap: found, extended: tail[found - 1] !== held[L - 1] };
}

export function isBlankCell(cell) {
  const ch = cell.getChars();
  return (
    (ch === "" || ch === " ") &&
    cell.isBgDefault() &&
    !cell.isInverse() &&
    !cell.isUnderline() &&
    !cell.isStrikethrough?.()
  );
}

// A wrapped flag outlives the text that set it (a row rewritten shorter
// after an autowrap keeps it), so a row only continues a line whose last
// column still holds text.
function continuesLine(prev, row) {
  if (!row?.isWrapped || !prev) return false;
  const cell = prev.getCell(prev.length - 1);
  return !!cell && (cell.getWidth() === 0 || !isBlankCell(cell));
}

// Rows grouped into logical lines.
export function logicalLines(rows) {
  const lines = [];
  for (const row of rows) {
    if (lines.length && continuesLine(lines.at(-1).at(-1), row)) {
      lines.at(-1).push(row);
    } else {
      lines.push([row]);
    }
  }
  return lines;
}

function write(term, data) {
  return new Promise((resolve) => term.write(data, resolve));
}

const historyText = (lines) => lines.map((l) => `\x1b[0m${l}\r\n`).join("");

// Upper bound on a line's cell width.
function lineWidth(line) {
  let width = 0;
  for (const ch of line.replace(ESC_RE, "")) {
    width += ch.codePointAt(0) >= 0x1100 ? 2 : 1;
  }
  return width;
}

// Upper bound on the display rows a line takes at `cols` (a wide character
// that does not fit a row's end moves to the next one).
export const plainText = (line) => line.replace(ESC_RE, "");

export function displayRowsOf(line, cols) {
  return Math.max(1, Math.ceil(lineWidth(line) / Math.max(1, cols - 1)));
}

// `scrollback` is the display's: history and normal-screen scrollback
// rows together never exceed it, so the display never trims on its own.
export function createTerminalBuffer({ cols, rows, scrollback }) {
  const Headless = window.XtermHeadless;
  if (!Headless || !Headless.Terminal) {
    throw new Error(
      "xterm headless bundle not loaded — check vendor/xterm-headless.bundle.js",
    );
  }
  const screenLimit = Math.min(SCREEN_SCROLLBACK, Math.floor(scrollback / 10));
  const historyBudget = scrollback - screenLimit;
  const newTerminal = (options) =>
    new Headless.Terminal({ allowProposedApi: true, ...options });

  const screen = newTerminal({ cols, rows, scrollback: screenLimit });

  const modes = new Map(MIRRORED_MODES.map((m) => [m, m === 25]));
  const mouseModes = new Map();
  const scalar = (p) => (Array.isArray(p) ? p[0] : p);
  // A screen switch reports a scroll of its own; it moves no rows.
  let switching = false;
  const SCREEN_SWITCHES = new Set([47, 1047, 1049]);
  let syncSince = null;
  const subs = ["h", "l"].map((final) =>
    screen.parser.registerCsiHandler({ prefix: "?", final }, (params) => {
      for (const m of params.map(scalar)) {
        if (modes.has(m)) modes.set(m, final === "h");
        if (MOUSE_TRACKING_MODES.has(m) || m === MOUSE_SGR_MODE) {
          mouseModes.set(m, final === "h");
        }
        if (SCREEN_SWITCHES.has(m)) switching = true;
        if (m === SYNC_MODE) {
          if (final === "l") syncSince = null;
          else syncSince ??= performance.now();
        }
      }
      return false;
    }),
  );

  // tmux's status lines take the top or bottom rows of its client screen;
  // the rest is the pane.
  let statusLines = 1;
  let statusTop = false;
  const paneRows = () => {
    const first = statusTop ? statusLines : 0;
    return { first, last: Math.max(first, first + rows - statusLines - 1) };
  };

  // Rows that left the top of the pane since the last sync: tmux scrolls
  // its client with a linefeed at the bottom of a scroll region that starts
  // at the pane's top row, or with scroll-up. Those rows are the pane lines
  // entering tmux's history. Each is kept as whether it ended its line: a
  // row whose line goes on in the next row moves no line key.
  let scrolled = [];
  const scrolledLines = () => scrolled.filter(Boolean).length;
  const paneRow = (y) => {
    const buf = screen.buffer.active;
    return buf.getLine(buf.baseY + paneRows().first + y);
  };
  let regionTop = 1;
  // Rows the normal screen dropped off the top of its full scrollback: a
  // scroll of a region that starts at the top row, with the scrollback full
  // before and after it.
  let screenTrimmed = 0;
  let normalBase = 0;
  const countTrim = () => {
    const normal = screen.buffer.normal;
    if (screen.buffer.active.type !== "normal") return;
    const full = normal.baseY === screenLimit && normalBase === screenLimit;
    if (full && regionTop === 1) screenTrimmed++;
    normalBase = normal.baseY;
  };
  const scrollsHistory = () =>
    regionTop === paneRows().first + 1 &&
    screen.buffer.active.type === "alternate";
  subs.push(
    screen.parser.registerCsiHandler({ final: "r" }, (params) => {
      regionTop = scalar(params[0]) || 1;
      return false;
    }),
    screen.parser.registerCsiHandler({ final: "S" }, (params) => {
      if (!scrollsHistory()) return false;
      const n = scalar(params[0]) || 1;
      for (let y = 0; y < n; y++) {
        scrolled.push(!continuesLine(paneRow(y), paneRow(y + 1)));
      }
      return false;
    }),
    screen.onScroll(() => {
      if (switching) {
        switching = false;
        return;
      }
      if (scrollsHistory()) scrolled.push(!paneRow(0)?.isWrapped);
      countTrim();
    }),
    screen.onWriteParsed(() => {
      switching = false;
      changed();
    }),
  );

  // Two rows, so the last line stays on screen and can be rewritten when it
  // grows; everything above it is scrollback.
  const newHistory = (width) =>
    newTerminal({ cols: width, rows: 2, scrollback: historyBudget + 10 });
  let history = newHistory(cols);
  let historyCols = cols;
  let historyLines = [];
  // The key of the first history line: keys stay with their line.
  let historyStart = 0;
  let lastContinues = false;
  // Screen row 0 as it was when a capture said the last line continues into
  // it: once the screen has scrolled past it, it no longer does.
  let straddleRow = null;
  let lastRevision = 0;
  // Bumped each time the history is rebuilt other than by dropping lines
  // off its top.
  let historyEpoch = 0;
  let ops = Promise.resolve();
  const changeSubs = new Set();

  function changed() {
    for (const cb of [...changeSubs]) cb();
  }

  function queue(op) {
    const run = ops.then(op).then((result) => {
      changed();
      return result;
    });
    ops = run.catch(() => {});
    return run;
  }

  // Build the history terminal aside and swap it in once parsed, so a draw
  // never sees a half-parsed history.
  async function rebuild(lines, start, trim = false) {
    const width = lines.reduce((w, l) => Math.max(w, lineWidth(l)), cols);
    const next = newHistory(width);
    if (lines.length) await write(next, historyText(lines));
    history.dispose();
    history = next;
    historyCols = width;
    historyLines = lines;
    historyStart = start;
    if (!trim) historyEpoch++;
  }

  function widen(lines) {
    const width = lines.reduce((w, l) => Math.max(w, lineWidth(l)), 0);
    if (width <= historyCols) return;
    historyCols = width;
    history.resize(historyCols, 2);
  }

  async function extendLast(line) {
    widen([line]);
    await write(history, `\x1b[A\r\x1b[2K\x1b[0m${line}\r\n`);
    historyLines = historyLines.slice(0, -1).concat([line]);
    lastRevision++;
  }

  async function append(fresh) {
    if (!fresh.length) return;
    widen(fresh);
    await write(history, historyText(fresh));
    historyLines = historyLines.concat(fresh);
  }

  // Past the row budget, drop lines off the top down to two thirds of it,
  // so the display redraws once per third of a budget, not per line.
  async function cap() {
    const rowsOf = historyLines.map((l) => displayRowsOf(l, cols));
    let total = rowsOf.reduce((a, b) => a + b, 0);
    if (total <= historyBudget) return;
    let drop = 0;
    while (total > (historyBudget * 2) / 3) total -= rowsOf[drop++];
    await rebuild(historyLines.slice(drop), historyStart + drop, true);
  }

  const end = () => historyStart + historyLines.length;

  async function syncWhole(raw, continues) {
    const next = normalizeCapture(raw);
    const before = end();
    const aligned = alignHistory(historyLines, next, lastContinues);
    let result = { replaced: false, before };
    if (!aligned) {
      await rebuild(next, before);
      result = { replaced: true, before };
    } else if (aligned.keep === historyLines.length - aligned.drop) {
      const segments = grownSegments(
        aligned.extended ? next[aligned.keep - 1] : null,
        next.slice(aligned.keep),
      );
      if (aligned.extended) await extendLast(next[aligned.keep - 1]);
      await append(next.slice(aligned.keep));
      result = { replaced: false, before, segments };
    } else {
      const kept = historyLines.slice(0, aligned.drop);
      await rebuild(kept.concat(next), historyStart);
    }
    setContinues(continues);
    await cap();
    return { ...result, after: end() };
  }

  async function syncTail(raw, continues, minOverlap) {
    const tail = normalizeCapture(raw);
    const aligned = alignTail(historyLines, tail, minOverlap, lastContinues);
    if (!aligned) return null;
    const before = end();
    const segments = grownSegments(
      aligned.extended ? tail[aligned.overlap - 1] : null,
      tail.slice(aligned.overlap),
    );
    if (aligned.extended) await extendLast(tail[aligned.overlap - 1]);
    await append(tail.slice(aligned.overlap));
    setContinues(continues);
    await cap();
    return { replaced: false, before, after: end(), segments };
  }

  // What a sync added to history, as the screen rows it scrolled off: the
  // rows a grown last line gained, then each new line's rows at the pane
  // width.
  function grownSegments(grownLast, fresh) {
    const segments = [];
    const rowsOf = (line) => displayRowsOf(line, cols);
    if (grownLast !== null) {
      const held = historyLines[historyLines.length - 1];
      segments.push({
        key: end() - 1,
        rows: rowsOf(grownLast) - rowsOf(held),
        text: plainText(grownLast),
      });
    }
    fresh.forEach((line, i) => {
      segments.push({
        key: end() + i,
        rows: rowsOf(line),
        text: plainText(line),
      });
    });
    return segments;
  }

  function viewportRows() {
    const buf = screen.buffer.active;
    const out = [];
    for (let r = 0; r < rows; r++) out.push(buf.getLine(buf.baseY + r));
    return out;
  }

  function scrollbackRows(from = 0) {
    const normal = screen.buffer.normal;
    const out = [];
    for (let i = from; i < normal.baseY; i++) out.push(normal.getLine(i));
    return out;
  }

  function statusRows() {
    const { first, last } = paneRows();
    return viewportRows().filter((_, r) => r < first || r > last);
  }

  // The pane's lines, each { rows, row } with `row` counted from the pane's
  // top.
  function paneLines() {
    const { first, last } = paneRows();
    let row = 0;
    return logicalLines(viewportRows().slice(first, last + 1)).map((line) => {
      const out = { rows: line, row };
      row += line.length;
      return out;
    });
  }

  const firstRowText = () =>
    viewportRows()[paneRows().first]?.translateToString(false) ?? "";

  function setContinues(continues) {
    lastContinues = continues;
    straddleRow = continues ? firstRowText() : null;
  }

  // The pane's first line is the rest of the last history line when that
  // one straddles the top of the pane.
  const straddles = () =>
    lastContinues &&
    historyLines.length > 0 &&
    screen.buffer.normal.baseY === 0 &&
    firstRowText() === straddleRow;

  return {
    get cols() {
      return cols;
    },
    get rows() {
      return rows;
    },

    writeScreen(text) {
      return write(screen, text);
    },
    resize(nextCols, nextRows) {
      cols = nextCols;
      rows = nextRows;
      screen.resize(cols, rows);
      regionTop = 1;
      queue(cap);
    },

    // How many status lines tmux draws, and whether at the top.
    setStatus(lines, position) {
      const top = position === "top";
      if (lines === statusLines && top === statusTop) return;
      statusLines = lines;
      statusTop = top;
      changed();
    },
    paneRows,
    statusRows,

    // Fires after the screen parses a write, after a history change and
    // when the status lines move.
    onChange(cb) {
      changeSubs.add(cb);
      return { dispose: () => changeSubs.delete(cb) };
    },

    isAlternate() {
      return screen.buffer.active.type === "alternate";
    },
    onBell(cb) {
      return screen.onBell(cb);
    },
    onData(cb) {
      return screen.onData(cb);
    },
    registerOscHandler(id, cb) {
      return screen.parser.registerOscHandler(id, cb);
    },
    modes() {
      return modes;
    },
    synchronizedFor() {
      return syncSince === null ? null : performance.now() - syncSince;
    },
    mouse() {
      const tracking = [...MOUSE_TRACKING_MODES].some((m) => mouseModes.get(m));
      return { tracking, sgr: mouseModes.get(MOUSE_SGR_MODE) === true };
    },

    historyRowCount() {
      return historyLines.length;
    },
    historyRow(i) {
      return history.buffer.active.getLine(i);
    },
    historyStart() {
      return historyStart;
    },
    historyEpoch() {
      return historyEpoch;
    },
    screenTrimmed() {
      return screenTrimmed;
    },
    // Bumped each time the last history line is rewritten because it grew.
    lastLineRevision() {
      return lastRevision;
    },
    straddles,
    screenScrollbackCount() {
      return screen.buffer.normal.baseY;
    },
    scrollbackRows,
    viewportRows,
    cursor() {
      const buf = screen.buffer.active;
      return { x: buf.cursorX, y: buf.cursorY };
    },
    // Line keys: history lines from `historyStart`, then the normal
    // screen's scrollback lines, then the pane's lines — the first of which
    // shares the last history line's key while it straddles. Status lines
    // have no key.
    screenKeyBase() {
      const base =
        end() + logicalLines(scrollbackRows()).length + scrolledLines();
      return straddles() ? base - 1 : base;
    },
    scrolledRows() {
      return scrolled.length;
    },
    consumeScrolledRows(n) {
      scrolled = scrolled.slice(n);
    },
    // The key of the line pane row `r` belongs to, or null.
    rowKey(r) {
      const base = this.screenKeyBase();
      const lines = paneLines();
      const index = lines.findIndex(
        (l) => r >= l.row && r < l.row + l.rows.length,
      );
      return index === -1 ? null : base + index;
    },
    // The pane's lines as { key, row, rows, text }.
    screenLines() {
      const base = this.screenKeyBase();
      return paneLines().map((line, index) => ({
        key: base + index,
        row: line.row,
        rows: line.rows,
        text: line.rows.map((l) => l?.translateToString(true) ?? "").join(""),
      }));
    },
    historyText(i) {
      return plainText(historyLines[i] ?? "");
    },
    // Where the cursor is, for a marker: its line key now, its pane row
    // counted from the rows scrolled off since the last sync, and the
    // pane's lines then with the cursor's among them.
    cursorPlace() {
      const y = screen.buffer.active.cursorY - paneRows().first;
      const lines = this.screenLines();
      let lineIndex = 0;
      while (lineIndex + 1 < lines.length && lines[lineIndex + 1].row <= y) {
        lineIndex++;
      }
      return {
        key: this.cursorLineKey(),
        vrow: scrolled.length + y,
        snapshot: lines.map((l) => l.text),
        lineIndex,
      };
    },
    cursorLineKey() {
      const y = screen.buffer.active.cursorY - paneRows().first;
      const lines = paneLines();
      let index = 0;
      while (index + 1 < lines.length && lines[index + 1].row <= y) index++;
      return this.screenKeyBase() + index;
    },

    // A whole capture. Lines that line up keep their key; with nothing
    // lined up (history cleared) the old keys are gone and the screen's
    // move from `before` to `after`.
    syncWhole(raw, continues) {
      return queue(() => syncWhole(raw, continues));
    },
    // A captured tail; null when it cannot be placed.
    syncTail(raw, continues, minOverlap) {
      return queue(() => syncTail(raw, continues, minOverlap));
    },
    clearHistory() {
      return queue(async () => {
        const before = end();
        await rebuild([], before);
        setContinues(false);
        return { replaced: true, before, after: end() };
      });
    },

    dispose() {
      changeSubs.clear();
      for (const sub of subs) sub.dispose();
      history.dispose();
      screen.dispose();
    },
  };
}