#[cfg(feature = "tty")]
use crate::{Attribute, Color};
use crate::style::CellAlignment;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Cell {
pub(crate) content: Vec<String>,
pub(crate) delimiter: Option<char>,
pub(crate) alignment: Option<CellAlignment>,
#[cfg(feature = "tty")]
pub(crate) fg: Option<Color>,
#[cfg(feature = "tty")]
pub(crate) bg: Option<Color>,
#[cfg(feature = "tty")]
pub(crate) attributes: Vec<Attribute>,
}
impl Cell {
#[allow(clippy::needless_pass_by_value)]
pub fn new<T: ToString>(content: T) -> Self {
let content = content.to_string();
#[cfg_attr(not(feature = "custom_styling"), allow(unused_mut))]
let mut split_content: Vec<String> = content.split('\n').map(ToString::to_string).collect();
#[cfg(feature = "custom_styling")]
crate::utils::formatting::content_split::fix_style_in_split_str(&mut split_content);
Self {
content: split_content,
delimiter: None,
alignment: None,
#[cfg(feature = "tty")]
fg: None,
#[cfg(feature = "tty")]
bg: None,
#[cfg(feature = "tty")]
attributes: Vec::new(),
}
}
pub fn content(&self) -> String {
self.content.join("\n")
}
#[must_use]
pub fn set_delimiter(mut self, delimiter: char) -> Self {
self.delimiter = Some(delimiter);
self
}
#[must_use]
pub fn set_alignment(mut self, alignment: CellAlignment) -> Self {
self.alignment = Some(alignment);
self
}
#[cfg(feature = "tty")]
#[must_use]
pub fn fg(mut self, color: Color) -> Self {
self.fg = Some(color);
self
}
#[cfg(feature = "tty")]
#[must_use]
pub fn bg(mut self, color: Color) -> Self {
self.bg = Some(color);
self
}
#[cfg(feature = "tty")]
#[must_use]
pub fn add_attribute(mut self, attribute: Attribute) -> Self {
self.attributes.push(attribute);
self
}
#[cfg(feature = "tty")]
#[must_use]
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) -> Self {
Self::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) -> Self {
Self(cells.into_iter().map(Into::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.content(), content);
}
}