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:
38
cli-ink.js
38
cli-ink.js
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user