diff --git a/Cargo.toml b/Cargo.toml index dd2495a207bd63fef1ecfea9c6baded02568c509..9f5bbebd5fe1e104e29b09471ad09870d61755e3 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/lib.rs b/src/lib.rs index 7e286bb4b2fc4077a20823c7c5d5e18ce7eb1604..8dd4d2fac24b00d1157065945d06bbac9324e134 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 19f377ba803e60afdde851fdc46c341134064b83..3317ce523c466c45d35ccb712e7ee2881d632208 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 9788e54f9c90bb0cdaba9ba689c37fbb0c2892f5..90a97e05f89255141ab447a53f3c6be3b5c2f676 100644 --- a/src/tty.rs +++ b/src/tty.rs @@ -11,8 +11,15 @@ pub fn is_tty<T: AsRawFd>(stream: &T) -> bool { /// This will panic. #[cfg(target_os = "redox")] -pub fn is_tty<T: AsRawFd>(_stream: &T) -> bool { - unimplemented!(); +pub fn is_tty<T: AsRawFd>(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.