kkawakam / rustyline

Readline Implementation in Rust
https://crates.io/crates/rustyline/
MIT License
1.5k stars 173 forks source link

Abort readline? #780

Open zambony opened 1 week ago

zambony commented 1 week ago

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 commented 6 days ago

We need to check if Drop::drop is called on exit(1) or not. Because normally, original mode should be restored. See https://github.com/kkawakam/rustyline/blob/99aeb432ad195e635a70d55cf341d5a5fa8864b0/src/lib.rs#L667-L674 If not, maybe try to close StdIn or send a signal (currently, rustyline only listen to SIGWINCH) ?

zambony commented 6 days ago

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).