blob: db2d269ddf39bc3e324519a974c547ad6a27a65c (
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.clone();
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();
}
}
|