use unicode_width::UnicodeWidthChar as _;
const CODEPOINTS_IN_CELL: usize = 6;
#[derive(Clone, Debug, Default, Eq)]
pub struct Cell {
contents: [char; CODEPOINTS_IN_CELL],
len: u8,
attrs: crate::attrs::Attrs,
}
#[allow(clippy::collapsible_if)]
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 {
#[inline]
fn len(&self) -> usize {
(self.len & 0x0f) as usize
}
pub(crate) fn set(&mut self, c: char, a: crate::attrs::Attrs) {
self.contents[0] = c;
self.len = 1;
self.set_wide(c.width().unwrap_or(0) > 1);
self.attrs = a;
}
pub(crate) fn append(&mut self, c: char) {
if self.len() >= CODEPOINTS_IN_CELL {
return;
}
if self.len() == 0 {
self.contents[self.len()] = ' ';
self.len += 1;
}
self.contents[self.len()] = c;
self.len += 1;
}
pub(crate) fn clear(&mut self, attrs: crate::attrs::Attrs) {
self.len = 0;
self.attrs = attrs;
}
#[must_use]
pub fn contents(&self) -> String {
let mut s = String::with_capacity(CODEPOINTS_IN_CELL * 4);
for c in self.contents.iter().take(self.len()) {
s.push(*c);
}
s
}
#[must_use]
pub fn has_contents(&self) -> bool {
self.len > 0
}
#[must_use]
pub fn is_wide(&self) -> bool {
self.len & 0x80 == 0x80
}
#[must_use]
pub fn is_wide_continuation(&self) -> bool {
self.len & 0x40 == 0x40
}
fn set_wide(&mut self, wide: bool) {
if wide {
self.len |= 0x80;
} else {
self.len &= 0x7f;
}
}
pub(crate) fn set_wide_continuation(&mut self, wide: bool) {
if wide {
self.len |= 0x40;
} else {
self.len &= 0xbf;
}
}
pub(crate) fn attrs(&self) -> &crate::attrs::Attrs {
&self.attrs
}
#[must_use]
pub fn fgcolor(&self) -> crate::attrs::Color {
self.attrs.fgcolor
}
#[must_use]
pub fn bgcolor(&self) -> crate::attrs::Color {
self.attrs.bgcolor
}
#[must_use]
pub fn bold(&self) -> bool {
self.attrs.bold()
}
#[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()
}
}