termion/src/size.rs

30 lines
664 B
Rust
Raw Normal View History

2016-03-07 15:01:20 +00:00
use libc::ioctl;
use libc::{c_ushort, STDOUT_FILENO};
use std::mem;
2016-03-07 21:19:35 +00:00
use termios::tiocgwinsz;
use TerminalError;
2016-03-07 15:01:20 +00:00
#[repr(C)]
struct TermSize {
row: c_ushort,
col: c_ushort,
_x: c_ushort,
_y: c_ushort,
}
/// Get the size of the terminal. If the program isn't running in a terminal, it will return
/// `None`.
2016-03-07 21:19:35 +00:00
pub fn termsize() -> Result<(usize, usize), TerminalError> {
2016-03-07 15:01:20 +00:00
unsafe {
let mut size: TermSize = mem::zeroed();
if ioctl(STDOUT_FILENO, tiocgwinsz as u64, &mut size as *mut _) == 0 {
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
}
}
}