@bastani/atomic-tui 0.9.88 → 0.9.89-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/autocomplete.d.ts +1 -0
  2. package/dist/autocomplete.d.ts.map +1 -1
  3. package/dist/autocomplete.js +9 -9
  4. package/dist/autocomplete.js.map +1 -1
  5. package/dist/components/editor.d.ts +16 -0
  6. package/dist/components/editor.d.ts.map +1 -1
  7. package/dist/components/editor.js +71 -17
  8. package/dist/components/editor.js.map +1 -1
  9. package/dist/components/markdown.d.ts +2 -0
  10. package/dist/components/markdown.d.ts.map +1 -1
  11. package/dist/components/markdown.js +7 -4
  12. package/dist/components/markdown.js.map +1 -1
  13. package/dist/components/scroll-view.d.ts +1 -1
  14. package/dist/components/scroll-view.d.ts.map +1 -1
  15. package/dist/components/scroll-view.js +13 -4
  16. package/dist/components/scroll-view.js.map +1 -1
  17. package/dist/components/select-list.d.ts +1 -0
  18. package/dist/components/select-list.d.ts.map +1 -1
  19. package/dist/components/select-list.js +11 -7
  20. package/dist/components/select-list.js.map +1 -1
  21. package/dist/components/windowed-container.d.ts +68 -0
  22. package/dist/components/windowed-container.d.ts.map +1 -0
  23. package/dist/components/windowed-container.js +357 -0
  24. package/dist/components/windowed-container.js.map +1 -0
  25. package/dist/editor-component.d.ts +1 -0
  26. package/dist/editor-component.d.ts.map +1 -1
  27. package/dist/editor-component.js.map +1 -1
  28. package/dist/fuzzy.d.ts.map +1 -1
  29. package/dist/fuzzy.js +7 -4
  30. package/dist/fuzzy.js.map +1 -1
  31. package/dist/index.d.ts +2 -1
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +2 -1
  34. package/dist/index.js.map +1 -1
  35. package/dist/layout-node.d.ts +2 -1
  36. package/dist/layout-node.d.ts.map +1 -1
  37. package/dist/layout-node.js.map +1 -1
  38. package/dist/layout.d.ts +4 -0
  39. package/dist/layout.d.ts.map +1 -1
  40. package/dist/layout.js +56 -5
  41. package/dist/layout.js.map +1 -1
  42. package/dist/stdin-buffer.d.ts.map +1 -1
  43. package/dist/stdin-buffer.js +5 -0
  44. package/dist/stdin-buffer.js.map +1 -1
  45. package/dist/terminal-image.d.ts.map +1 -1
  46. package/dist/terminal-image.js +20 -13
  47. package/dist/terminal-image.js.map +1 -1
  48. package/dist/terminal.d.ts +3 -1
  49. package/dist/terminal.d.ts.map +1 -1
  50. package/dist/terminal.js +27 -24
  51. package/dist/terminal.js.map +1 -1
  52. package/dist/tui-alt-screen.d.ts +27 -1
  53. package/dist/tui-alt-screen.d.ts.map +1 -1
  54. package/dist/tui-alt-screen.js +294 -54
  55. package/dist/tui-alt-screen.js.map +1 -1
  56. package/dist/tui-main-screen.d.ts.map +1 -1
  57. package/dist/tui-main-screen.js +17 -6
  58. package/dist/tui-main-screen.js.map +1 -1
  59. package/dist/tui.d.ts +38 -1
  60. package/dist/tui.d.ts.map +1 -1
  61. package/dist/tui.js +58 -17
  62. package/dist/tui.js.map +1 -1
  63. package/package.json +2 -2
@@ -1,9 +1,10 @@
1
1
  import { AltScreenSearchComponent, AltScreenSearchIndex, getAltScreenSearchMatchKey, } from "./alt-screen-search.js";
2
2
  import { AltScreenFlashContainer } from "./components/alt-screen-flash.js";
3
3
  import { ScrollView } from "./components/scroll-view.js";
4
+ import { WindowedContainer } from "./components/windowed-container.js";
4
5
  import { getKeybindings } from "./keybindings.js";
5
6
  import { isKeyRelease } from "./keys.js";
6
- import { getLayoutBoxesAt, getScrollbarGeometry, getScrollViewBox, getScrollViewsAt, renderLayoutFrame, } from "./layout.js";
7
+ import { getLayoutBoxesAt, getScrollbarGeometry, getScrollContentRows, getScrollViewBox, getScrollViewsAt, renderLayoutFrame, } from "./layout.js";
7
8
  import { getLayoutNode } from "./layout-node.js";
8
9
  import { deleteAllKittyImages, deleteAllKittyPlacements, deleteKittyImage, getCapabilities, getKittyImagePlacement, getKittyImagePlacementRows, isImageLine, setCapabilities, } from "./terminal-image.js";
9
10
  import { Container, CURSOR_MARKER, compositeTuiLine, dispatchMouseEvent, retargetMouseEvent, TuiBase, VIEWPORT_TUI, } from "./tui.js";
@@ -20,6 +21,9 @@ const FOCUS_IN = "\x1b[I";
20
21
  const FOCUS_OUT = "\x1b[O";
21
22
  const BEGIN_SYNCHRONIZED_OUTPUT = "\x1b[?2026h";
22
23
  const END_SYNCHRONIZED_OUTPUT = "\x1b[?2026l";
24
+ const EXIT_TRANSCRIPT_START = `${BEGIN_SYNCHRONIZED_OUTPUT}${EXIT_ALT_SCREEN}${DISABLE_AUTOWRAP}`;
25
+ const EXIT_TRANSCRIPT_END = `\x1b[0m${ENABLE_AUTOWRAP}\r\n\x1b[?25h${END_SYNCHRONIZED_OUTPUT}`;
26
+ const EXIT_TRANSCRIPT_RESTORE = `\x1b[0m${ENABLE_AUTOWRAP}\x1b[?25h${END_SYNCHRONIZED_OUTPUT}`;
23
27
  const OSC133_ZONE_PREFIX = /^(?:\x1b\]133;[ABC](?:;[^\x07\x1b]*)?(?:\x07|\x1b\\))+/;
24
28
  const OSC133_PROMPT_START = /^\x1b\]133;A(?:;[^\x07\x1b]*)?(?:\x07|\x1b\\)/;
25
29
  const PAGE_SCROLL_OVERLAP = 4;
@@ -27,6 +31,9 @@ const ALT_WHEEL_SCROLL_MULTIPLIER = 5;
27
31
  const MAX_CACHED_OFFSCREEN_KITTY_IMAGES = 16;
28
32
  const MAX_CACHED_OFFSCREEN_KITTY_TRANSMISSION_BYTES = 32 * 1024 * 1024;
29
33
  const MAX_CACHED_OFFSCREEN_KITTY_DECODED_BYTES = 64 * 1024 * 1024;
34
+ const EXIT_WRITE_CHARACTERS = 16 * 1024;
35
+ const EXIT_PREPARE_ROWS = 512;
36
+ const EXIT_SLICE_BUDGET_MS = 8;
30
37
  const DOUBLE_CLICK_INTERVAL_MS = 500;
31
38
  const COPY_ERROR_FLASH_DURATION_MS = 5000;
32
39
  // Regular mode delegates double-click selection to the terminal emulator. Fullscreen owns mouse selection,
@@ -34,14 +41,68 @@ const COPY_ERROR_FLASH_DURATION_MS = 5000;
34
41
  // https://github.com/gnachman/iTerm2/blob/7f4ff739721f95d0b93003d8701c4523a7edf261/sources/Settings/iTermPreferences.m#L743
35
42
  const TERMINAL_WORD_SELECTION_JOINERS = new Set(["/", "-", "+", "\\", "~", "_", "."]);
36
43
  const wordSegmenter = getWordSegmenter();
44
+ /** Whether the same components are drawn at the pointer in the same places: the frame's geometry there. */
45
+ function samePointerGeometry(a, b) {
46
+ return (a !== undefined &&
47
+ a.overlay?.component === b.overlay?.component &&
48
+ a.overlay?.row === b.overlay?.row &&
49
+ a.overlay?.col === b.overlay?.col &&
50
+ a.overlay?.width === b.overlay?.width &&
51
+ a.overlay?.height === b.overlay?.height &&
52
+ a.decorated === b.decorated &&
53
+ a.boxes.length === b.boxes.length &&
54
+ a.boxes.every((box, index) => box.component === b.boxes[index]?.component && box.x === b.boxes[index]?.x && box.y === b.boxes[index]?.y));
55
+ }
56
+ /**
57
+ * The same geometry at the pointer, drawn by the same components naming the same item there. What a target that took
58
+ * the press needs for its click: its own rows may repaint, as a pressed button's do, but it must still be drawn there.
59
+ */
60
+ function sameDrawnAtPointer(a, b) {
61
+ return (a !== undefined &&
62
+ a.drawn.length === b.drawn.length &&
63
+ a.drawn.every((item, index) => item === b.drawn[index]) &&
64
+ samePointerGeometry(a, b));
65
+ }
66
+ /** The same geometry at the pointer, and the same painted row under it, drawn by the same components. */
67
+ function samePointerFrame(a, b) {
68
+ return a !== undefined && a.line === b.line && sameDrawnAtPointer(a, b);
69
+ }
70
+ /** Bounds the walk into nested containers when recording what a frame drew at the pointer. */
71
+ const MAX_DRAWN_DEPTH = 32;
72
+ function stacksChildren(component) {
73
+ return typeof component.childAtRow === "function";
74
+ }
75
+ /**
76
+ * The components a frame drew at `row` of `component`, laid out `width` wide, from `component` itself inward, then
77
+ * the innermost one's row key. A root that draws the row itself is the only component named.
78
+ */
79
+ function drawnAt(component, width, row) {
80
+ const drawn = [component];
81
+ let current = component;
82
+ let at = row;
83
+ for (let depth = 0; depth < MAX_DRAWN_DEPTH && stacksChildren(current); depth++) {
84
+ const hit = current.childAtRow(width, at);
85
+ if (!hit)
86
+ break;
87
+ drawn.push(hit.component);
88
+ current = hit.component;
89
+ at = hit.row;
90
+ }
91
+ const innermost = current;
92
+ drawn.push(innermost.paintedRowKey?.(at));
93
+ return drawn;
94
+ }
37
95
  /** Alternate-screen TUI with a scrollable, application-owned viewport. */
38
96
  export class TuiAltScreen extends TuiBase {
39
97
  mode = "fullscreen";
40
98
  [VIEWPORT_TUI] = true;
41
99
  previousScreen = [];
42
100
  lastDocument = [];
101
+ exitWrites = 0;
43
102
  previousScreenWidth = 0;
44
103
  previousScreenHeight = 0;
104
+ /** The last layout drawn had image lines, which an overlay does not cover. */
105
+ coveredLayoutHasImages = false;
45
106
  layoutRoot;
46
107
  currentLayout;
47
108
  implicitDocument;
@@ -65,10 +126,16 @@ export class TuiAltScreen extends TuiBase {
65
126
  scrollToEndIndicatorRect;
66
127
  activeSearch;
67
128
  pressedUrl;
129
+ /** What the screen showed under the pointer when the left button went down. */
130
+ pressFrame;
131
+ /** Rows of the primary transcript that a drawn-over row covers: the pinned prompt and the jump-to-latest row. */
132
+ decoratedRows = new Map();
68
133
  selectionDragged = false;
69
134
  mouseCapture;
70
135
  mousePressTarget;
71
136
  mousePressPoint;
137
+ /** What the screen showed at a press a component took: its click needs the same geometry at the release. */
138
+ mousePressFrame;
72
139
  mousePressMoved = false;
73
140
  lastComponentClick;
74
141
  wheelScroll;
@@ -84,14 +151,16 @@ export class TuiAltScreen extends TuiBase {
84
151
  copySelection;
85
152
  constructor(terminal, showHardwareCursor, logDirectory, options = {}) {
86
153
  super(terminal, showHardwareCursor, logDirectory);
87
- this.implicitDocument = {
154
+ const implicitDocument = {
88
155
  render: (width) => super.render(width),
89
156
  handleMouse: (event) => super.handleMouse(event),
157
+ childAtRow: (width, row) => super.childAtRow(width, row),
90
158
  invalidate: () => {
91
159
  for (const child of this.children)
92
160
  child.invalidate();
93
161
  },
94
162
  };
163
+ this.implicitDocument = implicitDocument;
95
164
  this.implicitScrollView = new ScrollView(this.implicitDocument, { follow: "end", primary: true });
96
165
  this.flashes = new AltScreenFlashContainer(() => this.requestRender());
97
166
  this.wheelScroll = new WheelScrollAccelerator(options.wheelScrollLines ?? 1);
@@ -159,6 +228,7 @@ export class TuiAltScreen extends TuiBase {
159
228
  return this.currentLayout?.primaryScrollView ?? this.implicitScrollView;
160
229
  }
161
230
  beforeTerminalStart() {
231
+ this.exitWrites++;
162
232
  this.stopSelectionAutoScroll();
163
233
  this.selectionPressActive = false;
164
234
  this.stopScrollbarHover();
@@ -213,26 +283,96 @@ export class TuiAltScreen extends TuiBase {
213
283
  if (!this.altScreenActive)
214
284
  return;
215
285
  this.altScreenActive = false;
286
+ const savedCapabilities = this.savedCapabilities;
287
+ this.savedCapabilities = undefined;
288
+ const restoreCapabilities = () => {
289
+ if (savedCapabilities)
290
+ setCapabilities(savedCapabilities);
291
+ };
216
292
  if (options.preserveScreen) {
217
293
  this.terminal.write(`${BEGIN_SYNCHRONIZED_OUTPUT}${EXIT_ALT_SCREEN}\x1b[?25h${END_SYNCHRONIZED_OUTPUT}`);
218
294
  }
295
+ else if (options.writeInSlices) {
296
+ const restoreOnExit = () => {
297
+ try {
298
+ this.terminal.restoreOnExit?.(EXIT_TRANSCRIPT_RESTORE, "");
299
+ }
300
+ catch { }
301
+ };
302
+ process.on("exit", restoreOnExit);
303
+ this.stopOutput = this.writeTranscriptInSlices(++this.exitWrites).then(() => {
304
+ process.off("exit", restoreOnExit);
305
+ restoreCapabilities();
306
+ }, async (error) => {
307
+ restoreCapabilities();
308
+ try {
309
+ this.terminal.write(EXIT_TRANSCRIPT_RESTORE);
310
+ await this.terminal.flush?.();
311
+ process.off("exit", restoreOnExit);
312
+ }
313
+ catch { }
314
+ throw error;
315
+ });
316
+ return;
317
+ }
219
318
  else {
220
319
  const width = Math.max(1, this.terminal.columns);
221
- const documentLines = this.resolveFakeCursors(this.render(width)).map((line) => line.replace(OSC133_ZONE_PREFIX, ""));
222
- this.lastDocument = this.applyLineResets(documentLines.map((line) => line.replaceAll(CURSOR_MARKER, ""))).map((line) => (isImageLine(line) || visibleWidth(line) <= width ? line : sliceByColumn(line, 0, width, true)));
223
- let buffer = `${BEGIN_SYNCHRONIZED_OUTPUT}${EXIT_ALT_SCREEN}${DISABLE_AUTOWRAP}`;
320
+ this.lastDocument = this.prepareExitRows(this.render(width), width);
321
+ let buffer = EXIT_TRANSCRIPT_START;
224
322
  for (let row = 0; row < this.lastDocument.length; row++) {
225
323
  if (row > 0)
226
324
  buffer += "\r\n";
227
325
  buffer += `\r\x1b[2K${this.lastDocument[row] ?? ""}`;
228
326
  }
229
- buffer += `\x1b[0m${ENABLE_AUTOWRAP}\r\n\x1b[?25h${END_SYNCHRONIZED_OUTPUT}`;
327
+ buffer += EXIT_TRANSCRIPT_END;
230
328
  this.terminal.write(buffer);
231
329
  }
232
- if (this.savedCapabilities) {
233
- setCapabilities(this.savedCapabilities);
234
- this.savedCapabilities = undefined;
330
+ restoreCapabilities();
331
+ }
332
+ prepareExitRows(lines, width) {
333
+ const documentLines = this.resolveFakeCursors(lines).map((line) => line.replace(OSC133_ZONE_PREFIX, ""));
334
+ return this.applyLineResets(documentLines.map((line) => line.replaceAll(CURSOR_MARKER, ""))).map((line) => isImageLine(line) || visibleWidth(line) <= width ? line : sliceByColumn(line, 0, width, true));
335
+ }
336
+ async writeTranscriptInSlices(write) {
337
+ const width = Math.max(1, this.terminal.columns);
338
+ const lines = this.layoutRoot instanceof WindowedContainer
339
+ ? await this.layoutRoot.renderInSlices(width)
340
+ : this.render(width);
341
+ let deadline = performance.now() + EXIT_SLICE_BUDGET_MS;
342
+ const proceed = async () => {
343
+ if (performance.now() >= deadline) {
344
+ await new Promise((resolve) => setImmediate(resolve));
345
+ deadline = performance.now() + EXIT_SLICE_BUDGET_MS;
346
+ }
347
+ return write === this.exitWrites;
348
+ };
349
+ let buffer = EXIT_TRANSCRIPT_START;
350
+ const writeBuffer = async (minimum) => {
351
+ while (buffer.length > 0 && buffer.length >= minimum) {
352
+ let end = Math.min(buffer.length, EXIT_WRITE_CHARACTERS);
353
+ const last = buffer.charCodeAt(end - 1);
354
+ if (end < buffer.length && end > 1 && last >= 0xd800 && last <= 0xdbff)
355
+ end--;
356
+ this.terminal.write(buffer.slice(0, end));
357
+ buffer = buffer.slice(end);
358
+ await this.terminal.flush?.();
359
+ if (!(await proceed()))
360
+ return false;
361
+ }
362
+ return true;
363
+ };
364
+ for (let start = 0; start < lines.length; start += EXIT_PREPARE_ROWS) {
365
+ const rows = this.prepareExitRows(lines.slice(start, start + EXIT_PREPARE_ROWS), width);
366
+ for (let index = 0; index < rows.length; index++) {
367
+ if (start + index > 0)
368
+ buffer += "\r\n";
369
+ buffer += `\r\x1b[2K${rows[index] ?? ""}`;
370
+ }
371
+ if (!(await writeBuffer(EXIT_WRITE_CHARACTERS)) || !(await proceed()))
372
+ return;
235
373
  }
374
+ buffer += EXIT_TRANSCRIPT_END;
375
+ await writeBuffer(0);
236
376
  }
237
377
  getExitRestorePrefix() {
238
378
  const prefix = super.getExitRestorePrefix();
@@ -316,11 +456,13 @@ export class TuiAltScreen extends TuiBase {
316
456
  if (!this.currentLayout)
317
457
  return;
318
458
  const scrollView = this.getPrimaryScrollView();
319
- const lines = getScrollViewBox(this.currentLayout, scrollView)?.scrollContentLines;
459
+ if (this.settleScrollContent(scrollView))
460
+ this.currentLayout = this.layoutFrame();
461
+ const lines = getScrollContentRows(getScrollViewBox(this.currentLayout, scrollView));
320
462
  if (!lines)
321
463
  return;
322
464
  for (let row = scrollView.scrollTop + direction; row >= 0 && row < lines.length; row += direction) {
323
- if (!OSC133_PROMPT_START.test(lines[row] ?? ""))
465
+ if (!OSC133_PROMPT_START.test(lines.at(row) ?? ""))
324
466
  continue;
325
467
  scrollView.scrollTo(row);
326
468
  this.requestRender();
@@ -405,8 +547,8 @@ export class TuiAltScreen extends TuiBase {
405
547
  return false;
406
548
  const scrollView = layout.primaryScrollView ?? this.implicitScrollView;
407
549
  const box = getScrollViewBox(layout, scrollView);
408
- const lines = box?.scrollContentLines;
409
- if (!lines || !search.query.trim()) {
550
+ const rows = getScrollContentRows(box);
551
+ if (!rows || !search.query.trim()) {
410
552
  search.matches = [];
411
553
  search.selectedIndex = -1;
412
554
  search.selectedKey = undefined;
@@ -415,6 +557,7 @@ export class TuiAltScreen extends TuiBase {
415
557
  return false;
416
558
  }
417
559
  const shouldRevealSelection = search.selectionMode !== "retain";
560
+ const lines = Array.isArray(rows) ? rows : Array.from({ length: rows.length }, (_, row) => rows.at(row) ?? "");
418
561
  const result = search.index.search(lines, search.query);
419
562
  const matches = result.matches;
420
563
  search.matches = matches;
@@ -491,6 +634,7 @@ export class TuiAltScreen extends TuiBase {
491
634
  this.mouseCapture = undefined;
492
635
  this.mousePressTarget = undefined;
493
636
  this.mousePressPoint = undefined;
637
+ this.mousePressFrame = undefined;
494
638
  this.mousePressMoved = false;
495
639
  }
496
640
  handleViewportInput(data) {
@@ -736,7 +880,12 @@ export class TuiAltScreen extends TuiBase {
736
880
  if (targetResult)
737
881
  render = this.applyMouseDispatchResult(event, targetResult);
738
882
  if (raw.release) {
739
- if (!this.mousePressMoved && this.mousePressPoint?.x === raw.x && this.mousePressPoint.y === raw.y) {
883
+ // The release always reaches the target, so it can end what its press started. The click needs the
884
+ // same target still drawn where it was pressed, not another that took its place.
885
+ if (!this.mousePressMoved &&
886
+ this.mousePressPoint?.x === raw.x &&
887
+ this.mousePressPoint.y === raw.y &&
888
+ sameDrawnAtPointer(this.mousePressFrame, this.pointerFrame(raw.x, raw.y))) {
740
889
  const clickEvent = this.createMouseEvent("click", raw.button, raw.x, raw.y, {
741
890
  clickCount: this.getComponentClickCount(target, raw.x, raw.y),
742
891
  });
@@ -772,6 +921,7 @@ export class TuiAltScreen extends TuiBase {
772
921
  this.clearTextSelection();
773
922
  this.mousePressTarget = result.target;
774
923
  this.mousePressPoint = { x: raw.x, y: raw.y };
924
+ this.mousePressFrame = this.pointerFrame(raw.x, raw.y);
775
925
  this.mousePressMoved = false;
776
926
  }
777
927
  if (render)
@@ -953,7 +1103,7 @@ export class TuiAltScreen extends TuiBase {
953
1103
  if (visibleBottom < visibleTop)
954
1104
  return undefined;
955
1105
  const pointerRow = Math.max(visibleTop, Math.min(visibleBottom, y));
956
- const maxContentRow = Math.max(0, (box.scrollContentLines?.length ?? 1) - 1);
1106
+ const maxContentRow = Math.max(0, (getScrollContentRows(box)?.length ?? 1) - 1);
957
1107
  return {
958
1108
  row: Math.max(0, Math.min(maxContentRow, scrollView.scrollTop + pointerRow - box.rect.y)),
959
1109
  col: Math.max(0, Math.min(box.rect.width - 1, x - box.rect.x)),
@@ -973,9 +1123,9 @@ export class TuiAltScreen extends TuiBase {
973
1123
  }
974
1124
  getSelectionSourceLine(point) {
975
1125
  if (point.scrollView && this.currentLayout) {
976
- const lines = getScrollViewBox(this.currentLayout, point.scrollView)?.scrollContentLines;
1126
+ const lines = getScrollContentRows(getScrollViewBox(this.currentLayout, point.scrollView));
977
1127
  if (lines)
978
- return lines[point.row] ?? "";
1128
+ return lines.at(point.row) ?? "";
979
1129
  }
980
1130
  return this.previousScreen[point.row] ?? "";
981
1131
  }
@@ -1032,30 +1182,58 @@ export class TuiAltScreen extends TuiBase {
1032
1182
  this.selectionFocus = range.end;
1033
1183
  }
1034
1184
  }
1035
- getClickCount(point, word) {
1185
+ getClickCount(screenRow, screenCol, point, word, drawn) {
1036
1186
  const now = Date.now();
1037
1187
  const previous = this.lastClick;
1038
- const count = word &&
1039
- previous &&
1188
+ // Clicks count on the screen, in rows and columns alike: a press located in a scroll view and one located on
1189
+ // the screen, because an overlay opened between them, are on the same spot. A second click counts on the same
1190
+ // word, or on the same cell where there is no word, so a fast second click on empty space is a double-click.
1191
+ const toScreen = screenCol - point.col;
1192
+ const start = (word?.start.col ?? point.col) + toScreen;
1193
+ const end = (word?.end.col ?? point.col) + toScreen;
1194
+ const count = previous &&
1040
1195
  now - previous.timestamp <= DOUBLE_CLICK_INTERVAL_MS &&
1041
- previous.row === point.row &&
1042
- previous.scrollView === point.scrollView &&
1043
- previous.wordStart === word.start.col &&
1044
- previous.wordEnd === word.end.col
1196
+ previous.row === screenRow &&
1197
+ previous.wordStart === start &&
1198
+ previous.wordEnd === end &&
1199
+ previous.drawn.length === drawn.length &&
1200
+ previous.drawn.every((item, index) => item === drawn[index])
1045
1201
  ? (previous.count % 3) + 1
1046
1202
  : 1;
1047
- this.lastClick = word
1048
- ? {
1049
- timestamp: now,
1050
- count,
1051
- row: point.row,
1052
- scrollView: point.scrollView,
1053
- wordStart: word.start.col,
1054
- wordEnd: word.end.col,
1055
- }
1056
- : undefined;
1203
+ this.lastClick = {
1204
+ timestamp: now,
1205
+ count,
1206
+ row: screenRow,
1207
+ col: screenCol,
1208
+ wordStart: start,
1209
+ wordEnd: end,
1210
+ drawn,
1211
+ awaitingPaint: false,
1212
+ };
1057
1213
  return count;
1058
1214
  }
1215
+ /** What the screen shows at `x`, `y`: the painted row, the layout boxes and overlay drawn there. */
1216
+ pointerFrame(x, y) {
1217
+ const decoration = this.decoratedRows.get(y);
1218
+ const overlay = this.overlayAt(x, y);
1219
+ const boxes = this.currentLayout ? getLayoutBoxesAt(this.currentLayout, x, y) : [];
1220
+ const innermost = boxes[0];
1221
+ return {
1222
+ line: this.previousScreen[y],
1223
+ boxes: boxes.map((box) => ({ component: box.component, x: box.rect.x, y: box.rect.y })),
1224
+ drawn: overlay
1225
+ ? drawnAt(overlay.component, overlay.width, y - overlay.row)
1226
+ : innermost
1227
+ ? drawnAt(innermost.component, innermost.rect.width, y - innermost.rect.y)
1228
+ : [],
1229
+ overlay,
1230
+ // An overlay drawn over the row takes its clicks; the decoration under it is hidden.
1231
+ decorated: overlay === undefined &&
1232
+ decoration !== undefined &&
1233
+ x >= decoration.x &&
1234
+ x < decoration.x + decoration.width,
1235
+ };
1236
+ }
1059
1237
  updateSelectionAutoScroll(event) {
1060
1238
  const scrollView = this.selectionAnchor?.scrollView;
1061
1239
  if (!scrollView || !this.currentLayout) {
@@ -1120,10 +1298,15 @@ export class TuiAltScreen extends TuiBase {
1120
1298
  if (!this.selectionAnchor)
1121
1299
  return;
1122
1300
  this.updateSelectionFocus(point);
1301
+ const releaseFrame = this.pointerFrame(event.x, event.y);
1123
1302
  const isClick = !this.selectionDragged &&
1124
1303
  this.selectionAnchor.scrollView === point.scrollView &&
1125
1304
  this.selectionAnchor.row === point.row &&
1126
1305
  this.selectionAnchor.col === point.col;
1306
+ // A click acts on what the screen showed when it was pressed: a repaint that changed what is under the
1307
+ // pointer before the release makes it no click at all.
1308
+ const samePaint = samePointerFrame(this.pressFrame, releaseFrame);
1309
+ this.pressFrame = undefined;
1127
1310
  const clickedUrl = isClick ? this.pressedUrl : undefined;
1128
1311
  this.pressedUrl = undefined;
1129
1312
  if (clickedUrl && this.openUrl) {
@@ -1138,7 +1321,7 @@ export class TuiAltScreen extends TuiBase {
1138
1321
  this.requestRender();
1139
1322
  return;
1140
1323
  }
1141
- if (isClick) {
1324
+ if (isClick && samePaint && !releaseFrame.decorated) {
1142
1325
  const clickEvent = this.createMouseEvent("click", event.button, event.x, event.y, {
1143
1326
  clickCount: this.lastClick?.count ?? 1,
1144
1327
  });
@@ -1147,6 +1330,8 @@ export class TuiAltScreen extends TuiBase {
1147
1330
  if (result) {
1148
1331
  const render = this.applyMouseDispatchResult(clickEvent, result);
1149
1332
  this.clearTextSelection();
1333
+ if (this.lastClick)
1334
+ this.lastClick.awaitingPaint = true;
1150
1335
  if (render)
1151
1336
  this.requestRender();
1152
1337
  return;
@@ -1175,13 +1360,15 @@ export class TuiAltScreen extends TuiBase {
1175
1360
  : undefined;
1176
1361
  const anchor = this.getSelectionPoint(event, scrollView);
1177
1362
  const word = this.getWordSelection(anchor);
1178
- const clickCount = this.getClickCount(anchor, word);
1363
+ const pressFrame = this.pointerFrame(event.x, event.y);
1364
+ const clickCount = this.getClickCount(event.y, Math.max(0, Math.min(this.terminal.columns - 1, event.x)), anchor, word, pressFrame.drawn);
1179
1365
  const range = clickCount === 2 ? word : clickCount === 3 ? this.getLineSelection(anchor) : undefined;
1180
1366
  this.selectionGranularity = range ? (clickCount === 2 ? "word" : "line") : "character";
1181
1367
  this.selectionInitialRange = range;
1182
1368
  this.selectionAnchor = range?.start ?? anchor;
1183
1369
  this.selectionFocus = range?.end ?? anchor;
1184
1370
  this.selectionDragged = false;
1371
+ this.pressFrame = pressFrame;
1185
1372
  this.pressedUrl = range
1186
1373
  ? undefined
1187
1374
  : getOsc8LinkAtColumn(this.previousScreen[Math.max(0, Math.min(this.terminal.rows - 1, event.y))] ?? "", Math.max(0, Math.min(this.terminal.columns - 1, event.x)));
@@ -1224,14 +1411,14 @@ export class TuiAltScreen extends TuiBase {
1224
1411
  if (selection.start.scrollView) {
1225
1412
  if (!this.currentLayout)
1226
1413
  return undefined;
1227
- const box = getScrollViewBox(this.currentLayout, selection.start.scrollView);
1228
- if (!box?.scrollContentLines)
1414
+ const rows = getScrollContentRows(getScrollViewBox(this.currentLayout, selection.start.scrollView));
1415
+ if (!rows)
1229
1416
  return undefined;
1230
- sourceLines = box.scrollContentLines;
1417
+ sourceLines = rows;
1231
1418
  }
1232
1419
  const lines = [];
1233
1420
  for (let row = selection.start.row; row <= selection.end.row; row++) {
1234
- const line = sourceLines[row] ?? "";
1421
+ const line = sourceLines.at(row) ?? "";
1235
1422
  const columns = this.getSelectionColumns(line, row, selection);
1236
1423
  lines.push(stripTerminalSequences(sliceByColumn(line, columns.start, Math.max(0, columns.end - columns.start), true)).trimEnd());
1237
1424
  }
@@ -1416,6 +1603,7 @@ export class TuiAltScreen extends TuiBase {
1416
1603
  }
1417
1604
  compositeScrollToEndIndicator(screen, layout, width) {
1418
1605
  this.scrollToEndIndicatorRect = undefined;
1606
+ this.decoratedRows = new Map();
1419
1607
  const scrollView = layout.primaryScrollView ?? this.implicitScrollView;
1420
1608
  if (!this.scrollToEndIndicator || !scrollView.followEnd || scrollView.isFollowingEnd)
1421
1609
  return screen;
@@ -1439,6 +1627,8 @@ export class TuiAltScreen extends TuiBase {
1439
1627
  const result = [...screen];
1440
1628
  result[row] = compositeTuiLine(result[row] ?? "", text, column, textWidth, width);
1441
1629
  this.scrollToEndIndicatorRect = { row, column, width: textWidth };
1630
+ // The rest of the row still shows the transcript, but its row is half covered: no click reaches it.
1631
+ this.decoratedRows.set(row, { x: clip.x, width: clip.width });
1442
1632
  return result;
1443
1633
  }
1444
1634
  compositeFlashes(screen, width, height) {
@@ -1457,6 +1647,45 @@ export class TuiAltScreen extends TuiBase {
1457
1647
  }
1458
1648
  return result;
1459
1649
  }
1650
+ /**
1651
+ * The layout and the overlays composited over it. An overlay that covered the whole screen last frame and still
1652
+ * shows hides the layout under it, so the layout is not drawn again until the overlay stops covering it: a
1653
+ * full-screen view's frames cost only the view. A layout with images is always drawn, because an image line
1654
+ * shows through an overlay.
1655
+ */
1656
+ renderScreen(width, height) {
1657
+ const covered = this.currentLayout;
1658
+ if (covered !== undefined &&
1659
+ !this.coveredLayoutHasImages &&
1660
+ this.previousScreenWidth === width &&
1661
+ this.previousScreenHeight === height &&
1662
+ this.overlaysCoverScreen(width, height)) {
1663
+ const screen = this.compositeOverlays(Array.from({ length: height }, () => ""), width, height);
1664
+ // An overlay that shrank or hid in this frame shows the layout after all.
1665
+ if (this.overlaysCoverScreen(width, height))
1666
+ return { layout: covered, screen };
1667
+ }
1668
+ const layout = this.renderLayout(width, height);
1669
+ return { layout, screen: this.compositeOverlays(this.composeLayout(layout, width), width, height) };
1670
+ }
1671
+ renderLayout(width, height) {
1672
+ if (this.activeSearch?.query.trim())
1673
+ this.settleScrollContent(this.getPrimaryScrollView());
1674
+ const root = this.layoutRoot ?? this.implicitScrollView;
1675
+ let layout = renderLayoutFrame(root, width, height, () => this.requestRender());
1676
+ if (this.refreshSearch(layout))
1677
+ layout = renderLayoutFrame(root, width, height, () => this.requestRender());
1678
+ this.coveredLayoutHasImages = layout.lines.some(isImageLine);
1679
+ return layout;
1680
+ }
1681
+ /** The layout's rows with its search highlights and scroll decorations, ready for the overlays. */
1682
+ composeLayout(layout, width) {
1683
+ // Resolve fake cursors before highlighting and compositing, which only track SGR codes
1684
+ let screen = this.resolveFakeCursors(layout.lines.map((line) => line.replace(OSC133_ZONE_PREFIX, "")));
1685
+ screen = this.applySearchHighlights(screen, layout);
1686
+ screen = this.compositeScrollToEndIndicator(screen, layout, width);
1687
+ return this.compositeScrollPinnedPrompt(screen, layout, width);
1688
+ }
1460
1689
  compositeScrollPinnedPrompt(screen, layout, width) {
1461
1690
  if (!this.scrollPinnedPrompt)
1462
1691
  return screen;
@@ -1467,7 +1696,7 @@ export class TuiAltScreen extends TuiBase {
1467
1696
  return screen;
1468
1697
  const box = getScrollViewBox(layout, scrollView);
1469
1698
  const clip = box?.clip;
1470
- const lines = box?.scrollContentLines;
1699
+ const lines = getScrollContentRows(box);
1471
1700
  if (!clip || !lines || clip.width <= 0 || clip.height < 2)
1472
1701
  return screen;
1473
1702
  const pin = this.scrollPinnedPrompt;
@@ -1487,24 +1716,30 @@ export class TuiAltScreen extends TuiBase {
1487
1716
  const padded = text + " ".repeat(Math.max(0, available - textWidth));
1488
1717
  const result = [...screen];
1489
1718
  result[row] = compositeTuiLine(result[row] ?? "", padded, clip.x, available, width);
1719
+ this.decoratedRows.set(row, { x: clip.x, width: available });
1490
1720
  return result;
1491
1721
  }
1722
+ layoutFrame() {
1723
+ const width = Math.max(1, this.terminal.columns);
1724
+ const height = Math.max(1, this.terminal.rows);
1725
+ return renderLayoutFrame(this.layoutRoot ?? this.implicitScrollView, width, height, () => this.requestRender());
1726
+ }
1727
+ settleScrollContent(scrollView) {
1728
+ const box = this.currentLayout ? getScrollViewBox(this.currentLayout, scrollView) : undefined;
1729
+ const content = box?.children[0]?.component;
1730
+ if (!(content instanceof WindowedContainer))
1731
+ return false;
1732
+ content.settleRows();
1733
+ return true;
1734
+ }
1492
1735
  doRender() {
1493
1736
  if (this.stopped || !this.altScreenActive)
1494
1737
  return;
1495
1738
  const width = Math.max(1, this.terminal.columns);
1496
1739
  const height = Math.max(1, this.terminal.rows);
1497
- const root = this.layoutRoot ?? this.implicitScrollView;
1498
- let nextLayout = renderLayoutFrame(root, width, height, () => this.requestRender());
1499
- if (this.refreshSearch(nextLayout)) {
1500
- nextLayout = renderLayoutFrame(root, width, height, () => this.requestRender());
1501
- }
1502
- // Resolve fake cursors before highlighting and compositing, which only track SGR codes
1503
- let screen = this.resolveFakeCursors(nextLayout.lines.map((line) => line.replace(OSC133_ZONE_PREFIX, "")));
1504
- screen = this.applySearchHighlights(screen, nextLayout);
1505
- screen = this.compositeScrollToEndIndicator(screen, nextLayout, width);
1506
- screen = this.compositeScrollPinnedPrompt(screen, nextLayout, width);
1507
- screen = this.compositeOverlays(screen, width, height);
1740
+ const rendered = this.renderScreen(width, height);
1741
+ const nextLayout = rendered.layout;
1742
+ let screen = rendered.screen;
1508
1743
  if (screen.length > height)
1509
1744
  screen = screen.slice(screen.length - height);
1510
1745
  screen = this.applySelection(screen, nextLayout);
@@ -1598,14 +1833,19 @@ export class TuiAltScreen extends TuiBase {
1598
1833
  this.previousScreenWidth = width;
1599
1834
  this.previousScreenHeight = height;
1600
1835
  this.currentLayout = nextLayout;
1836
+ const click = this.lastClick;
1837
+ if (click?.awaitingPaint) {
1838
+ click.awaitingPaint = false;
1839
+ click.drawn = this.pointerFrame(click.col, click.row).drawn;
1840
+ }
1601
1841
  }
1602
1842
  }
1603
1843
  export function turnPromptAbove(lines, top, isPrompt) {
1604
1844
  for (let start = Math.min(top, lines.length - 1); start >= 0; start--) {
1605
- if (!isPrompt(lines[start] ?? ""))
1845
+ if (!isPrompt(lines.at(start) ?? ""))
1606
1846
  continue;
1607
1847
  for (let row = start; row < top; row++) {
1608
- const text = (lines[row] ?? "").replace(OSC133_ZONE_PREFIX, "");
1848
+ const text = (lines.at(row) ?? "").replace(OSC133_ZONE_PREFIX, "");
1609
1849
  if (text.replace(/\x1b\[[0-9;]*m/g, "").trim())
1610
1850
  return text;
1611
1851
  }