I have a simple bit of code for reading input for a terminal-based build script.
async function readLine(): Promise<string> {
return new Promise<string>(resolve => {
const callback = (data: any) => {
process.stdin.off('data', callback);
resolve(data.toString().trim());
};
process.stdin.on('data', callback);
});
}
This works well enough, but I'd like to be able to detect if an up-arrow is pressed so a user can back up to a previous prompt.
Every example I've seen so far that would allow me to detect an up-arrow key requires totally abandoning line-based input, instead handling all input one character at a time. I'd rather not do that.
What I'd like is to have a callback that's triggered only by the up-arrow key (and maybe other special keys later on), with all other keys coming through the usual 'data' callback, one full Enter-terminated line at a time.
Is there a way to do this?