use compact_str::CompactString;
use crate::style::Style;
#[derive(Debug, Clone)]
pub struct Cell {
content: CompactString,
pub style: Style,
width: u8,
}
impl PartialEq for Cell {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.width == other.width && self.style == other.style && self.content == other.content
}
}
impl Eq for Cell {}
impl Default for Cell {
fn default() -> Self {
Self::BLANK
}
}
impl Cell {
pub const BLANK: Cell = Cell {
content: CompactString::const_new(" "),
style: Style::EMPTY,
width: 1,
};
pub const CONTINUATION: Cell = Cell {
content: CompactString::const_new(""),
style: Style::EMPTY,
width: 0,
};
pub fn new(content: impl Into<CompactString>, width: u8) -> Self {
Cell {
content: content.into(),
style: Style::default(),
width,
}
}
#[inline]
pub fn is_wide(&self) -> bool {
self.width > 1
}
#[inline]
pub fn is_continuation(&self) -> bool {
self.width == 0
}
pub fn is_blank(&self) -> bool {
self.width == 1 && (self.content == " " || self.content.is_empty())
}
pub(crate) fn draw_bytes(&self) -> &[u8] {
const BLANKS: [u8; u8::MAX as usize] = [b' '; u8::MAX as usize];
if self.content.is_empty() {
&BLANKS[..self.width as usize]
} else {
self.content.as_bytes()
}
}
#[inline]
pub fn content(&self) -> &str {
self.content.as_str()
}
#[inline]
pub fn width(&self) -> u8 {
self.width
}
pub fn style(mut self, style: impl Into<Style>) -> Self {
self.style = style.into();
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_blank_cell() {
let c = Cell::BLANK;
assert_eq!(c.width(), 1);
assert!(c.is_blank());
assert!(c.style.is_empty());
}
#[test]
fn test_narrow_cell() {
let c = Cell::new("A", 1);
assert_eq!(c.content(), "A");
assert!(!c.is_wide());
assert_eq!(c.width(), 1);
}
#[test]
fn test_wide_cell() {
let c = Cell::new("中", 2);
assert_eq!(c.content(), "中");
assert!(c.is_wide());
assert_eq!(c.width(), 2);
}
#[test]
fn test_continuation_cell() {
let c = Cell::CONTINUATION;
assert!(c.is_continuation());
assert_eq!(c.width(), 0);
assert!(!c.is_blank());
}
#[test]
fn only_a_cell_drawing_one_blank_column_is_blank() {
assert!(Cell::new(" ", 1).is_blank());
let empty = Cell::new("", 1);
assert_eq!(empty.draw_bytes(), b" ");
assert!(empty.is_blank());
assert!(Cell::BLANK.style(Style::default().bold()).is_blank());
assert!(!Cell::new(" ", 2).is_blank());
assert!(!Cell::new("", 2).is_blank());
assert!(!Cell::new("x", 1).is_blank());
}
#[test]
fn the_continuation_constant_matches_one_built_by_hand() {
assert_eq!(Cell::CONTINUATION, Cell::new("", 0));
}
#[test]
fn a_cell_may_own_more_than_two_columns() {
let family = "\u{1f468}\u{200d}\u{1f469}\u{200d}\u{1f467}\u{200d}\u{1f466}";
let c = Cell::new(family, 8);
assert_eq!(c.width(), 8);
assert!(c.is_wide());
assert!(!c.is_continuation());
}
#[test]
fn width_alone_decides_the_structural_role() {
assert!(Cell::new("a", 0).is_continuation());
assert!(Cell::new("", 2).is_wide());
assert!(Cell::new("\u{1f469}", 2).is_wide());
}
#[test]
fn a_cell_claiming_no_column_is_a_continuation_whatever_it_stores() {
use crate::buffer::{Buffer, Surface, SurfaceMut};
use crate::text::Encode;
let mut buf = Buffer::new(3, 1);
buf.set_cell((0, 0).into(), &Cell::new("A", 1));
buf.set_cell((1, 0).into(), &Cell::new("\u{301}", 0));
buf.set_cell((2, 0).into(), &Cell::new("B", 1));
assert!(Cell::new("\u{301}", 0).is_continuation());
assert_eq!(
buf.cell((1, 0).into()).unwrap().content(),
" ",
"a loose continuation is declined, leaving the blank"
);
assert_eq!(buf.display().to_string(), "A B");
}
#[test]
fn test_cell_with_style() {
let c = Cell::new("x", 1).style(Style::default().bold());
assert!(c.style.attrs.contains(crate::style::AttrFlags::BOLD));
}
}