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

Make it possible to cancel console-ui prompts #1035

Closed
quintesse opened this issue Jul 11, 2024 · 4 comments · Fixed by #1046
Closed

Make it possible to cancel console-ui prompts #1035

quintesse opened this issue Jul 11, 2024 · 4 comments · Fixed by #1046

Comments

@quintesse
Copy link
Contributor

Right now you can only EXIT by making a selection or hitting enter, etc. It would be nice to be able to cancel out of a prompt, for example by using the escape key.

Doing that by default might not be backward-compatible, but perhaps we could add a .cancelable(true) option to the prompt builders which would then add an extra CANCEL action to the prompt's key bindings. If the user then hits the escape key we could have the prompt return null to indicate that the prompt was cancelled..

If there's interest for this feature I could try to create a PR for it.

@gnodet
Copy link
Member

gnodet commented Jul 17, 2024

@mattirn ?

@mattirn
Copy link
Collaborator

mattirn commented Jul 18, 2024

@quintesse, I think that the possibility canceling prompts would be nice to have... I wrote an other similar ticket #1042 if you are interested to create PR also for it.

@quintesse
Copy link
Contributor Author

hi @mattirn , that's indeed an issue that's very related to this one! In fact ESC could simply be used to cancel the current question, which would then mean returning to the previous one. And only when you hit ESC on the first question would we exit. That seems like a good way to fix both issues.

quintesse added a commit to quintesse/jline3 that referenced this issue Jul 24, 2024
Cancelling a prompt by hitting the ESC key makes it go back to the
previous prompt. If we're already at the fist prompt the result returned
will be `null`.

Fixes jline#1035
@quintesse
Copy link
Contributor Author

I opened a PR for this feature, not sure if this is how you'd want to tackle this, but I wanted to maintain backwards compatibility, meaning that old code doesn't suddenly reacts differently by allowing prompts to be cancelled when before that wasn't possible.

How it works is that at any time the user can hit the ESC key and that will cancel the current prompt item. If there were previously answered prompts the code will back up a single question and re-execute that prompt. If we were at the first or only prompt the result of the entire prompt will be null;

How it was implemented:

  1. added a CANCEL operation to all prompt items
  2. added a cancellable property to all prompt items that has to be passed to their constructor (but devs should be using the builders so there shouldn't be a problem with this change)
  3. in the bindKeys() of each prompt item we check if cancellable is true and only then we add a binding between CANCEL and the ESC key.
  4. added a cancellable(boolean) method to ConsolePrompt, its value will be passed to the constructors of all prompt items

This gives devs a very simple way to enable this feature, they can simply change a line like:

ConsolePrompt prompt = new ConsolePrompt(....);

to:

ConsolePrompt prompt = new ConsolePrompt(....).cancellable(true);

and they'll have opted in to the new feature.

I thought about making it possible to set this property on a per-item basis but it would mean a lot more changes while not making the feature much more useful. (Is it really useful to be able to cancel some questions but not others?)

I also thought about adding the cancellable(boolean) method to the PromptBuilder instead, but the way the API is designed makes that somewhat ambiguous, so I decided against that option.

quintesse added a commit to quintesse/jline3 that referenced this issue Aug 14, 2024
Cancelling a prompt by hitting the ESC key makes it go back to the
previous prompt. By default, if we're already at the fist prompt we'll
repeat the question until the user either answers it or forcefully quits
the application. If on the other hand the `cancellableFirstPrompt` option
of `UiConfig` has been set to `true`, the `prompt()` method will return
`null`.

Fixes jline#1035
quintesse added a commit to quintesse/jline3 that referenced this issue Aug 15, 2024
Cancelling a prompt by hitting the ESC key makes it go back to the
previous prompt. By default, if we're already at the fist prompt we'll
repeat the question until the user either answers it or forcefully quits
the application. If on the other hand the `cancellableFirstPrompt` option
of `UiConfig` has been set to `true`, the `prompt()` method will return
`null`.

Fixes jline#1035
quintesse added a commit to quintesse/jline3 that referenced this issue Sep 2, 2024
Cancelling a prompt by hitting the ESC key makes it go back to the
previous prompt. By default, if we're already at the fist prompt we'll
repeat the question until the user either answers it or forcefully quits
the application. If on the other hand the `cancellableFirstPrompt` option
of `UiConfig` has been set to `true`, the `prompt()` method will return
`null`.

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

Successfully merging a pull request may close this issue.

3 participants