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

Prompt & input remain on screen after hitting Ctrl-C #11

Closed
jwodder opened this issue Nov 22, 2022 · 9 comments
Closed

Prompt & input remain on screen after hitting Ctrl-C #11

jwodder opened this issue Nov 22, 2022 · 9 comments

Comments

@jwodder
Copy link
Collaborator

jwodder commented Nov 22, 2022

When a line of input is terminated by pressing Ctrl-C, the prompt and any text entered so far will remain on the screen even as new lines are printed below them. This is unlike lines terminated by pressing Enter, for which the line and prompt disappear, and it leads to a messy-looking interface.

Program that can be used to reproduce this behavior:

use rustyline_async::{Readline, ReadlineError};
use std::io::Write;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() -> Result<(), ReadlineError> {
    let (mut rl, mut stdout) = Readline::new("prompt> ".into())?;
    loop {
        tokio::select! {
            _ = sleep(Duration::from_secs(1)) => {
                writeln!(stdout, "Message received!")?;
            }
            cmd = rl.readline() => match cmd {
                Ok(line) => {
                    writeln!(stdout, "You entered: {line:?}")?;
                    rl.add_history_entry(line.clone());
                    if line == "quit" {
                        break;
                    }
                }
                Err(ReadlineError::Eof) => {
                    writeln!(stdout, "<EOF>")?;
                    break;
                }
                Err(ReadlineError::Interrupted) => {writeln!(stdout, "^C")?; continue; }
                Err(e) => {
                    writeln!(stdout, "Error: {e:?}")?;
                    break;
                }
            }
        }
    }
    rl.flush()?;
    Ok(())
}

Recording of this behavior:

cancel-bug

(Incidentally, if no text is printed in response to the Ctrl-C, things get a bit messed up on the next output; perhaps that should be a separate issue.)

@zyansheep
Copy link
Owner

Good catch! It makes a lot more sense for the line to stay when you press so you can see what commands you've sent previously.

@zyansheep
Copy link
Owner

I added a fix in the main branch. Take a look if you would like. The Ctrl-C issue might be a bit more annoying to fix tho...

@jwodder
Copy link
Collaborator Author

jwodder commented Apr 9, 2023

Sorry for not getting back to this sooner. I've tried out the code in the OP with rustyline-async 0.3.1 and crossterm 0.26.1, but the behavior seems to be the same.

@zyansheep
Copy link
Owner

I made it so that lines stay on screen if enter is pressed to be consistent with ctrl-c. Is this not the behavior you see?

@jwodder
Copy link
Collaborator Author

jwodder commented Apr 15, 2023

Oh, I see what you mean. That's actually the opposite of the behavior I want, as in the program I'm writing, I already output what the user just entered, and sometimes it's with a timestamp prepended, so just getting rid of the output wouldn't get the results I want. Could you add an option for whether to leave input lines on the screen?

@zyansheep
Copy link
Owner

Alright, I added two config options, one for enter and one for ctrl-c. There is some weird rendering behavior when switching between them in the middle of a readline, but it seems to work if you set it at the beginning. I don't have a lot of time to work on this project though, so if further changes are required, a PR would be much appreciated :)

@jwodder
Copy link
Collaborator Author

jwodder commented Apr 17, 2023

Thanks, that works! I submitted #13 to add a doc comment for the new method.

@zyansheep
Copy link
Owner

Cool, I will close this when I get around to doing a cargo publish

@zyansheep
Copy link
Owner

Fixed in Release 0.3.2 :)

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

2 participants