#[derive(Debug, Copy, Clone)]
pub struct Size {
pub width: usize,
pub height: usize,
}
impl Default for Size {
fn default() -> Size {
Size {
width: 80,
height: 24,
}
}
}
impl Size {
fn new(width: usize, height: usize) -> Size {
Size { width, height }
}
pub fn from_env() -> Option<Size> {
let columns = std::env::var("COLUMNS")
.ok()
.and_then(|value| value.parse::<usize>().ok());
let rows = std::env::var("LINES")
.ok()
.and_then(|value| value.parse::<usize>().ok());
match (columns, rows) {
(Some(columns), Some(rows)) => Some(Size::new(columns, rows)),
_ => None,
}
}
pub fn detect() -> Option<Size> {
term_size::dimensions()
.map(|(w, h)| Size::new(w, h))
.or_else(Size::from_env)
}
}