diff --git a/Cargo.toml b/Cargo.toml index 0be9051..3d1d394 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,8 @@ exclude = ["target", "CHANGELOG.md", "image.png", "Cargo.lock"] [target.'cfg(not(target_os = "redox"))'.dependencies] libc = "0.2.8" + + +[target.'cfg(target_os = "redox")'.dependencies] +redox_syscall = "0.1" +redox_termios = "0.1" diff --git a/src/input.rs b/src/input.rs index a9e874d..6f6dd17 100644 --- a/src/input.rs +++ b/src/input.rs @@ -27,15 +27,28 @@ impl Iterator for Keys { } /// An iterator over input events. -pub struct Events { - source: R, - leftover: Option, +pub struct Events { + inner: EventsAndRaw } impl Iterator for Events { type Item = Result; fn next(&mut self) -> Option> { + self.inner.next().map(|tuple| tuple.map(|(event, _raw)| event)) + } +} + +/// An iterator over input events and the bytes that define them. +pub struct EventsAndRaw { + source: R, + leftover: Option, +} + +impl Iterator for EventsAndRaw { + type Item = Result<(Event, Vec), io::Error>; + + fn next(&mut self) -> Option), io::Error>> { let mut source = &mut self.source; if let Some(c) = self.leftover { @@ -53,7 +66,7 @@ impl Iterator for Events { Ok(0) => return None, Ok(1) => { match buf[0] { - b'\x1B' => Ok(Event::Key(Key::Esc)), + b'\x1B' => Ok((Event::Key(Key::Esc), vec![b'\x1B'])), c => parse_event(c, &mut source.bytes()), } } @@ -75,7 +88,7 @@ impl Iterator for Events { } } -fn parse_event(item: u8, iter: &mut I) -> Result +fn parse_event(item: u8, iter: &mut I) -> Result<(Event, Vec), io::Error> where I: Iterator> { let mut buf = vec![item]; @@ -85,7 +98,7 @@ fn parse_event(item: u8, iter: &mut I) -> Result }); event::parse_event(item, &mut iter) }; - result.or(Ok(Event::Unsupported(buf))) + result.or(Ok(Event::Unsupported(buf.clone()))).map(|e| (e, buf)) } @@ -113,11 +126,11 @@ pub trait TermRead { } } -impl TermRead for R { + +impl TermRead for R { fn events(self) -> Events { Events { - source: self, - leftover: None, + inner: self.events_and_raw() } } fn keys(self) -> Keys { @@ -145,6 +158,21 @@ impl TermRead for R { } } +/// Extension to `TermRead` trait. A separate trait in order to maintain backwards compatibility. +pub trait TermReadEventsAndRaw { + /// An iterator over input events and the bytes that define them. + fn events_and_raw(self) -> EventsAndRaw where Self: Sized; +} + +impl TermReadEventsAndRaw for R { + fn events_and_raw(self) -> EventsAndRaw { + EventsAndRaw { + source: self, + leftover: None, + } + } +} + /// A sequence of escape codes to enable terminal mouse support. const ENTER_MOUSE_SEQUENCE: &'static str = csi!("?1000h\x1b[?1002h\x1b[?1015h\x1b[?1006h"); @@ -241,6 +269,38 @@ mod test { assert!(i.next().is_none()); } + #[test] + fn test_events_and_raw() { + let input = b"\x1B[\x00bc\x7F\x1B[D\ + \x1B[M\x00\x22\x24\x1B[<0;2;4;M\x1B[32;2;4M\x1B[<0;2;4;m\x1B[35;2;4Mb"; + let mut output = Vec::::new(); + { + let mut i = input.events_and_raw().map(|res| res.unwrap()) + .inspect(|&(_, ref raw)| { output.extend(raw); }).map(|(event, _)| event); + + assert_eq!(i.next().unwrap(), + Event::Unsupported(vec![0x1B, b'[', 0x00])); + assert_eq!(i.next().unwrap(), Event::Key(Key::Char('b'))); + assert_eq!(i.next().unwrap(), Event::Key(Key::Char('c'))); + assert_eq!(i.next().unwrap(), Event::Key(Key::Backspace)); + assert_eq!(i.next().unwrap(), Event::Key(Key::Left)); + assert_eq!(i.next().unwrap(), + Event::Mouse(MouseEvent::Press(MouseButton::WheelUp, 2, 4))); + assert_eq!(i.next().unwrap(), + Event::Mouse(MouseEvent::Press(MouseButton::Left, 2, 4))); + assert_eq!(i.next().unwrap(), + Event::Mouse(MouseEvent::Press(MouseButton::Left, 2, 4))); + assert_eq!(i.next().unwrap(), + Event::Mouse(MouseEvent::Release(2, 4))); + assert_eq!(i.next().unwrap(), + Event::Mouse(MouseEvent::Release(2, 4))); + assert_eq!(i.next().unwrap(), Event::Key(Key::Char('b'))); + assert!(i.next().is_none()); + } + + assert_eq!(input.iter().map(|b| *b).collect::>(), output) + } + #[test] fn test_function_keys() { let mut st = b"\x1BOP\x1BOQ\x1BOR\x1BOS".keys(); diff --git a/src/lib.rs b/src/lib.rs index 7e286bb..8dd4d2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,12 @@ extern crate libc; #[cfg(not(target_os = "redox"))] mod termios; +#[cfg(target_os = "redox")] +extern crate redox_termios; + +#[cfg(target_os = "redox")] +extern crate syscall; + mod async; pub use async::{AsyncReader, async_stdin}; diff --git a/src/size.rs b/src/size.rs index 19f377b..3317ce5 100644 --- a/src/size.rs +++ b/src/size.rs @@ -57,16 +57,21 @@ pub fn terminal_size() -> io::Result<(u16, u16)> { /// Get the size of the terminal. #[cfg(target_os = "redox")] pub fn terminal_size() -> io::Result<(u16, u16)> { - use std::env; + use redox_termios; + use syscall; - let width = try!(env::var("COLUMNS").map_err(|x| io::Error::new(io::ErrorKind::NotFound, x))) - .parse() - .unwrap_or(0); - let height = try!(env::var("LINES").map_err(|x| io::Error::new(io::ErrorKind::NotFound, x))) - .parse() - .unwrap_or(0); + if let Ok(fd) = syscall::dup(1, b"winsize") { + let mut winsize = redox_termios::Winsize::default(); + let res = syscall::read(fd, &mut winsize); + let _ = syscall::close(fd); + if let Ok(count) = res { + if count == winsize.len() { + return Ok((winsize.ws_col, winsize.ws_row)); + } + } + } - Ok((width, height)) + Err(io::Error::new(io::ErrorKind::Other, "Unable to get the terminal size.")) } #[cfg(test)] diff --git a/src/tty.rs b/src/tty.rs index 9788e54..90a97e0 100644 --- a/src/tty.rs +++ b/src/tty.rs @@ -11,8 +11,15 @@ pub fn is_tty(stream: &T) -> bool { /// This will panic. #[cfg(target_os = "redox")] -pub fn is_tty(_stream: &T) -> bool { - unimplemented!(); +pub fn is_tty(stream: &T) -> bool { + use syscall; + + if let Ok(fd) = syscall::dup(stream.as_raw_fd(), b"termios") { + let _ = syscall::close(fd); + true + } else { + false + } } /// Get the TTY device.