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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::default::Default;
use std::fmt;

use lazy_static::lazy_static;
use serde_derive::Deserialize;

use serde::de::{self, Deserialize, Deserializer, MapAccess, Visitor};

use crate::util;

lazy_static! {
    pub static ref SLICK: Theme = Theme {
        native_separators: Some(false),
        idle_bg: Some(String::from("#424242")),
        idle_fg: Some(String::from("#ffffff")),
        info_bg: Some(String::from("#2196f3")),
        info_fg: Some(String::from("#ffffff")),
        good_bg: Some(String::from("#8bc34a")),
        good_fg: Some(String::from("#000000")),
        warning_bg: Some(String::from("#ffc107")),
        warning_fg: Some(String::from("#000000")),
        critical_bg: Some(String::from("#f44336")),
        critical_fg: Some(String::from("#ffffff")),
        separator: "\u{e0b2}".to_owned(),
        separator_bg: Some(String::from("auto")),
        separator_fg: Some(String::from("auto")),
        alternating_tint_bg: Some(String::from("#111111")),
        alternating_tint_fg: Some(String::from("#111111")),
    };

    pub static ref SOLARIZED_DARK: Theme = Theme {
        native_separators: Some(false),
        idle_bg: Some(String::from("#002b36")),      // base03
        idle_fg: Some(String::from("#93a1a1")),      // base1
        info_bg: Some(String::from("#268bd2")),      // blue
        info_fg: Some(String::from("#002b36")),      // base03
        good_bg: Some(String::from("#859900")),      // green
        good_fg: Some(String::from("#002b36")),      // base03
        warning_bg: Some(String::from("#b58900")),   // yellow
        warning_fg: Some(String::from("#002b36")),   // base03
        critical_bg: Some(String::from("#dc322f")),  // red
        critical_fg: Some(String::from("#002b36")),  // base03
        separator: "\u{e0b2}".to_owned(),
        separator_bg: Some(String::from("auto")),
        separator_fg: Some(String::from("auto")),
        alternating_tint_bg: None.to_owned(),
        alternating_tint_fg: None.to_owned(),
    };

    pub static ref SOLARIZED_LIGHT: Theme = Theme {
        native_separators: Some(false),
        idle_bg: Some(String::from("#fdf6e3")),      // base3
        idle_fg: Some(String::from("#586e75")),      // base01
        info_bg: Some(String::from("#268bd2")),      // blue
        info_fg: Some(String::from("#fdf6e3")),      // base3
        good_bg: Some(String::from("#859900")),      // green
        good_fg: Some(String::from("#fdf6e3")),      // base3
        warning_bg: Some(String::from("#b58900")),   // yellow
        warning_fg: Some(String::from("#fdf6e3")),   // base3
        critical_bg: Some(String::from("#dc322f")),  // red
        critical_fg: Some(String::from("#fdf6e3")),  // base3
        separator: "\u{e0b2}".to_owned(),
        separator_bg: Some(String::from("auto")),
        separator_fg: Some(String::from("auto")),
        alternating_tint_bg: None.to_owned(),
        alternating_tint_fg: None.to_owned(),
    };

    pub static ref MODERN: Theme = Theme {
        native_separators: Some(false),
        idle_bg: Some(String::from("#222D32")),
        idle_fg: Some(String::from("#CFD8DC")),
        info_bg: Some(String::from("#449CDB")),
        info_fg: Some(String::from("#1D1F21")),
        good_bg: Some(String::from("#99b938")),
        good_fg: Some(String::from("#1D1F21")),
        warning_bg: Some(String::from("#FE7E29")),
        warning_fg: Some(String::from("#1D1F21")),
        critical_bg: Some(String::from("#ff5252")),
        critical_fg: Some(String::from("#1D1F21")),
        separator: "\u{e0b2}".to_owned(),
        separator_bg: Some(String::from("auto")),
        separator_fg: Some(String::from("auto")),
        alternating_tint_bg: None.to_owned(),
        alternating_tint_fg: None.to_owned(),
    };

    pub static ref PLAIN: Theme = Theme {
        native_separators: Some(false),
        idle_bg: Some(String::from("#000000")),
        idle_fg: Some(String::from("#93a1a1")),
        info_bg: Some(String::from("#000000")),
        info_fg: Some(String::from("#93a1a1")),
        good_bg: Some(String::from("#000000")),
        good_fg: Some(String::from("#859900")),
        warning_bg: Some(String::from("#000000")),
        warning_fg: Some(String::from("#b58900")),
        critical_bg: Some(String::from("#000000")),
        critical_fg: Some(String::from("#dc322f")),
        separator: "|".to_owned(),
        separator_bg: Some(String::from("#000000")),
        separator_fg: Some(String::from("#a9a9a9")),
        alternating_tint_bg: None.to_owned(),
        alternating_tint_fg: None.to_owned(),
    };

    pub static ref BAD_WOLF: Theme = Theme {
        native_separators: Some(false),
        idle_bg: Some(String::from("#444444")),
        idle_fg: Some(String::from("#f5f5f5")),
        info_bg: Some(String::from("#626262")),
        info_fg: Some(String::from("#ffd680")),
        good_bg: Some(String::from("#afff00")),
        good_fg: Some(String::from("#000000")),
        warning_bg: Some(String::from("#ffaf00")),
        warning_fg: Some(String::from("#000000")),
        critical_bg: Some(String::from("#d70000")),
        critical_fg: Some(String::from("#000000")),
        separator: "\u{e0b2}".to_owned(),
        separator_bg: Some(String::from("auto")),
        separator_fg: Some(String::from("auto")),
        alternating_tint_bg: None.to_owned(),
        alternating_tint_fg: None.to_owned(),
    };

    pub static ref GRUVBOX_LIGHT: Theme = Theme {
        native_separators: Some(false),
        idle_bg: Some(String::from("#fbf1c7")),
        idle_fg: Some(String::from("#3c3836")),
        info_bg: Some(String::from("#458588")),
        info_fg: Some(String::from("#fbf1c7")),
        good_bg: Some(String::from("#98971a")),
        good_fg: Some(String::from("#fbf1c7")),
        warning_bg: Some(String::from("#d79921")),
        warning_fg: Some(String::from("#fbf1c7")),
        critical_bg: Some(String::from("#cc241d")),
        critical_fg: Some(String::from("#fbf1c7")),
        separator: "\u{e0b2}".to_owned(),
        separator_bg: Some(String::from("auto")),
        separator_fg: Some(String::from("auto")),
        alternating_tint_bg: None.to_owned(),
        alternating_tint_fg: None.to_owned(),
    };

    pub static ref GRUVBOX_DARK: Theme = Theme {
        native_separators: Some(false),
        idle_bg: Some(String::from("#282828")),
        idle_fg: Some(String::from("#ebdbb2")),
        info_bg: Some(String::from("#458588")),
        info_fg: Some(String::from("#ebdbb2")),
        good_bg: Some(String::from("#98971a")),
        good_fg: Some(String::from("#ebdbb2")),
        warning_bg: Some(String::from("#d79921")),
        warning_fg: Some(String::from("#ebdbb2")),
        critical_bg: Some(String::from("#cc241d")),
        critical_fg: Some(String::from("#ebdbb2")),
        separator: "\u{e0b2}".to_owned(),
        separator_bg: Some(String::from("auto")),
        separator_fg: Some(String::from("auto")),
        alternating_tint_bg: None.to_owned(),
        alternating_tint_fg: None.to_owned(),
    };

    pub static ref SPACE_VILLAIN: Theme = Theme {
        native_separators: Some(false),
        idle_bg: Some(String::from("#06060f")), //Rich black
        idle_fg: Some(String::from("#c1c1c1")), //Silver
        info_bg: Some(String::from("#00223f")), //Maastricht Blue
        info_fg: Some(String::from("#c1c1c1")), //Silver
        good_bg: Some(String::from("#394049")), //Arsenic
        good_fg: Some(String::from("#c1c1c1")), //Silver
        warning_bg: Some(String::from("#2d1637")), //Dark Purple
        warning_fg: Some(String::from("#c1c1c1")), //Silver
        critical_bg: Some(String::from("#c1c1c1")), //Silver
        critical_fg: Some(String::from("#2c1637")), //Dark Purple
        separator: "\u{e0b2}".to_owned(),
        separator_bg: Some(String::from("auto")),
        separator_fg: Some(String::from("auto")),
        alternating_tint_bg: None.to_owned(),
        alternating_tint_fg: None.to_owned(),
    };

    pub static ref SEMI_NATIVE: Theme = Theme {
        native_separators: Some(true),
        idle_bg: None.to_owned(),
        idle_fg: Some(String::from("#93a1a1")),
        info_bg: None.to_owned(),
        info_fg: Some(String::from("#93a1a1")),
        good_bg: None.to_owned(),
        good_fg: Some(String::from("#859900")),
        warning_bg: None.to_owned(),
        warning_fg: Some(String::from("#b58900")),
        critical_bg: None.to_owned(),
        critical_fg: Some(String::from("#dc322f")),
        separator: "".to_owned(),
        separator_bg: None.to_owned(),
        separator_fg: None.to_owned(),
        alternating_tint_bg: None.to_owned(),
        alternating_tint_fg: None.to_owned(),
    };

    pub static ref NATIVE: Theme = Theme {
        native_separators: Some(true),
        idle_bg: None.to_owned(),
        idle_fg: None.to_owned(),
        info_bg: None.to_owned(),
        info_fg: None.to_owned(),
        good_bg: None.to_owned(),
        good_fg: None.to_owned(),
        warning_bg: None.to_owned(),
        warning_fg: None.to_owned(),
        critical_bg: None.to_owned(),
        critical_fg: None.to_owned(),
        separator: "".to_owned(),
        separator_bg: None.to_owned(),
        separator_fg: None.to_owned(),
        alternating_tint_bg: None.to_owned(),
        alternating_tint_fg: None.to_owned(),
    };

}

#[derive(Debug, Clone)]
pub struct Theme {
    pub native_separators: Option<bool>,
    pub idle_bg: Option<String>,
    pub idle_fg: Option<String>,
    pub info_bg: Option<String>,
    pub info_fg: Option<String>,
    pub good_bg: Option<String>,
    pub good_fg: Option<String>,
    pub warning_bg: Option<String>,
    pub warning_fg: Option<String>,
    pub critical_bg: Option<String>,
    pub critical_fg: Option<String>,
    pub separator: String,
    pub separator_bg: Option<String>,
    pub separator_fg: Option<String>,
    pub alternating_tint_bg: Option<String>,
    pub alternating_tint_fg: Option<String>,
}

impl Default for Theme {
    fn default() -> Self {
        PLAIN.clone()
    }
}

#[derive(Deserialize, Debug, Clone)]
pub struct ThemeFromFile {
    pub native_separators: Option<bool>,
    pub idle_bg: Option<String>,
    pub idle_fg: Option<String>,
    pub info_bg: Option<String>,
    pub info_fg: Option<String>,
    pub good_bg: Option<String>,
    pub good_fg: Option<String>,
    pub warning_bg: Option<String>,
    pub warning_fg: Option<String>,
    pub critical_bg: Option<String>,
    pub critical_fg: Option<String>,
    pub separator: String,
    pub separator_bg: Option<String>,
    pub separator_fg: Option<String>,
    pub alternating_tint_bg: Option<String>,
    pub alternating_tint_fg: Option<String>,
}

impl Into<Theme> for ThemeFromFile {
    fn into(self) -> Theme {
        Theme {
            native_separators: self.native_separators,
            idle_bg: self.idle_bg,
            idle_fg: self.idle_fg,
            info_bg: self.info_bg,
            info_fg: self.info_fg,
            good_bg: self.good_bg,
            good_fg: self.good_fg,
            warning_bg: self.warning_bg,
            warning_fg: self.warning_fg,
            critical_bg: self.critical_bg,
            critical_fg: self.critical_fg,
            separator: self.separator,
            separator_bg: self.separator_bg,
            separator_fg: self.separator_fg,
            alternating_tint_bg: self.alternating_tint_bg,
            alternating_tint_fg: self.alternating_tint_fg,
        }
    }
}

impl Theme {
    pub fn from_name(name: &str) -> Option<Theme> {
        match name {
            "slick" => Some(SLICK.clone()),
            "solarized-dark" => Some(SOLARIZED_DARK.clone()),
            "solarized-light" => Some(SOLARIZED_LIGHT.clone()),
            "plain" => Some(PLAIN.clone()),
            "modern" => Some(MODERN.clone()),
            "bad-wolf" => Some(BAD_WOLF.clone()),
            "gruvbox-light" => Some(GRUVBOX_LIGHT.clone()),
            "gruvbox-dark" => Some(GRUVBOX_DARK.clone()),
            "space-villain" => Some(SPACE_VILLAIN.clone()),
            "semi-native" => Some(SEMI_NATIVE.clone()),
            "native" => Some(NATIVE.clone()),
            _ => None,
        }
    }

    pub fn from_file(file: &str) -> Option<Theme> {
        let file = util::find_file(file, Some("themes"), Some(".toml"))?;
        let theme: ThemeFromFile = util::deserialize_file(&file).ok()?;
        Some(theme.into())
    }
}

#[derive(Deserialize, Debug, Default, Clone)]
#[serde(deny_unknown_fields)]
pub struct ThemeOverrides {
    idle_bg: Option<String>,
    idle_fg: Option<String>,
    info_bg: Option<String>,
    info_fg: Option<String>,
    good_bg: Option<String>,
    good_fg: Option<String>,
    warning_bg: Option<String>,
    warning_fg: Option<String>,
    critical_bg: Option<String>,
    critical_fg: Option<String>,
    separator: Option<String>,
    separator_bg: Option<String>,
    separator_fg: Option<String>,
    alternating_tint_bg: Option<String>,
    alternating_tint_fg: Option<String>,
}

impl<'de> Deserialize<'de> for Theme {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(field_identifier, rename_all = "lowercase")]
        enum Field {
            Name,
            File,
            Overrides,
        }

        struct ThemeVisitor;

        impl<'de> Visitor<'de> for ThemeVisitor {
            type Value = Theme;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct Theme")
            }

            /// Handle configs like:
            ///
            /// ```toml
            /// theme = "slick"
            /// ```
            fn visit_str<E>(self, name: &str) -> Result<Theme, E>
            where
                E: de::Error,
            {
                Theme::from_name(name)
                    .ok_or_else(|| de::Error::custom(format!("Theme \"{}\" not found.", name)))
            }

            /// Handle configs like:
            ///
            /// ```toml
            /// [theme]
            /// name = "modern"
            /// ```
            fn visit_map<V>(self, mut map: V) -> Result<Theme, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut theme = None;
                let mut overrides: Option<ThemeOverrides> = None;
                while let Some(key) = map.next_key()? {
                    match key {
                        Field::Name => {
                            if theme.is_some() {
                                return Err(de::Error::duplicate_field("name or file"));
                            }
                            let name = map.next_value()?;
                            theme = Some(Theme::from_name(name).ok_or_else(|| {
                                de::Error::custom(format!("Theme \"{}\" not found.", name))
                            })?);
                        }
                        Field::File => {
                            if theme.is_some() {
                                return Err(de::Error::duplicate_field("name or file"));
                            }
                            let file = map.next_value()?;
                            theme = Some(Theme::from_file(file).ok_or_else(|| {
                                de::Error::custom(format!(
                                    "Failed to load theme from file {}.",
                                    file
                                ))
                            })?);
                        }
                        Field::Overrides => {
                            if overrides.is_some() {
                                return Err(de::Error::duplicate_field("overrides"));
                            }
                            overrides = Some(map.next_value()?);
                        }
                    }
                }
                let mut theme = theme.unwrap_or_default();
                if let Some(overrides) = overrides {
                    theme.idle_bg = overrides.idle_bg.or(theme.idle_bg);
                    theme.idle_fg = overrides.idle_fg.or(theme.idle_fg);
                    theme.info_bg = overrides.info_bg.or(theme.info_bg);
                    theme.info_fg = overrides.info_fg.or(theme.info_fg);
                    theme.good_bg = overrides.good_bg.or(theme.good_bg);
                    theme.good_fg = overrides.good_fg.or(theme.good_fg);
                    theme.warning_bg = overrides.warning_bg.or(theme.warning_bg);
                    theme.warning_fg = overrides.warning_fg.or(theme.warning_fg);
                    theme.critical_bg = overrides.critical_bg.or(theme.critical_bg);
                    theme.critical_fg = overrides.critical_fg.or(theme.critical_fg);
                    theme.separator = overrides.separator.unwrap_or(theme.separator);
                    theme.separator_bg = overrides.separator_bg.or(theme.separator_bg);
                    theme.separator_fg = overrides.separator_fg.or(theme.separator_fg);
                    theme.alternating_tint_bg =
                        overrides.alternating_tint_bg.or(theme.alternating_tint_bg);
                    theme.alternating_tint_fg =
                        overrides.alternating_tint_fg.or(theme.alternating_tint_fg);
                }
                Ok(theme)
            }
        }

        deserializer.deserialize_any(ThemeVisitor)
    }
}