blob: 909ba9e22d61cff7623df7c333910939215bccb1 (
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
35
36
37
38
39
40
41
42
43
44
45
|
use std::os::unix::io::AsRawFd as _;
pub struct RawGuard {
termios: nix::sys::termios::Termios,
cleaned_up: bool,
}
impl RawGuard {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let stdin = std::io::stdin().as_raw_fd();
let termios = nix::sys::termios::tcgetattr(stdin).unwrap();
let mut termios_raw = termios.clone();
nix::sys::termios::cfmakeraw(&mut termios_raw);
nix::sys::termios::tcsetattr(
stdin,
nix::sys::termios::SetArg::TCSANOW,
&termios_raw,
)
.unwrap();
Self {
termios,
cleaned_up: false,
}
}
pub fn cleanup(&mut self) {
if self.cleaned_up {
return;
}
self.cleaned_up = true;
let stdin = std::io::stdin().as_raw_fd();
let _ = nix::sys::termios::tcsetattr(
stdin,
nix::sys::termios::SetArg::TCSANOW,
&self.termios,
);
}
}
impl Drop for RawGuard {
fn drop(&mut self) {
self.cleanup();
}
}
|