use super::{ColChar, Vec2D, View};
use crate::utils;
#[non_exhaustive]
pub struct ScaleFitView {
pub view: View,
pub empty_row_count: isize,
}
impl Default for ScaleFitView {
fn default() -> Self {
Self::new(ColChar::EMPTY)
}
}
impl ScaleFitView {
#[must_use]
pub fn new(background_char: ColChar) -> Self {
let mut tmp = Self {
view: View::new(0, 0, background_char),
empty_row_count: 1,
};
tmp.update();
tmp
}
#[must_use]
pub const fn with_empty_row_count(mut self, empty_row_count: isize) -> Self {
self.empty_row_count = empty_row_count;
self
}
#[must_use]
pub fn intended_size(&self) -> Vec2D {
let mut term_size = utils::get_terminal_size_as_vec2d().expect("Failed to get terminal size");
term_size.y -= self.empty_row_count + 1;
assert_ne!(term_size.x, 0, "Terminal width detected to be 0");
assert_ne!(term_size.y, 0, "Terminal height detected to be 0");
term_size
}
pub fn update(&mut self) {
let term_size = self.intended_size();
self.view.width = term_size.x as usize;
self.view.height = term_size.y as usize;
self.view.clear();
}
}