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,
/// 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.",
}
}
}

View File

@ -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;

View File

@ -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)))
}