Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when i accidentally hit enter before propts appeared, propts will receive this enter #1303

Open
kongku opened this issue Sep 12, 2023 · 3 comments
Labels

Comments

@kongku
Copy link

kongku commented Sep 12, 2023

when i accidentally hit enter before propts appeared, propts will receive this enter,How to fix this situation?

import { select } from '@inquirer/prompts'

console.log('wait.....')

setTimeout(() => {
  select({
    message: 'select',
    choices: [
      { name: 'a', value: 'a' },
      { name: 'b', value: 'b' },
      { name: 'c', value: 'c' },
    ],
  })
}, 2000)
@SBoudrias
Copy link
Owner

Thanks for the bug report and the code example. I can reproduce it with your snippet easily.

This is a weird behaviour because at the time we press "enter", the readline isn't set, and there's no keypress listener. So it's like there's some kind of replay of the events.

@PoHengLinTW
Copy link
Contributor

Using a debugger helps me find out what might be the cause of the problem.

In create-prompt.mts

  const prompt: Prompt<Value, Config> = (config, context) => {
    // Default `input` to stdin
    const input = context?.input ?? process.stdin;

    // Add mute capabilities to the output
    const output = new MuteStream();
    output.pipe(context?.output ?? process.stdout);

    const rl = readline.createInterface({
      terminal: true,
      input,
      output,
    }) as InquirerReadline;
    const screen = new ScreenManager(rl);

Even though rl is set here, the ReadableStream (process.stdin) already has data from user in the stream when an enter key is pressed.

After the 2 seconds wait, the readline would register keypress event automatically for the input ("This is automatically called by any readline instance on its input if the input is a terminal." from Node's doc) and the data hasn't been read in input would emit the keypress events.

Haven't thought of a good solution to the problem for now, but I would like to share what I found.

@SBoudrias
Copy link
Owner

Yeah that's the problem. I tried a few approach, but can't get to flush the stdin before the prompt shows...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants