termion/sys/unix/
size.rs

1use std::{io, mem};
2
3use super::cvt;
4use super::libc::{c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ};
5
6#[repr(C)]
7struct TermSize {
8    row: c_ushort,
9    col: c_ushort,
10    _x: c_ushort,
11    _y: c_ushort,
12}
13/// Get the size of the terminal.
14pub fn terminal_size() -> io::Result<(u16, u16)> {
15    unsafe {
16        let mut size: TermSize = mem::zeroed();
17        cvt(ioctl(STDOUT_FILENO, TIOCGWINSZ.into(), &mut size as *mut _))?;
18        Ok((size.col as u16, size.row as u16))
19    }
20}