osric.uk

Commands can call a movement function that returns a (start, end) pair that identifies the character(s) indicated by the movement.

Problem is that I've been thinking in terms of "wait for next char from stdin", but what I've actually got is "wait for next key event".

Should prototype this, but could turn events into promises something like:

const nextKeyEvent = await new Promise((resolve, reject) => {
    const handler = e => {
        target.removeEventListener(handler);
        resolve(e);
    }
    target.addEventListener("keydown", handler);
});

But I don't think that would work with held down keys. Also, I don't like repeatedly adding and removing the event handler.

Maybe generators?

async function* nextKey(target) {
    const handler = e => {
        yield e;
    }

    target.addEventListener("keydown", handler);
}

That feels better. I'm going to try it.