diff options
Diffstat (limited to 'src/term.rs')
| -rw-r--r-- | src/term.rs | 65 |
1 files changed, 54 insertions, 11 deletions
diff --git a/src/term.rs b/src/term.rs index f8246fd..149f00f 100644 --- a/src/term.rs +++ b/src/term.rs @@ -1,18 +1,61 @@ -use libc::{c_int, winsize}; +use libc::{termios, winsize}; pub fn size() -> Option<winsize> { - let mut window: winsize = winsize { - ws_row: 0, - ws_col: 0, - ws_xpixel: 0, - ws_ypixel: 0, - }; - let result: c_int; unsafe { - result = libc::ioctl(1, libc::TIOCGWINSZ, &mut window); + let mut window: winsize = std::mem::zeroed(); + if libc::ioctl(1, libc::TIOCGWINSZ, &mut window) < 0 { + return None; + } + return Some(window); + } +} + +fn get_attr() -> Option<termios> { + unsafe { + let mut attr: termios = std::mem::zeroed(); + if libc::tcgetattr(1, &mut attr) < 0 { + return None; + } + return Some(attr); } - if result < 0 { +} + +fn set_attr(attrs: &termios) -> Option<()> { + unsafe { + if libc::tcsetattr(1, libc::TCSANOW, attrs) < 0 { + return None; + } + return Some(()); + } +} + +fn make_raw() -> termios { + unsafe { + let mut attr: termios = std::mem::zeroed(); + libc::cfmakeraw(&mut attr); + return attr; + } +} + +pub struct RawHandle { + old: termios, +} + +impl Drop for RawHandle { + fn drop(&mut self) { + set_attr(&self.old); + } +} + +pub fn raw() -> Option<RawHandle> { + let old = match get_attr() { + Some(old) => old, + None => { + return None; + } + }; + if let None = set_attr(&make_raw()) { return None; } - return Some(window); + return Some(RawHandle { old: old }); } |
