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

Add Terminal Reset Helper #203

Open
mitsuhiko opened this issue Jan 24, 2024 · 1 comment
Open

Add Terminal Reset Helper #203

mitsuhiko opened this issue Jan 24, 2024 · 1 comment

Comments

@mitsuhiko
Copy link
Collaborator

mitsuhiko commented Jan 24, 2024

This came out of the issue filed over at dialoguer: console-rs/dialoguer#294

The TLDR is that when the application terminates in unexpected ways (or really just ctrl+c) it's pretty easy for the terminal to be left in an unfortunate state. While there might be ways in which APIs can be restructured to cause this less, this is generally quite unfortunate and too easy to cause.

It also will in most cases require either using raw inputs to ensure that ctrl+c is manually handled on all inputs to reset the terminal state locally or (more correctly) to handle cltr+c globally. The latter however requires the user to be involved here as there can really only be one handler via the ctrlc crate or similar.

As a strawman proposal I'm considering adding a helper that people can put into their own ctrlc handlers which resets the terminal back to the state it was in when the application was started.

fn main() {
    let state = console::capture_terminal_state();
    ctrlc::set_handler(move || {
        state.restore();
        // ...
    });
    // stuff happens here
}
@mitsuhiko
Copy link
Collaborator Author

What's kind of ironic here is that the actual terminal settings are usually already properly reset, what's not reset is the cursor which is not in the terminal state that is manipulated via normal operations. Instead the cursor is hidden and shown via ANSI codes and there is no reasonable way to query the state of that.

So in a way what would need to be done is to carefully manage the cursor state within console/dialoguer independently. I'm really not sure what the most reasonable approach is here.

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

1 participant