From 84e0cd89a82b7a0a04071692c2ff3d56c0bc5889 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 23 Aug 2019 13:33:18 -0400 Subject: [PATCH] Proper raw mode... by properly combining bitflags --- src/helpers.rs | 60 +++++++++++++++++++------------------------------- 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index aba1f3b..4df82ec 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,15 +1,10 @@ /// Helper functions, especially to reproduce C std/posix functions use nix::errno::*; -use nix::Error as NixError; use nix::sys::termios; use nix::sys::termios::{ - ControlFlags, - InputFlags, - LocalFlags, - OutputFlags, - SpecialCharacterIndices, - Termios + ControlFlags, InputFlags, LocalFlags, OutputFlags, SpecialCharacterIndices, Termios, }; +use nix::Error as NixError; use std::io::Error; use std::os::unix::io::RawFd; @@ -20,13 +15,6 @@ pub const STDIN_FILENO: i32 = 0; // pub const STDOUT_FILENO: i32 = 1; // pub const STDERR_FILENO: i32 = 2; -/// Is this character an ASCII control character? -pub fn is_cntrl(c: char) -> bool { - let code = c as u8; - - code < 32 || code == 127 -} - pub fn die(code: &Errno, msg: &str) -> ! { eprintln!("{:?} ({})", code, msg); @@ -41,31 +29,29 @@ pub fn enable_raw_mode() -> Result<(), Error> { let mut raw = get_termios(STDIN_FILENO); raw.input_flags.remove( - InputFlags::BRKINT & - InputFlags::IGNCR & - InputFlags::IGNBRK & - InputFlags::ICRNL & - InputFlags::INLCR & - InputFlags::INPCK & - InputFlags::ISTRIP & - InputFlags::IXON & - InputFlags::PARMRK + InputFlags::BRKINT + | InputFlags::IGNCR + | InputFlags::IGNBRK + | InputFlags::ICRNL + | InputFlags::INLCR + | InputFlags::INPCK + | InputFlags::ISTRIP + | InputFlags::IXON + | InputFlags::PARMRK, ); raw.output_flags.remove(OutputFlags::OPOST); raw.local_flags.remove( - LocalFlags::ECHO & - LocalFlags::ECHONL & - LocalFlags::ICANON & - LocalFlags::IEXTEN & - LocalFlags::ISIG + LocalFlags::ECHO + | LocalFlags::ECHONL + | LocalFlags::ICANON + | LocalFlags::IEXTEN + | LocalFlags::ISIG, ); - raw.control_flags.remove( - ControlFlags::CSIZE & - ControlFlags::PARENB - ); + raw.control_flags + .remove(ControlFlags::CSIZE | ControlFlags::PARENB); // 8 bit characters raw.control_flags |= termios::ControlFlags::CS8; @@ -77,8 +63,8 @@ pub fn enable_raw_mode() -> Result<(), Error> { Ok(()) => Ok(()), Err(e) => match e.as_errno() { Some(errno) => die(&errno, "tcsetattr"), - None => panic!("Failed to enable raw mode") - } + None => panic!("Failed to enable raw mode"), + }, } } @@ -87,7 +73,7 @@ pub fn disable_raw_mode(original: &Termios) -> Result<(), Error> { Ok(()) => Ok(()), Err(E) => match E.as_errno() { Some(errno) => die(&errno, "tcsetattr"), - None => panic!("Failed to disable raw mode") - } + None => panic!("Failed to disable raw mode"), + }, } -} \ No newline at end of file +}