use crossterm::style::{Attribute, Color};
use crate::style::CellAlignment;
#[derive(Clone, Debug)]
pub struct Cell {
pub(crate) content: Vec<String>,
pub(crate) delimiter: Option<char>,
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(),
delimiter: None,
alignment: None,
fg: None,
bg: None,
attributes: Vec::new(),
}
}
pub fn get_content(&self) -> String {
self.content.join("\n")
}
pub fn set_delimiter(mut self, delimiter: char) -> Self {
self.delimiter = Some(delimiter);
self
}
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
}
}
impl<T: ToString> From<T> for Cell {
fn from(content: T) -> Cell {
Cell::new(content)
}
}
pub struct Cells(pub Vec<Cell>);
impl<T> From<T> for Cells
where
T: IntoIterator,
T::Item: Into<Cell>,
{
fn from(cells: T) -> Cells {
Cells(cells.into_iter().map(|item| item.into()).collect())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_column_generation() {
let content = "This is\nsome multiline\nstring".to_string();
let cell = Cell::new(content.clone());
assert_eq!(cell.get_content(), content);
}
}