Inquirer.js icon indicating copy to clipboard operation
Inquirer.js copied to clipboard

The default value with timeout

Open wxu-aa opened this issue 5 years ago • 3 comments

Could we provide the countdown for default value?

If user didn't pick one value before timeout, like in 5s, it will automatically pick the default value and move to next step.

Could we support such feature?

Should be very useful in many scenarios with default value and this won't let people wait for too long time before they realized they have to pick one option.

wxu-aa avatar Jun 25 '19 01:06 wxu-aa

Sounds good, you could make an Inquirer plugin for this idea.

LitoMore avatar Jul 15 '19 09:07 LitoMore

I like this would be a feature.

It would be nice to skip stop or fill a question on a external event. For example a timebased multiple choise quiz.

I can't find a option to skip it, without user input

zoutepopcorn avatar Nov 16 '19 09:11 zoutepopcorn

Simply put the prompt in a Promise and use a normal setTimeout:

async function timeoutPrompt(question, defaultAnswer, timeMs) {
    return await new Promise(async (resolve) => {
        let options = {
            name: 'yourKey',
            message: question,
        };
        setTimeout(function () {
            resolve(defaultAnswer);
        }, timeMs);
        let answer = await Inquirer.prompt([options]);
        resolve(answer);
    });
}

(async () => {
    let answer = await timeoutPrompt('Can you wait 5 seconds please', 'no', 4999);
    console.table(answer);
})();

If you want to use a callback, simply replace resolve by your callback function.

automatize-softwares avatar Feb 22 '21 10:02 automatize-softwares

@SBoudrias could you add this, this is simple and very useful base functionality. Thanks a lot!

andrisi avatar Dec 15 '22 21:12 andrisi

TBH, I think it's easy enough to wrap a setTimeout on the developers' side. It's not necessary to make this a base functionality.

LitoMore avatar May 22 '23 06:05 LitoMore

The whole point of theese brilliant libraries, that they do make it easy for developers to implement things in a given area of coding, something that's repeated millions of times. Yes, this can be done on the developer's side, but I guess most will just look for another library that has this built in or do some hackish solution or fail. Actually whatever this module does can be done by developers, so why does it even exist? It's a balance.

andrisi avatar May 22 '23 11:05 andrisi

A good project should be simple and easy to use for everyone.

Take this Timeout feature as an example, we need to think about:

  • When the user sends keyboard events, which events will refresh the countdown?
  • In which scenarios will this function be needed?
  • What are the benefits of being built-in? Why not make it a plugin?
  • What other products have a countdown at the CLI prompt?
  • How to test this?

We can keep this issue open and let's see how many users want this.

LitoMore avatar May 22 '23 12:05 LitoMore

@SBoudrias I just realize we now have no way to manually end the current prompt.

Previously we can use prompt.ui.activePrompt.done() to set the prompt to done. But for our @inquirer/prompts, we don't have an API like that.

LitoMore avatar May 27 '23 12:05 LitoMore

@andrisi For the old inquirer, you can use Promise.race to implement this feature.

const prompt = inquirer.prompt(...);

const defaultValue = new Promise(resolve => {
	setTimeout(() => {
		resolve(...)
		prompt.ui.activePrompt.done();
	}, 5000);
});

const answers = await Promise.race([defaultValue, prompt])

This is easy enough.

LitoMore avatar May 27 '23 14:05 LitoMore

There's now a similar solution with the new API. Details on the readme

Thanks @LitoMore!

SBoudrias avatar Jun 03 '23 20:06 SBoudrias

We can change the documentation to await setTimeout() once we target our package with Node.js 16.

See https://nodejs.org/api/timers.html#timerspromisessettimeoutdelay-value-options

LitoMore avatar Jun 03 '23 20:06 LitoMore

@LitoMore want to send a PR to do this now? In the documention, it's fine to target new Node; especially in recipes!

SBoudrias avatar Jun 03 '23 21:06 SBoudrias

@SBoudrias np. But we have already released a new major version of our package.

The "engines": ">=16" change will be another breaking to our package.

What do you think? Or just ignore this since we don't have too much users right now?

LitoMore avatar Jun 03 '23 21:06 LitoMore

Oh, I don't mean we should bump require v16 today. I meant that the recipes can refer to timers/promises even if it won't work for Node 14 users.

SBoudrias avatar Jun 03 '23 21:06 SBoudrias

Got it.

LitoMore avatar Jun 03 '23 21:06 LitoMore