terminal_size() for Redox

This commit is contained in:
Ticki 2016-03-08 10:31:12 +01:00
parent 54ce18f17d
commit 22ef240967
3 changed files with 28 additions and 10 deletions

View File

@ -14,6 +14,8 @@ pub enum TerminalError {
StdoutError, StdoutError,
/// Failed to read from stdin. /// Failed to read from stdin.
StdinError, StdinError,
/// Failed to parse number.
ParseError,
} }
impl TerminalError { impl TerminalError {
@ -24,6 +26,7 @@ impl TerminalError {
TerminalError::TermSizeError => "Failed to get terminal size.", TerminalError::TermSizeError => "Failed to get terminal size.",
TerminalError::StdoutError => "Failed to write to stdout.", TerminalError::StdoutError => "Failed to write to stdout.",
TerminalError::StdinError => "Failed to read from stdin.", TerminalError::StdinError => "Failed to read from stdin.",
TerminalError::ParseError => "Failed to parse number.",
} }
} }
} }

View File

@ -18,10 +18,7 @@ pub use error::TerminalError;
mod raw; mod raw;
pub use raw::{IntoRawMode, TerminalRestorer}; pub use raw::{IntoRawMode, TerminalRestorer};
// TODO Redox terminal size
#[cfg(not(target_os = "redox"))]
mod size; mod size;
#[cfg(not(target_os = "redox"))]
pub use size::terminal_size; pub use size::terminal_size;
mod color; mod color;

View File

@ -1,11 +1,9 @@
use libc::ioctl; #[cfg(not(target_os = "redox"))]
use libc::{c_ushort, STDOUT_FILENO}; use libc::c_ushort;
use std::mem;
use termios::TIOCGWINSZ;
use TerminalError; use TerminalError;
#[cfg(not(target_os = "redox"))]
#[repr(C)] #[repr(C)]
struct TermSize { struct TermSize {
row: c_ushort, row: c_ushort,
@ -14,9 +12,14 @@ struct TermSize {
_y: c_ushort, _y: c_ushort,
} }
/// Get the size of the terminal. If the program isn't running in a terminal, it will return /// Get the size of the terminal.
/// `None`. #[cfg(not(target_os = "redox"))]
pub fn terminal_size() -> Result<(usize, usize), TerminalError> { pub fn terminal_size() -> Result<(usize, usize), TerminalError> {
use libc::ioctl;
use libc::STDOUT_FILENO;
use termios::TIOCGWINSZ;
use std::mem;
unsafe { unsafe {
let mut size: TermSize = mem::zeroed(); 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)))
}