1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::ops::{Deref, DerefMut};

use crate::grid::{
    ansi::ANSIBuf,
    config::{Entity, EntityMap, SpannedConfig},
};

/// A spanned configuration plus colors for cells.
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct ColoredConfig {
    config: SpannedConfig,
    colors: ColorMap,
}

impl ColoredConfig {
    /// Create a new colored config.
    pub fn new(config: SpannedConfig) -> Self {
        Self {
            config,
            colors: ColorMap::default(),
        }
    }

    /// Set a color for a given cell.
    ///
    /// The outcome is the same as if you'd use [`Format`] and added a color but it'd work only with `color` feature on.
    /// While this method works in all contexts.
    ///
    /// [`Format`]: crate::settings::Format
    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
    }

    /// Set a list of colors.
    pub fn set_colors(&mut self, colors: EntityMap<ANSIBuf>) -> &mut Self {
        self.colors = ColorMap(Some(colors));
        self
    }

    /// Remove a color for a given cell.
    pub fn remove_color(&mut self, pos: Entity) -> &mut Self {
        if let Some(colors) = self.colors.0.as_mut() {
            colors.remove(pos);
        }

        self
    }

    /// Returns a list of colors.
    pub fn get_colors(&self) -> &ColorMap {
        &self.colors
    }

    /// Returns an inner config.
    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
    }
}

/// A colors structure for [`ColoredConfig`].
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct ColorMap(Option<EntityMap<ANSIBuf>>);

impl ColorMap {
    /// Checks if any colors is set on.
    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)
    }
}