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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use crate::style::{Color, Colored};

/// Represents, optionally, a foreground and/or a background color.
///
/// It can be applied using the `SetColors` command.
///
/// It can also be created from a [Colored](enum.Colored.html) value or a tuple of
/// `(Color, Color)` in the order `(foreground, background)`.
///
/// The [then](#method.then) method can be used to combine `Colors` values.
///
/// For example:
/// ```no_run
/// use crossterm::style::{Color, Colors, Colored};
///
/// // An example color, loaded from a config, file in ANSI format.
/// let config_color = "38;2;23;147;209";
///
/// // Default to green text on a black background.
/// let default_colors = Colors::new(Color::Green, Color::Black);
/// // Load a colored value from a config and override the default colors
/// let colors = match Colored::parse_ansi(config_color) {
///     Some(colored) => default_colors.then(&colored.into()),
///     None => default_colors,
/// };
/// ```
///
/// See [Color](enum.Color.html).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Colors {
    pub foreground: Option<Color>,
    pub background: Option<Color>,
}

impl Colors {
    /// Returns a new `Color` which, when applied, has the same effect as applying `self` and *then*
    /// `other`.
    pub fn then(&self, other: &Colors) -> Colors {
        Colors {
            foreground: other.foreground.or(self.foreground),
            background: other.background.or(self.background),
        }
    }
}

impl Colors {
    pub fn new(foreground: Color, background: Color) -> Colors {
        Colors {
            foreground: Some(foreground),
            background: Some(background),
        }
    }
}

impl From<Colored> for Colors {
    fn from(colored: Colored) -> Colors {
        match colored {
            Colored::ForegroundColor(color) => Colors {
                foreground: Some(color),
                background: None,
            },
            Colored::BackgroundColor(color) => Colors {
                foreground: None,
                background: Some(color),
            },
            Colored::UnderlineColor(color) => Colors {
                foreground: None,
                background: Some(color),
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::style::{Color, Colors};

    #[test]
    fn test_colors_then() {
        use Color::*;

        assert_eq!(
            Colors {
                foreground: None,
                background: None,
            }
            .then(&Colors {
                foreground: None,
                background: None,
            }),
            Colors {
                foreground: None,
                background: None,
            }
        );

        assert_eq!(
            Colors {
                foreground: None,
                background: None,
            }
            .then(&Colors {
                foreground: Some(Black),
                background: None,
            }),
            Colors {
                foreground: Some(Black),
                background: None,
            }
        );

        assert_eq!(
            Colors {
                foreground: None,
                background: None,
            }
            .then(&Colors {
                foreground: None,
                background: Some(Grey),
            }),
            Colors {
                foreground: None,
                background: Some(Grey),
            }
        );

        assert_eq!(
            Colors {
                foreground: None,
                background: None,
            }
            .then(&Colors::new(White, Grey)),
            Colors::new(White, Grey),
        );

        assert_eq!(
            Colors {
                foreground: None,
                background: Some(Blue),
            }
            .then(&Colors::new(White, Grey)),
            Colors::new(White, Grey),
        );

        assert_eq!(
            Colors {
                foreground: Some(Blue),
                background: None,
            }
            .then(&Colors::new(White, Grey)),
            Colors::new(White, Grey),
        );

        assert_eq!(
            Colors::new(Blue, Green).then(&Colors::new(White, Grey)),
            Colors::new(White, Grey),
        );

        assert_eq!(
            Colors {
                foreground: Some(Blue),
                background: Some(Green),
            }
            .then(&Colors {
                foreground: None,
                background: Some(Grey),
            }),
            Colors {
                foreground: Some(Blue),
                background: Some(Grey),
            }
        );

        assert_eq!(
            Colors {
                foreground: Some(Blue),
                background: Some(Green),
            }
            .then(&Colors {
                foreground: Some(White),
                background: None,
            }),
            Colors {
                foreground: Some(White),
                background: Some(Green),
            }
        );

        assert_eq!(
            Colors {
                foreground: Some(Blue),
                background: Some(Green),
            }
            .then(&Colors {
                foreground: None,
                background: None,
            }),
            Colors {
                foreground: Some(Blue),
                background: Some(Green),
            }
        );

        assert_eq!(
            Colors {
                foreground: None,
                background: Some(Green),
            }
            .then(&Colors {
                foreground: None,
                background: None,
            }),
            Colors {
                foreground: None,
                background: Some(Green),
            }
        );

        assert_eq!(
            Colors {
                foreground: Some(Blue),
                background: None,
            }
            .then(&Colors {
                foreground: None,
                background: None,
            }),
            Colors {
                foreground: Some(Blue),
                background: None,
            }
        );
    }
}