-
Notifications
You must be signed in to change notification settings - Fork 219
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
Dynamic console-ui prompts #1051
Comments
In itself the idea wouldn't be too hard to implement but the current console-ui wasn't designed with dynamic prompts in mind so it's not immediately a great fit for this idea.
ConsolePrompt prompt = new ConsolePrompt(terminal);
prompt.start(); // Sets up prompt
var countryListPrompt = promptBuilder.createListPrompt()
.name("country")
.newItem("germany").add()
.newItem("italy").add()
.newItem("spain").add()
.build();
String country= prompt.prompt(countryListPrompt).getResult();
var cityListPrompt = getCitiesListPrompt(promptBuilder, country);
String city= prompt.prompt(cityListPrompt).getResult();
prompt.end(); // resets terminal back to normal etc
try (ConsolePrompt prompt = ConsolePrompt.execute(terminal)) {
var countryListPrompt = ... ;
String country= prompt.prompt(countryListPrompt).getResult();
var cityListPrompt = ... ;
String city= prompt.prompt(cityListPrompt).getResult();
} Edit: After thinking about it some more I've realized that the above idea might be nice but isn't compatible with the new feature where you can cancel out of questions to go back to the previous question. At least not without making the code a whole lot uglier. Need to think about this some more. |
NB: One issue that affects any kind of solution to this is that the various builder classes aren't clean builders, they not only build but also add the result to the |
One option would be that instead of passing a list of prompt elements to public Map<String, PromptResultItemIF> prompt(
List<AttributedString> header,
Function<Map<String, PromptResultItemIF>, PromptableElementIF> promptableElementProvider
) throws IOException {
. . .
} Usage would not be as nice as the first example I gave, but would look more like this: void myShowPrompt(Terminal terminal) {
ConsolePrompt prompt = new ConsolePrompt(terminal);
var result = prompt.prompt(this::nextQuestion);
// Do something with result here
}
PromptableElementIF nextQuestion(Map<String, PromptResultItemIF> results) {
if ((!results.containsKey("country")) {
// First question
return ListPromptBuilder.create()
.name("country")
.newItem("germany").add()
.newItem("italy").add()
.newItem("spain").add()
.build();
} else if (!results.containsKey("city")) {
// Second question
String country = results.get("country").getResult();
return getCitiesListPrompt(promptBuilder, country);
} else if ( ... ) {
// Any other questions
}
return null; // No further questions
} |
# Conflicts: # console-ui/src/main/java/org/jline/consoleui/prompt/ConsolePrompt.java
Ok, this is creepy, this is so close to what I had locally that I had to check three times to make sure I never actually pushed my code somewhere 🤣 |
This adds an easier way to create complex dynamic prompts
This adds an easier way to create complex dynamic prompts
This adds an easier way to create complex dynamic prompts
This adds an easier way to create complex dynamic prompts
Right now the type and contents of each item in a prompt has to be defined before executing the prompt. This makes the prompt a very static thing that can't react to a user's input. It would be nice if a prompt could be more dynamic and change depending on external factors (most likely user input).
A possible example could be a question that asks to select a country and then follows up with a question to select a city. The answer to the first question obviously affects the possible option in the second one.
But one could also imagine prompts where the number and type of questions changes completely.
For example when asking for a payment method: a) credit card b) bank account c) paypal and then depending on the answer the follow up questions would either be [credit card number, expiration date, cvv], [iban] or [email].
The text was updated successfully, but these errors were encountered: