2016-03-08 09:31:12 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
|
|
|
use libc::c_ushort;
|
2016-03-07 15:01:20 +00:00
|
|
|
|
2016-03-07 21:19:35 +00:00
|
|
|
use TerminalError;
|
2016-03-07 15:01:20 +00:00
|
|
|
|
2016-03-08 09:31:12 +00:00
|
|
|
#[cfg(not(target_os = "redox"))]
|
2016-03-07 15:01:20 +00:00
|
|
|
#[repr(C)]
|
|
|
|
struct TermSize {
|
|
|
|
row: c_ushort,
|
|
|
|
col: c_ushort,
|
|
|
|
_x: c_ushort,
|
|
|
|
_y: c_ushort,
|
|
|
|
}
|
|
|
|
|
2016-03-08 09:31:12 +00:00
|
|
|
/// Get the size of the terminal.
|
|
|
|
#[cfg(not(target_os = "redox"))]
|
2016-03-08 09:08:50 +00:00
|
|
|
pub fn terminal_size() -> Result<(usize, usize), TerminalError> {
|
2016-03-08 09:31:12 +00:00
|
|
|
use libc::ioctl;
|
|
|
|
use libc::STDOUT_FILENO;
|
|
|
|
use termios::TIOCGWINSZ;
|
|
|
|
|
|
|
|
use std::mem;
|
2016-03-07 15:01:20 +00:00
|
|
|
unsafe {
|
|
|
|
let mut size: TermSize = mem::zeroed();
|
|
|
|
|
2016-03-08 09:08:50 +00:00
|
|
|
if ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut size as *mut _) == 0 {
|
2016-03-07 15:01:20 +00:00
|
|
|
Ok((size.col as usize, size.row as usize))
|
|
|
|
} else {
|
2016-03-07 21:19:35 +00:00
|
|
|
Err(TerminalError::TermSizeError)
|
2016-03-07 15:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 09:31:12 +00:00
|
|
|
|
|
|
|
/// 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)))
|
|
|
|
}
|