Skip to content

Instantly share code, notes, and snippets.

@thomasballinger

thomasballinger/myrepl.mjs

Last active May 30, 2020
Embed
What would you like to do?
import tty from 'tty'
import { emitKeypressEvents } from 'readline';
let drain;
process.stdout.on('drain', () => { drain() });
const waitForDrain = () => new Promise(r => {drain = r});
function waitForDrainify(func) {
return async (...args) => {
if (!func(...args)) {
await waitForDrain();
}
}
};
const write = waitForDrainify((s) => process.stdout.write(s));
const moveCursor = waitForDrainify((x, y) => process.stdout.moveCursor(x, y));
(async function simpleRepl() {
emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true)
let currentInput = '';
const processInp = async (str, key) => {
await moveCursor(0, 1);
const info = `pressed ${key.name}`
await write(info)
await moveCursor(-info.length, -1)
if (key.name === 'return') {
await write(`\n${currentInput.toUpperCase()}\n> `)
currentInput = ''
} else if (key.name === 'backspace') {
if (currentInput.length) await write('\b \b')
currentInput = currentInput.slice(0, currentInput.length-1);
} else {
currentInput += key.sequence
await write(key.sequence);
}
};
let lastKeyPromise = Promise.resolve();
process.stdin.on('keypress', async (str, key) => {
if(key.sequence === '\u0003') { // this is ctrl-c
process.exit();
}
const prevKeyFinished = lastKeyPromise;
let inputDoneBeingProcessed;
lastKeyPromise = new Promise(r => {inputDoneBeingProcessed = r});
await prevKeyFinished;
await processInp(str, key)
inputDoneBeingProcessed();
});
await write('> ')
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment