use unicode_width::{UnicodeWidthChar as _, UnicodeWidthStr as _};
const CONTENT_BYTES: usize = 22;
const IS_WIDE: u8 = 0b1000_0000;
const IS_WIDE_CONTINUATION: u8 = 0b0100_0000;
const LEN_BITS: u8 = 0b0001_1111;
#[derive(Clone, Debug, Eq)]
pub struct Cell {
contents: [u8; CONTENT_BYTES],
len: u8,
attrs: crate::attrs::Attrs,
}
const _: () = assert!(core::mem::size_of::<Cell>() == 32);
impl PartialEq<Self> for Cell {
fn eq(&self, other: &Self) -> bool {
if self.len != other.len {
return false;
}
if self.attrs != other.attrs {
return false;
}
let len = self.len();
self.contents[..len] == other.contents[..len]
}
}
impl Cell {
pub(crate) fn new() -> Self {
Self {
contents: Default::default(),
len: 0,
attrs: crate::attrs::Attrs::default(),
}
}
fn len(&self) -> usize {
usize::from(self.len & LEN_BITS)
}
pub(crate) fn set(&mut self, c: char, a: crate::attrs::Attrs) {
self.len = 0;
self.append_char(0, c);
self.set_wide(c.width().unwrap_or(1) > 1);
self.attrs = a;
}
pub(crate) fn append(&mut self, c: char) {
let len = self.len();
if len >= CONTENT_BYTES - 4 {
return;
}
if len == 0 {
self.contents[0] = b' ';
self.len += 1;
}
self.append_char(self.len(), c);
}
pub(crate) fn set_contents(&mut self, s: &str, a: crate::attrs::Attrs) {
let mut chars = s.chars();
let Some(c) = chars.next() else {
return;
};
self.set(c, a);
for c in chars {
self.append(c);
}
}
fn append_char(&mut self, start: usize, c: char) {
c.encode_utf8(&mut self.contents[start..]);
self.len += u8::try_from(c.len_utf8()).unwrap();
if c == '\u{FE0F}' {
self.set_wide(self.contents().width() > 1);
}
}
pub(crate) fn clear(&mut self, attrs: crate::attrs::Attrs) {
self.len = 0;
self.attrs = attrs;
}
#[allow(clippy::missing_panics_doc)]
#[must_use]
pub fn contents(&self) -> &str {
core::str::from_utf8(&self.contents[..self.len()]).unwrap()
}
#[must_use]
pub fn has_contents(&self) -> bool {
self.len() > 0
}
#[must_use]
pub fn is_wide(&self) -> bool {
self.len & IS_WIDE != 0
}
#[must_use]
pub fn is_wide_continuation(&self) -> bool {
self.len & IS_WIDE_CONTINUATION != 0
}
fn set_wide(&mut self, wide: bool) {
if wide {
self.len |= IS_WIDE;
} else {
self.len &= !IS_WIDE;
}
}
pub(crate) fn set_wide_continuation(&mut self, wide: bool) {
if wide {
self.len |= IS_WIDE_CONTINUATION;
} else {
self.len &= !IS_WIDE_CONTINUATION;
}
}
pub(crate) fn attrs(&self) -> &crate::attrs::Attrs {
&self.attrs
}
#[must_use]
pub fn fgcolor(&self) -> crate::Color {
self.attrs.fgcolor
}
#[must_use]
pub fn bgcolor(&self) -> crate::Color {
self.attrs.bgcolor
}
#[must_use]
pub fn bold(&self) -> bool {
self.attrs.bold()
}
#[must_use]
pub fn dim(&self) -> bool {
self.attrs.dim()
}
#[must_use]
pub fn italic(&self) -> bool {
self.attrs.italic()
}
#[must_use]
pub fn underline(&self) -> bool {
self.attrs.underline()
}
#[must_use]
pub fn inverse(&self) -> bool {
self.attrs.inverse()
}
}