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

facilities to correctly handle environments with atypical stty intr config #931

Open
crabdancing opened this issue Sep 23, 2024 · 0 comments

Comments

@crabdancing
Copy link

crabdancing commented Sep 23, 2024

Is your feature request related to a problem? Please describe.

Downstream programs would ideally be capable of responding correctly to stty interrupt in raw terminals. As it stands, there seems to be a lot of confusion surrounding how to implement this feature, if it's even possible. (I personally haven't used crossterm directly that much, and I'm not sure how to do it, if indeed it is possible.)

Describe the solution you'd like

Documentation on, or facilities added in order to, support a crossterm equivelant of this code:

use std::io::{self, Read};
use std::os::unix::io::AsRawFd;
use termios::*;

fn main() {
    let stdin_fd = io::stdin().as_raw_fd();
    let original_term_config = Termios::from_fd(stdin_fd).unwrap();

    let interrupt_char = original_term_config.c_cc[VINTR];

    let mut raw_termios = original_term_config;
    cfmakeraw(&mut raw_termios);
    tcsetattr(stdin_fd, TCSANOW, &mut raw_termios).unwrap();

    println!("Press the interrupt character to exit...\r");

    let mut buffer = [0u8; 1];
    loop {
        io::stdin().read_exact(&mut buffer).unwrap();
        eprintln!("Got: {:?}\r", &buffer);
        if buffer[0] == interrupt_char {
            break;
        }
    }

    tcsetattr(stdin_fd, TCSANOW, &original_term_config).unwrap();
}

The purpose being to correctly detect the interrupt character, and watch for it in the raw terminal. So, e.g., if you do stty intr ^Q, and then run this program, it will respect your terminal's interrupt config. (To wit, ideally if you hit Ctrl+Q, the program will correctly be interrupted -- and the program will conversely NOT treat Ctrl+C as an interrupt character.)

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