#![allow(dead_code)]
use crate::{options::Options, row::Row, HAlign, VAlign};
pub struct GridBuilder {
inner: Grid,
}
pub struct Grid {
pub default_options: Options,
pub column_width: Option<usize>,
pub padding_size: Option<usize>,
pub rows: Vec<Row>,
}
impl Grid {
pub fn new(rows: Vec<Row>) -> Self {
let default_options = Options {
col_span: None,
h_align: None,
v_align: None,
blank_char: None,
};
let grid = Self {
default_options,
column_width: None,
padding_size: None,
rows,
};
grid
}
pub fn builder(rows: Vec<Row>) -> GridBuilder {
GridBuilder {
inner: Grid::new(rows),
}
}
fn render(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for row in &self.rows {
row.render(
f,
&self.default_options,
self.column_width,
self.padding_size,
)?;
}
Ok(())
}
}
impl std::fmt::Display for Grid {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.render(f)
}
}
impl GridBuilder {
pub fn build(self) -> Grid {
self.inner
}
pub fn default_colspan(mut self, default_colspan: usize) -> Self {
if default_colspan == 0 {
panic!("Column span cannot be 0!");
}
self.inner.default_options.col_span = Some(default_colspan);
self
}
pub fn default_h_align(mut self, default_h_align: HAlign) -> Self {
self.inner.default_options.h_align = Some(default_h_align);
self
}
pub fn default_v_align(mut self, default_v_align: VAlign) -> Self {
self.inner.default_options.v_align = Some(default_v_align);
self
}
pub fn default_blank_char(mut self, default_blank_char: char) -> Self {
self.inner.default_options.blank_char = Some(default_blank_char);
self
}
pub fn column_width(mut self, column_width: usize) -> Self {
self.inner.column_width = Some(column_width);
self
}
pub fn padding_size(mut self, padding_size: usize) -> Self {
self.inner.padding_size = Some(padding_size);
self
}
}