use std::fmt::Display;
use compact_str::{CompactString, ToCompactString};
use crate::{
enums::{Color, Modifier},
style::Style,
};
#[derive(Debug, Clone, PartialEq)]
pub struct Cell {
pub fg: Color,
pub bg: Color,
pub modifier: Modifier,
pub val: CompactString,
}
impl Cell {
pub fn new(val: &'static str) -> Self {
Self {
val: CompactString::const_new(val),
..Default::default()
}
}
pub fn empty() -> Self {
Self::default()
}
pub fn val(&mut self, val: &str) -> &mut Self {
self.val = CompactString::new(val);
self
}
pub fn char(&mut self, c: char) -> &mut Self {
self.val = c.to_compact_string();
self
}
pub fn fg(&mut self, fg: Color) -> &mut Self {
self.fg = fg;
self
}
pub fn bg(&mut self, bg: Color) -> &mut Self {
self.bg = bg;
self
}
pub fn modifier(&mut self, flag: Modifier) -> &mut Self {
self.modifier = Modifier::empty();
self.modifier.insert(flag);
self
}
pub fn style<T>(&mut self, style: T) -> &mut Self
where
T: Into<Style>,
{
let style = style.into();
if let Some(fg) = style.fg {
self.fg = fg;
}
if let Some(bg) = style.bg {
self.bg = bg;
}
self.modifier = style.modifier;
self
}
pub fn reset(&mut self) {
self.fg = Color::Default;
self.bg = Color::Default;
self.modifier = Modifier::empty();
self.val = CompactString::const_new(" ");
}
}
impl Display for Cell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}{}{}{}",
self.modifier,
self.fg.to_fg(),
self.bg.to_bg(),
self.val
)
}
}
impl Default for Cell {
fn default() -> Self {
Self {
fg: Color::Default,
bg: Color::Default,
modifier: Modifier::empty(),
val: CompactString::const_new(" "),
}
}
}