termrs_core 0.3.0

The core library of termrs
Documentation
use super::{Margin, Position};

#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
pub struct Size {
    width: u16,
    height: u16,
}

impl From<(u16, u16)> for Size {
    fn from(value: (u16, u16)) -> Self {
        Size {
            width: value.0,
            height: value.1,
        }
    }
}

impl Size {
    pub const EMPTY: Size = Size {
        width: 0,
        height: 0,
    };

    pub fn new(width: u16, height: u16) -> Self {
        Self { width, height }
    }

    pub fn width(&self) -> u16 {
        self.width
    }

    pub fn height(&self) -> u16 {
        self.height
    }

    pub fn set_width(&mut self, width: u16) {
        self.width = width;
    }

    pub fn set_height(&mut self, height: u16) {
        self.height = height;
    }

    pub fn is_empty(&self) -> bool {
        self.width == 0 && self.height == 0
    }

    pub fn contains(&self, position: Position) -> bool {
        position.column <= self.width && position.row <= self.height
    }

    pub fn exceed(&self, other: Size) -> bool {
        self.width > other.width || self.height > other.height
    }

    pub fn clip(&self, other: Size) -> Size {
        Size {
            width: self.width.min(other.width),
            height: self.height.min(other.height),
        }
    }

    pub fn sub_margin(mut self, margin: &Margin) -> Self {
        self.width = u16::max(self.width - margin.left() - margin.right(), 0);
        self.height = u16::max(self.height - margin.top() - margin.bottom(), 0);
        self
    }

    pub fn add_margin(mut self, margin: &Margin) -> Self {
        self.width = self.width + margin.left() + margin.right();
        self.height = self.height + margin.top() + margin.bottom();
        self
    }
}