diff --git a/src/error.rs b/src/error.rs index 627b20d..6c07980 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,6 +14,8 @@ pub enum TerminalError { StdoutError, /// Failed to read from stdin. StdinError, + /// Failed to parse number. + ParseError, } impl TerminalError { @@ -24,6 +26,7 @@ impl TerminalError { TerminalError::TermSizeError => "Failed to get terminal size.", TerminalError::StdoutError => "Failed to write to stdout.", TerminalError::StdinError => "Failed to read from stdin.", + TerminalError::ParseError => "Failed to parse number.", } } } diff --git a/src/lib.rs b/src/lib.rs index 22044aa..c29790d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,10 +18,7 @@ pub use error::TerminalError; mod raw; pub use raw::{IntoRawMode, TerminalRestorer}; -// TODO Redox terminal size -#[cfg(not(target_os = "redox"))] mod size; -#[cfg(not(target_os = "redox"))] pub use size::terminal_size; mod color; diff --git a/src/size.rs b/src/size.rs index 9a5047a..695a241 100644 --- a/src/size.rs +++ b/src/size.rs @@ -1,11 +1,9 @@ -use libc::ioctl; -use libc::{c_ushort, STDOUT_FILENO}; +#[cfg(not(target_os = "redox"))] +use libc::c_ushort; -use std::mem; - -use termios::TIOCGWINSZ; use TerminalError; +#[cfg(not(target_os = "redox"))] #[repr(C)] struct TermSize { row: c_ushort, @@ -14,9 +12,14 @@ struct TermSize { _y: c_ushort, } -/// Get the size of the terminal. If the program isn't running in a terminal, it will return -/// `None`. +/// Get the size of the terminal. +#[cfg(not(target_os = "redox"))] pub fn terminal_size() -> Result<(usize, usize), TerminalError> { + use libc::ioctl; + use libc::STDOUT_FILENO; + use termios::TIOCGWINSZ; + + use std::mem; unsafe { let mut size: TermSize = mem::zeroed(); @@ -27,3 +30,18 @@ pub fn terminal_size() -> Result<(usize, usize), TerminalError> { } } } + +/// Get the size of the terminal. +#[cfg(target_os = "redox")] +pub fn terminal_size() -> Result<(usize, usize), TerminalError> { + use std::env::var; + + let w = var("COLUMNS").map_err(|_| TerminalError::TermSizeError).and_then(|x| { + x.parse().map_err(|_| TerminalError::ParseError) + }); + let h = var("LINES").map_err(|_| TerminalError::TermSizeError).and_then(|x| { + x.parse().map_err(|_| TerminalError::ParseError) + }); + + Ok((try!(w), try!(h))) +}