use crate::{graphics::Character, prelude::CharAttribute};
pub struct AttributeText<'a> {
pub(super) chars: &'a mut [Character],
}
impl<'a> AttributeText<'a> {
#[inline(always)]
pub fn count(&self) -> usize {
self.chars.len()
}
#[inline(always)]
pub fn char(&self, index: usize) -> Option<char> {
self.chars.get(index).map(|c| c.code)
}
pub fn set_char(&mut self, index: usize, ch: char) {
if let Some(c) = self.chars.get_mut(index) {
c.code = ch;
} else {
debug_assert!(false, "AttributeText::set_color index out of range");
}
}
pub fn set_attr(&mut self, index: usize, attr: CharAttribute) {
if let Some(ch) = self.chars.get_mut(index) {
ch.foreground = attr.foreground;
ch.background = attr.background;
ch.flags = attr.flags;
} else {
debug_assert!(false, "AttributeText::set_color index out of range");
}
}
pub fn set_range_attr(&mut self, start: usize, end: usize, attr: CharAttribute) {
let len = self.chars.len();
let start = start.min(len);
let end = end.min(len);
if start >= end {
return;
}
for ch in &mut self.chars[start..end] {
ch.foreground = attr.foreground;
ch.background = attr.background;
ch.flags = attr.flags;
}
}
pub fn reset_all(&mut self, attr: CharAttribute) {
for ch in self.chars.iter_mut() {
ch.foreground = attr.foreground;
ch.background = attr.background;
ch.flags = attr.flags;
}
}
}