use crate::{core::Canvas, view::term_utils};
use super::{ColChar, Vec2D, View};
#[non_exhaustive]
pub struct ScaleFitView {
pub view: View,
pub empty_row_count: i64,
}
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: i64) -> Self {
self.empty_row_count = empty_row_count;
self
}
#[must_use]
pub fn intended_size(&self) -> Vec2D {
let mut term_size =
term_utils::get_terminal_size_as_vec2d().expect("Failed to get terminal size");
term_size.y -= self.empty_row_count + 1;
term_size.max(Vec2D::ZERO)
}
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();
}
}
impl Canvas for ScaleFitView {
fn plot(&mut self, pos: Vec2D, c: ColChar) {
self.view.plot(pos, c);
}
}