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

Clearing the screen from the previous prompts #108

Open
hideki2k02 opened this issue Apr 3, 2023 · 5 comments
Open

Clearing the screen from the previous prompts #108

hideki2k02 opened this issue Apr 3, 2023 · 5 comments

Comments

@hideki2k02
Copy link

Is your feature request related to a problem? Please describe.
If you're designing something like an Interactive Menu, its not possible to clear the screen from the previous prompts with traditional methods.

Describe the solution you'd like
I was imagining something like this:

let confirmation_choice = Confirm::new().with_default(false).prompt();

// That would move the cursor to the prompt's position in the terminal and overwrite
// The characters there with a Control Characters (empty characters)
confirmation_choice.Clear();

Describe alternatives you've considered
I've tried things like the following, but all of those only run after the program is stopped for some reason

print!("{}[2J", 27 as char);
print!("{esc}[2J{esc}[1;1H", esc = 27 as char);
print!("\x1B[2J");

Additional context
Some back-end libraries (like crosstext) do have an option to Clear the text, so it should not be that difficult to implement.

@hideki2k02
Copy link
Author

I've found out a workaround, however something on the library would be nice if possible

use std::io::{self, Write};

// Select an Operation:
// > Foo
// Bar

// The commands below would clear the lines above
print!("{esc}[2J{esc}[1;1H", esc = 27 as char);
io::stdout().flush().unwrap();

@mikaelmello
Copy link
Owner

let confirmation_choice = Confirm::new().with_default(false).with_autoclear().prompt();

Would something like that be a good UX for you?

It might also make sense to have that included in the RenderConfig. A render_answer boolean that would be true by default, or (any other naming approach).

Implementing this will be easy as we already have the entire logic of cleaning up all lines used by the prompt, so it would come down to not rendering the last line containing the final answer.

So, possible approaches:

  1. Add that as an option in the prompt.
  • Quick and easy for users.
  • If the goal is to enable this in several prompts, lots of repeating lines.
  1. Add that as an option in the RenderConfig
  • Makes more sense semantically, it is ultimately a decision on how to render a prompt state.
  • With the global render config, you can configure this once and it will apply to all prompts.
  • For one-off prompts/changes, it will mean a lot of boilerplate: cloning the global config, modifying a single property and calling the prompt with a custom render config.

It seems to me that this kind of feature would either be enabled by all prompts in a program (be it 1 or many), or none of them. The scenario where some prompts showing the final answer and others are cleared from the screen is harder for me to picture, so it makes sense to optimize for the common case and go with approach #2. What are your thoughts?

@hideki2k02
Copy link
Author

Hello! That was quite quick for an answer haha, yes it is perfect! I think passing it as an option would be better as the user probably would like to triple-check what he did haha

In my very specific case, i was clearing all the previous prompts because i've made a "Back to previous Menu" thing:

> Select an Operation: Check the Database
> Select the entry: japa4551

// This is the part that i would display some data, but i'm not there (yet)

> Would you like to select another entry? No

// In case the yes/no prompt above returns no, it clears the screen/prompts and "returns to the main menu"
// AKA sets "entry_selection_loop" to false and returns to the main loop

@mikaelmello
Copy link
Owner

Having an option in the prompt makes sense to me, but I also think it's important for the setting to be in RenderConfig.

It'll probably be both :)

It's on my radar, but it might take a while as I'm refactoring the code base to allow for some future improvements. If you're interested in creating a PR you have my support!

@FintanH
Copy link

FintanH commented Mar 22, 2024

I think I've run into something similar. We're using your library and it's great! The only thing that would improve it is that when we're using the password validator, if the user enters their password incorrectly it doesn't clear the prompt to try again -- they have to physically delete all the characters 😩

So having an option to clear would be amazing 🙏

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

No branches or pull requests

3 participants