use crate::{Align, BlockStyle, Canvas, GridStyle, Key, StyledText, TextStyle, VerticalAlign};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProjectionBoundary {
Preserve,
Clamp,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Projection {
origin: i64,
boundary: ProjectionBoundary,
}
impl Projection {
pub const fn new(origin: i64, boundary: ProjectionBoundary) -> Self {
Self { origin, boundary }
}
pub const fn origin(&self) -> i64 {
self.origin
}
pub const fn boundary(&self) -> ProjectionBoundary {
self.boundary
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Viewport {
horizontal: Option<Projection>,
vertical: Option<Projection>,
}
impl Viewport {
pub const fn horizontal(projection: Projection) -> Self {
Self {
horizontal: Some(projection),
vertical: None,
}
}
pub const fn vertical(projection: Projection) -> Self {
Self {
horizontal: None,
vertical: Some(projection),
}
}
pub const fn both(horizontal: Projection, vertical: Projection) -> Self {
Self {
horizontal: Some(horizontal),
vertical: Some(vertical),
}
}
pub const fn horizontal_projection(&self) -> Option<Projection> {
self.horizontal
}
pub const fn vertical_projection(&self) -> Option<Projection> {
self.vertical
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockTitle {
text: StyledText,
align: Align,
padding: u16,
}
impl BlockTitle {
pub fn new(text: impl Into<StyledText>) -> Self {
let text = text.into();
assert!(
!text.as_str().contains('\n'),
"a block title must be exactly one line"
);
Self {
text,
align: Align::Left,
padding: 1,
}
}
#[must_use]
pub const fn align(mut self, align: Align) -> Self {
self.align = align;
self
}
#[must_use]
pub const fn padding(mut self, padding: u16) -> Self {
self.padding = padding;
self
}
pub const fn text(&self) -> &StyledText {
&self.text
}
pub const fn alignment(&self) -> Align {
self.align
}
pub const fn horizontal_padding(&self) -> u16 {
self.padding
}
}
impl From<&str> for BlockTitle {
fn from(text: &str) -> Self {
Self::new(text)
}
}
impl From<String> for BlockTitle {
fn from(text: String) -> Self {
Self::new(text)
}
}
impl From<StyledText> for BlockTitle {
fn from(text: StyledText) -> Self {
Self::new(text)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum View {
Text(StyledText),
Block(BlockStyle, Option<BlockTitle>, Box<View>),
Row(VerticalAlign, Vec<View>),
Column(Align, Vec<View>),
Grid(GridStyle, Vec<Vec<View>>),
Canvas(Canvas),
Viewport(Viewport, Box<View>),
AnchorBlock(Key, BlockStyle, Option<BlockTitle>, Box<View>),
}
impl Default for View {
fn default() -> Self {
Self::empty()
}
}
impl View {
pub fn text(text: impl Into<String>, style: TextStyle) -> Self {
Self::Text(StyledText::new(text, style))
}
pub const fn styled_text(text: StyledText) -> Self {
Self::Text(text)
}
pub fn block(style: BlockStyle, child: Self) -> Self {
Self::Block(style, None, Box::new(child))
}
pub fn titled_block(style: BlockStyle, title: impl Into<BlockTitle>, child: Self) -> Self {
assert_title_edge(&style);
Self::Block(style, Some(title.into()), Box::new(child))
}
pub fn row(align: VerticalAlign, children: impl IntoIterator<Item = Self>) -> Self {
Self::Row(align, children.into_iter().collect())
}
pub fn column(align: Align, children: impl IntoIterator<Item = Self>) -> Self {
Self::Column(align, children.into_iter().collect())
}
pub fn anchor_block(key: impl Into<Key>, style: BlockStyle, child: Self) -> Self {
Self::AnchorBlock(key.into(), style, None, Box::new(child))
}
pub fn titled_anchor_block(
key: impl Into<Key>,
style: BlockStyle,
title: impl Into<BlockTitle>,
child: Self,
) -> Self {
assert_title_edge(&style);
Self::AnchorBlock(key.into(), style, Some(title.into()), Box::new(child))
}
pub fn anchor(key: impl Into<Key>) -> Self {
Self::anchor_block(key, BlockStyle::new(), Self::empty())
}
pub fn grid<R>(style: GridStyle, rows: impl IntoIterator<Item = R>) -> Self
where
R: IntoIterator<Item = Self>,
{
Self::Grid(
style,
rows.into_iter()
.map(|row| row.into_iter().collect())
.collect(),
)
}
pub const fn canvas(canvas: Canvas) -> Self {
Self::Canvas(canvas)
}
pub fn viewport(viewport: Viewport, child: impl Into<View>) -> Self {
Self::Viewport(viewport, Box::new(child.into()))
}
pub const fn empty() -> Self {
Self::Column(Align::Left, Vec::new())
}
}
fn assert_title_edge(style: &BlockStyle) {
assert!(
style.get_border().is_some() && style.get_border_top(),
"a titled block requires an enabled top border"
);
}