use ::crossterm::style::{Attribute, Color};
use crate::style::CellAlignment;
#[derive(Clone)]
pub struct Cell {
pub(crate) content: Vec<String>,
pub(crate) alignment: Option<CellAlignment>,
pub(crate) fg: Option<Color>,
pub(crate) bg: Option<Color>,
pub(crate) attributes: Vec<Attribute>,
}
impl Cell {
pub fn new<T: ToString>(content: T) -> Self {
Cell {
content: content
.to_string()
.split('\n')
.map(|content| content.to_string())
.collect(),
alignment: None,
fg: None,
bg: None,
attributes: Vec::new(),
}
}
pub fn get_content(&self) -> String {
return self.content.join("\n").clone();
}
pub fn set_alignment(mut self, alignment: CellAlignment) -> Self {
self.alignment = Some(alignment);
self
}
pub fn fg(mut self, color: Color) -> Self {
self.fg = Some(color);
self
}
pub fn bg(mut self, color: Color) -> Self {
self.bg = Some(color);
self
}
pub fn add_attribute(mut self, attribute: Attribute) -> Self {
self.attributes.push(attribute);
self
}
pub fn add_attributes(mut self, mut attribute: Vec<Attribute>) -> Self {
self.attributes.append(&mut attribute);
self
}
}
pub trait ToCells {
fn to_cells(self) -> Vec<Cell>;
}
impl<T: IntoIterator> ToCells for T
where
T::Item: ToCell,
{
fn to_cells(self) -> Vec<Cell> {
self.into_iter().map(|item| item.to_cell()).collect()
}
}
pub trait ToCell {
fn to_cell(self) -> Cell;
}
impl<T: ToString> ToCell for T {
fn to_cell(self) -> Cell {
Cell::new(self.to_string())
}
}
impl ToCell for Cell {
fn to_cell(self) -> Cell {
self
}
}