Refactor CLI input handling in cli-ink.js to improve ESC key detection for clean exit. Update InkApp import to .jsx extension and change start:ink script to use tsx for better compatibility. Remove deprecated InkApp.js file to streamline codebase.

This commit is contained in:
sebseb7
2025-08-12 02:56:13 +02:00
parent edf0d3cffb
commit ce6933377a
4 changed files with 117 additions and 91 deletions

View File

@@ -2,21 +2,45 @@
import 'dotenv/config';
import React from 'react';
import { render } from 'ink';
import InkApp from './src/ui/InkApp.js';
import InkApp from './src/ui/InkApp.jsx';
const { unmount } = render(React.createElement(InkApp));
// ESC to exit
// ESC to exit (only bare ESC, not escape sequences like arrows)
if (process.stdin.isTTY) {
try { process.stdin.setRawMode(true); } catch { }
let escPending = false;
let escTimer = null;
const exitCleanly = () => {
unmount();
try { process.stdin.setRawMode(false); } catch { }
process.exit(0);
};
const onData = (data) => {
const str = typeof data === 'string' ? data : String(data);
if (str === '\u001b' || (str.length && str.charCodeAt(0) === 27)) {
unmount();
try { process.stdin.setRawMode(false); } catch { }
process.exit(0);
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(String(data));
for (const byte of buffer) {
if (!escPending) {
if (byte === 0x1b) { // ESC
escPending = true;
escTimer = setTimeout(() => {
// No additional byte followed: treat as bare ESC
escPending = false;
escTimer = null;
exitCleanly();
}, 120);
}
// else: ignore other bytes
} else {
// Some byte followed ESC quickly: it's an escape sequence → cancel exit
if (escTimer) { clearTimeout(escTimer); escTimer = null; }
escPending = false;
// Do not process further for exit
}
}
};
process.stdin.on('data', onData);
}