#[cfg(unix)]
pub fn cell_size_in_pixels() -> std::io::Result<(u32, u32)> {
use {
libc::{
c_ushort,
ioctl,
STDOUT_FILENO,
TIOCGWINSZ,
},
std::io,
};
#[repr(C)]
struct winsize {
ws_row: c_ushort,
ws_col: c_ushort,
ws_xpixel: c_ushort,
ws_ypixel: c_ushort,
}
let w = winsize {
ws_row: 0,
ws_col: 0,
ws_xpixel: 0,
ws_ypixel: 0,
};
#[allow(clippy::useless_conversion)]
let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ.into(), &w) };
if r == 0 && w.ws_xpixel > w.ws_col && w.ws_ypixel > w.ws_row {
Ok((
(w.ws_xpixel / w.ws_col) as u32,
(w.ws_ypixel / w.ws_row) as u32,
))
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"failed to fetch terminal dimension with ioctl",
))
}
}
#[cfg(not(unix))]
pub fn cell_size_in_pixels() -> std::io::Result<(u32, u32)> {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"fetching cell size isn't supported on Windows",
))
}