use std::io::{self, Write}; use std::ops::{Deref, DerefMut}; /// A terminal restorer, which keeps the previous state of the terminal, and restores it, when /// dropped. #[cfg(target_os = "redox")] pub struct RawTerminal { output: W, } #[cfg(target_os = "redox")] impl Drop for RawTerminal { fn drop(&mut self) { use control::TermWrite; let _ = self.csi(b"R"); } } #[cfg(not(target_os = "redox"))] use termios::Termios; /// A terminal restorer, which keeps the previous state of the terminal, and restores it, when /// dropped. #[cfg(not(target_os = "redox"))] pub struct RawTerminal { prev_ios: Termios, output: W, } #[cfg(not(target_os = "redox"))] impl Drop for RawTerminal { fn drop(&mut self) { use termios::set_terminal_attr; set_terminal_attr(&mut self.prev_ios as *mut _); } } impl Deref for RawTerminal { type Target = W; fn deref(&self) -> &W { &self.output } } impl DerefMut for RawTerminal { fn deref_mut(&mut self) -> &mut W { &mut self.output } } impl Write for RawTerminal { fn write(&mut self, buf: &[u8]) -> io::Result { self.output.write(buf) } fn flush(&mut self) -> io::Result<()> { self.output.flush() } } /// Types which can be converted into "raw mode". pub trait IntoRawMode: Write + Sized { /// Switch to raw mode. /// /// Raw mode means that stdin won't be printed (it will instead have to be written manually by the /// program). Furthermore, the input isn't canonicalised or buffered (that is, you can read from /// stdin one byte of a time). The output is neither modified in any way. fn into_raw_mode(self) -> io::Result>; } impl IntoRawMode for W { #[cfg(not(target_os = "redox"))] fn into_raw_mode(self) -> io::Result> { use termios::{cfmakeraw, get_terminal_attr, set_terminal_attr}; let (mut ios, exit) = get_terminal_attr(); let prev_ios = ios.clone(); if exit != 0 { return Err(io::Error::new(io::ErrorKind::Other, "Unable to get Termios attribute.")); } unsafe { cfmakeraw(&mut ios); } if set_terminal_attr(&mut ios as *mut _) != 0 { Err(io::Error::new(io::ErrorKind::Other, "Unable to set Termios attribute.")) } else { Ok(RawTerminal { prev_ios: prev_ios, output: self, }) } } #[cfg(target_os = "redox")] fn into_raw_mode(mut self) -> io::Result> { use control::TermWrite; self.csi(b"r").map(|_| RawTerminal { output: self, }) } } #[cfg(test)] mod test { use super::*; use std::io::{Write, stdout}; #[test] fn test_into_raw_mode() { let mut out = stdout().into_raw_mode().unwrap(); out.write(b"this is a test, muahhahahah").unwrap(); } }