use crossterm::style::Color;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct EdgeSizes {
pub top: usize,
pub right: usize,
pub bottom: usize,
pub left: usize,
}
impl EdgeSizes {
pub const fn new(top: usize, right: usize, bottom: usize, left: usize) -> Self {
Self {
top,
right,
bottom,
left,
}
}
pub const fn horizontal(&self) -> usize {
self.left.saturating_add(self.right)
}
pub const fn vertical(&self) -> usize {
self.top.saturating_add(self.bottom)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct MarginSizes {
pub top: usize,
pub right: isize,
pub bottom: usize,
pub left: isize,
}
impl MarginSizes {
pub const fn new(top: usize, right: isize, bottom: usize, left: isize) -> Self {
Self {
top,
right,
bottom,
left,
}
}
pub const fn horizontal(&self) -> isize {
self.left.saturating_add(self.right)
}
pub const fn vertical(&self) -> usize {
self.top.saturating_add(self.bottom)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct EdgeAuto {
pub top: bool,
pub right: bool,
pub bottom: bool,
pub left: bool,
}
impl EdgeAuto {
pub const fn new(top: bool, right: bool, bottom: bool, left: bool) -> Self {
Self {
top,
right,
bottom,
left,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BorderStyle {
#[default]
None,
Solid,
}
impl BorderStyle {
pub fn parse(s: &str) -> Self {
match s.trim().to_ascii_lowercase().as_str() {
"none" | "hidden" => BorderStyle::None,
_ => BorderStyle::Solid,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Border {
pub width: usize,
pub style: BorderStyle,
pub color: Option<Color>,
}
impl Border {
pub const fn new(width: usize, style: BorderStyle, color: Option<Color>) -> Self {
Self {
width,
style,
color,
}
}
pub const fn thickness(&self) -> usize {
match self.style {
BorderStyle::Solid => self.width,
BorderStyle::None => 0,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct BorderSides {
pub top: Border,
pub right: Border,
pub bottom: Border,
pub left: Border,
}
impl BorderSides {
pub const fn all(b: Border) -> Self {
Self {
top: b,
right: b,
bottom: b,
left: b,
}
}
pub fn any_visible(&self) -> bool {
self.top.thickness() > 0
|| self.right.thickness() > 0
|| self.bottom.thickness() > 0
|| self.left.thickness() > 0
}
pub const fn horizontal_thickness(&self) -> usize {
self.left.thickness().saturating_add(self.right.thickness())
}
pub const fn vertical_thickness(&self) -> usize {
self.top.thickness().saturating_add(self.bottom.thickness())
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct BoxEdges {
pub padding: EdgeSizes,
pub border: BorderSides,
pub margin: MarginSizes,
}
impl BoxEdges {
pub const fn inner_horizontal(&self) -> usize {
self.padding.horizontal() + self.border.horizontal_thickness()
}
pub const fn inner_vertical(&self) -> usize {
self.padding.vertical() + self.border.vertical_thickness()
}
}