agentlas 1.0.42 → 1.0.44

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 (70) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/engine/agentlas-workforce.cjs +1 -1
  3. package/engine/hephaestus/runtime.cjs +1 -1
  4. package/engine/storm/storm.cjs +1 -1
  5. package/engine/storm/swarm.cjs +1 -1
  6. package/engine/ui/palette.cjs +1 -0
  7. package/engine/ui/repl.cjs +34 -4
  8. package/engine/ui/shell.cjs +22 -3
  9. package/engine/vendor/mermaid/LICENSE +205 -0
  10. package/engine/vendor/mermaid/ansi.js +23 -0
  11. package/engine/vendor/mermaid/canvas.js +366 -0
  12. package/engine/vendor/mermaid/graph.js +91 -0
  13. package/engine/vendor/mermaid/index.js +100 -0
  14. package/engine/vendor/mermaid/labels.js +324 -0
  15. package/engine/vendor/mermaid/layout-seq.js +194 -0
  16. package/engine/vendor/mermaid/layout.js +881 -0
  17. package/engine/vendor/mermaid/package.json +1 -0
  18. package/engine/vendor/mermaid/parse.js +1108 -0
  19. package/engine/vendor/mermaid/source-box.js +78 -0
  20. package/engine/vendor/mermaid/types.js +1 -0
  21. package/engine/vendor/mermaid/width-data.js +994 -0
  22. package/engine/vendor/mermaid/width.js +76 -0
  23. package/engine/vendor/tui/LICENSE +20 -0
  24. package/engine/vendor/tui/autocomplete.js +632 -0
  25. package/engine/vendor/tui/components/alt-screen-flash.js +37 -0
  26. package/engine/vendor/tui/components/box.js +104 -0
  27. package/engine/vendor/tui/components/cancellable-loader.js +35 -0
  28. package/engine/vendor/tui/components/editor.js +1961 -0
  29. package/engine/vendor/tui/components/h-stack.js +43 -0
  30. package/engine/vendor/tui/components/image.js +90 -0
  31. package/engine/vendor/tui/components/input.js +378 -0
  32. package/engine/vendor/tui/components/loader.js +69 -0
  33. package/engine/vendor/tui/components/markdown.js +806 -0
  34. package/engine/vendor/tui/components/scroll-view.js +173 -0
  35. package/engine/vendor/tui/components/select-list.js +159 -0
  36. package/engine/vendor/tui/components/settings-list.js +182 -0
  37. package/engine/vendor/tui/components/spacer.js +23 -0
  38. package/engine/vendor/tui/components/stack.js +111 -0
  39. package/engine/vendor/tui/components/text.js +89 -0
  40. package/engine/vendor/tui/components/truncated-text.js +51 -0
  41. package/engine/vendor/tui/components/v-stack.js +26 -0
  42. package/engine/vendor/tui/deps/east-asian-width/LICENSE +9 -0
  43. package/engine/vendor/tui/deps/east-asian-width/index.js +30 -0
  44. package/engine/vendor/tui/deps/east-asian-width/lookup-data.js +21 -0
  45. package/engine/vendor/tui/deps/east-asian-width/lookup.js +138 -0
  46. package/engine/vendor/tui/deps/east-asian-width/utilities.js +24 -0
  47. package/engine/vendor/tui/deps/marked/LICENSE +44 -0
  48. package/engine/vendor/tui/deps/marked/index.js +77 -0
  49. package/engine/vendor/tui/editor-component.js +2 -0
  50. package/engine/vendor/tui/fuzzy.js +110 -0
  51. package/engine/vendor/tui/index.js +42 -0
  52. package/engine/vendor/tui/keybindings.js +209 -0
  53. package/engine/vendor/tui/keys.js +1174 -0
  54. package/engine/vendor/tui/kill-ring.js +44 -0
  55. package/engine/vendor/tui/latex.js +1264 -0
  56. package/engine/vendor/tui/layout-node.js +6 -0
  57. package/engine/vendor/tui/layout.js +314 -0
  58. package/engine/vendor/tui/native-modifiers.js +60 -0
  59. package/engine/vendor/tui/package.json +1 -0
  60. package/engine/vendor/tui/stdin-buffer.js +361 -0
  61. package/engine/vendor/tui/terminal-colors.js +59 -0
  62. package/engine/vendor/tui/terminal-image.js +518 -0
  63. package/engine/vendor/tui/terminal.js +436 -0
  64. package/engine/vendor/tui/tui-alt-screen.js +902 -0
  65. package/engine/vendor/tui/tui-main-screen.js +533 -0
  66. package/engine/vendor/tui/tui.js +937 -0
  67. package/engine/vendor/tui/undo-stack.js +25 -0
  68. package/engine/vendor/tui/utils.js +1191 -0
  69. package/engine/vendor/tui/word-navigation.js +96 -0
  70. package/package.json +2 -6
@@ -0,0 +1,902 @@
1
+ import { AltScreenFlashContainer } from "./components/alt-screen-flash.js";
2
+ import { ScrollView } from "./components/scroll-view.js";
3
+ import { getKeybindings } from "./keybindings.js";
4
+ import { isKeyRelease } from "./keys.js";
5
+ import { getScrollbarGeometry, getScrollViewBox, getScrollViewsAt, renderLayoutFrame, } from "./layout.js";
6
+ import { deleteAllKittyImages, deleteAllKittyPlacements, deleteKittyImage, getCapabilities, getKittyImagePlacement, isImageLine, setCapabilities, } from "./terminal-image.js";
7
+ import { CURSOR_MARKER, compositeTuiLine, TuiBase, VIEWPORT_TUI, } from "./tui.js";
8
+ import { extractAnsiCode, getGraphemeCellRange, getOsc8LinkAtColumn, getWordSegmenter, sliceByColumn, stripTerminalSequences, visibleWidth, } from "./utils.js";
9
+ const ENTER_ALT_SCREEN = "\x1b[?1049h";
10
+ const EXIT_ALT_SCREEN = "\x1b[?1049l";
11
+ const DISABLE_AUTOWRAP = "\x1b[?7l";
12
+ const ENABLE_AUTOWRAP = "\x1b[?7h";
13
+ const ENABLE_BUTTON_MOTION_MOUSE = "\x1b[?1000h\x1b[?1002h\x1b[?1004h\x1b[?1006h";
14
+ const ENABLE_ALL_MOTION_MOUSE = "\x1b[?1000h\x1b[?1002h\x1b[?1003h\x1b[?1004h\x1b[?1006h";
15
+ const DISABLE_MOUSE = "\x1b[?1006l\x1b[?1004l\x1b[?1003l\x1b[?1002l\x1b[?1000l";
16
+ const FOCUS_IN = "\x1b[I";
17
+ const FOCUS_OUT = "\x1b[O";
18
+ const BEGIN_SYNCHRONIZED_OUTPUT = "\x1b[?2026h";
19
+ const END_SYNCHRONIZED_OUTPUT = "\x1b[?2026l";
20
+ const OSC133_ZONE_PREFIX = /^(?:\x1b\]133;[ABC](?:\x07|\x1b\\))+/;
21
+ const OSC133_PROMPT_START = /^\x1b\]133;A(?:\x07|\x1b\\)/;
22
+ const PAGE_SCROLL_OVERLAP = 4;
23
+ const MAX_CACHED_OFFSCREEN_KITTY_IMAGES = 16;
24
+ const MAX_CACHED_OFFSCREEN_KITTY_TRANSMISSION_BYTES = 32 * 1024 * 1024;
25
+ const MAX_CACHED_OFFSCREEN_KITTY_DECODED_BYTES = 64 * 1024 * 1024;
26
+ const DOUBLE_CLICK_INTERVAL_MS = 500;
27
+ const wordSegmenter = getWordSegmenter();
28
+ /** Alternate-screen TUI with a scrollable, application-owned viewport. */
29
+ export class TuiAltScreen extends TuiBase {
30
+ mode = "fullscreen";
31
+ [VIEWPORT_TUI] = true;
32
+ previousScreen = [];
33
+ lastDocument = [];
34
+ previousScreenWidth = 0;
35
+ previousScreenHeight = 0;
36
+ layoutRoot;
37
+ currentLayout;
38
+ implicitDocument;
39
+ implicitScrollView;
40
+ flashes;
41
+ altScreenActive = false;
42
+ imageProtocol = null;
43
+ savedCapabilities;
44
+ uploadedKittyImages = new Map();
45
+ selectionAnchor;
46
+ selectionFocus;
47
+ selectionGranularity = "character";
48
+ selectionInitialRange;
49
+ lastClick;
50
+ selectionDragPointer;
51
+ selectionAutoScrollDirection = 0;
52
+ selectionAutoScrollTimer;
53
+ selectionPressActive = false;
54
+ scrollbarDrag;
55
+ scrollbarHover;
56
+ pressedUrl;
57
+ selectionDragged = false;
58
+ wheelScrollLines;
59
+ mouseEnabled;
60
+ openUrl;
61
+ onRightClickPaste;
62
+ constructor(terminal, showHardwareCursor, logDirectory, options = {}) {
63
+ super(terminal, showHardwareCursor, logDirectory);
64
+ this.implicitDocument = {
65
+ render: (width) => super.render(width),
66
+ invalidate: () => {
67
+ for (const child of this.children)
68
+ child.invalidate();
69
+ },
70
+ };
71
+ this.implicitScrollView = new ScrollView(this.implicitDocument, { follow: "end", primary: true });
72
+ this.flashes = new AltScreenFlashContainer(() => this.requestRender());
73
+ this.wheelScrollLines = Math.max(1, Math.floor(options.wheelScrollLines ?? 1));
74
+ this.mouseEnabled = options.mouse ?? true;
75
+ this.openUrl = options.openUrl;
76
+ this.onRightClickPaste = options.onRightClickPaste;
77
+ this.addInputListener((data) => this.handleViewportInput(data));
78
+ }
79
+ get viewportTop() {
80
+ return this.getPrimaryScrollView().scrollTop;
81
+ }
82
+ get isFollowingOutput() {
83
+ return this.getPrimaryScrollView().isFollowingEnd;
84
+ }
85
+ setLayoutRoot(component) {
86
+ if (this.layoutRoot === component)
87
+ return;
88
+ this.layoutRoot = component;
89
+ this.currentLayout = undefined;
90
+ this.requestRender();
91
+ }
92
+ render(width) {
93
+ return this.layoutRoot?.render(width) ?? super.render(width);
94
+ }
95
+ getMountedRoots() {
96
+ return this.layoutRoot ? [this.layoutRoot] : this.children;
97
+ }
98
+ getPrimaryScrollView() {
99
+ return this.currentLayout?.primaryScrollView ?? this.implicitScrollView;
100
+ }
101
+ beforeTerminalStart() {
102
+ this.stopSelectionAutoScroll();
103
+ this.selectionPressActive = false;
104
+ this.stopScrollbarHover();
105
+ this.stopScrollbarDrag();
106
+ this.flashes.dispose();
107
+ this.altScreenActive = true;
108
+ const capabilities = getCapabilities();
109
+ this.imageProtocol = capabilities.images;
110
+ this.uploadedKittyImages.clear();
111
+ if (capabilities.images === "iterm2") {
112
+ this.savedCapabilities = capabilities;
113
+ setCapabilities({ ...capabilities, images: null });
114
+ this.invalidate();
115
+ }
116
+ this.lastDocument = [];
117
+ this.selectionAnchor = undefined;
118
+ this.selectionFocus = undefined;
119
+ this.selectionGranularity = "character";
120
+ this.selectionInitialRange = undefined;
121
+ this.lastClick = undefined;
122
+ this.pressedUrl = undefined;
123
+ this.selectionDragged = false;
124
+ this.resetRenderState();
125
+ const term = process.env.TERM?.toLowerCase() ?? "";
126
+ // Multiplexers can lag when every pointer movement is forwarded. Button-motion
127
+ // tracking preserves clicks, wheel events, selections, and scrollbar dragging.
128
+ const mouseSequence = process.env.TMUX !== undefined ||
129
+ process.env.ZELLIJ !== undefined ||
130
+ process.env.STY !== undefined ||
131
+ term.startsWith("tmux") ||
132
+ term.startsWith("screen")
133
+ ? ENABLE_BUTTON_MOTION_MOUSE
134
+ : ENABLE_ALL_MOTION_MOUSE;
135
+ this.terminal.write(`${ENTER_ALT_SCREEN}${DISABLE_AUTOWRAP}${this.mouseEnabled ? mouseSequence : ""}\x1b[2J\x1b[H\x1b[?25l`);
136
+ }
137
+ beforeTerminalStop(_options) {
138
+ this.stopSelectionAutoScroll();
139
+ this.selectionPressActive = false;
140
+ this.stopScrollbarHover();
141
+ this.stopScrollbarDrag();
142
+ this.flashes.dispose();
143
+ if (!this.altScreenActive)
144
+ return;
145
+ this.terminal.write(`${BEGIN_SYNCHRONIZED_OUTPUT}${this.deleteKittyImages()}${this.mouseEnabled ? DISABLE_MOUSE : ""}${ENABLE_AUTOWRAP}${END_SYNCHRONIZED_OUTPUT}`);
146
+ this.uploadedKittyImages.clear();
147
+ }
148
+ afterTerminalStop(options) {
149
+ if (!this.altScreenActive)
150
+ return;
151
+ this.altScreenActive = false;
152
+ if (options.preserveScreen) {
153
+ this.terminal.write(`${BEGIN_SYNCHRONIZED_OUTPUT}${EXIT_ALT_SCREEN}\x1b[?25h${END_SYNCHRONIZED_OUTPUT}`);
154
+ }
155
+ else {
156
+ const width = Math.max(1, this.terminal.columns);
157
+ const documentLines = this.render(width).map((line) => line.replace(OSC133_ZONE_PREFIX, ""));
158
+ this.lastDocument = this.applyLineResets(documentLines.map((line) => line.replaceAll(CURSOR_MARKER, ""))).map((line) => (isImageLine(line) || visibleWidth(line) <= width ? line : sliceByColumn(line, 0, width, true)));
159
+ let buffer = `${BEGIN_SYNCHRONIZED_OUTPUT}${EXIT_ALT_SCREEN}${DISABLE_AUTOWRAP}`;
160
+ for (let row = 0; row < this.lastDocument.length; row++) {
161
+ if (row > 0)
162
+ buffer += "\r\n";
163
+ buffer += `\r\x1b[2K${this.lastDocument[row] ?? ""}`;
164
+ }
165
+ buffer += `\x1b[0m${ENABLE_AUTOWRAP}\r\n\x1b[?25h${END_SYNCHRONIZED_OUTPUT}`;
166
+ this.terminal.write(buffer);
167
+ }
168
+ if (this.savedCapabilities) {
169
+ setCapabilities(this.savedCapabilities);
170
+ this.savedCapabilities = undefined;
171
+ }
172
+ }
173
+ deleteKittyImages() {
174
+ return this.imageProtocol === "kitty" ? deleteAllKittyImages() : "";
175
+ }
176
+ prepareKittyScreen(screen) {
177
+ const visibleImageIds = new Set();
178
+ const lines = screen.map((line) => {
179
+ const placement = getKittyImagePlacement(line);
180
+ if (!placement)
181
+ return line;
182
+ visibleImageIds.add(placement.imageId);
183
+ const cachedImage = this.uploadedKittyImages.get(placement.imageId);
184
+ const nextCachedImage = {
185
+ transmissionGeneration: placement.transmissionGeneration,
186
+ transmissionBytes: placement.transmissionBytes,
187
+ estimatedDecodedBytes: placement.estimatedDecodedBytes,
188
+ };
189
+ if (cachedImage)
190
+ this.uploadedKittyImages.delete(placement.imageId);
191
+ this.uploadedKittyImages.set(placement.imageId, nextCachedImage);
192
+ return cachedImage?.transmissionGeneration === placement.transmissionGeneration
193
+ ? placement.replacementLine
194
+ : line;
195
+ });
196
+ let cachedOffscreenImageCount = 0;
197
+ let cachedOffscreenTransmissionBytes = 0;
198
+ let cachedOffscreenDecodedBytes = 0;
199
+ for (const [imageId, cachedImage] of this.uploadedKittyImages) {
200
+ if (visibleImageIds.has(imageId))
201
+ continue;
202
+ cachedOffscreenImageCount += 1;
203
+ cachedOffscreenTransmissionBytes += cachedImage.transmissionBytes;
204
+ cachedOffscreenDecodedBytes += cachedImage.estimatedDecodedBytes;
205
+ }
206
+ let evictedImageDeletion = "";
207
+ for (const [imageId, cachedImage] of this.uploadedKittyImages) {
208
+ if (cachedOffscreenImageCount <= MAX_CACHED_OFFSCREEN_KITTY_IMAGES &&
209
+ cachedOffscreenTransmissionBytes <= MAX_CACHED_OFFSCREEN_KITTY_TRANSMISSION_BYTES &&
210
+ cachedOffscreenDecodedBytes <= MAX_CACHED_OFFSCREEN_KITTY_DECODED_BYTES) {
211
+ break;
212
+ }
213
+ if (visibleImageIds.has(imageId))
214
+ continue;
215
+ evictedImageDeletion += deleteKittyImage(imageId);
216
+ this.uploadedKittyImages.delete(imageId);
217
+ cachedOffscreenImageCount -= 1;
218
+ cachedOffscreenTransmissionBytes -= cachedImage.transmissionBytes;
219
+ cachedOffscreenDecodedBytes -= cachedImage.estimatedDecodedBytes;
220
+ }
221
+ return { lines, evictedImageDeletion };
222
+ }
223
+ resetRenderState() {
224
+ this.previousScreen = [];
225
+ this.previousScreenWidth = 0;
226
+ this.previousScreenHeight = 0;
227
+ this.currentLayout = undefined;
228
+ }
229
+ scrollBy(lines) {
230
+ this.getPrimaryScrollView().scrollBy(lines);
231
+ this.requestRender();
232
+ }
233
+ scrollToTop() {
234
+ this.getPrimaryScrollView().scrollToStart();
235
+ this.requestRender();
236
+ }
237
+ scrollToBottom() {
238
+ this.getPrimaryScrollView().scrollToEnd();
239
+ this.requestRender();
240
+ }
241
+ scrollToPrompt(direction) {
242
+ if (!this.currentLayout)
243
+ return;
244
+ const scrollView = this.getPrimaryScrollView();
245
+ const lines = getScrollViewBox(this.currentLayout, scrollView)?.scrollContentLines;
246
+ if (!lines)
247
+ return;
248
+ for (let row = scrollView.scrollTop + direction; row >= 0 && row < lines.length; row += direction) {
249
+ if (!OSC133_PROMPT_START.test(lines[row] ?? ""))
250
+ continue;
251
+ scrollView.scrollTo(row);
252
+ this.requestRender();
253
+ return;
254
+ }
255
+ }
256
+ /** Show a transient message in the alternate-screen flash stack. */
257
+ flash(message, durationMs) {
258
+ this.flashes.flash(message, durationMs);
259
+ }
260
+ handleViewportInput(data) {
261
+ if (data === FOCUS_OUT) {
262
+ const hadActiveSelection = this.selectionPressActive;
263
+ this.selectionPressActive = false;
264
+ this.stopSelectionAutoScroll();
265
+ this.stopScrollbarHover();
266
+ this.stopScrollbarDrag();
267
+ this.pressedUrl = undefined;
268
+ this.selectionDragged = false;
269
+ if (hadActiveSelection) {
270
+ this.selectionAnchor = undefined;
271
+ this.selectionFocus = undefined;
272
+ this.selectionGranularity = "character";
273
+ this.selectionInitialRange = undefined;
274
+ }
275
+ this.lastClick = undefined;
276
+ this.requestRender();
277
+ return { consume: true };
278
+ }
279
+ if (data === FOCUS_IN)
280
+ return { consume: true };
281
+ const wheelEvent = this.parseWheelEvent(data);
282
+ if (wheelEvent) {
283
+ this.routeWheel(wheelEvent);
284
+ return { consume: true };
285
+ }
286
+ const mouseEvent = this.parseSgrMouseEvent(data);
287
+ if (mouseEvent) {
288
+ if (this.handleRightClickPaste(mouseEvent))
289
+ return { consume: true };
290
+ const handled = this.handleScrollbarMouseEvent(mouseEvent);
291
+ if (!this.scrollbarDrag)
292
+ this.updateScrollbarHover(mouseEvent.x, mouseEvent.y);
293
+ if (!handled)
294
+ this.handleSelectionMouseEvent(mouseEvent);
295
+ return { consume: true };
296
+ }
297
+ if (this.isMouseSequence(data))
298
+ return { consume: true };
299
+ const keybindings = getKeybindings();
300
+ const isRelease = isKeyRelease(data);
301
+ if (keybindings.matches(data, "tui.altScreen.pageUp")) {
302
+ if (!isRelease) {
303
+ this.scrollBy(-Math.max(1, this.getPrimaryScrollView().viewportHeight - PAGE_SCROLL_OVERLAP));
304
+ }
305
+ return { consume: true };
306
+ }
307
+ if (keybindings.matches(data, "tui.altScreen.pageDown")) {
308
+ if (!isRelease) {
309
+ this.scrollBy(Math.max(1, this.getPrimaryScrollView().viewportHeight - PAGE_SCROLL_OVERLAP));
310
+ }
311
+ return { consume: true };
312
+ }
313
+ if (keybindings.matches(data, "tui.altScreen.halfPageUp")) {
314
+ if (!isRelease)
315
+ this.scrollBy(-Math.max(1, Math.floor(this.getPrimaryScrollView().viewportHeight / 2)));
316
+ return { consume: true };
317
+ }
318
+ if (keybindings.matches(data, "tui.altScreen.halfPageDown")) {
319
+ if (!isRelease)
320
+ this.scrollBy(Math.max(1, Math.floor(this.getPrimaryScrollView().viewportHeight / 2)));
321
+ return { consume: true };
322
+ }
323
+ if (keybindings.matches(data, "tui.altScreen.previousPrompt")) {
324
+ if (!isRelease)
325
+ this.scrollToPrompt(-1);
326
+ return { consume: true };
327
+ }
328
+ if (keybindings.matches(data, "tui.altScreen.nextPrompt")) {
329
+ if (!isRelease)
330
+ this.scrollToPrompt(1);
331
+ return { consume: true };
332
+ }
333
+ if (keybindings.matches(data, "tui.altScreen.top")) {
334
+ if (!isRelease)
335
+ this.scrollToTop();
336
+ return { consume: true };
337
+ }
338
+ if (keybindings.matches(data, "tui.altScreen.bottom")) {
339
+ if (!isRelease)
340
+ this.scrollToBottom();
341
+ return { consume: true };
342
+ }
343
+ return undefined;
344
+ }
345
+ parseWheelEvent(data) {
346
+ const sgr = /^\x1b\[<(\d+);(\d+);(\d+)[Mm]$/.exec(data);
347
+ if (sgr) {
348
+ const button = Number.parseInt(sgr[1], 10);
349
+ if ((button & 64) === 0)
350
+ return undefined;
351
+ const direction = button & 3;
352
+ if (direction !== 0 && direction !== 1)
353
+ return undefined;
354
+ return {
355
+ direction: direction === 0 ? -1 : 1,
356
+ x: Number.parseInt(sgr[2], 10) - 1,
357
+ y: Number.parseInt(sgr[3], 10) - 1,
358
+ };
359
+ }
360
+ if (data.length === 6 && data.startsWith("\x1b[M")) {
361
+ const button = data.charCodeAt(3) - 32;
362
+ if ((button & 64) === 0)
363
+ return undefined;
364
+ const direction = button & 3;
365
+ if (direction !== 0 && direction !== 1)
366
+ return undefined;
367
+ return {
368
+ direction: direction === 0 ? -1 : 1,
369
+ x: data.charCodeAt(4) - 33,
370
+ y: data.charCodeAt(5) - 33,
371
+ };
372
+ }
373
+ return undefined;
374
+ }
375
+ routeWheel(event) {
376
+ let remaining = event.direction * this.wheelScrollLines;
377
+ const seen = new Set();
378
+ for (const scrollView of this.currentLayout ? getScrollViewsAt(this.currentLayout, event.x, event.y) : []) {
379
+ seen.add(scrollView);
380
+ remaining = scrollView.scrollBy(remaining);
381
+ if (remaining === 0 || scrollView.overscroll === "contain")
382
+ break;
383
+ }
384
+ const primary = this.getPrimaryScrollView();
385
+ if (remaining !== 0 && !seen.has(primary))
386
+ primary.scrollBy(remaining);
387
+ this.updateScrollbarHover(event.x, event.y);
388
+ this.requestRender();
389
+ }
390
+ parseSgrMouseEvent(data) {
391
+ const match = /^\x1b\[<(\d+);(\d+);(\d+)([Mm])$/.exec(data);
392
+ if (!match)
393
+ return undefined;
394
+ return {
395
+ button: Number.parseInt(match[1], 10),
396
+ x: Number.parseInt(match[2], 10) - 1,
397
+ y: Number.parseInt(match[3], 10) - 1,
398
+ release: match[4] === "m",
399
+ };
400
+ }
401
+ handleRightClickPaste(event) {
402
+ if (!this.onRightClickPaste || process.platform !== "win32" || event.release || event.button !== 2) {
403
+ return false;
404
+ }
405
+ try {
406
+ this.onRightClickPaste();
407
+ }
408
+ catch {
409
+ // Clipboard paste is best-effort.
410
+ }
411
+ return true;
412
+ }
413
+ getScrollbarTargetAt(x, y) {
414
+ if (this.hasOverlay() || !this.currentLayout)
415
+ return undefined;
416
+ for (const scrollView of getScrollViewsAt(this.currentLayout, x, y)) {
417
+ const box = getScrollViewBox(this.currentLayout, scrollView);
418
+ const geometry = box ? getScrollbarGeometry(box) : undefined;
419
+ if (geometry &&
420
+ x === geometry.column &&
421
+ y >= geometry.thumbTop &&
422
+ y < geometry.thumbTop + geometry.thumbHeight) {
423
+ return { scrollView, geometry };
424
+ }
425
+ }
426
+ return undefined;
427
+ }
428
+ setScrollbarHover(scrollView) {
429
+ if (scrollView === this.scrollbarHover)
430
+ return;
431
+ this.scrollbarHover?.setScrollbarActive(false);
432
+ this.scrollbarHover = scrollView;
433
+ this.scrollbarHover?.setScrollbarActive(true);
434
+ }
435
+ updateScrollbarHover(x, y) {
436
+ this.setScrollbarHover(this.getScrollbarTargetAt(x, y)?.scrollView);
437
+ }
438
+ stopScrollbarHover() {
439
+ this.setScrollbarHover(undefined);
440
+ }
441
+ handleScrollbarMouseEvent(event) {
442
+ if (this.scrollbarDrag) {
443
+ if (event.release) {
444
+ this.stopScrollbarDrag();
445
+ return true;
446
+ }
447
+ const box = this.currentLayout
448
+ ? getScrollViewBox(this.currentLayout, this.scrollbarDrag.scrollView)
449
+ : undefined;
450
+ const geometry = box ? getScrollbarGeometry(box) : undefined;
451
+ if (geometry) {
452
+ const maxThumbOffset = geometry.trackHeight - geometry.thumbHeight;
453
+ const thumbOffset = Math.max(0, Math.min(maxThumbOffset, event.y - geometry.trackTop - this.scrollbarDrag.grabOffset));
454
+ const scrollTop = maxThumbOffset === 0 ? 0 : Math.round((thumbOffset / maxThumbOffset) * geometry.maxScrollTop);
455
+ this.scrollbarDrag.scrollView.scrollTo(scrollTop);
456
+ }
457
+ return true;
458
+ }
459
+ if (event.release || (event.button & 32) !== 0 || (event.button & 3) !== 0)
460
+ return false;
461
+ const target = this.getScrollbarTargetAt(event.x, event.y);
462
+ if (!target)
463
+ return false;
464
+ this.stopSelectionAutoScroll();
465
+ this.selectionPressActive = false;
466
+ this.selectionAnchor = undefined;
467
+ this.selectionFocus = undefined;
468
+ this.selectionGranularity = "character";
469
+ this.selectionInitialRange = undefined;
470
+ this.lastClick = undefined;
471
+ this.pressedUrl = undefined;
472
+ this.selectionDragged = false;
473
+ this.setScrollbarHover(target.scrollView);
474
+ this.scrollbarDrag = {
475
+ scrollView: target.scrollView,
476
+ grabOffset: event.y - target.geometry.thumbTop,
477
+ };
478
+ return true;
479
+ }
480
+ stopScrollbarDrag() {
481
+ this.scrollbarDrag = undefined;
482
+ }
483
+ getScrollSelectionPoint(scrollView, x, y) {
484
+ if (!this.currentLayout)
485
+ return undefined;
486
+ const box = getScrollViewBox(this.currentLayout, scrollView);
487
+ if (!box || box.rect.height <= 0 || box.clip.height <= 0)
488
+ return undefined;
489
+ const visibleTop = Math.max(0, box.rect.y, box.clip.y);
490
+ const visibleBottom = Math.min(this.terminal.rows - 1, box.rect.y + box.rect.height - 1, box.clip.y + box.clip.height - 1);
491
+ if (visibleBottom < visibleTop)
492
+ return undefined;
493
+ const pointerRow = Math.max(visibleTop, Math.min(visibleBottom, y));
494
+ const maxContentRow = Math.max(0, (box.scrollContentLines?.length ?? 1) - 1);
495
+ return {
496
+ row: Math.max(0, Math.min(maxContentRow, scrollView.scrollTop + pointerRow - box.rect.y)),
497
+ col: Math.max(0, Math.min(box.rect.width - 1, x - box.rect.x)),
498
+ scrollView,
499
+ };
500
+ }
501
+ getSelectionPoint(event, scrollView) {
502
+ if (scrollView) {
503
+ const point = this.getScrollSelectionPoint(scrollView, event.x, event.y);
504
+ if (point)
505
+ return point;
506
+ }
507
+ return {
508
+ row: Math.max(0, Math.min(this.terminal.rows - 1, event.y)),
509
+ col: Math.max(0, Math.min(this.terminal.columns - 1, event.x)),
510
+ };
511
+ }
512
+ getSelectionSourceLine(point) {
513
+ if (point.scrollView && this.currentLayout) {
514
+ const lines = getScrollViewBox(this.currentLayout, point.scrollView)?.scrollContentLines;
515
+ if (lines)
516
+ return lines[point.row] ?? "";
517
+ }
518
+ return this.previousScreen[point.row] ?? "";
519
+ }
520
+ getWordSelection(point) {
521
+ const line = stripTerminalSequences(this.getSelectionSourceLine(point));
522
+ let start = 0;
523
+ for (const segment of wordSegmenter.segment(line)) {
524
+ const end = start + visibleWidth(segment.segment);
525
+ if (point.col >= start && point.col < end) {
526
+ return {
527
+ start: { ...point, col: start },
528
+ end: { ...point, col: end, boundary: true },
529
+ };
530
+ }
531
+ start = end;
532
+ }
533
+ return undefined;
534
+ }
535
+ getLineSelection(point) {
536
+ return {
537
+ start: { ...point, col: 0 },
538
+ end: { ...point, col: visibleWidth(this.getSelectionSourceLine(point)), boundary: true },
539
+ };
540
+ }
541
+ updateSelectionFocus(point) {
542
+ if (this.selectionGranularity === "character" || !this.selectionInitialRange) {
543
+ this.selectionFocus = point;
544
+ return;
545
+ }
546
+ const range = this.selectionGranularity === "word" ? this.getWordSelection(point) : this.getLineSelection(point);
547
+ if (!range)
548
+ return;
549
+ const initial = this.selectionInitialRange;
550
+ const targetBeforeInitial = range.start.row < initial.start.row ||
551
+ (range.start.row === initial.start.row && range.start.col < initial.start.col);
552
+ if (targetBeforeInitial) {
553
+ this.selectionAnchor = initial.end;
554
+ this.selectionFocus = range.start;
555
+ }
556
+ else {
557
+ this.selectionAnchor = initial.start;
558
+ this.selectionFocus = range.end;
559
+ }
560
+ }
561
+ getClickCount(point, word) {
562
+ const now = Date.now();
563
+ const previous = this.lastClick;
564
+ const count = word &&
565
+ previous &&
566
+ now - previous.timestamp <= DOUBLE_CLICK_INTERVAL_MS &&
567
+ previous.row === point.row &&
568
+ previous.scrollView === point.scrollView &&
569
+ previous.wordStart === word.start.col &&
570
+ previous.wordEnd === word.end.col
571
+ ? (previous.count % 3) + 1
572
+ : 1;
573
+ this.lastClick = word
574
+ ? {
575
+ timestamp: now,
576
+ count,
577
+ row: point.row,
578
+ scrollView: point.scrollView,
579
+ wordStart: word.start.col,
580
+ wordEnd: word.end.col,
581
+ }
582
+ : undefined;
583
+ return count;
584
+ }
585
+ updateSelectionAutoScroll(event) {
586
+ const scrollView = this.selectionAnchor?.scrollView;
587
+ if (!scrollView || !this.currentLayout) {
588
+ this.stopSelectionAutoScroll();
589
+ return;
590
+ }
591
+ const box = getScrollViewBox(this.currentLayout, scrollView);
592
+ if (!box || box.rect.height <= 0 || box.clip.height <= 0) {
593
+ this.stopSelectionAutoScroll();
594
+ return;
595
+ }
596
+ const visibleTop = Math.max(0, box.rect.y, box.clip.y);
597
+ const visibleBottom = Math.min(this.terminal.rows - 1, box.rect.y + box.rect.height - 1, box.clip.y + box.clip.height - 1);
598
+ this.selectionDragPointer = { x: event.x, y: event.y };
599
+ this.selectionAutoScrollDirection = event.y <= visibleTop ? -1 : event.y >= visibleBottom ? 1 : 0;
600
+ if (this.selectionAutoScrollDirection === 0) {
601
+ this.stopSelectionAutoScroll();
602
+ return;
603
+ }
604
+ if (this.selectionAutoScrollTimer)
605
+ return;
606
+ this.selectionAutoScrollTimer = setInterval(() => this.autoScrollSelection(), 50);
607
+ this.selectionAutoScrollTimer.unref();
608
+ }
609
+ autoScrollSelection() {
610
+ const scrollView = this.selectionAnchor?.scrollView;
611
+ const pointer = this.selectionDragPointer;
612
+ const direction = this.selectionAutoScrollDirection;
613
+ if (!scrollView || !pointer || direction === 0) {
614
+ this.stopSelectionAutoScroll();
615
+ return;
616
+ }
617
+ const remaining = scrollView.scrollBy(direction);
618
+ if (remaining === direction) {
619
+ this.stopSelectionAutoScroll();
620
+ return;
621
+ }
622
+ const point = this.getScrollSelectionPoint(scrollView, pointer.x, pointer.y);
623
+ if (point)
624
+ this.updateSelectionFocus(point);
625
+ this.requestRender();
626
+ }
627
+ stopSelectionAutoScroll() {
628
+ if (this.selectionAutoScrollTimer) {
629
+ clearInterval(this.selectionAutoScrollTimer);
630
+ this.selectionAutoScrollTimer = undefined;
631
+ }
632
+ this.selectionAutoScrollDirection = 0;
633
+ this.selectionDragPointer = undefined;
634
+ }
635
+ handleSelectionMouseEvent(event) {
636
+ if ((event.button & 3) !== 0)
637
+ return;
638
+ const anchorScrollView = this.selectionAnchor?.scrollView;
639
+ const point = this.getSelectionPoint(event, anchorScrollView);
640
+ if (event.release) {
641
+ if (!this.selectionPressActive)
642
+ return;
643
+ this.selectionPressActive = false;
644
+ this.stopSelectionAutoScroll();
645
+ if (!this.selectionAnchor)
646
+ return;
647
+ this.updateSelectionFocus(point);
648
+ const clickedUrl = !this.selectionDragged &&
649
+ this.selectionAnchor.scrollView === point.scrollView &&
650
+ this.selectionAnchor.row === point.row &&
651
+ this.selectionAnchor.col === point.col
652
+ ? this.pressedUrl
653
+ : undefined;
654
+ this.pressedUrl = undefined;
655
+ if (clickedUrl && this.openUrl) {
656
+ this.selectionAnchor = undefined;
657
+ this.selectionFocus = undefined;
658
+ try {
659
+ this.openUrl(clickedUrl);
660
+ }
661
+ catch {
662
+ // URL activation is best-effort.
663
+ }
664
+ this.requestRender();
665
+ return;
666
+ }
667
+ this.copySelectionToClipboard();
668
+ this.requestRender();
669
+ return;
670
+ }
671
+ if ((event.button & 32) !== 0) {
672
+ if (!this.selectionPressActive || !this.selectionAnchor)
673
+ return;
674
+ this.selectionDragged = true;
675
+ this.lastClick = undefined;
676
+ this.pressedUrl = undefined;
677
+ this.updateSelectionFocus(point);
678
+ this.updateSelectionAutoScroll(event);
679
+ this.requestRender();
680
+ return;
681
+ }
682
+ this.stopSelectionAutoScroll();
683
+ this.selectionPressActive = true;
684
+ const scrollView = !this.hasOverlay() && this.currentLayout
685
+ ? getScrollViewsAt(this.currentLayout, event.x, event.y)[0]
686
+ : undefined;
687
+ const anchor = this.getSelectionPoint(event, scrollView);
688
+ const word = this.getWordSelection(anchor);
689
+ const clickCount = this.getClickCount(anchor, word);
690
+ const range = clickCount === 2 ? word : clickCount === 3 ? this.getLineSelection(anchor) : undefined;
691
+ this.selectionGranularity = range ? (clickCount === 2 ? "word" : "line") : "character";
692
+ this.selectionInitialRange = range;
693
+ this.selectionAnchor = range?.start ?? anchor;
694
+ this.selectionFocus = range?.end ?? anchor;
695
+ this.selectionDragged = false;
696
+ this.pressedUrl = range
697
+ ? undefined
698
+ : 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)));
699
+ this.requestRender();
700
+ }
701
+ getSelectionBounds() {
702
+ if (!this.selectionAnchor || !this.selectionFocus)
703
+ return undefined;
704
+ if (this.selectionAnchor.scrollView !== this.selectionFocus.scrollView)
705
+ return undefined;
706
+ const anchorBeforeFocus = this.selectionAnchor.row < this.selectionFocus.row ||
707
+ (this.selectionAnchor.row === this.selectionFocus.row && this.selectionAnchor.col < this.selectionFocus.col);
708
+ if (this.selectionAnchor.row === this.selectionFocus.row &&
709
+ this.selectionAnchor.col === this.selectionFocus.col) {
710
+ return undefined;
711
+ }
712
+ return anchorBeforeFocus
713
+ ? { start: this.selectionAnchor, end: this.selectionFocus }
714
+ : { start: this.selectionFocus, end: this.selectionAnchor };
715
+ }
716
+ getSelectionColumns(line, row, selection, minColumn = 0, maxColumn = visibleWidth(line)) {
717
+ const lineWidth = visibleWidth(line);
718
+ let start = Math.max(0, minColumn);
719
+ let end = Math.min(lineWidth, maxColumn);
720
+ if (row === selection.start.row) {
721
+ start = getGraphemeCellRange(line, selection.start.col)?.start ?? Math.min(selection.start.col, lineWidth);
722
+ }
723
+ if (row === selection.end.row) {
724
+ end = selection.end.boundary
725
+ ? Math.min(selection.end.col, lineWidth)
726
+ : (getGraphemeCellRange(line, selection.end.col)?.end ?? Math.min(selection.end.col + 1, lineWidth));
727
+ }
728
+ return { start: Math.max(minColumn, start), end: Math.min(maxColumn, end) };
729
+ }
730
+ copySelectionToClipboard() {
731
+ const selection = this.getSelectionBounds();
732
+ if (!selection)
733
+ return;
734
+ let sourceLines = this.previousScreen;
735
+ if (selection.start.scrollView) {
736
+ if (!this.currentLayout)
737
+ return;
738
+ const box = getScrollViewBox(this.currentLayout, selection.start.scrollView);
739
+ if (!box?.scrollContentLines)
740
+ return;
741
+ sourceLines = box.scrollContentLines;
742
+ }
743
+ const lines = [];
744
+ for (let row = selection.start.row; row <= selection.end.row; row++) {
745
+ const line = sourceLines[row] ?? "";
746
+ const columns = this.getSelectionColumns(line, row, selection);
747
+ lines.push(stripTerminalSequences(sliceByColumn(line, columns.start, Math.max(0, columns.end - columns.start), true)).trimEnd());
748
+ }
749
+ const text = lines.join("\n");
750
+ if (text.length === 0)
751
+ return;
752
+ this.terminal.write(`\x1b]52;c;${Buffer.from(text).toString("base64")}\x07`);
753
+ this.flash("Copied!");
754
+ }
755
+ applySelectionHighlight(text) {
756
+ let result = "\x1b[7m";
757
+ let index = 0;
758
+ while (index < text.length) {
759
+ const ansi = extractAnsiCode(text, index);
760
+ if (!ansi) {
761
+ result += text[index];
762
+ index += 1;
763
+ continue;
764
+ }
765
+ result += ansi.code;
766
+ if (ansi.code.endsWith("m"))
767
+ result += "\x1b[7m";
768
+ index += ansi.length;
769
+ }
770
+ return `${result}\x1b[27m`;
771
+ }
772
+ applySelection(screen, layout = this.currentLayout) {
773
+ const selection = this.getSelectionBounds();
774
+ if (!selection)
775
+ return screen;
776
+ let screenSelection = selection;
777
+ let minRow = 0;
778
+ let maxRow = screen.length - 1;
779
+ let minColumn = 0;
780
+ let maxColumn = this.terminal.columns;
781
+ if (selection.start.scrollView) {
782
+ if (!layout)
783
+ return screen;
784
+ const box = getScrollViewBox(layout, selection.start.scrollView);
785
+ if (!box)
786
+ return screen;
787
+ minRow = Math.max(0, box.rect.y, box.clip.y);
788
+ maxRow = Math.min(screen.length - 1, box.rect.y + box.rect.height - 1, box.clip.y + box.clip.height - 1);
789
+ minColumn = Math.max(0, box.rect.x, box.clip.x);
790
+ maxColumn = Math.min(this.terminal.columns, box.rect.x + box.rect.width, box.clip.x + box.clip.width);
791
+ screenSelection = {
792
+ start: {
793
+ ...selection.start,
794
+ row: box.rect.y + selection.start.row - selection.start.scrollView.scrollTop,
795
+ col: box.rect.x + selection.start.col,
796
+ },
797
+ end: {
798
+ ...selection.end,
799
+ row: box.rect.y + selection.end.row - selection.start.scrollView.scrollTop,
800
+ col: box.rect.x + selection.end.col,
801
+ },
802
+ };
803
+ }
804
+ return screen.map((line, row) => {
805
+ if (row < minRow ||
806
+ row > maxRow ||
807
+ row < screenSelection.start.row ||
808
+ row > screenSelection.end.row ||
809
+ isImageLine(line)) {
810
+ return line;
811
+ }
812
+ const lineWidth = visibleWidth(line);
813
+ const columns = this.getSelectionColumns(line, row, screenSelection, minColumn, maxColumn);
814
+ if (columns.end <= columns.start)
815
+ return line;
816
+ const before = sliceByColumn(line, 0, columns.start, true);
817
+ const selected = sliceByColumn(line, columns.start, columns.end - columns.start, true);
818
+ const after = sliceByColumn(line, columns.end, Math.max(0, lineWidth - columns.end), true);
819
+ return `${before}${this.applySelectionHighlight(selected)}${after}`;
820
+ });
821
+ }
822
+ isMouseSequence(data) {
823
+ return /^\x1b\[<\d+;\d+;\d+[Mm]$/.test(data) || (data.length === 6 && data.startsWith("\x1b[M"));
824
+ }
825
+ compositeFlashes(screen, width, height) {
826
+ const flashLines = this.flashes.render(width).slice(-height);
827
+ if (flashLines.length === 0)
828
+ return screen;
829
+ const result = [...screen];
830
+ while (result.length < height)
831
+ result.push("");
832
+ for (let row = 0; row < flashLines.length; row++) {
833
+ const line = flashLines[row];
834
+ const flashWidth = visibleWidth(line);
835
+ if (flashWidth === 0)
836
+ continue;
837
+ result[row] = compositeTuiLine(result[row] ?? "", line, width - flashWidth, flashWidth, width);
838
+ }
839
+ return result;
840
+ }
841
+ doRender() {
842
+ if (this.stopped || !this.altScreenActive)
843
+ return;
844
+ const width = Math.max(1, this.terminal.columns);
845
+ const height = Math.max(1, this.terminal.rows);
846
+ const root = this.layoutRoot ?? this.implicitScrollView;
847
+ const nextLayout = renderLayoutFrame(root, width, height, () => this.requestRender());
848
+ let screen = nextLayout.lines.map((line) => line.replace(OSC133_ZONE_PREFIX, ""));
849
+ screen = this.compositeOverlays(screen, width, height);
850
+ if (screen.length > height)
851
+ screen = screen.slice(screen.length - height);
852
+ screen = this.applySelection(screen, nextLayout);
853
+ screen = this.compositeFlashes(screen, width, height);
854
+ const cursorPos = this.extractCursorPosition(screen, height);
855
+ screen = this.applyLineResets(screen).map((line) => {
856
+ if (isImageLine(line) || visibleWidth(line) <= width)
857
+ return line;
858
+ return sliceByColumn(line, 0, width, true);
859
+ });
860
+ const fullRedraw = this.previousScreen.length === 0 || this.previousScreenWidth !== width || this.previousScreenHeight !== height;
861
+ const imagesNeedRedraw = screen.some((line, row) => line !== this.previousScreen[row] && (isImageLine(line) || isImageLine(this.previousScreen[row] ?? "")));
862
+ const redrawImages = fullRedraw || imagesNeedRedraw;
863
+ const hadUploadedKittyImages = this.uploadedKittyImages.size > 0;
864
+ const preparedKittyScreen = redrawImages && this.imageProtocol === "kitty"
865
+ ? this.prepareKittyScreen(screen)
866
+ : { lines: screen, evictedImageDeletion: "" };
867
+ let buffer = BEGIN_SYNCHRONIZED_OUTPUT;
868
+ if (fullRedraw) {
869
+ this.fullRedrawCount += 1;
870
+ const clearImages = this.imageProtocol === "kitty" && hadUploadedKittyImages
871
+ ? deleteAllKittyPlacements()
872
+ : this.deleteKittyImages();
873
+ buffer += `${clearImages}\x1b[2J`;
874
+ }
875
+ else if (imagesNeedRedraw) {
876
+ if (this.imageProtocol === "iterm2")
877
+ buffer += "\x1b[2J";
878
+ else if (this.imageProtocol === "kitty")
879
+ buffer += deleteAllKittyPlacements();
880
+ }
881
+ buffer += preparedKittyScreen.evictedImageDeletion;
882
+ for (let row = 0; row < height; row++) {
883
+ if (!fullRedraw && !imagesNeedRedraw && screen[row] === this.previousScreen[row])
884
+ continue;
885
+ buffer += `\x1b[${row + 1};1H\x1b[2K${preparedKittyScreen.lines[row] ?? ""}`;
886
+ }
887
+ if (cursorPos) {
888
+ buffer += `\x1b[${cursorPos.row + 1};${Math.min(width, cursorPos.col) + 1}H`;
889
+ buffer += this.getShowHardwareCursor() ? "\x1b[?25h" : "\x1b[?25l";
890
+ }
891
+ else {
892
+ buffer += "\x1b[?25l";
893
+ }
894
+ buffer += END_SYNCHRONIZED_OUTPUT;
895
+ this.terminal.write(buffer);
896
+ this.previousScreen = screen;
897
+ this.previousScreenWidth = width;
898
+ this.previousScreenHeight = height;
899
+ this.currentLayout = nextLayout;
900
+ }
901
+ }
902
+ //# sourceMappingURL=tui-alt-screen.js.map