less-pager-mini
Version:
A scrollable terminal pager for Node.js CLI apps (terminal only)
38 lines (37 loc) • 1.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.readKey = readKey;
/**
* Reads a single keypress from the terminal asynchronously.
*
* - Handles escape sequences by combining `\x1B` with the following character.
* - Times out after 50ms to detect standalone ESC key.
* - Requires the terminal to be in TTY mode.
*
* @returns A promise that resolves to the pressed key string.
* @throws If the terminal is not interactive (non-TTY).
*/
async function readKey() {
if (!process.stdin.isTTY) {
throw new Error('Interactive terminal (TTY) is required to use this feature.');
}
return new Promise(resolve => {
const resolveKey = (key) => {
process.stdin.removeListener('data', keyListener);
clearTimeout(timer);
resolve(key);
};
let timer;
const keyListener = (key) => {
if (timer) {
resolveKey('\x1B' + key);
}
if (key !== '\x1B')
resolveKey(key);
timer = setTimeout(() => {
resolveKey('\x1B');
}, 50);
};
process.stdin.on('data', keyListener);
});
}