aboutsummaryrefslogtreecommitdiffstats
path: root/src/raw.rs
blob: e1166208ea549d441796e97b741f20e04d508c65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use termios::*;

/// can toggle raw mode on a fd, at the latest disables it when it gets dropped
pub struct ScopedRawMode {
    fd: i32,
    settings: Termios,
}

impl Drop for ScopedRawMode {
    fn drop(&mut self) {
        self.disable();
    }
}

impl ScopedRawMode {
    pub fn on_fd(fd: i32) -> Self {
        let settings = Termios::from_fd(fd).unwrap();
        Self { fd, settings }
    }

    pub fn enable(&self) {
        let mut settings = self.settings.clone();
        cfmakeraw(&mut settings);
        tcsetattr(self.fd, TCSANOW, &settings).unwrap();
    }

    pub fn disable(&self) {
        tcsetattr(self.fd, TCSANOW, &self.settings).unwrap();
    }
}