fix(tui): deduplicate kitty printable input

closes #3780
This commit is contained in:
Mario Zechner
2026-04-27 21:02:40 +02:00
parent 27c0c2ec13
commit bdb416cbc0
3 changed files with 52 additions and 7 deletions

View File

@@ -181,6 +181,14 @@ function isCompleteApcSequence(data: string): "complete" | "incomplete" {
/**
* Split accumulated buffer into complete sequences
*/
function parseUnmodifiedKittyPrintableCodepoint(sequence: string): number | undefined {
const match = sequence.match(/^\x1b\[(\d+)(?::\d*)?(?::\d+)?u$/);
if (!match) return undefined;
const codepoint = parseInt(match[1]!, 10);
return codepoint >= 32 ? codepoint : undefined;
}
function extractCompleteSequences(buffer: string): { sequences: string[]; remainder: string } {
const sequences: string[] = [];
let pos = 0;
@@ -246,6 +254,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
private readonly timeoutMs: number;
private pasteMode: boolean = false;
private pasteBuffer: string = "";
private pendingKittyPrintableCodepoint: number | undefined;
constructor(options: StdinBufferOptions = {}) {
super();
@@ -274,7 +283,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
}
if (str.length === 0 && this.buffer.length === 0) {
this.emit("data", "");
this.emitDataSequence("");
return;
}
@@ -291,6 +300,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
this.pasteMode = false;
this.pasteBuffer = "";
this.pendingKittyPrintableCodepoint = undefined;
this.emit("paste", pastedContent);
@@ -307,10 +317,11 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
const beforePaste = this.buffer.slice(0, startIndex);
const result = extractCompleteSequences(beforePaste);
for (const sequence of result.sequences) {
this.emit("data", sequence);
this.emitDataSequence(sequence);
}
}
this.pendingKittyPrintableCodepoint = undefined;
this.buffer = this.buffer.slice(startIndex + BRACKETED_PASTE_START.length);
this.pasteMode = true;
this.pasteBuffer = this.buffer;
@@ -323,6 +334,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
this.pasteMode = false;
this.pasteBuffer = "";
this.pendingKittyPrintableCodepoint = undefined;
this.emit("paste", pastedContent);
@@ -337,7 +349,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
this.buffer = result.remainder;
for (const sequence of result.sequences) {
this.emit("data", sequence);
this.emitDataSequence(sequence);
}
if (this.buffer.length > 0) {
@@ -345,12 +357,23 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
const flushed = this.flush();
for (const sequence of flushed) {
this.emit("data", sequence);
this.emitDataSequence(sequence);
}
}, this.timeoutMs);
}
}
private emitDataSequence(sequence: string): void {
const rawCodepoint = sequence.length === 1 ? sequence.codePointAt(0) : undefined;
if (rawCodepoint !== undefined && rawCodepoint === this.pendingKittyPrintableCodepoint) {
this.pendingKittyPrintableCodepoint = undefined;
return;
}
this.pendingKittyPrintableCodepoint = parseUnmodifiedKittyPrintableCodepoint(sequence);
this.emit("data", sequence);
}
flush(): string[] {
if (this.timeout) {
clearTimeout(this.timeout);
@@ -363,6 +386,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
const sequences = [this.buffer];
this.buffer = "";
this.pendingKittyPrintableCodepoint = undefined;
return sequences;
}
@@ -374,6 +398,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
this.buffer = "";
this.pasteMode = false;
this.pasteBuffer = "";
this.pendingKittyPrintableCodepoint = undefined;
}
getBuffer(): string {