aboutsummaryrefslogtreecommitdiffstats
path: root/src/raw.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/raw.rs')
-rw-r--r--src/raw.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/raw.rs b/src/raw.rs
new file mode 100644
index 0000000..e116620
--- /dev/null
+++ b/src/raw.rs
@@ -0,0 +1,30 @@
+use termios::*;
+
+/// 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();
+ }
+
+ pub fn disable(&self) {
+ tcsetattr(self.fd, TCSANOW, &self.settings).unwrap();
+ }
+}