termion/src/size.rs

77 lines
1.8 KiB
Rust
Raw Normal View History

2016-03-15 19:32:25 +00:00
use std::io;
2016-03-13 10:55:24 +00:00
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-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 10:27:53 +00:00
// Since attributes on non-item statements is not stable yet, we use a function.
2016-03-16 18:59:12 +00:00
#[cfg(not(target_os = "redox"))]
2016-03-08 10:27:53 +00:00
#[cfg(target_pointer_width = "64")]
fn tiocgwinsz() -> u64 {
use termios::TIOCGWINSZ;
TIOCGWINSZ as u64
}
2016-03-16 18:59:12 +00:00
#[cfg(not(target_os = "redox"))]
2016-03-08 10:27:53 +00:00
#[cfg(target_pointer_width = "32")]
fn tiocgwinsz() -> u32 {
use termios::TIOCGWINSZ;
TIOCGWINSZ as u32
}
2016-03-08 09:31:12 +00:00
/// Get the size of the terminal.
#[cfg(not(target_os = "redox"))]
2016-03-15 19:32:25 +00:00
pub fn terminal_size() -> io::Result<(usize, usize)> {
2016-03-08 09:31:12 +00:00
use libc::ioctl;
use libc::STDOUT_FILENO;
use std::mem;
2016-03-07 15:01:20 +00:00
unsafe {
let mut size: TermSize = mem::zeroed();
2016-03-08 10:27:53 +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-15 19:32:25 +00:00
Err(io::Error::new(io::ErrorKind::Other, "Unable to get the terminal size."))
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")]
2016-03-15 19:32:25 +00:00
pub fn terminal_size() -> io::Result<(usize, usize)> {
2016-03-16 18:59:12 +00:00
/*
2016-03-15 19:32:25 +00:00
fn get_int(s: &'static str) -> io::Result<usize> {
use std::env;
2016-03-08 09:31:12 +00:00
2016-03-15 19:32:25 +00:00
env::var(s).map_err(|e| match e {
env::VarError::NotPresent => io::Error::new(io::ErrorKind::NotFound, e),
env::VarError::NotUnicode(u) => io::Error::new(io::ErrorKind::InvalidData, u),
2016-03-13 10:55:24 +00:00
}).and_then(|x| {
2016-03-15 19:32:25 +00:00
x.parse().map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
2016-03-13 10:55:24 +00:00
})
}
2016-03-08 09:31:12 +00:00
2016-03-13 10:55:24 +00:00
Ok((try!(get_int("COLUMNS")), try!(get_int("LINES"))))
2016-03-16 18:59:12 +00:00
*/
Ok((128,48))
2016-03-08 09:31:12 +00:00
}
2016-03-09 18:17:00 +00:00
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_size() {
assert!(terminal_size().is_ok());
}
}