@gajae-code/tui 0.21.0 → 0.21.1
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/CHANGELOG.md +15 -0
- package/dist/types/keybindings.d.ts +0 -5
- package/dist/types/keys.d.ts +6 -0
- package/dist/types/stdin-buffer.d.ts +18 -0
- package/package.json +3 -3
- package/src/components/editor.ts +13 -7
- package/src/keybindings.ts +0 -2
- package/src/keys.ts +9 -0
- package/src/stdin-buffer.ts +212 -40
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.21.1] - 2026-11-17
|
|
6
|
+
|
|
7
|
+
### Removed
|
|
8
|
+
|
|
9
|
+
- Removed the `tui.input.copy` keybinding. Nothing dispatched it, so it only advertised a Ctrl+C copy that never happened.
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
- Inserting a newline closes an open autocomplete list, so the next Enter submits instead of only dismissing the stale list.
|
|
14
|
+
|
|
15
|
+
- With an autocomplete list open, a bare line feed (Ctrl+J, or a Shift+Enter→LF terminal mapping) inserts a newline instead of applying the highlighted completion.
|
|
16
|
+
|
|
17
|
+
- A bracketed paste whose end marker never arrives now delivers what arrived after a 1 s inactivity watchdog, or at a 64 MiB cap, instead of leaving input dead.
|
|
18
|
+
- Unbracketed multi-line pastes (for example `tmux paste-buffer` without `-p`) arrive as one paste instead of submitting line by line.
|
|
19
|
+
|
|
5
20
|
## [0.21.0] - 2026-11-16
|
|
6
21
|
|
|
7
22
|
### Performance
|
|
@@ -28,7 +28,6 @@ export interface Keybindings {
|
|
|
28
28
|
"tui.input.newLine": true;
|
|
29
29
|
"tui.input.submit": true;
|
|
30
30
|
"tui.input.tab": true;
|
|
31
|
-
"tui.input.copy": true;
|
|
32
31
|
"tui.select.up": true;
|
|
33
32
|
"tui.select.down": true;
|
|
34
33
|
"tui.select.pageUp": true;
|
|
@@ -142,10 +141,6 @@ export declare const TUI_KEYBINDINGS: {
|
|
|
142
141
|
readonly defaultKeys: "tab";
|
|
143
142
|
readonly description: "Tab / autocomplete";
|
|
144
143
|
};
|
|
145
|
-
readonly "tui.input.copy": {
|
|
146
|
-
readonly defaultKeys: "ctrl+c";
|
|
147
|
-
readonly description: "Copy selection";
|
|
148
|
-
};
|
|
149
144
|
readonly "tui.select.up": {
|
|
150
145
|
readonly defaultKeys: "up";
|
|
151
146
|
readonly description: "Move selection up";
|
package/dist/types/keys.d.ts
CHANGED
|
@@ -219,6 +219,12 @@ export declare function decodePrintableKey(data: string): string | undefined;
|
|
|
219
219
|
* @param keyId - Key identifier (e.g., "ctrl+c", "escape", Key.ctrl("c"))
|
|
220
220
|
*/
|
|
221
221
|
export declare function matchesKey(data: string, keyId: KeyId): boolean;
|
|
222
|
+
/**
|
|
223
|
+
* Whether `data` is a bare line feed: legacy Ctrl+J, or a terminal's Shift+Enter→LF
|
|
224
|
+
* mapping. `matchesKey` reports it as "enter" so selectors confirm on it; text entry
|
|
225
|
+
* treats it as a newline instead.
|
|
226
|
+
*/
|
|
227
|
+
export declare function isBareLineFeed(data: string): boolean;
|
|
222
228
|
/**
|
|
223
229
|
* Parse terminal input and return a normalized key identifier.
|
|
224
230
|
*
|
|
@@ -25,6 +25,17 @@ export type StdinBufferOptions = {
|
|
|
25
25
|
* After this time, the buffer is flushed even if incomplete
|
|
26
26
|
*/
|
|
27
27
|
timeout?: number;
|
|
28
|
+
/**
|
|
29
|
+
* Paste-mode inactivity watchdog (default: 1000ms). If no input arrives for
|
|
30
|
+
* this long while waiting for the bracketed-paste end marker, the paste is
|
|
31
|
+
* assumed truncated: accumulated bytes are delivered and input recovers.
|
|
32
|
+
*/
|
|
33
|
+
pasteTimeout?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Paste-mode byte cap (default: 64 MiB). Exceeding it aborts paste mode the
|
|
36
|
+
* same way, bounding memory when the end marker never arrives.
|
|
37
|
+
*/
|
|
38
|
+
pasteByteLimit?: number;
|
|
28
39
|
};
|
|
29
40
|
export type StdinBufferEventMap = {
|
|
30
41
|
data: [string];
|
|
@@ -44,6 +55,13 @@ export type StdinBufferEventMap = {
|
|
|
44
55
|
export declare class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
45
56
|
#private;
|
|
46
57
|
constructor(options?: StdinBufferOptions);
|
|
58
|
+
/**
|
|
59
|
+
* Whether a bracketed paste has started and its end marker has not arrived.
|
|
60
|
+
* Terminal skips its per-read DEC 2004 reassertion meanwhile: the host is
|
|
61
|
+
* evidently in bracketed-paste mode, and a paste delivered one key record
|
|
62
|
+
* per read would otherwise cost one terminal write per pasted key.
|
|
63
|
+
*/
|
|
64
|
+
get pasting(): boolean;
|
|
47
65
|
process(data: string | Buffer): void;
|
|
48
66
|
/**
|
|
49
67
|
* ProcessTerminal calls this right after writing a capability probe so a lone
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@gajae-code/tui",
|
|
4
|
-
"version": "0.21.
|
|
4
|
+
"version": "0.21.1",
|
|
5
5
|
"description": "Terminal User Interface library with differential rendering for efficient text-based applications",
|
|
6
6
|
"homepage": "https://gajae-code.com",
|
|
7
7
|
"author": "Yeachan-Heo and Gajae Code Contributors",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"fmt": "biome format --write ."
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@gajae-code/natives": "0.21.
|
|
40
|
-
"@gajae-code/utils": "0.21.
|
|
39
|
+
"@gajae-code/natives": "0.21.1",
|
|
40
|
+
"@gajae-code/utils": "0.21.1",
|
|
41
41
|
"lru-cache": "11.3.6",
|
|
42
42
|
"marked": "18.0.6"
|
|
43
43
|
},
|
package/src/components/editor.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
} from "../autocomplete";
|
|
11
11
|
import { BracketedPasteHandler } from "../bracketed-paste";
|
|
12
12
|
import { getKeybindings, type KeybindingsManager } from "../keybindings";
|
|
13
|
-
import { extractPrintableText, matchesKey } from "../keys";
|
|
13
|
+
import { extractPrintableText, isBareLineFeed, matchesKey } from "../keys";
|
|
14
14
|
import { KillRing } from "../kill-ring";
|
|
15
15
|
import type { SymbolTheme } from "../symbols";
|
|
16
16
|
import { type Component, CURSOR_MARKER, type Focusable } from "../tui";
|
|
@@ -1253,8 +1253,7 @@ export class Editor implements Component, Focusable {
|
|
|
1253
1253
|
kb.matches(data, "tui.select.down") ||
|
|
1254
1254
|
kb.matches(data, "tui.select.pageUp") ||
|
|
1255
1255
|
kb.matches(data, "tui.select.pageDown") ||
|
|
1256
|
-
kb.matches(data, "tui.input.submit") ||
|
|
1257
|
-
data === "\n" ||
|
|
1256
|
+
(kb.matches(data, "tui.input.submit") && !isBareLineFeed(data)) ||
|
|
1258
1257
|
kb.matches(data, "tui.input.tab")
|
|
1259
1258
|
) {
|
|
1260
1259
|
// Only pass navigation keys to the list, not Enter/Tab (we handle those directly)
|
|
@@ -1310,7 +1309,7 @@ export class Editor implements Component, Focusable {
|
|
|
1310
1309
|
|
|
1311
1310
|
// If Enter was pressed on a slash command, apply completion and submit
|
|
1312
1311
|
if (
|
|
1313
|
-
|
|
1312
|
+
kb.matches(data, "tui.input.submit") &&
|
|
1314
1313
|
this.#autocompleteState === "regular" &&
|
|
1315
1314
|
this.#autocompletePrefix.startsWith("/")
|
|
1316
1315
|
) {
|
|
@@ -1336,7 +1335,7 @@ export class Editor implements Component, Focusable {
|
|
|
1336
1335
|
// Don't return - fall through to submission logic
|
|
1337
1336
|
}
|
|
1338
1337
|
// If Enter was pressed on a file path, apply completion
|
|
1339
|
-
else if (kb.matches(data, "tui.input.submit")
|
|
1338
|
+
else if (kb.matches(data, "tui.input.submit")) {
|
|
1340
1339
|
if (!this.#isAutocompleteSelectionCurrent()) {
|
|
1341
1340
|
this.#cancelAutocomplete();
|
|
1342
1341
|
return;
|
|
@@ -1435,7 +1434,7 @@ export class Editor implements Component, Focusable {
|
|
|
1435
1434
|
data === "\x1b[13;2~" || // Shift+Enter in some terminals (legacy format)
|
|
1436
1435
|
kb.matches(data, "tui.input.newLine") || // Shift+Enter (Kitty protocol, handles lock bits)
|
|
1437
1436
|
(data.length > 1 && data.includes("\x1b") && data.includes("\r")) ||
|
|
1438
|
-
(data
|
|
1437
|
+
isBareLineFeed(data) // Ctrl+J, or a terminal's Shift+Enter→LF mapping
|
|
1439
1438
|
) {
|
|
1440
1439
|
if (this.#shouldSubmitOnBackslashEnter(data, kb)) {
|
|
1441
1440
|
this.#handleBackspace();
|
|
@@ -1445,7 +1444,7 @@ export class Editor implements Component, Focusable {
|
|
|
1445
1444
|
this.#addNewLine();
|
|
1446
1445
|
}
|
|
1447
1446
|
// Plain Enter - submit (handles both legacy \r and Kitty protocol with lock bits)
|
|
1448
|
-
else if (kb.matches(data, "tui.input.submit")
|
|
1447
|
+
else if (kb.matches(data, "tui.input.submit")) {
|
|
1449
1448
|
// If submit is disabled, do nothing
|
|
1450
1449
|
if (this.disableSubmit) {
|
|
1451
1450
|
return;
|
|
@@ -2147,6 +2146,13 @@ export class Editor implements Component, Focusable {
|
|
|
2147
2146
|
this.#state.cursorLine++;
|
|
2148
2147
|
this.#setCursorCol(0);
|
|
2149
2148
|
|
|
2149
|
+
// The completion token ended with the old line; a list left open would
|
|
2150
|
+
// swallow the next Enter.
|
|
2151
|
+
if (this.#autocompleteState) {
|
|
2152
|
+
this.#cancelAutocomplete();
|
|
2153
|
+
this.onAutocompleteUpdate?.();
|
|
2154
|
+
}
|
|
2155
|
+
|
|
2150
2156
|
if (this.onChange) {
|
|
2151
2157
|
this.onChange(this.getText());
|
|
2152
2158
|
}
|
package/src/keybindings.ts
CHANGED
|
@@ -31,7 +31,6 @@ export interface Keybindings {
|
|
|
31
31
|
"tui.input.newLine": true;
|
|
32
32
|
"tui.input.submit": true;
|
|
33
33
|
"tui.input.tab": true;
|
|
34
|
-
"tui.input.copy": true;
|
|
35
34
|
// Generic selection actions
|
|
36
35
|
"tui.select.up": true;
|
|
37
36
|
"tui.select.down": true;
|
|
@@ -123,7 +122,6 @@ export const TUI_KEYBINDINGS = {
|
|
|
123
122
|
"tui.input.newLine": { defaultKeys: "shift+enter", description: "Insert newline" },
|
|
124
123
|
"tui.input.submit": { defaultKeys: "enter", description: "Submit input" },
|
|
125
124
|
"tui.input.tab": { defaultKeys: "tab", description: "Tab / autocomplete" },
|
|
126
|
-
"tui.input.copy": { defaultKeys: "ctrl+c", description: "Copy selection" },
|
|
127
125
|
"tui.select.up": { defaultKeys: "up", description: "Move selection up" },
|
|
128
126
|
"tui.select.down": { defaultKeys: "down", description: "Move selection down" },
|
|
129
127
|
"tui.select.pageUp": { defaultKeys: "pageUp", description: "Selection page up" },
|
package/src/keys.ts
CHANGED
|
@@ -770,6 +770,15 @@ export function matchesKey(data: string, keyId: KeyId): boolean {
|
|
|
770
770
|
);
|
|
771
771
|
}
|
|
772
772
|
|
|
773
|
+
/**
|
|
774
|
+
* Whether `data` is a bare line feed: legacy Ctrl+J, or a terminal's Shift+Enter→LF
|
|
775
|
+
* mapping. `matchesKey` reports it as "enter" so selectors confirm on it; text entry
|
|
776
|
+
* treats it as a newline instead.
|
|
777
|
+
*/
|
|
778
|
+
export function isBareLineFeed(data: string): boolean {
|
|
779
|
+
return data === "\n";
|
|
780
|
+
}
|
|
781
|
+
|
|
773
782
|
/**
|
|
774
783
|
* Parse terminal input and return a normalized key identifier.
|
|
775
784
|
*
|
package/src/stdin-buffer.ts
CHANGED
|
@@ -23,6 +23,16 @@ import { EventEmitter } from "events";
|
|
|
23
23
|
const ESC = "\x1b";
|
|
24
24
|
const BRACKETED_PASTE_START = "\x1b[200~";
|
|
25
25
|
const BRACKETED_PASTE_END = "\x1b[201~";
|
|
26
|
+
// Paste-mode recovery bounds: a lost/corrupted end marker (ssh/tmux
|
|
27
|
+
// truncation) must not hang input forever or grow memory unboundedly.
|
|
28
|
+
const PASTE_INACTIVITY_TIMEOUT_MS = 1000;
|
|
29
|
+
const PASTE_MAX_BYTES = 64 * 1024 * 1024;
|
|
30
|
+
// Raw-paste classification holds CR/LF-bearing, ESC-free input briefly so
|
|
31
|
+
// adjacent stdin reads from one unmarked paste can be considered together.
|
|
32
|
+
// Fixed from the first break-bearing read (not an inactivity debounce): normal
|
|
33
|
+
// Enter latency and candidate memory remain bounded even under a continuous
|
|
34
|
+
// stream. Ten milliseconds spans adjacent PTY reads without becoming perceptible.
|
|
35
|
+
const RAW_PASTE_CLASSIFICATION_TIMEOUT_MS = 10;
|
|
26
36
|
const SGR_QUARANTINE_MAX_BYTES = 256;
|
|
27
37
|
const SGR_QUARANTINE_TIMEOUT_MS = 100;
|
|
28
38
|
|
|
@@ -45,6 +55,32 @@ function isIncompleteProbeReplyPrefix(buffer: string): boolean {
|
|
|
45
55
|
return /^\x1b\[\??[\d;]*$/.test(buffer);
|
|
46
56
|
}
|
|
47
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Whether `text` has two completed logical line breaks (three line segments).
|
|
60
|
+
*
|
|
61
|
+
* A single Enter may be batched with surrounding keystrokes in one stdin read,
|
|
62
|
+
* so one break is ambiguous and must stay on the key path. CRLF counts as one
|
|
63
|
+
* logical break. Content after the second break completes the third segment;
|
|
64
|
+
* until then the classification window keeps buffering.
|
|
65
|
+
*/
|
|
66
|
+
function isRawMultilineBurst(text: string): boolean {
|
|
67
|
+
let breaks = 0;
|
|
68
|
+
for (let i = 0; i < text.length; i++) {
|
|
69
|
+
const code = text.charCodeAt(i);
|
|
70
|
+
if (code === 0x0d) {
|
|
71
|
+
breaks++;
|
|
72
|
+
if (text.charCodeAt(i + 1) === 0x0a) i++;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (code === 0x0a) {
|
|
76
|
+
breaks++;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (breaks >= 2) return true;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
|
|
48
84
|
/** True for complete SGR mouse CSI reports. These remain control input, never text. */
|
|
49
85
|
export function isSgrMouseSequence(sequence: string): boolean {
|
|
50
86
|
return /^\x1b\[<\d+;\d+;\d+[Mm]$/.test(sequence);
|
|
@@ -427,6 +463,17 @@ export type StdinBufferOptions = {
|
|
|
427
463
|
* After this time, the buffer is flushed even if incomplete
|
|
428
464
|
*/
|
|
429
465
|
timeout?: number;
|
|
466
|
+
/**
|
|
467
|
+
* Paste-mode inactivity watchdog (default: 1000ms). If no input arrives for
|
|
468
|
+
* this long while waiting for the bracketed-paste end marker, the paste is
|
|
469
|
+
* assumed truncated: accumulated bytes are delivered and input recovers.
|
|
470
|
+
*/
|
|
471
|
+
pasteTimeout?: number;
|
|
472
|
+
/**
|
|
473
|
+
* Paste-mode byte cap (default: 64 MiB). Exceeding it aborts paste mode the
|
|
474
|
+
* same way, bounding memory when the end marker never arrives.
|
|
475
|
+
*/
|
|
476
|
+
pasteByteLimit?: number;
|
|
430
477
|
};
|
|
431
478
|
|
|
432
479
|
export type StdinBufferEventMap = {
|
|
@@ -452,8 +499,14 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
452
499
|
#bufferedEscapeRunLength = 0;
|
|
453
500
|
#timeout?: NodeJS.Timeout;
|
|
454
501
|
readonly #timeoutMs: number;
|
|
502
|
+
readonly #pasteTimeoutMs: number;
|
|
503
|
+
readonly #pasteByteLimit: number;
|
|
455
504
|
#pasteMode: boolean = false;
|
|
456
505
|
#pasteBuffer: string = "";
|
|
506
|
+
#pasteBytes = 0;
|
|
507
|
+
#pasteWatchdog?: NodeJS.Timeout;
|
|
508
|
+
#rawPasteCandidate = "";
|
|
509
|
+
#rawPasteTimer?: NodeJS.Timeout;
|
|
457
510
|
#pendingKittyPrintableCodepoint: number | undefined;
|
|
458
511
|
// Persistent UTF-8 decoder. Holds an incomplete trailing multi-byte
|
|
459
512
|
// sequence between chunks so split reads (e.g. a 3-byte Korean syllable
|
|
@@ -474,6 +527,18 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
474
527
|
constructor(options: StdinBufferOptions = {}) {
|
|
475
528
|
super();
|
|
476
529
|
this.#timeoutMs = options.timeout ?? 10;
|
|
530
|
+
this.#pasteTimeoutMs = options.pasteTimeout ?? PASTE_INACTIVITY_TIMEOUT_MS;
|
|
531
|
+
this.#pasteByteLimit = options.pasteByteLimit ?? PASTE_MAX_BYTES;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Whether a bracketed paste has started and its end marker has not arrived.
|
|
536
|
+
* Terminal skips its per-read DEC 2004 reassertion meanwhile: the host is
|
|
537
|
+
* evidently in bracketed-paste mode, and a paste delivered one key record
|
|
538
|
+
* per read would otherwise cost one terminal write per pasted key.
|
|
539
|
+
*/
|
|
540
|
+
get pasting(): boolean {
|
|
541
|
+
return this.#pasteMode;
|
|
477
542
|
}
|
|
478
543
|
|
|
479
544
|
process(data: string | Buffer): void {
|
|
@@ -536,7 +601,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
536
601
|
str = data;
|
|
537
602
|
}
|
|
538
603
|
|
|
539
|
-
if (str.length === 0 && this.#buffer.length === 0) {
|
|
604
|
+
if (str.length === 0 && this.#buffer.length === 0 && this.#rawPasteCandidate.length === 0) {
|
|
540
605
|
// A Buffer that decoded to nothing means the decoder is holding an
|
|
541
606
|
// incomplete UTF-8 prefix; emit nothing and wait for the completing
|
|
542
607
|
// bytes. Preserve the historical empty 'data' event for explicit
|
|
@@ -552,31 +617,44 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
552
617
|
if (str.length === 0) return;
|
|
553
618
|
}
|
|
554
619
|
|
|
555
|
-
this.#buffer += str;
|
|
556
|
-
|
|
557
620
|
if (this.#pasteMode) {
|
|
558
|
-
this.#
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
const endIndex = this.#pasteBuffer.indexOf(BRACKETED_PASTE_END);
|
|
563
|
-
if (endIndex !== -1) {
|
|
564
|
-
const pastedContent = this.#pasteBuffer.slice(0, endIndex);
|
|
565
|
-
const remaining = this.#pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length);
|
|
566
|
-
|
|
567
|
-
this.#pasteMode = false;
|
|
568
|
-
this.#pasteBuffer = "";
|
|
569
|
-
this.#pendingKittyPrintableCodepoint = undefined;
|
|
570
|
-
|
|
571
|
-
this.emit("paste", pastedContent);
|
|
621
|
+
this.#consumePasteChunk(str);
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
572
624
|
|
|
573
|
-
|
|
574
|
-
|
|
625
|
+
if (this.#rawPasteCandidate.length > 0) {
|
|
626
|
+
if (str.indexOf(ESC) !== -1) {
|
|
627
|
+
// Escape-bearing input cannot belong to an unmarked raw paste.
|
|
628
|
+
// Replay the ambiguous prefix as keys before parsing the escape.
|
|
629
|
+
this.#flushRawPasteCandidate();
|
|
630
|
+
} else {
|
|
631
|
+
this.#rawPasteCandidate += str;
|
|
632
|
+
if (isRawMultilineBurst(this.#rawPasteCandidate)) {
|
|
633
|
+
this.#classifyRawPasteCandidate();
|
|
575
634
|
}
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
if (
|
|
640
|
+
this.#buffer.length === 0 &&
|
|
641
|
+
str.indexOf(ESC) === -1 &&
|
|
642
|
+
(str.indexOf("\r") !== -1 || str.indexOf("\n") !== -1)
|
|
643
|
+
) {
|
|
644
|
+
// Hold the first break-bearing read briefly. A split raw paste can
|
|
645
|
+
// then accumulate enough logical lines to classify; an ordinary
|
|
646
|
+
// Enter is replayed unchanged when the fixed window expires.
|
|
647
|
+
this.#rawPasteCandidate = str;
|
|
648
|
+
if (isRawMultilineBurst(str)) {
|
|
649
|
+
this.#classifyRawPasteCandidate();
|
|
650
|
+
} else {
|
|
651
|
+
this.#armRawPasteTimer();
|
|
576
652
|
}
|
|
577
653
|
return;
|
|
578
654
|
}
|
|
579
655
|
|
|
656
|
+
this.#buffer += str;
|
|
657
|
+
|
|
580
658
|
// A known all-Escape prefix cannot contain the paste introducer, so start
|
|
581
659
|
// the scan just far enough back to catch a marker straddling the boundary.
|
|
582
660
|
// Rescanning the whole retained run on every read made a long run quadratic.
|
|
@@ -599,27 +677,13 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
599
677
|
}
|
|
600
678
|
|
|
601
679
|
this.#pendingKittyPrintableCodepoint = undefined;
|
|
602
|
-
|
|
603
|
-
this.#pasteMode = true;
|
|
604
|
-
this.#pasteBuffer = this.#buffer;
|
|
680
|
+
const firstChunk = this.#buffer.slice(startIndex + BRACKETED_PASTE_START.length);
|
|
605
681
|
this.#buffer = "";
|
|
606
682
|
this.#bufferedEscapeRunLength = 0;
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
const remaining = this.#pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length);
|
|
612
|
-
|
|
613
|
-
this.#pasteMode = false;
|
|
614
|
-
this.#pasteBuffer = "";
|
|
615
|
-
this.#pendingKittyPrintableCodepoint = undefined;
|
|
616
|
-
|
|
617
|
-
this.emit("paste", pastedContent);
|
|
618
|
-
|
|
619
|
-
if (remaining.length > 0) {
|
|
620
|
-
this.process(remaining);
|
|
621
|
-
}
|
|
622
|
-
}
|
|
683
|
+
this.#pasteMode = true;
|
|
684
|
+
this.#pasteBuffer = "";
|
|
685
|
+
this.#pasteBytes = 0;
|
|
686
|
+
this.#consumePasteChunk(firstChunk);
|
|
623
687
|
return;
|
|
624
688
|
}
|
|
625
689
|
|
|
@@ -641,6 +705,109 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
641
705
|
}
|
|
642
706
|
}
|
|
643
707
|
|
|
708
|
+
/**
|
|
709
|
+
* Accumulate pasted text until the end marker. The marker search resumes
|
|
710
|
+
* just before the new chunk so one that straddles reads is still found
|
|
711
|
+
* without rescanning the whole paste.
|
|
712
|
+
*/
|
|
713
|
+
#consumePasteChunk(chunk: string): void {
|
|
714
|
+
const searchFrom = Math.max(0, this.#pasteBuffer.length - (BRACKETED_PASTE_END.length - 1));
|
|
715
|
+
this.#pasteBuffer += chunk;
|
|
716
|
+
const endIndex = this.#pasteBuffer.indexOf(BRACKETED_PASTE_END, searchFrom);
|
|
717
|
+
if (endIndex !== -1) {
|
|
718
|
+
this.#finishPaste(endIndex, endIndex + BRACKETED_PASTE_END.length);
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
this.#pasteBytes += Buffer.byteLength(chunk, "utf8");
|
|
722
|
+
if (this.#pasteBytes > this.#pasteByteLimit) {
|
|
723
|
+
this.#abortPaste();
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
this.#armPasteWatchdog();
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* Deliver the paste ending at `contentEnd`; input from `remainingStart` on
|
|
731
|
+
* was read after it and takes the normal route.
|
|
732
|
+
*/
|
|
733
|
+
#finishPaste(contentEnd: number, remainingStart: number): void {
|
|
734
|
+
const content = this.#pasteBuffer.slice(0, contentEnd);
|
|
735
|
+
const remaining = this.#pasteBuffer.slice(remainingStart);
|
|
736
|
+
this.#clearPasteWatchdog();
|
|
737
|
+
this.#pasteMode = false;
|
|
738
|
+
this.#pasteBuffer = "";
|
|
739
|
+
this.#pasteBytes = 0;
|
|
740
|
+
this.#pendingKittyPrintableCodepoint = undefined;
|
|
741
|
+
|
|
742
|
+
this.emit("paste", content);
|
|
743
|
+
if (remaining.length > 0) this.process(remaining);
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/** Re-arm the paste-mode inactivity watchdog after pasted text arrives. */
|
|
747
|
+
#armPasteWatchdog(): void {
|
|
748
|
+
this.#clearPasteWatchdog();
|
|
749
|
+
this.#pasteWatchdog = setTimeout(() => {
|
|
750
|
+
this.#pasteWatchdog = undefined;
|
|
751
|
+
this.#abortPaste();
|
|
752
|
+
}, this.#pasteTimeoutMs);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
#clearPasteWatchdog(): void {
|
|
756
|
+
if (this.#pasteWatchdog) {
|
|
757
|
+
clearTimeout(this.#pasteWatchdog);
|
|
758
|
+
this.#pasteWatchdog = undefined;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* Recover from a paste whose end marker never arrived (dropped or corrupted
|
|
764
|
+
* in transit, or past the byte cap): exit paste mode and deliver the
|
|
765
|
+
* accumulated bytes as a paste, so they are neither lost, replayed as
|
|
766
|
+
* keystrokes, nor accumulated forever while input appears dead.
|
|
767
|
+
*/
|
|
768
|
+
#abortPaste(): void {
|
|
769
|
+
const length = this.#pasteBuffer.length;
|
|
770
|
+
this.#finishPaste(length, length);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/** Start one fixed window from the first break-bearing raw read. */
|
|
774
|
+
#armRawPasteTimer(): void {
|
|
775
|
+
if (this.#rawPasteTimer) return;
|
|
776
|
+
this.#rawPasteTimer = setTimeout(() => {
|
|
777
|
+
this.#rawPasteTimer = undefined;
|
|
778
|
+
this.#flushRawPasteCandidate();
|
|
779
|
+
}, RAW_PASTE_CLASSIFICATION_TIMEOUT_MS);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
#takeRawPasteCandidate(): string {
|
|
783
|
+
if (this.#rawPasteTimer) {
|
|
784
|
+
clearTimeout(this.#rawPasteTimer);
|
|
785
|
+
this.#rawPasteTimer = undefined;
|
|
786
|
+
}
|
|
787
|
+
const content = this.#rawPasteCandidate;
|
|
788
|
+
this.#rawPasteCandidate = "";
|
|
789
|
+
return content;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
/** Deliver a classified raw multiline burst as one paste. */
|
|
793
|
+
#classifyRawPasteCandidate(): void {
|
|
794
|
+
const content = this.#takeRawPasteCandidate();
|
|
795
|
+
this.#pendingKittyPrintableCodepoint = undefined;
|
|
796
|
+
this.emit("paste", content);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
/** Replay an ambiguous raw candidate as the original per-key data events. */
|
|
800
|
+
#flushRawPasteCandidate(): void {
|
|
801
|
+
for (const sequence of this.#takeRawPasteSequences()) this.#emitDataSequence(sequence);
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
#takeRawPasteSequences(): string[] {
|
|
805
|
+
const content = this.#takeRawPasteCandidate();
|
|
806
|
+
if (content.length === 0) return [];
|
|
807
|
+
const result = extractCompleteSequences(content);
|
|
808
|
+
return result.remainder.length > 0 ? [...result.sequences, result.remainder] : result.sequences;
|
|
809
|
+
}
|
|
810
|
+
|
|
644
811
|
#beginSgrQuarantine(): void {
|
|
645
812
|
const suffix = this.#buffer.slice(3);
|
|
646
813
|
let semicolons = 0;
|
|
@@ -789,8 +956,10 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
789
956
|
|
|
790
957
|
const pendingMeta = this.#consumePendingSingleUtf8LeadAsMeta();
|
|
791
958
|
|
|
959
|
+
// A raw candidate only exists while #buffer is empty.
|
|
960
|
+
const rawSequences = this.#takeRawPasteSequences();
|
|
792
961
|
if (this.#buffer.length === 0) {
|
|
793
|
-
return pendingMeta === undefined ?
|
|
962
|
+
return pendingMeta === undefined ? rawSequences : [pendingMeta, ...rawSequences];
|
|
794
963
|
}
|
|
795
964
|
|
|
796
965
|
if (isSgrMousePrefix(this.#buffer)) {
|
|
@@ -823,8 +992,11 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
823
992
|
}
|
|
824
993
|
this.#buffer = "";
|
|
825
994
|
this.#bufferedEscapeRunLength = 0;
|
|
995
|
+
this.#clearPasteWatchdog();
|
|
996
|
+
this.#takeRawPasteCandidate();
|
|
826
997
|
this.#pasteMode = false;
|
|
827
998
|
this.#pasteBuffer = "";
|
|
999
|
+
this.#pasteBytes = 0;
|
|
828
1000
|
this.#pendingKittyPrintableCodepoint = undefined;
|
|
829
1001
|
this.#sgrQuarantine = false;
|
|
830
1002
|
this.#sgrQuarantineBytes = 0;
|
|
@@ -840,7 +1012,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
840
1012
|
}
|
|
841
1013
|
|
|
842
1014
|
getBuffer(): string {
|
|
843
|
-
return this.#buffer
|
|
1015
|
+
return `${this.#rawPasteCandidate}${this.#buffer}`;
|
|
844
1016
|
}
|
|
845
1017
|
|
|
846
1018
|
destroy(): void {
|