use crate::core::style::Style;
use crate::core::text::Text;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Align {
#[default]
Left,
Center,
Right,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum VAlign {
#[default]
Top,
Middle,
Bottom,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Position {
#[default]
Top,
Bottom,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Edges {
pub top: u16,
pub right: u16,
pub bottom: u16,
pub left: u16,
}
impl Edges {
pub fn all(value: u16) -> Self {
Self {
top: value,
right: value,
bottom: value,
left: value,
}
}
pub fn symmetric(vertical: u16, horizontal: u16) -> Self {
Self {
top: vertical,
right: horizontal,
bottom: vertical,
left: horizontal,
}
}
pub fn horizontal(self) -> u16 {
self.left + self.right
}
pub fn vertical(self) -> u16 {
self.top + self.bottom
}
}
#[derive(Debug, Clone, Default)]
pub struct Title {
pub content: Text,
pub align: Align,
pub position: Position,
pub pad: u16,
}
impl Title {
pub fn new(content: impl Into<Text>) -> Self {
Self {
content: content.into(),
align: Align::Left,
position: Position::Top,
pad: 1,
}
}
#[must_use]
pub fn align(mut self, align: Align) -> Self {
self.align = align;
self
}
#[must_use]
pub fn position(mut self, position: Position) -> Self {
self.position = position;
self
}
#[must_use]
pub fn pad(mut self, pad: u16) -> Self {
self.pad = pad;
self
}
#[must_use]
pub fn style(mut self, style: Style) -> Self {
for line in &mut self.content.lines {
for span in &mut line.spans {
span.style = span.style.patch(style);
}
}
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn edges_all_sets_every_side() {
let edges = Edges::all(2);
assert_eq!(edges.horizontal(), 4);
assert_eq!(edges.vertical(), 4);
}
#[test]
fn edges_symmetric_splits_axes() {
let edges = Edges::symmetric(1, 3);
assert_eq!(edges.top, 1);
assert_eq!(edges.left, 3);
}
#[test]
fn title_defaults_to_top_left() {
let title = Title::new("hello");
assert_eq!(title.align, Align::Left);
assert_eq!(title.position, Position::Top);
}
}