fix(tui): cancel prompts on raw Esc input in minimal mode

This commit is contained in:
William Valentin
2026-02-16 11:36:14 -08:00
parent 205203a458
commit a5e8d6c216
+26
View File
@@ -217,11 +217,31 @@ export class MinimalTui {
}
let settled = false;
const stdin = process.stdin as NodeJS.ReadStream & {
isRaw?: boolean;
setRawMode?: (mode: boolean) => void;
};
const wasRaw = Boolean(stdin.isRaw);
let enabledRawForPrompt = false;
let dataListener: ((chunk: Buffer) => void) | null = null;
if (stdin.isTTY && stdin.setRawMode && !wasRaw) {
stdin.setRawMode(true);
enabledRawForPrompt = true;
}
const finish = (answer: string) => {
if (settled) {
return;
}
settled = true;
if (dataListener) {
stdin.removeListener('data', dataListener);
dataListener = null;
}
if (enabledRawForPrompt && stdin.setRawMode) {
stdin.setRawMode(false);
}
this.activePromptCancel = null;
this.rl?.removeListener('close', onClose);
resolve(answer);
@@ -229,6 +249,12 @@ export class MinimalTui {
const onClose = () => finish('');
this.rl.once('close', onClose);
dataListener = (chunk: Buffer) => {
if (chunk.length === 1 && chunk[0] === 0x1b) {
this.activePromptCancel?.();
}
};
stdin.on('data', dataListener);
this.activePromptCancel = () => {
if (!this.rl) {
finish('');