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

Abort readline? #780

Open
zambony opened this issue Jun 30, 2024 · 4 comments
Open

Abort readline? #780

zambony opened this issue Jun 30, 2024 · 4 comments

Comments

@zambony
Copy link

zambony commented Jun 30, 2024

I'm new to Rust and am not sure if this is the right way to handle things, but I am using rustyline to create a program that accepts user input to send over TCP.

I've set up a separate task to run in the background to poll if the TCP socket is open, and, if not, it will exit the program. The problem is that I'm not sure how to get rustyline to stop waiting for input when I want to shutdown, so I use exit(1), but this does not let rustyline restore the terminal. Should I just manually disable raw mode before exiting, or is there an approved way to abort a readline so I can gracefully shutdown?

Thanks for the great crate, too.

@gwenn
Copy link
Collaborator

gwenn commented Jul 1, 2024

We need to check if Drop::drop is called on exit(1) or not.
Because normally, original mode should be restored.
See

rustyline/src/lib.rs

Lines 667 to 674 in 99aeb43

let guard = Guard(&original_mode);
let user_input = self.readline_edit(prompt, initial, &original_mode, term_key_map);
if self.config.auto_add_history() {
if let Ok(ref line) = user_input {
self.add_history_entry(line.as_str())?;
}
}
drop(guard); // disable_raw_mode(original_mode)?;

If not, maybe try to close StdIn or send a signal (currently, rustyline only listen to SIGWINCH) ?

@zambony
Copy link
Author

zambony commented Jul 1, 2024

Thanks for the suggestions. From what I've found online, I don't think there is a way for me to close the main Stdin of the program. I think I'll need to fork my process or somehow do the input processing on a dedicated thread that I can kill (e.g., not join on exit).

@xeruf
Copy link

xeruf commented Aug 13, 2024

@eldoccc
Copy link

eldoccc commented Sep 6, 2024

If not, maybe try to close StdIn or send a signal (currently, rustyline only listen to SIGWINCH) ?

listening for SIGINT would be it right ?

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

4 participants