aboutsummaryrefslogtreecommitdiffstats
path: root/examples/raw_guard/mod.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-03-03 01:26:18 -0500
committerJesse Luehrs <doy@tozt.net>2021-03-03 01:26:18 -0500
commitc78084caca2563c52e563901a94e4d094e88cd1c (patch)
tree7a7e5ebb51857005307be5d7069fe6bb9421064c /examples/raw_guard/mod.rs
parentf7531c298e90252507c6224b78219a560c8a629f (diff)
downloadpty-process-c78084caca2563c52e563901a94e4d094e88cd1c.tar.gz
pty-process-c78084caca2563c52e563901a94e4d094e88cd1c.zip
refactor examples
Diffstat (limited to 'examples/raw_guard/mod.rs')
-rw-r--r--examples/raw_guard/mod.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/examples/raw_guard/mod.rs b/examples/raw_guard/mod.rs
new file mode 100644
index 0000000..2930a08
--- /dev/null
+++ b/examples/raw_guard/mod.rs
@@ -0,0 +1,32 @@
+use std::os::unix::io::AsRawFd as _;
+
+pub struct RawGuard {
+ termios: nix::sys::termios::Termios,
+}
+
+impl RawGuard {
+ 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 }
+ }
+}
+
+impl Drop for RawGuard {
+ fn drop(&mut self) {
+ let stdin = std::io::stdin().as_raw_fd();
+ let _ = nix::sys::termios::tcsetattr(
+ stdin,
+ nix::sys::termios::SetArg::TCSANOW,
+ &self.termios,
+ );
+ }
+}