use super::style::{Color, Modifier, Style};
use compact_str::CompactString;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cell {
pub symbol: CompactString,
pub fg: Color,
pub bg: Color,
pub underline_color: Color,
pub modifier: Modifier,
}
impl Default for Cell {
fn default() -> Self {
Self {
symbol: CompactString::const_new(" "),
fg: Color::Reset,
bg: Color::Reset,
underline_color: Color::Reset,
modifier: Modifier::NONE,
}
}
}
impl Cell {
pub const EMPTY_SYMBOL: &'static str = " ";
pub fn set_symbol(&mut self, symbol: &str) -> &mut Self {
self.symbol = CompactString::from(symbol);
self
}
pub fn set_char(&mut self, ch: char) -> &mut Self {
let mut buf = [0u8; 4];
self.symbol = CompactString::from(ch.encode_utf8(&mut buf) as &str);
self
}
pub fn set_style(&mut self, style: Style) -> &mut Self {
if let Some(fg) = style.fg {
self.fg = fg;
}
if let Some(bg) = style.bg {
self.bg = bg;
}
if let Some(uc) = style.underline_color {
self.underline_color = uc;
}
self.modifier = self
.modifier
.difference(style.sub_modifier)
.union(style.add_modifier);
self
}
pub fn style(&self) -> Style {
Style {
fg: Some(self.fg),
bg: Some(self.bg),
underline_color: Some(self.underline_color),
add_modifier: self.modifier,
sub_modifier: Modifier::NONE,
}
}
pub fn reset(&mut self) {
self.symbol = CompactString::const_new(" ");
self.fg = Color::Reset;
self.bg = Color::Reset;
self.underline_color = Color::Reset;
self.modifier = Modifier::NONE;
}
pub fn is_empty(&self) -> bool {
self.symbol == " "
&& self.fg == Color::Reset
&& self.bg == Color::Reset
&& self.modifier.is_empty()
}
}