sindresorhus / ora

Elegant terminal spinner
MIT License
9.08k stars 269 forks source link

spinner breaks readline loop #220

Closed jschuur closed 1 year ago

jschuur commented 1 year ago

I'm building a small REPL with readline that runs an async function between prompts and when I show a spinner when the async function runs, this seems to break the loop.

Full CodeSandbox repo, but here's the key parts:

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

async function processPrompt(input) {
  const spinner = ora(`Thinking (${input}) ...`).start();
  await sleep(1000);
  spinner.succeed("Response in 1.2s");
}

(async () => {
  rl.setPrompt('> ');

  for await (const input of rl) {
    if (input.startsWith("!")) {
      await runCommand(input.slice(1));
    } else {
      await processPrompt(input);
    }
  }

  rl.on("close", function () {
    console.log("See you later!");
    process.exit(0);
  });
})();

The version without a spinner behaves as expected, but the spinner version simply ends after the first loop. I expected it to continuously wait for a new prompt and run that through the async function again.

Perhaps related to this is that I don't see my > prompt (and I don't user rl.prompt(), because the above for await (const input of rl) is how I've seen endless loops documented).

cyyyu commented 1 year ago

I had the same issue in my app. discardStdin: false works for me.

jschuur commented 1 year ago

Worked for me too. Thanks, @cyyyu!