jsgotty 1.1.1 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +100 -39
- package/README.md +116 -53
- package/client.mjs +8 -2
- package/gotty.js +137 -44
- package/package.json +1 -1
- package/static/css/kitty.css +7 -0
- package/static/help.md +1 -1
- package/static/js/kitty.js +284 -48
- package/tests/kitty-order.test.js +58 -0
- package/viu.mjs +8 -1
package/static/js/kitty.js
CHANGED
|
@@ -19,9 +19,56 @@
|
|
|
19
19
|
touchBound: false,
|
|
20
20
|
touchScroll: null,
|
|
21
21
|
contextMenu: null,
|
|
22
|
-
imageContextBound: false
|
|
22
|
+
imageContextBound: false,
|
|
23
|
+
socket: null
|
|
23
24
|
};
|
|
24
25
|
|
|
26
|
+
function browserTerminalState() {
|
|
27
|
+
const xterm = terminal && terminal.__gottyXterm;
|
|
28
|
+
const active = xterm && xterm.buffer && xterm.buffer.active;
|
|
29
|
+
if (!active) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const { screen, viewport } = xtermElements();
|
|
33
|
+
const terminalRect = terminal.getBoundingClientRect();
|
|
34
|
+
const screenRect = screen && screen.getBoundingClientRect();
|
|
35
|
+
const viewportRect = viewport && viewport.getBoundingClientRect();
|
|
36
|
+
return {
|
|
37
|
+
type: active.type || null,
|
|
38
|
+
baseY: active.baseY,
|
|
39
|
+
viewportY: active.viewportY,
|
|
40
|
+
cursorX: active.cursorX,
|
|
41
|
+
cursorY: active.cursorY,
|
|
42
|
+
cursorCol: active.cursorX + 1,
|
|
43
|
+
cursorRow: active.cursorY + 1,
|
|
44
|
+
absoluteCursorRow: active.baseY + active.cursorY + 1,
|
|
45
|
+
windowInnerWidth: window.innerWidth,
|
|
46
|
+
documentClientWidth: document.documentElement.clientWidth,
|
|
47
|
+
terminalWidth: terminalRect.width,
|
|
48
|
+
screenWidth: screenRect?.width ?? null,
|
|
49
|
+
viewportWidth: viewportRect?.width ?? null,
|
|
50
|
+
viewportClientWidth: viewport?.clientWidth ?? null,
|
|
51
|
+
viewportScrollWidth: viewport?.scrollWidth ?? null,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function sendBrowserDebug(stage, details = {}) {
|
|
56
|
+
if (!KITTY_TRACE) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const socket = state.socket;
|
|
60
|
+
if (!socket || socket.readyState !== 1) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
socket.send("K" + JSON.stringify({
|
|
65
|
+
stage,
|
|
66
|
+
terminal: browserTerminalState(),
|
|
67
|
+
...details,
|
|
68
|
+
}));
|
|
69
|
+
} catch (error) {}
|
|
70
|
+
}
|
|
71
|
+
|
|
25
72
|
function xtermElements() {
|
|
26
73
|
if (!terminal) {
|
|
27
74
|
return {};
|
|
@@ -98,36 +145,65 @@
|
|
|
98
145
|
return `${imageId}:${control.p || "0"}`;
|
|
99
146
|
}
|
|
100
147
|
|
|
101
|
-
function currentCursorPosition() {
|
|
102
|
-
const xterm = terminal && terminal.__gottyXterm;
|
|
103
|
-
const active = xterm && xterm.buffer && xterm.buffer.active;
|
|
104
|
-
if (!active) {
|
|
105
|
-
return null;
|
|
106
|
-
}
|
|
107
|
-
return {
|
|
108
|
-
bufferRow: active.baseY + active.cursorY + 1,
|
|
109
|
-
col: active.cursorX + 1
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
148
|
function currentViewportY() {
|
|
114
149
|
const xterm = terminal && terminal.__gottyXterm;
|
|
115
150
|
const active = xterm && xterm.buffer && xterm.buffer.active;
|
|
116
151
|
return active ? active.viewportY : 0;
|
|
117
152
|
}
|
|
118
153
|
|
|
119
|
-
function createPlacementMarker() {
|
|
154
|
+
function createPlacementMarker(cursor) {
|
|
120
155
|
const xterm = terminal && terminal.__gottyXterm;
|
|
121
156
|
if (!xterm || typeof xterm.registerMarker !== "function") {
|
|
122
157
|
return null;
|
|
123
158
|
}
|
|
124
159
|
try {
|
|
125
|
-
|
|
160
|
+
const active = xterm.buffer && xterm.buffer.active;
|
|
161
|
+
let offset = 0;
|
|
162
|
+
if (active && cursor && Number.isFinite(cursor.bufferRow)) {
|
|
163
|
+
const currentBufferRow = active.baseY + active.cursorY + 1;
|
|
164
|
+
offset = Math.trunc(cursor.bufferRow - currentBufferRow);
|
|
165
|
+
} else if (
|
|
166
|
+
active && cursor && Number.isFinite(cursor.row) &&
|
|
167
|
+
cursor.row >= 1 && cursor.row <= state.rows
|
|
168
|
+
) {
|
|
169
|
+
// Kitty's cursor row is 1-based within the live terminal screen.
|
|
170
|
+
// registerMarker() takes an offset from xterm's current 0-based row.
|
|
171
|
+
offset = Math.trunc(cursor.row - (active.cursorY + 1));
|
|
172
|
+
}
|
|
173
|
+
const marker = xterm.registerMarker(offset);
|
|
174
|
+
sendBrowserDebug("marker-created", {
|
|
175
|
+
protocolCursor: cursor,
|
|
176
|
+
offset,
|
|
177
|
+
markerLine: marker?.line ?? null,
|
|
178
|
+
});
|
|
179
|
+
return marker;
|
|
126
180
|
} catch (error) {
|
|
127
181
|
return null;
|
|
128
182
|
}
|
|
129
183
|
}
|
|
130
184
|
|
|
185
|
+
function disposePlacementMarker(node) {
|
|
186
|
+
if (!node || !node.__kittyMarker) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
node.__kittyMarker.dispose();
|
|
191
|
+
} catch (error) {
|
|
192
|
+
// The marker may already have been disposed by xterm buffer trimming.
|
|
193
|
+
}
|
|
194
|
+
node.__kittyMarker = null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function removePlacementNode(node) {
|
|
198
|
+
if (!node) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
disposePlacementMarker(node);
|
|
202
|
+
node.__kittyGeneration = (node.__kittyGeneration || 0) + 1;
|
|
203
|
+
revokeNodeUrl(node);
|
|
204
|
+
node.remove();
|
|
205
|
+
}
|
|
206
|
+
|
|
131
207
|
function decodeRawImage(image) {
|
|
132
208
|
const binary = atob(image.data);
|
|
133
209
|
const bytes = new Uint8ClampedArray(binary.length);
|
|
@@ -197,6 +273,35 @@
|
|
|
197
273
|
});
|
|
198
274
|
}
|
|
199
275
|
|
|
276
|
+
function sourceRectangle(width, height, control = {}) {
|
|
277
|
+
const sourceWidth = Math.max(1, Math.trunc(Number(width) || 1));
|
|
278
|
+
const sourceHeight = Math.max(1, Math.trunc(Number(height) || 1));
|
|
279
|
+
const requestedX = Math.max(0, Math.trunc(Number.parseInt(control.x || "0", 10) || 0));
|
|
280
|
+
const requestedY = Math.max(0, Math.trunc(Number.parseInt(control.y || "0", 10) || 0));
|
|
281
|
+
const requestedWidth = Math.max(0, Math.trunc(Number.parseInt(control.w || "0", 10) || 0));
|
|
282
|
+
const requestedHeight = Math.max(0, Math.trunc(Number.parseInt(control.h || "0", 10) || 0));
|
|
283
|
+
const right = requestedWidth > 0 ? requestedX + requestedWidth : sourceWidth;
|
|
284
|
+
const bottom = requestedHeight > 0 ? requestedY + requestedHeight : sourceHeight;
|
|
285
|
+
const x = Math.min(sourceWidth, requestedX);
|
|
286
|
+
const y = Math.min(sourceHeight, requestedY);
|
|
287
|
+
const clippedRight = Math.min(sourceWidth, Math.max(x, right));
|
|
288
|
+
const clippedBottom = Math.min(sourceHeight, Math.max(y, bottom));
|
|
289
|
+
const cropWidth = clippedRight - x;
|
|
290
|
+
const cropHeight = clippedBottom - y;
|
|
291
|
+
if (cropWidth < 1 || cropHeight < 1) {
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
return {
|
|
295
|
+
x,
|
|
296
|
+
y,
|
|
297
|
+
width: cropWidth,
|
|
298
|
+
height: cropHeight,
|
|
299
|
+
sourceWidth,
|
|
300
|
+
sourceHeight,
|
|
301
|
+
cropped: x !== 0 || y !== 0 || cropWidth !== sourceWidth || cropHeight !== sourceHeight,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
200
305
|
async function blobDimensions(blob) {
|
|
201
306
|
if (typeof createImageBitmap === "function") {
|
|
202
307
|
const bitmap = await createImageBitmap(blob);
|
|
@@ -224,23 +329,35 @@
|
|
|
224
329
|
});
|
|
225
330
|
}
|
|
226
331
|
|
|
227
|
-
async function resolveImageAsset(image) {
|
|
332
|
+
async function resolveImageAsset(image, control = {}) {
|
|
228
333
|
if (!image) {
|
|
229
334
|
return null;
|
|
230
335
|
}
|
|
336
|
+
let blob;
|
|
337
|
+
let dimensions;
|
|
231
338
|
if (image.format === 100) {
|
|
232
|
-
|
|
339
|
+
blob = new Blob([binaryFromBase64(image.data)], {
|
|
233
340
|
type: inferBlobMimeType(image, image.control || {})
|
|
234
341
|
});
|
|
235
|
-
|
|
236
|
-
|
|
342
|
+
dimensions = await blobDimensions(blob);
|
|
343
|
+
} else {
|
|
344
|
+
const canvas = decodeRawImage(image);
|
|
345
|
+
if (!canvas) {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
blob = await canvasToBlob(canvas);
|
|
349
|
+
dimensions = { width: canvas.width, height: canvas.height };
|
|
237
350
|
}
|
|
238
|
-
const
|
|
239
|
-
if (!
|
|
351
|
+
const sourceRect = sourceRectangle(dimensions.width, dimensions.height, control);
|
|
352
|
+
if (!sourceRect) {
|
|
240
353
|
return null;
|
|
241
354
|
}
|
|
242
|
-
|
|
243
|
-
|
|
355
|
+
return {
|
|
356
|
+
blob,
|
|
357
|
+
width: sourceRect.width,
|
|
358
|
+
height: sourceRect.height,
|
|
359
|
+
sourceRect,
|
|
360
|
+
};
|
|
244
361
|
}
|
|
245
362
|
|
|
246
363
|
function revokeNodeUrl(node) {
|
|
@@ -254,22 +371,116 @@
|
|
|
254
371
|
if (!node || !node.__kittyPlacement) {
|
|
255
372
|
return;
|
|
256
373
|
}
|
|
257
|
-
const { control, cursor, intrinsicWidth, intrinsicHeight } = node.__kittyPlacement;
|
|
374
|
+
const { control, cursor, intrinsicWidth, intrinsicHeight, sourceRect } = node.__kittyPlacement;
|
|
258
375
|
const metrics = cellMetrics();
|
|
259
376
|
const anchor = anchorOffset();
|
|
260
|
-
const
|
|
261
|
-
const
|
|
377
|
+
const controlCols = Math.max(0, Number.parseInt(control.c || "0", 10) || 0);
|
|
378
|
+
const controlRows = Math.max(0, Number.parseInt(control.r || "0", 10) || 0);
|
|
379
|
+
let requestedCols;
|
|
380
|
+
let requestedRows;
|
|
381
|
+
if (controlCols > 0 && controlRows > 0) {
|
|
382
|
+
requestedCols = controlCols;
|
|
383
|
+
requestedRows = controlRows;
|
|
384
|
+
} else if (controlCols > 0) {
|
|
385
|
+
requestedCols = controlCols;
|
|
386
|
+
requestedRows = Math.max(1, Math.round(
|
|
387
|
+
controlCols * metrics.width * intrinsicHeight / intrinsicWidth / metrics.height,
|
|
388
|
+
));
|
|
389
|
+
} else if (controlRows > 0) {
|
|
390
|
+
requestedRows = controlRows;
|
|
391
|
+
requestedCols = Math.max(1, Math.round(
|
|
392
|
+
controlRows * metrics.height * intrinsicWidth / intrinsicHeight / metrics.width,
|
|
393
|
+
));
|
|
394
|
+
} else {
|
|
395
|
+
requestedCols = Math.max(1, Math.round(intrinsicWidth / Math.max(1, metrics.width)));
|
|
396
|
+
requestedRows = Math.max(1, Math.round(intrinsicHeight / Math.max(1, metrics.height)));
|
|
397
|
+
}
|
|
398
|
+
const xterm = terminal && terminal.__gottyXterm;
|
|
399
|
+
const viewerCols = Math.max(1, Math.trunc(Number(xterm?.cols) || state.cols));
|
|
400
|
+
const viewerRows = Math.max(1, Math.trunc(Number(xterm?.rows) || state.rows));
|
|
401
|
+
const cursorCol = Math.max(1, Math.trunc(Number(cursor?.col) || 1));
|
|
402
|
+
// A fixed-width PTY may be wider than the browser's xterm instance. Match
|
|
403
|
+
// viu's final-column safety margin using the viewer's real column count,
|
|
404
|
+
// then scale both axes so the image is visible rather than merely clipped.
|
|
405
|
+
const availableViewerCols = Math.max(1, viewerCols - cursorCol);
|
|
406
|
+
const scale = Math.min(1, availableViewerCols / requestedCols);
|
|
407
|
+
const cols = Math.max(1, Math.min(requestedCols, availableViewerCols));
|
|
408
|
+
const rows = Math.max(1, Math.round(requestedRows * scale));
|
|
262
409
|
const marker = node.__kittyMarker;
|
|
263
|
-
const
|
|
264
|
-
|
|
410
|
+
const activeBuffer = xterm?.buffer?.active;
|
|
411
|
+
const useProtocolRow = activeBuffer?.type === "alternate" && cursor && Number.isFinite(cursor.row);
|
|
412
|
+
const row = useProtocolRow
|
|
413
|
+
? cursor.row
|
|
414
|
+
: marker && !marker.isDisposed && Number.isFinite(marker.line)
|
|
415
|
+
? marker.line - currentViewportY() + 1
|
|
265
416
|
: cursor && Number.isFinite(cursor.bufferRow)
|
|
266
417
|
? cursor.bufferRow - currentViewportY()
|
|
267
418
|
: (cursor && cursor.row) || 1;
|
|
419
|
+
// Fixed-size PTYs can have more rows than the browser-side xterm. Leave
|
|
420
|
+
// the viewer's final row for the TUI status line in that mismatch mode.
|
|
421
|
+
const viewerBottomRow = Math.max(1, viewerRows - (viewerRows < state.rows ? 1 : 0));
|
|
422
|
+
const visibleRows = Math.max(0, Math.min(rows, viewerBottomRow - row + 1));
|
|
423
|
+
node.style.display = visibleRows > 0 ? "block" : "none";
|
|
268
424
|
node.style.left = `${anchor.left + (Math.max(1, cursor.col) - 1) * metrics.width}px`;
|
|
269
425
|
node.style.top = `${anchor.top + (row - 1) * metrics.height}px`;
|
|
270
426
|
node.style.width = `${cols * metrics.width}px`;
|
|
271
|
-
node.style.height = `${
|
|
427
|
+
node.style.height = `${visibleRows * metrics.height}px`;
|
|
272
428
|
node.style.zIndex = String(Number.parseInt(control.z || "0", 10) || 0);
|
|
429
|
+
const imageElement = node.__kittyImageElement;
|
|
430
|
+
if (imageElement && sourceRect) {
|
|
431
|
+
const targetWidth = cols * metrics.width;
|
|
432
|
+
const targetHeight = rows * metrics.height;
|
|
433
|
+
const sourceScaleX = targetWidth / sourceRect.width;
|
|
434
|
+
const sourceScaleY = targetHeight / sourceRect.height;
|
|
435
|
+
imageElement.style.left = `${-sourceRect.x * sourceScaleX}px`;
|
|
436
|
+
imageElement.style.top = `${-sourceRect.y * sourceScaleY}px`;
|
|
437
|
+
imageElement.style.width = `${sourceRect.sourceWidth * sourceScaleX}px`;
|
|
438
|
+
imageElement.style.height = `${sourceRect.sourceHeight * sourceScaleY}px`;
|
|
439
|
+
}
|
|
440
|
+
const { screen, viewport } = xtermElements();
|
|
441
|
+
const overlayRect = overlay.getBoundingClientRect();
|
|
442
|
+
const terminalRect = terminal.getBoundingClientRect();
|
|
443
|
+
const screenRect = screen && screen.getBoundingClientRect();
|
|
444
|
+
const viewportRect = viewport && viewport.getBoundingClientRect();
|
|
445
|
+
sendBrowserDebug("layout", {
|
|
446
|
+
imageId: control.i || control.I || null,
|
|
447
|
+
placementId: control.p || null,
|
|
448
|
+
requestedCols,
|
|
449
|
+
requestedRows,
|
|
450
|
+
viewerCols,
|
|
451
|
+
viewerRows,
|
|
452
|
+
viewerBottomRow,
|
|
453
|
+
terminalVisibleRows: visibleRows,
|
|
454
|
+
availableViewerCols,
|
|
455
|
+
viewerScale: scale,
|
|
456
|
+
positionSource: useProtocolRow ? "protocol" : "marker",
|
|
457
|
+
sourceRect: sourceRect || null,
|
|
458
|
+
protocolCursor: cursor,
|
|
459
|
+
markerLine: marker?.line ?? null,
|
|
460
|
+
markerDisposed: marker?.isDisposed ?? null,
|
|
461
|
+
currentViewportY: currentViewportY(),
|
|
462
|
+
selectedRow: row,
|
|
463
|
+
cssLeft: node.style.left,
|
|
464
|
+
cssTop: node.style.top,
|
|
465
|
+
cssWidth: node.style.width,
|
|
466
|
+
cssHeight: node.style.height,
|
|
467
|
+
cellWidth: metrics.width,
|
|
468
|
+
cellHeight: metrics.height,
|
|
469
|
+
geometry: {
|
|
470
|
+
windowInnerWidth: window.innerWidth,
|
|
471
|
+
documentClientWidth: document.documentElement.clientWidth,
|
|
472
|
+
terminalWidth: terminalRect.width,
|
|
473
|
+
screenWidth: screenRect?.width ?? null,
|
|
474
|
+
viewportWidth: viewportRect?.width ?? null,
|
|
475
|
+
viewportClientWidth: viewport?.clientWidth ?? null,
|
|
476
|
+
viewportScrollWidth: viewport?.scrollWidth ?? null,
|
|
477
|
+
overlayWidth: overlayRect.width,
|
|
478
|
+
overlayClientWidth: overlay.clientWidth,
|
|
479
|
+
overlayScrollWidth: overlay.scrollWidth,
|
|
480
|
+
overlayComputedWidth: getComputedStyle(overlay).width,
|
|
481
|
+
overlayOverflow: getComputedStyle(overlay).overflow,
|
|
482
|
+
},
|
|
483
|
+
});
|
|
273
484
|
}
|
|
274
485
|
|
|
275
486
|
function relayoutAll() {
|
|
@@ -297,7 +508,10 @@
|
|
|
297
508
|
if (!viewport) {
|
|
298
509
|
return;
|
|
299
510
|
}
|
|
300
|
-
viewport.addEventListener("scroll",
|
|
511
|
+
viewport.addEventListener("scroll", () => {
|
|
512
|
+
sendBrowserDebug("viewport-scroll", { currentViewportY: currentViewportY() });
|
|
513
|
+
relayoutAll();
|
|
514
|
+
}, { passive: true });
|
|
301
515
|
state.scrollBound = true;
|
|
302
516
|
}
|
|
303
517
|
|
|
@@ -455,8 +669,7 @@
|
|
|
455
669
|
}
|
|
456
670
|
const node = state.placements.get(state.contextMenu.targetKey);
|
|
457
671
|
if (node) {
|
|
458
|
-
|
|
459
|
-
node.remove();
|
|
672
|
+
removePlacementNode(node);
|
|
460
673
|
state.placements.delete(state.contextMenu.targetKey);
|
|
461
674
|
}
|
|
462
675
|
hideContextMenu();
|
|
@@ -577,15 +790,24 @@
|
|
|
577
790
|
const key = placementKey(message);
|
|
578
791
|
const control = message.control || {};
|
|
579
792
|
const cursor = message.bufferCursor || message.cursor || { row: 1, col: 1 };
|
|
793
|
+
sendBrowserDebug("placement-start", {
|
|
794
|
+
key,
|
|
795
|
+
control,
|
|
796
|
+
protocolCursor: message.cursor || null,
|
|
797
|
+
selectedCursor: cursor,
|
|
798
|
+
});
|
|
580
799
|
if (message.image) {
|
|
581
800
|
message.image.control = control;
|
|
582
801
|
}
|
|
583
|
-
|
|
584
802
|
let node = state.placements.get(key);
|
|
585
803
|
if (!node) {
|
|
586
|
-
node = document.createElement("
|
|
804
|
+
node = document.createElement("div");
|
|
587
805
|
node.className = "kitty-image";
|
|
588
|
-
|
|
806
|
+
const imageElement = document.createElement("img");
|
|
807
|
+
imageElement.className = "kitty-image__content";
|
|
808
|
+
imageElement.alt = "";
|
|
809
|
+
node.__kittyImageElement = imageElement;
|
|
810
|
+
node.appendChild(imageElement);
|
|
589
811
|
node.__kittyObjectUrl = "";
|
|
590
812
|
node.__kittyMarker = null;
|
|
591
813
|
node.addEventListener("contextmenu", (event) => {
|
|
@@ -595,12 +817,13 @@
|
|
|
595
817
|
overlay.appendChild(node);
|
|
596
818
|
}
|
|
597
819
|
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
820
|
+
disposePlacementMarker(node);
|
|
821
|
+
node.__kittyMarker = createPlacementMarker(cursor);
|
|
822
|
+
const generation = (node.__kittyGeneration || 0) + 1;
|
|
823
|
+
node.__kittyGeneration = generation;
|
|
601
824
|
|
|
602
|
-
const asset = await resolveImageAsset(message.image);
|
|
603
|
-
if (!asset) {
|
|
825
|
+
const asset = await resolveImageAsset(message.image, control);
|
|
826
|
+
if (!asset || node.__kittyGeneration !== generation) {
|
|
604
827
|
return;
|
|
605
828
|
}
|
|
606
829
|
const objectUrl = URL.createObjectURL(asset.blob);
|
|
@@ -610,9 +833,10 @@
|
|
|
610
833
|
control,
|
|
611
834
|
cursor,
|
|
612
835
|
intrinsicWidth: asset.width,
|
|
613
|
-
intrinsicHeight: asset.height
|
|
836
|
+
intrinsicHeight: asset.height,
|
|
837
|
+
sourceRect: asset.sourceRect,
|
|
614
838
|
};
|
|
615
|
-
node.src = objectUrl;
|
|
839
|
+
node.__kittyImageElement.src = objectUrl;
|
|
616
840
|
bindViewportScroll();
|
|
617
841
|
bindTerminalRender();
|
|
618
842
|
bindTouchScrolling();
|
|
@@ -622,10 +846,13 @@
|
|
|
622
846
|
|
|
623
847
|
function deleteImages(message) {
|
|
624
848
|
const info = message.delete || {};
|
|
625
|
-
|
|
849
|
+
sendBrowserDebug("delete", {
|
|
850
|
+
delete: info,
|
|
851
|
+
placementKeys: Array.from(state.placements.keys()),
|
|
852
|
+
});
|
|
853
|
+
if (info.scope === "a" || info.scope === "A") {
|
|
626
854
|
for (const node of state.placements.values()) {
|
|
627
|
-
|
|
628
|
-
node.remove();
|
|
855
|
+
removePlacementNode(node);
|
|
629
856
|
}
|
|
630
857
|
state.placements.clear();
|
|
631
858
|
return;
|
|
@@ -634,8 +861,7 @@
|
|
|
634
861
|
if (info.imageId) {
|
|
635
862
|
for (const [key, node] of state.placements.entries()) {
|
|
636
863
|
if (key.startsWith(`${info.imageId}:`)) {
|
|
637
|
-
|
|
638
|
-
node.remove();
|
|
864
|
+
removePlacementNode(node);
|
|
639
865
|
state.placements.delete(key);
|
|
640
866
|
}
|
|
641
867
|
}
|
|
@@ -716,8 +942,16 @@
|
|
|
716
942
|
|
|
717
943
|
function handleKittyMessage(payload) {
|
|
718
944
|
const message = JSON.parse(payload);
|
|
945
|
+
sendBrowserDebug("kitty-message", {
|
|
946
|
+
kind: message.kind,
|
|
947
|
+
control: message.control || null,
|
|
948
|
+
protocolCursor: message.cursor || message.delete?.cursor || null,
|
|
949
|
+
delete: message.delete || null,
|
|
950
|
+
});
|
|
719
951
|
if (message.kind === "placement") {
|
|
720
|
-
|
|
952
|
+
// The server records this cursor exactly where the Kitty APC occurred.
|
|
953
|
+
// Do not replace it with xterm's later cursor, which may already have
|
|
954
|
+
// moved because of redraw or scrolling output queued after the APC.
|
|
721
955
|
placeImage(message);
|
|
722
956
|
return true;
|
|
723
957
|
}
|
|
@@ -735,6 +969,7 @@
|
|
|
735
969
|
return nativeOnOpen.call(this, type, listener, options);
|
|
736
970
|
}
|
|
737
971
|
const wrapped = function (event) {
|
|
972
|
+
state.socket = this;
|
|
738
973
|
inspectTerminalOutput(event.data);
|
|
739
974
|
if (typeof event.data === "string" && event.data[0] === MSG_KITTY) {
|
|
740
975
|
if (handleKittyMessage(event.data.slice(1))) {
|
|
@@ -774,6 +1009,7 @@
|
|
|
774
1009
|
return;
|
|
775
1010
|
}
|
|
776
1011
|
descriptor.set.call(this, function (event) {
|
|
1012
|
+
state.socket = this;
|
|
777
1013
|
inspectTerminalOutput(event.data);
|
|
778
1014
|
if (typeof event.data === "string" && event.data[0] === MSG_KITTY) {
|
|
779
1015
|
if (handleKittyMessage(event.data.slice(1))) {
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const assert = require("node:assert/strict");
|
|
2
|
+
const test = require("node:test");
|
|
3
|
+
const { CursorStateTracker, KittyGraphicsParser } = require("../gotty.js");
|
|
4
|
+
|
|
5
|
+
test("Kitty parsing preserves ANSI/APC order and captures the cursor at the APC", () => {
|
|
6
|
+
const parser = new KittyGraphicsParser();
|
|
7
|
+
const tracker = new CursorStateTracker();
|
|
8
|
+
const raw = Buffer.from(
|
|
9
|
+
"\x1b[5;4H" +
|
|
10
|
+
"\x1b_Ga=T,f=100,i=7,p=7,q=2,C=1,x=10,y=20,w=30,h=40,c=5,r=6,m=0;AA==\x1b\\" +
|
|
11
|
+
"\x1b[9;2H",
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const { events } = parser.consume(raw);
|
|
15
|
+
assert.deepEqual(events.map((event) => event.kind), ["plain", "kitty", "plain"]);
|
|
16
|
+
|
|
17
|
+
tracker.consume(events[0].data);
|
|
18
|
+
const graphic = parser.parsePacket(events[1].packet, tracker.snapshot());
|
|
19
|
+
assert.equal(graphic.kind, "placement");
|
|
20
|
+
assert.deepEqual(graphic.cursor, { row: 5, col: 4 });
|
|
21
|
+
assert.deepEqual(
|
|
22
|
+
Object.fromEntries(["x", "y", "w", "h", "c", "r"].map((key) => [key, graphic.control[key]])),
|
|
23
|
+
{ x: "10", y: "20", w: "30", h: "40", c: "5", r: "6" },
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
tracker.consume(events[2].data);
|
|
27
|
+
assert.deepEqual(tracker.snapshot(), { row: 9, col: 2 });
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("an APC split across PTY chunks stays ordered after preceding output", () => {
|
|
31
|
+
const parser = new KittyGraphicsParser();
|
|
32
|
+
const first = parser.consume(Buffer.from("\x1b[3;6H\x1b_Ga=d,d=i"));
|
|
33
|
+
assert.deepEqual(first.events.map((event) => event.kind), ["plain"]);
|
|
34
|
+
|
|
35
|
+
const second = parser.consume(Buffer.from(",i=9;\x1b\\tail"));
|
|
36
|
+
assert.deepEqual(second.events.map((event) => event.kind), ["kitty", "plain"]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("placement-only packets reuse the server-side cached image", () => {
|
|
40
|
+
const parser = new KittyGraphicsParser();
|
|
41
|
+
const parse = (packet) => {
|
|
42
|
+
const event = parser.consume(Buffer.from(packet)).events.find((item) => item.kind === "kitty");
|
|
43
|
+
return event ? parser.parsePacket(event.packet, { row: 1, col: 1 }) : null;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const transfer = parse("\x1b_Ga=T,f=100,i=7,p=7,U=image/png,m=0;AA==\x1b\\");
|
|
47
|
+
assert.equal(transfer.image.data, "AA==");
|
|
48
|
+
assert.equal(transfer.image.mime, "image/png");
|
|
49
|
+
|
|
50
|
+
const placement = parse("\x1b_Ga=p,i=7,p=7,c=5,r=2,C=1;\x1b\\");
|
|
51
|
+
assert.equal(placement.image.id, "7");
|
|
52
|
+
assert.equal(placement.image.data, "AA==");
|
|
53
|
+
|
|
54
|
+
parse("\x1b_Ga=d,d=i,i=7;\x1b\\");
|
|
55
|
+
assert.ok(parse("\x1b_Ga=p,i=7,p=7;\x1b\\"));
|
|
56
|
+
parse("\x1b_Ga=d,d=I,i=7;\x1b\\");
|
|
57
|
+
assert.equal(parse("\x1b_Ga=p,i=7,p=7;\x1b\\"), null);
|
|
58
|
+
});
|
package/viu.mjs
CHANGED
|
@@ -43,6 +43,7 @@ function buildControlFields(imageId, mime, cols, rows, more, compat) {
|
|
|
43
43
|
"a=T",
|
|
44
44
|
"f=100",
|
|
45
45
|
`i=${imageId}`,
|
|
46
|
+
`p=${imageId}`,
|
|
46
47
|
"q=2",
|
|
47
48
|
"t=d",
|
|
48
49
|
`c=${cols}`,
|
|
@@ -61,7 +62,13 @@ function writePacket(control, payload) {
|
|
|
61
62
|
|
|
62
63
|
function writeKittyImage(buffer, mime, width, height, compat) {
|
|
63
64
|
const base64 = buffer.toString("base64");
|
|
64
|
-
|
|
65
|
+
let imageId = Date.now() % 2147483647;
|
|
66
|
+
|
|
67
|
+
imageId = process.env.JSGOTTY_IMGID || imageId ;
|
|
68
|
+
|
|
69
|
+
if(process.env.JSGOTTY_DEBUG)
|
|
70
|
+
console.error(imageId)
|
|
71
|
+
|
|
65
72
|
const { cols, rows } = fitImageCells(width, height);
|
|
66
73
|
|
|
67
74
|
if (process.platform === "win32") {
|