aboutsummaryrefslogtreecommitdiffstats
path: root/src/raw_guard.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-03-09 02:33:01 -0500
committerJesse Luehrs <doy@tozt.net>2021-03-09 02:40:29 -0500
commitdd845e949ac59e08bf12d0fcac8b4069c5c7645c (patch)
tree3f9dc84ac00fb692f80726c3539084185a9f88fa /src/raw_guard.rs
parent3a92d03b3c47926b0eeaac8f301833b23d68a6ec (diff)
downloadtextmode-dd845e949ac59e08bf12d0fcac8b4069c5c7645c.tar.gz
textmode-dd845e949ac59e08bf12d0fcac8b4069c5c7645c.zip
add async implementation of Input
this is just copied and pasted for now, need to figure out how to generate one from the other
Diffstat (limited to 'src/raw_guard.rs')
-rw-r--r--src/raw_guard.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/raw_guard.rs b/src/raw_guard.rs
new file mode 100644
index 0000000..909ba9e
--- /dev/null
+++ b/src/raw_guard.rs
@@ -0,0 +1,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();
+ }
+}