@gajae-code/tui 0.13.1 → 0.13.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.
- package/CHANGELOG.md +11 -0
- package/package.json +3 -3
- package/src/stdin-buffer.ts +108 -10
- package/src/terminal.ts +12 -8
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.13.2] - 2026-08-13
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- A fast double-Esc (or triple-Esc) whose ESC bytes coalesce into one stdin chunk — which tmux always produces within its escape-time window, and SSH batching produces routinely — is now emitted as individual Escape key presses instead of a single `"\x1b\x1b"` sequence that parsed as the unbound `alt+escape` and silently swallowed both presses. This restores the double-Esc draft-clear and double-Esc selector gestures under tmux/SSH. Option-as-Meta sequences with a real continuation (e.g. Option+Up as `ESC ESC [ A`) remain atomic, and an ESC-cancelled incomplete sequence is still emitted whole.
|
|
10
|
+
- An ambiguous trailing run of Escape bytes now stays buffered until a continuation or the flush timeout resolves it, so `ESC ESC ESC` followed by `[A` in the next chunk still decodes as Escape then `alt+up` instead of two Escapes plus a plain Up that fired the destructive double-Escape gesture.
|
|
11
|
+
- Escape presses immediately followed by a bracketed paste in the same read are now emitted as individual Escape presses instead of one coalesced sequence that parsed as the unbound `alt+escape` and swallowed every press.
|
|
12
|
+
- A long run of Escape bytes arriving as many small reads no longer rescans the accumulated buffer on every read; only the two-byte ambiguous tail stays buffered, so 50,000 byte-by-byte Escape reads cost 50ms instead of 2.4s.
|
|
13
|
+
- A long run of Escape bytes followed by another key now decodes in linear time instead of rescanning the remaining input on every step, which blocked the event loop for over a second on a 50,000-byte run.
|
|
14
|
+
- Apple Terminal.app now retains its default keyboard mode when it does not support the Kitty keyboard protocol, avoiding the modifyOtherKeys fallback that breaks Korean/Hangul IME composition.
|
|
15
|
+
|
|
5
16
|
## [0.13.1] - 2026-08-11
|
|
6
17
|
|
|
7
18
|
### Added
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@gajae-code/tui",
|
|
4
|
-
"version": "0.13.
|
|
4
|
+
"version": "0.13.2",
|
|
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.13.
|
|
40
|
-
"@gajae-code/utils": "0.13.
|
|
39
|
+
"@gajae-code/natives": "0.13.2",
|
|
40
|
+
"@gajae-code/utils": "0.13.2",
|
|
41
41
|
"lru-cache": "11.3.6",
|
|
42
42
|
"marked": "18.0.6"
|
|
43
43
|
},
|
package/src/stdin-buffer.ts
CHANGED
|
@@ -278,12 +278,33 @@ function continuesAsStringTerminator(remaining: string, index: number): boolean
|
|
|
278
278
|
return afterEsc === undefined || afterEsc === "\\";
|
|
279
279
|
}
|
|
280
280
|
|
|
281
|
-
|
|
281
|
+
/**
|
|
282
|
+
* A buffered run of nothing but ESC bytes is N real Escape key presses, not an
|
|
283
|
+
* Option-as-Meta prefix. Emitting the run as one sequence parses as the unbound
|
|
284
|
+
* `alt+escape` and silently swallows every press, so any path that gives up on a
|
|
285
|
+
* continuation must split the run first.
|
|
286
|
+
*/
|
|
287
|
+
function splitResolvedEscapeRun(buffer: string): string[] {
|
|
288
|
+
return /^\x1b{2,}$/.test(buffer) ? buffer.split("") : [buffer];
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* `knownEscapeRunLength` is the number of leading ESC bytes a previous call
|
|
293
|
+
* already measured and returned as an all-Escape remainder. Resuming the scan
|
|
294
|
+
* there keeps a run delivered across many small reads linear overall instead of
|
|
295
|
+
* re-walking the whole accumulated prefix on every chunk.
|
|
296
|
+
*/
|
|
297
|
+
function extractCompleteSequences(
|
|
298
|
+
buffer: string,
|
|
299
|
+
knownEscapeRunLength = 0,
|
|
300
|
+
): { sequences: string[]; remainder: string; escapeRunRemainder: number } {
|
|
282
301
|
const sequences: string[] = [];
|
|
283
302
|
let pos = 0;
|
|
284
303
|
|
|
285
304
|
while (pos < buffer.length) {
|
|
286
|
-
|
|
305
|
+
// Slicing at 0 would copy the whole buffer on every call, which is the
|
|
306
|
+
// dominant cost when a long Escape run arrives as many single-byte reads.
|
|
307
|
+
const remaining = pos === 0 ? buffer : buffer.slice(pos);
|
|
287
308
|
|
|
288
309
|
// Try to extract a sequence starting at this position
|
|
289
310
|
if (remaining.startsWith(ESC)) {
|
|
@@ -299,6 +320,37 @@ function extractCompleteSequences(buffer: string): { sequences: string[]; remain
|
|
|
299
320
|
pos += 2;
|
|
300
321
|
continue;
|
|
301
322
|
}
|
|
323
|
+
// Measure the ESC run once. Testing the whole suffix on every iteration
|
|
324
|
+
// while the cut below advances only two bytes made a long run quadratic.
|
|
325
|
+
let runLength = pos === 0 ? Math.max(knownEscapeRunLength, 1) : 1;
|
|
326
|
+
while (runLength < remaining.length && remaining[runLength] === ESC) runLength++;
|
|
327
|
+
// A trailing run of nothing but ESC bytes is ambiguous: the next chunk
|
|
328
|
+
// may still deliver the continuation that turns its last ESC into a Meta
|
|
329
|
+
// prefix (ESC ESC ESC + "[A" is bare Escape then Option+Up). Splitting it
|
|
330
|
+
// now would emit an extra Escape and downgrade the wrapped key to a plain
|
|
331
|
+
// one, firing the destructive double-Escape gesture. Keep the whole run
|
|
332
|
+
// buffered; the flush timeout emits it as individual Escape presses.
|
|
333
|
+
if (runLength === remaining.length) {
|
|
334
|
+
// Only the last two bytes of the run are still ambiguous: a Meta prefix
|
|
335
|
+
// is at most ESC ESC, so any earlier ESC is already a settled press.
|
|
336
|
+
// Emitting them now keeps the retained buffer bounded; holding the whole
|
|
337
|
+
// run made every later read rescan it, which is quadratic for a long run
|
|
338
|
+
// delivered as many small chunks. Order of emitted presses is unchanged.
|
|
339
|
+
const settled = runLength - 2;
|
|
340
|
+
if (settled > 0) {
|
|
341
|
+
for (let index = 0; index < settled; index++) sequences.push(ESC);
|
|
342
|
+
return { sequences, remainder: remaining.slice(settled), escapeRunRemainder: 2 };
|
|
343
|
+
}
|
|
344
|
+
return { sequences, remainder: remaining, escapeRunRemainder: runLength };
|
|
345
|
+
}
|
|
346
|
+
// Only the final two ESC bytes can still form a Meta prefix for the
|
|
347
|
+
// continuation that follows the run; everything before them is a settled
|
|
348
|
+
// Escape press. Emitting them in one step keeps the walk linear.
|
|
349
|
+
if (runLength > 2) {
|
|
350
|
+
for (let index = 0; index < runLength - 2; index++) sequences.push(ESC);
|
|
351
|
+
pos += runLength - 2;
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
302
354
|
// Find the end of this escape sequence
|
|
303
355
|
let seqEnd = 1;
|
|
304
356
|
while (seqEnd <= remaining.length) {
|
|
@@ -315,7 +367,23 @@ function extractCompleteSequences(buffer: string): { sequences: string[]; remain
|
|
|
315
367
|
// here keeps an unterminated sequence from swallowing the next key.
|
|
316
368
|
// seqEnd === 1 is excluded so Meta sequences (ESC ESC) still parse.
|
|
317
369
|
if (remaining[seqEnd] === ESC && seqEnd >= 2 && !continuesAsStringTerminator(remaining, seqEnd)) {
|
|
318
|
-
|
|
370
|
+
// A bare Escape may be followed in the same read by a Meta-wrapped
|
|
371
|
+
// sequence (ESC ESC ESC [ A). Keep the final Meta prefix intact;
|
|
372
|
+
// splitting all three ESC bytes would turn the wrapped arrow into a
|
|
373
|
+
// plain arrow after a destructive double-Escape gesture.
|
|
374
|
+
const trailing = remaining.slice(seqEnd);
|
|
375
|
+
if (/^\x1b+$/.test(candidate) && /^\x1b[^\x1b]/.test(trailing)) {
|
|
376
|
+
sequences.push(...candidate.slice(0, -1).split(""));
|
|
377
|
+
pos += seqEnd - 1;
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
// A cut candidate of nothing but ESC bytes is real Escape key
|
|
381
|
+
// presses, not an Option-as-Meta prefix: a following ESC proves
|
|
382
|
+
// no continuation (like "[A") belongs to it. Emitting the pair
|
|
383
|
+
// as one sequence would parse as the unbound "alt+escape" and
|
|
384
|
+
// silently swallow both presses.
|
|
385
|
+
if (/^\x1b+$/.test(candidate)) sequences.push(...candidate.split(""));
|
|
386
|
+
else sequences.push(candidate);
|
|
319
387
|
pos += seqEnd;
|
|
320
388
|
break;
|
|
321
389
|
}
|
|
@@ -329,7 +397,7 @@ function extractCompleteSequences(buffer: string): { sequences: string[]; remain
|
|
|
329
397
|
}
|
|
330
398
|
|
|
331
399
|
if (seqEnd > remaining.length) {
|
|
332
|
-
return { sequences, remainder: remaining };
|
|
400
|
+
return { sequences, remainder: remaining, escapeRunRemainder: 0 };
|
|
333
401
|
}
|
|
334
402
|
} else {
|
|
335
403
|
// Not an escape sequence - take a single Unicode code point. Keep a
|
|
@@ -337,7 +405,7 @@ function extractCompleteSequences(buffer: string): { sequences: string[]; remain
|
|
|
337
405
|
// complete it.
|
|
338
406
|
const firstCodeUnit = remaining.charCodeAt(0);
|
|
339
407
|
if (isHighSurrogate(firstCodeUnit)) {
|
|
340
|
-
if (remaining.length === 1) return { sequences, remainder: remaining };
|
|
408
|
+
if (remaining.length === 1) return { sequences, remainder: remaining, escapeRunRemainder: 0 };
|
|
341
409
|
const secondCodeUnit = remaining.charCodeAt(1);
|
|
342
410
|
if (isLowSurrogate(secondCodeUnit)) {
|
|
343
411
|
sequences.push(remaining.slice(0, 2));
|
|
@@ -350,7 +418,7 @@ function extractCompleteSequences(buffer: string): { sequences: string[]; remain
|
|
|
350
418
|
}
|
|
351
419
|
}
|
|
352
420
|
|
|
353
|
-
return { sequences, remainder: "" };
|
|
421
|
+
return { sequences, remainder: "", escapeRunRemainder: 0 };
|
|
354
422
|
}
|
|
355
423
|
|
|
356
424
|
export type StdinBufferOptions = {
|
|
@@ -379,6 +447,9 @@ export type StdinBufferEventMap = {
|
|
|
379
447
|
*/
|
|
380
448
|
export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
381
449
|
#buffer: string = "";
|
|
450
|
+
// Length of the leading all-Escape run already measured in #buffer, so a run
|
|
451
|
+
// arriving as many small reads is scanned once overall instead of per chunk.
|
|
452
|
+
#bufferedEscapeRunLength = 0;
|
|
382
453
|
#timeout?: NodeJS.Timeout;
|
|
383
454
|
readonly #timeoutMs: number;
|
|
384
455
|
#pasteMode: boolean = false;
|
|
@@ -486,6 +557,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
486
557
|
if (this.#pasteMode) {
|
|
487
558
|
this.#pasteBuffer += this.#buffer;
|
|
488
559
|
this.#buffer = "";
|
|
560
|
+
this.#bufferedEscapeRunLength = 0;
|
|
489
561
|
|
|
490
562
|
const endIndex = this.#pasteBuffer.indexOf(BRACKETED_PASTE_END);
|
|
491
563
|
if (endIndex !== -1) {
|
|
@@ -505,7 +577,11 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
505
577
|
return;
|
|
506
578
|
}
|
|
507
579
|
|
|
508
|
-
|
|
580
|
+
// A known all-Escape prefix cannot contain the paste introducer, so start
|
|
581
|
+
// the scan just far enough back to catch a marker straddling the boundary.
|
|
582
|
+
// Rescanning the whole retained run on every read made a long run quadratic.
|
|
583
|
+
const pasteScanFrom = Math.max(0, this.#bufferedEscapeRunLength - BRACKETED_PASTE_START.length);
|
|
584
|
+
const startIndex = this.#buffer.indexOf(BRACKETED_PASTE_START, pasteScanFrom);
|
|
509
585
|
if (startIndex !== -1) {
|
|
510
586
|
if (startIndex > 0) {
|
|
511
587
|
const beforePaste = this.#buffer.slice(0, startIndex);
|
|
@@ -513,8 +589,12 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
513
589
|
for (const sequence of result.sequences) {
|
|
514
590
|
this.#emitDataSequence(sequence);
|
|
515
591
|
}
|
|
592
|
+
// A bracketed paste start proves no Meta continuation is coming for a
|
|
593
|
+
// buffered Escape run, so resolve it into individual presses here too.
|
|
516
594
|
if (result.remainder.length > 0) {
|
|
517
|
-
|
|
595
|
+
for (const sequence of splitResolvedEscapeRun(result.remainder)) {
|
|
596
|
+
this.#emitDataSequence(sequence);
|
|
597
|
+
}
|
|
518
598
|
}
|
|
519
599
|
}
|
|
520
600
|
|
|
@@ -523,6 +603,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
523
603
|
this.#pasteMode = true;
|
|
524
604
|
this.#pasteBuffer = this.#buffer;
|
|
525
605
|
this.#buffer = "";
|
|
606
|
+
this.#bufferedEscapeRunLength = 0;
|
|
526
607
|
|
|
527
608
|
const endIndex = this.#pasteBuffer.indexOf(BRACKETED_PASTE_END);
|
|
528
609
|
if (endIndex !== -1) {
|
|
@@ -542,8 +623,11 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
542
623
|
return;
|
|
543
624
|
}
|
|
544
625
|
|
|
545
|
-
const result = extractCompleteSequences(this.#buffer);
|
|
626
|
+
const result = extractCompleteSequences(this.#buffer, this.#bufferedEscapeRunLength);
|
|
546
627
|
this.#buffer = result.remainder;
|
|
628
|
+
// Remember an all-Escape remainder so the next chunk resumes the run scan
|
|
629
|
+
// at its end rather than re-walking every byte received so far.
|
|
630
|
+
this.#bufferedEscapeRunLength = result.escapeRunRemainder;
|
|
547
631
|
|
|
548
632
|
for (const sequence of result.sequences) {
|
|
549
633
|
if (isSgrMousePrefix(sequence) && !isSgrMouseSequence(sequence)) continue;
|
|
@@ -574,11 +658,13 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
574
658
|
}
|
|
575
659
|
const remainder = suffix.slice(index);
|
|
576
660
|
this.#buffer = "";
|
|
661
|
+
this.#bufferedEscapeRunLength = 0;
|
|
577
662
|
this.#pendingKittyPrintableCodepoint = undefined;
|
|
578
663
|
if (remainder) this.process(remainder);
|
|
579
664
|
return;
|
|
580
665
|
}
|
|
581
666
|
this.#buffer = "";
|
|
667
|
+
this.#bufferedEscapeRunLength = 0;
|
|
582
668
|
this.#pendingKittyPrintableCodepoint = undefined;
|
|
583
669
|
this.#sgrQuarantine = true;
|
|
584
670
|
this.#sgrQuarantineBytes = suffix.length;
|
|
@@ -709,12 +795,23 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
709
795
|
|
|
710
796
|
if (isSgrMousePrefix(this.#buffer)) {
|
|
711
797
|
this.#buffer = "";
|
|
798
|
+
this.#bufferedEscapeRunLength = 0;
|
|
712
799
|
this.#pendingKittyPrintableCodepoint = undefined;
|
|
713
800
|
return pendingMeta === undefined ? [] : [pendingMeta];
|
|
714
801
|
}
|
|
715
802
|
|
|
716
|
-
|
|
803
|
+
// A buffer of nothing but ESC bytes at flush time is N real Escape key
|
|
804
|
+
// presses that arrived faster than the flush window (tmux forwards a
|
|
805
|
+
// quick double-Esc as one "\x1b\x1b" chunk within escape-time). Keeping
|
|
806
|
+
// the pair atomic is only correct while a continuation can still turn it
|
|
807
|
+
// into an Option-as-Meta sequence (ESC ESC [ A); once the flush timeout
|
|
808
|
+
// fires, no continuation is coming, and emitting the pair as one
|
|
809
|
+
// sequence parses as the unbound "alt+escape" — silently swallowing
|
|
810
|
+
// both presses and breaking the double-Esc draft-clear gesture.
|
|
811
|
+
const flushedBuffer = splitResolvedEscapeRun(this.#buffer);
|
|
812
|
+
const sequences = pendingMeta === undefined ? flushedBuffer : [pendingMeta, ...flushedBuffer];
|
|
717
813
|
this.#buffer = "";
|
|
814
|
+
this.#bufferedEscapeRunLength = 0;
|
|
718
815
|
this.#pendingKittyPrintableCodepoint = undefined;
|
|
719
816
|
return sequences;
|
|
720
817
|
}
|
|
@@ -725,6 +822,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
|
725
822
|
this.#timeout = undefined;
|
|
726
823
|
}
|
|
727
824
|
this.#buffer = "";
|
|
825
|
+
this.#bufferedEscapeRunLength = 0;
|
|
728
826
|
this.#pasteMode = false;
|
|
729
827
|
this.#pasteBuffer = "";
|
|
730
828
|
this.#pendingKittyPrintableCodepoint = undefined;
|
package/src/terminal.ts
CHANGED
|
@@ -51,6 +51,10 @@ export function keyboardEnhancementEnabled(): boolean {
|
|
|
51
51
|
return $flag("GJC_TUI_KEYBOARD_PROTOCOL", true);
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
function isAppleTerminal(): boolean {
|
|
55
|
+
return $env.TERM_PROGRAM === "Apple_Terminal";
|
|
56
|
+
}
|
|
57
|
+
|
|
54
58
|
/**
|
|
55
59
|
* Minimal terminal interface for TUI
|
|
56
60
|
*/
|
|
@@ -794,18 +798,18 @@ export class ProcessTerminal implements Terminal {
|
|
|
794
798
|
}
|
|
795
799
|
this.#safeWrite("\x1b[?u");
|
|
796
800
|
this.#stdinBuffer?.noteProbeIssued();
|
|
797
|
-
// Windows Terminal and
|
|
801
|
+
// Windows Terminal and Apple Terminal do not implement the Kitty keyboard
|
|
798
802
|
// protocol, so the query above never activates it there. They do honor the
|
|
799
|
-
// modifyOtherKeys fallback below — but that mode breaks
|
|
800
|
-
//
|
|
801
|
-
//
|
|
803
|
+
// modifyOtherKeys fallback below — but that mode breaks CJK/Hangul IME
|
|
804
|
+
// composition: Alt+Enter (and other chords) bypass the IME commit, so the
|
|
805
|
+
// syllable still being composed is never delivered to the app and the
|
|
802
806
|
// action fires on empty text (e.g. queue-message no-ops unless the user
|
|
803
807
|
// types a trailing space to force a commit first). Skip the fallback on
|
|
804
|
-
//
|
|
805
|
-
// chords, and IME composition works again. Opt back in with
|
|
808
|
+
// these terminals; legacy encodings still deliver Alt+Enter (ESC CR) and
|
|
809
|
+
// the newline chords, and IME composition works again. Opt back in with
|
|
806
810
|
// GJC_TUI_KEYBOARD_PROTOCOL=0 disabling all enhancement, or force-enable
|
|
807
|
-
// elsewhere if a Kitty-capable
|
|
808
|
-
if (process.platform === "win32") {
|
|
811
|
+
// elsewhere if a Kitty-capable terminal appears.
|
|
812
|
+
if (process.platform === "win32" || isAppleTerminal()) {
|
|
809
813
|
return;
|
|
810
814
|
}
|
|
811
815
|
this.#modifyOtherKeysTimeout = setTimeout(() => {
|