use std::ops::{Deref, DerefMut};
use crate::grid::{
ansi::ANSIBuf,
config::{Entity, EntityMap, SpannedConfig},
};
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct ColoredConfig {
config: SpannedConfig,
colors: ColorMap,
}
impl ColoredConfig {
pub fn new(config: SpannedConfig) -> Self {
Self {
config,
colors: ColorMap::default(),
}
}
pub fn set_color(&mut self, pos: Entity, color: ANSIBuf) -> &mut Self {
match self.colors.0.as_mut() {
Some(map) => map.insert(pos, color),
None => {
let mut colors = EntityMap::default();
colors.insert(pos, color);
self.colors = ColorMap(Some(colors));
}
}
self
}
pub fn set_colors(&mut self, colors: EntityMap<ANSIBuf>) -> &mut Self {
self.colors = ColorMap(Some(colors));
self
}
pub fn remove_color(&mut self, pos: Entity) -> &mut Self {
if let Some(colors) = self.colors.0.as_mut() {
colors.remove(pos);
}
self
}
pub fn get_colors(&self) -> &ColorMap {
&self.colors
}
pub fn into_inner(self) -> SpannedConfig {
self.config
}
}
impl Deref for ColoredConfig {
type Target = SpannedConfig;
fn deref(&self) -> &Self::Target {
&self.config
}
}
impl DerefMut for ColoredConfig {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.config
}
}
impl From<SpannedConfig> for ColoredConfig {
fn from(value: SpannedConfig) -> Self {
Self::new(value)
}
}
impl AsRef<SpannedConfig> for ColoredConfig {
fn as_ref(&self) -> &SpannedConfig {
&self.config
}
}
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct ColorMap(Option<EntityMap<ANSIBuf>>);
impl ColorMap {
pub fn is_empty(&self) -> bool {
self.0.is_none()
}
}
impl crate::grid::colors::Colors for ColorMap {
type Color = ANSIBuf;
fn get_color(&self, (row, col): (usize, usize)) -> Option<&Self::Color> {
self.0.as_ref().map(|map| map.get(Entity::Cell(row, col)))
}
fn is_empty(&self) -> bool {
self.0
.as_ref()
.map(|cfg| cfg.is_empty() && cfg.get(Entity::Global).is_empty())
.unwrap_or(true)
}
}