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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use std::iter::FromIterator;

use super::{
    Color, ColorPair, ColorStyle, ColorType, Effect, Palette, PaletteColor,
    PaletteStyle,
};
use enumset::EnumSet;

/// Combine a color and an effect.
///
/// Represents any transformation that can be applied to text.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Style {
    /// Effect to apply.
    ///
    /// `None` to keep using previous effects.
    pub effects: EnumSet<Effect>,

    /// Color style to apply.
    ///
    /// `None` to keep using the previous colors.
    pub color: ColorStyle,
}

impl Default for Style {
    fn default() -> Self {
        Self::none()
    }
}

impl Style {
    /// Returns a new `Style` that doesn't apply anything.
    ///
    /// Same as [`Style::inherit_parent()`].
    pub fn none() -> Self {
        Self::inherit_parent()
    }

    /// Returns a new `Style` by merging all given styles.
    ///
    /// Will use the last non-`None` color, and will combine all effects.
    #[must_use]
    pub fn merge(styles: &[Style]) -> Self {
        styles.iter().collect()
    }

    /// Returns a combination of `self` and `other`.
    #[must_use]
    pub fn combine<S>(self, other: S) -> Self
    where
        S: Into<Style>,
    {
        Self::merge(&[self, other.into()])
    }

    /// Uses `ColorType::InheritParent` for both front and background.
    pub fn inherit_parent() -> Self {
        ColorStyle::inherit_parent().into()
    }

    /// Style set by terminal before entering a Cursive program.
    pub fn terminal_default() -> Self {
        ColorStyle::terminal_default().into()
    }

    /// Application background, where no view is present.
    pub fn background() -> Self {
        ColorStyle::background().into()
    }

    /// Color used by view shadows. Only background matters.
    pub fn shadow() -> Self {
        ColorStyle::shadow().into()
    }

    /// Style used for views.
    pub fn view() -> Self {
        ColorStyle::view().into()
    }

    /// Main text with default background.
    pub fn primary() -> Self {
        ColorStyle::primary().into()
    }

    /// Secondary text color, with default background.
    pub fn secondary() -> Self {
        ColorStyle::secondary().into()
    }

    /// Tertiary text color, with default background.
    pub fn tertiary() -> Self {
        ColorStyle::tertiary().into()
    }

    /// Title text color with default background.
    pub fn title_primary() -> Self {
        ColorStyle::title_primary().into()
    }

    /// Alternative color for a title.
    pub fn title_secondary() -> Self {
        ColorStyle::title_secondary().into()
    }

    /// Returns a highlight style.
    pub fn highlight() -> Self {
        Style {
            color: ColorStyle::highlight().invert(),
            effects: enumset::enum_set!(Effect::Reverse),
        }
    }

    /// Returns an inactive highlight style.
    pub fn highlight_inactive() -> Self {
        Style {
            color: ColorStyle::highlight_inactive().invert(),
            effects: enumset::enum_set!(Effect::Reverse),
        }
    }

    /// Parse a toml entry
    #[cfg(feature = "toml")]
    pub(crate) fn parse(table: &toml::Value) -> Option<Self> {
        let table = table.as_table()?;
        let mut effects: EnumSet<Effect> = EnumSet::new();

        for effect in table.get("effects")?.as_array()? {
            let effect = effect.as_str()?.parse().ok()?;
            effects.insert(effect);
        }

        let color = ColorStyle::parse(table)?;

        Some(Style { effects, color })
    }
}

impl From<Effect> for Style {
    fn from(effect: Effect) -> Self {
        Style {
            effects: EnumSet::only(effect),
            color: ColorStyle::inherit_parent(),
        }
    }
}

impl From<ColorStyle> for Style {
    fn from(color: ColorStyle) -> Self {
        Style {
            effects: EnumSet::new(),
            color,
        }
    }
}

impl From<ColorPair> for Style {
    fn from(color: ColorPair) -> Self {
        ColorStyle::from(color).into()
    }
}

impl From<Color> for Style {
    fn from(color: Color) -> Self {
        ColorStyle::from(color).into()
    }
}

impl From<PaletteColor> for Style {
    fn from(color: PaletteColor) -> Self {
        ColorStyle::from(color).into()
    }
}

impl From<ColorType> for Style {
    fn from(color: ColorType) -> Self {
        ColorStyle::from(color).into()
    }
}

/// Returns an direct Style that just inherits the parent color.
impl Default for StyleType {
    fn default() -> Self {
        Style::default().into()
    }
}

/// Type of style to apply to some text.
///
/// Can be either an entry in the style palette, or a direct explicit style.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum StyleType {
    /// References a style from the palette.
    Palette(PaletteStyle),

    /// A direct style.
    Style(Style),
}

impl StyleType {
    /// Given a palette, resolve `self` to a concrete style.
    pub fn resolve(self, palette: &Palette) -> Style {
        match self {
            StyleType::Style(style) => style,
            StyleType::Palette(style) => style.resolve(palette),
        }
    }

    /// Uses `ColorType::InheritParent` for both front and background.
    pub fn inherit_parent() -> Self {
        Style::inherit_parent().into()
    }

    /// Style set by terminal before entering a Cursive program.
    pub fn terminal_default() -> Self {
        Style::terminal_default().into()
    }

    /// Application background, where no view is present.
    pub fn background() -> Self {
        PaletteStyle::Background.into()
    }

    /// Color used by view shadows. Only background matters.
    pub fn shadow() -> Self {
        PaletteStyle::Shadow.into()
    }

    /// Style used for views.
    pub fn view() -> Self {
        PaletteStyle::View.into()
    }

    /// Main text with default background.
    pub fn primary() -> Self {
        PaletteStyle::Primary.into()
    }

    /// Secondary text color, with default background.
    pub fn secondary() -> Self {
        PaletteStyle::Secondary.into()
    }

    /// Tertiary text color, with default background.
    pub fn tertiary() -> Self {
        PaletteStyle::Tertiary.into()
    }

    /// Title text color with default background.
    pub fn title_primary() -> Self {
        PaletteStyle::TitlePrimary.into()
    }

    /// Alternative color for a title.
    pub fn title_secondary() -> Self {
        PaletteStyle::TitleSecondary.into()
    }

    /// Returns a highlight style.
    pub fn highlight() -> Self {
        PaletteStyle::Highlight.into()
    }

    /// Returns an inactive highlight style.
    pub fn highlight_inactive() -> Self {
        PaletteStyle::HighlightInactive.into()
    }
}

impl From<Effect> for StyleType {
    fn from(effect: Effect) -> Self {
        StyleType::Style(effect.into())
    }
}

impl From<ColorStyle> for StyleType {
    fn from(color: ColorStyle) -> Self {
        StyleType::Style(color.into())
    }
}

impl From<ColorPair> for StyleType {
    fn from(color: ColorPair) -> Self {
        StyleType::Style(color.into())
    }
}

impl From<Color> for StyleType {
    fn from(color: Color) -> Self {
        StyleType::Style(color.into())
    }
}

impl From<PaletteColor> for StyleType {
    fn from(color: PaletteColor) -> Self {
        StyleType::Style(color.into())
    }
}

impl From<ColorType> for StyleType {
    fn from(color: ColorType) -> Self {
        StyleType::Style(color.into())
    }
}

impl From<Style> for StyleType {
    fn from(style: Style) -> Self {
        StyleType::Style(style)
    }
}

impl From<PaletteStyle> for StyleType {
    fn from(style: PaletteStyle) -> Self {
        StyleType::Palette(style)
    }
}

/// Creates a new `Style` by merging all given styles.
///
/// Will use the last non-`None` color, and will combine all effects.
impl<'a> FromIterator<&'a Style> for Style {
    fn from_iter<I: IntoIterator<Item = &'a Style>>(iter: I) -> Style {
        let mut color = ColorStyle::inherit_parent();
        let mut effects = EnumSet::new();

        for style in iter {
            color = ColorStyle::merge(color, style.color);
            effects.insert_all(style.effects);
        }

        Style { effects, color }
    }
}

/// Creates a new `Style` by merging all given styles.
///
/// Will use the last non-`None` color, and will combine all effects.
impl<T: Into<Style>> FromIterator<T> for Style {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Style {
        iter.into_iter().map(Into::into).collect()
    }
}