aboutsummaryrefslogtreecommitdiffstats
path: root/src/raw.rs
blob: e3d66d126d163568e2a48d7c1aa511cfa52d51dc (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
31
32
33
34
use termios::*;

use crate::panic;

/// 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;
        cfmakeraw(&mut settings);
        tcsetattr(self.fd, TCSANOW, &settings).unwrap();
        panic::enable_cr();
    }

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