-
-
Notifications
You must be signed in to change notification settings - Fork 281
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
How to chain more than one call? #6
Comments
Just for information, then I tried to change my function so that it worked with a recursive function, note that unlike #5, I use a 'use strict';
const Enquirer = require('enquirer')
const inspect = require ('util').inspect
function query (i) {
let enquirer = new Enquirer ()
enquirer.register ('radio', require ('prompt-radio'))
enquirer.question ('argName', 'Name for the input argument (' + i + ')?')
enquirer.question ('argType', 'Type of argument (' + i + ')?', {
type: 'radio',
choices: ['STRING', 'INT32', 'BOOL']
})
enquirer.ask()
.then (answers => {
console.log ('Argument ' + i)
console.log (inspect (answers))
console.log ('--')
if (i > 0) {
console.log ('Asking for argument ' + i + ' now...')
query (i-1)
}
})
}
query (3) This time, I've got this on my console: ? Name for the input argument (3)? Foo
? Type of argument (3)? INT32
Argument 3
{ argName: 'Foo', argType: [ 'INT32' ] }
--
Asking for argument 2 now...
^C% So, as before, the first set of questions is ok: I'm asked for the name, then the type, but when I validate, this time it hangs, like in #5, but this is weird because it seems to be on a completely new Any chance? |
Thanks for the detailed explanation. We had this issue a couple of weeks ago and I'm struggling to remember how we resolved it. e.g. I thought this was fixed, but apparently it isn't. That said, I have a general idea of what's causing it but I'm not sure if I know enough to explain it yet. Let me look into it and I'll come back with updates as soon as I learn what's happening. thank you for creating the issue! any thoughts @doowb? |
Hum thanks. How come you did not run into this problem sooner? |
Just off the top of my head... As to why it's not showing the other prompts, I'll have to look into this a little closer because we have examples that are asking multiple questions that work fine. Just to be sure, do you have all of the latest code? I usually delete my I'll dig into it a little more in a bit. |
The reason actually makes sense (if it's what I think it is). After publishing enquirer I decided to refactor all of the individual prompts to make them completely self-encapsulated. Since each prompt needed to be able to control when and how the readline interface is creates, I had to refactor the logic around that to ensure that prompts and enquirer could both instantiate and terminate the interface when needed. I'm guessing this is where the issue is edit: and thank you for the kind words :) |
Hi @doowb , thanks for answering. I perfectly understand why the first example doesn't work (indeed tries to ask the three questions at once), but the second code snippet should be working. By the way, yes there are situations where several questions can be asked, but it's when you call Also from the top of my head, isn't it related to this where you somehow disable the |
It's called here, but I'm thinking the bug is in We had to make changes to allow prompts themselves to terminate a readline session. This was a little trickier than I thought it would be, because it's not at all implicit when a user plans to chain prompts - when the interface should just be terminated after the first prompt. To clarify, this is a lot easier to do when edit: and thanks for working through this with us |
Okay, I'll take a look at |
hey no worries, we're not expecting you to do any debugging or looking into it at all. Just your feedback on whether something is working for you etc. is great |
Yeah don't worry, but if I can help... By the way, I've just isolated 'use strict';
const Prompt = require ('prompt-base')
const inspect = require ('util').inspect
let prompt = new Prompt({
name: 'name',
message: 'Argument name?'
})
prompt.run()
.then (answer => {
console.log ('answers: ' + inspect (answer))
let prompt2 = new Prompt({
name: 'type',
message: 'Argument type?'
})
return prompt2.run()
})
.then( answer2 => {
console.log ('Answer2: ' + inspect (answer2))
})
.catch( err => {
console.error ('Err: ' + err)
}) And this works okay for me, here's the output of the console: ? Argument name? Foo
answers: 'Foo'
? Argument type? String
Answer2: 'String' So it does ask for the first argument's name then the argument's type. EDIT: I've just confirmed that I have no problems asking severals questions with 'use strict';
const Prompt = require ('prompt-base')
const inspect = require ('util').inspect
let names = []
function query(n) {
let prompt = new Prompt({
name: 'name',
message: 'Argument #' + n + '\'s name?'
})
return prompt.run()
.then (answer => {
if (answer !== '') {
console.log (`'${answer}' registered for argument #${n}`)
names.push (answer)
return query(++n)
}
})
}
query (1)
.then( () => {
console.log ('Questions finished, you\'ve supplied ' + names.length + ' arguments:\n' + inspect (names))
})
.catch( err => {
console.error ('Err: ' + err)
}) And the console session: ? Argument #1's name? foo
'foo' registered for argument #1
? Argument #2's name? bar
'bar' registered for argument #2
? Argument #3's name? quux
'quux' registered for argument #3
? Argument #4's name?
Questions finished, you've supplied 3 arguments:
[ 'foo', 'bar', 'quux' ] So it really seems to work. That being said, I'm sorry I have to leave. I hope we'll find the solution soon! A important tool of mine depends directly on this! |
As @jonschlinkert pointed out, the I think we had initially done this because using prompts multiple times was adding too many event listeners. This caused "earlier" prompts to still be listening for events on the UI event though they should be finished. We added a I think the change in Then in I've tried these out locally and will push up changes in a little bit if there are no objections. @nschoe thanks for the examples |
It doesn't hurt for users to be able to do this, but this shouldn't "need" to be done in enquirer. It should only be necessary when individual prompts are used without enquirer AND the prompt is chained or nested. In enquirer, the |
Good point... the original example will run when using the I think |
@nschoe please pull down the latest and try your example with the |
Hi @doowb, @jonschlinkert , thanks for the amazing work again, I'm happy that you seemed to have worked it out. I'm pulling the latest and trying again right now, I'll come back to you. In the meantime, reading your message, what I understand is that we have two functions |
Okay sorry for the delay I was dragged urgently by someone. ? Name for the input argument (2)? foo
? Type of argument (2)? BOOL
Argument 2
{ argName: 'foo', argType: [ 'BOOL' ] }
--
Asking for argument 1 now...
? Type of argument (2)? BOOL, BOOL
? Name for the input argument (1)? bar
? Type of argument (2)? BOOL, BOOL, STRING
? Type of argument (1)? INT32
Argument 1
{ argName: 'bar', argType: [ 'INT32' ] }
--
Asking for argument 0 now...
? Type of argument (2)? BOOL, BOOL, STRING, STRING
? Type of argument (1)? INT32, INT32
? Name for the input argument (0)? quux
? Type of argument (2)? BOOL, BOOL, STRING, STRING, STRING
? Type of argument (1)? INT32, INT32, INT32
? Type of argument (0)? STRING
Argument 0
{ argName: 'quux', argType: [ 'STRING' ] }
-- It is a bit bloated on the output, but at least it works: it asks me for several chained questions. It's only at the end that the output get a bit scrambled. Do you have ideas as to why? Anyway, thanks a lot guys for the amazing work and fast reactivity! As far as I'm concerned, you can close, I'm just leaving this opened to have a chance to discuss the scrambled display. |
There was a bug in prompt-checkbox (used by prompt-radio) that I fixed yesterday causing the extra output. Get the latest and it should be good. |
Awesome, thanks! What about the issue on enquirer/prompt-confirm#1, any idea? |
closing per #6 (comment) |
Hi,
I've seen #5 but this one is slightly different (at least I think so). I find the library very nice, but I'm failing at making a simple menu with 2 screens (the 2nd screen depending on the output of the 1st), which seems like a basic use of
enquirer
.So have I missed something here?
Here is my use case: I want to first ask the user for the name of an argument, and then make him chose between a predefined set of arguments (the use case for this is templating, but that doesn't matter)
Here is my code:
And when I run it, I've got:
I've got the first
Name input for the argument (1)?
, then I answeredFoo
, then I was presented the radio choice, I selectedINT32
with<Space>
and when I pressed entered, it dumped all of this.I did not have a second (and neither a third) choice. How can I achieve this?
The text was updated successfully, but these errors were encountered: