termusiclib/config/v2/tui/theme/
styles.rs

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
use serde::{Deserialize, Serialize};

/// All values correspond to the Theme's selected color for that
#[derive(Copy, Clone, Deserialize, Serialize, PartialEq, Eq, Debug)]
pub enum ColorTermusic {
    /// Reset to Terminal default (resulting color will depend on what context it is set)
    Reset = 0,
    Foreground = 1,
    Background = 2,
    Black = 3,
    Red = 4,
    Green = 5,
    Yellow = 6,
    Blue = 7,
    Magenta = 8,
    Cyan = 9,
    White = 10,
    LightBlack = 11,
    LightRed = 12,
    LightGreen = 13,
    LightYellow = 14,
    LightBlue = 15,
    LightMagenta = 16,
    LightCyan = 17,
    LightWhite = 18,
}

impl AsRef<str> for ColorTermusic {
    fn as_ref(&self) -> &str {
        match self {
            ColorTermusic::Reset => "reset",
            ColorTermusic::Foreground => "foreground",
            ColorTermusic::Background => "background",
            ColorTermusic::Black => "black",
            ColorTermusic::Red => "red",
            ColorTermusic::Green => "green",
            ColorTermusic::Yellow => "yellow",
            ColorTermusic::Blue => "blue",
            ColorTermusic::Magenta => "magenta",
            ColorTermusic::Cyan => "cyan",
            ColorTermusic::White => "white",
            ColorTermusic::LightBlack => "bright_black",
            ColorTermusic::LightRed => "bright_red",
            ColorTermusic::LightGreen => "bright_green",
            ColorTermusic::LightYellow => "bright_yellow",
            ColorTermusic::LightBlue => "bright_blue",
            ColorTermusic::LightMagenta => "bright_magenta",
            ColorTermusic::LightCyan => "bright_cyan",
            ColorTermusic::LightWhite => "bright_white",
        }
    }
}

impl ColorTermusic {
    pub const fn as_usize(self) -> usize {
        self as usize
    }
}

/// Style for the Library view
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Default)]
#[serde(default)] // allow missing fields and fill them with the `..Self::default()` in this struct
pub struct Styles {
    pub library: StyleLibrary,
    pub playlist: StylePlaylist,
    pub lyric: StyleLyric,
    pub progress: StyleProgress,
    pub important_popup: StyleImportantPopup,
    pub fallback: StyleFallback,
}

/// Style for the Library view
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default)] // allow missing fields and fill them with the `..Self::default()` in this struct
pub struct StyleLibrary {
    /// Music Library foreground color (text)
    pub foreground_color: ColorTermusic,
    /// Music Library background color (background)
    pub background_color: ColorTermusic,
    /// Music Library border color (when focused)
    pub border_color: ColorTermusic,
    /// Music Library selected node highlight color
    pub highlight_color: ColorTermusic,

    /// Music Library selected node highlight symbol
    pub highlight_symbol: String,
}

impl Default for StyleLibrary {
    fn default() -> Self {
        Self {
            foreground_color: ColorTermusic::Foreground,
            background_color: ColorTermusic::Reset,
            border_color: ColorTermusic::Blue,
            highlight_color: ColorTermusic::LightYellow,

            highlight_symbol: "🦄".into(),
        }
    }
}

/// Style for the Playlist Widget
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default)] // allow missing fields and fill them with the `..Self::default()` in this struct
pub struct StylePlaylist {
    /// Playlist foreground color (text)
    pub foreground_color: ColorTermusic,
    /// Playlist background color (text)
    pub background_color: ColorTermusic,
    /// Playlist border color (when focused)
    pub border_color: ColorTermusic,
    /// Playlist selected node highlight color
    pub highlight_color: ColorTermusic,

    /// Playlist selected track highlight symbol
    pub highlight_symbol: String,
    /// Playlist current playing track symbol
    pub current_track_symbol: String,

    /// If enabled use a symbol for the Loop-Mode, otherwise use text
    ///
    /// Example: true -> "Mode: 🔁"; false -> "Mode: playlist"
    pub use_loop_mode_symbol: bool,
}

impl Default for StylePlaylist {
    fn default() -> Self {
        Self {
            foreground_color: ColorTermusic::Foreground,
            background_color: ColorTermusic::Reset,
            border_color: ColorTermusic::Blue,
            highlight_color: ColorTermusic::LightYellow,

            highlight_symbol: "🚀".into(),
            current_track_symbol: "►".into(),

            use_loop_mode_symbol: true,
        }
    }
}

/// Style for the Lyric text view widget (also the radio text)
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default)] // allow missing fields and fill them with the `..Self::default()` in this struct
pub struct StyleLyric {
    /// Lyrics foreground color (text)
    pub foreground_color: ColorTermusic,
    /// Lyrics background color (background)
    pub background_color: ColorTermusic,
    /// Lyrics border color (when focused)
    pub border_color: ColorTermusic,
}

impl Default for StyleLyric {
    fn default() -> Self {
        Self {
            foreground_color: ColorTermusic::Foreground,
            background_color: ColorTermusic::Reset,
            border_color: ColorTermusic::Blue,
        }
    }
}

/// Style for the Player Progress widget
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default)] // allow missing fields and fill them with the `..Self::default()` in this struct
pub struct StyleProgress {
    /// Track Progressbar foreground color (text)
    pub foreground_color: ColorTermusic,
    /// Track Progressbar background color (background)
    pub background_color: ColorTermusic,
    /// Track Progressbar border (always)
    pub border_color: ColorTermusic,
}

impl Default for StyleProgress {
    fn default() -> Self {
        Self {
            foreground_color: ColorTermusic::LightBlack,
            background_color: ColorTermusic::Reset,
            border_color: ColorTermusic::Blue,
        }
    }
}

/// Style for Important Popups (quit, save config, delete, NOT Error)
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default)] // allow missing fields and fill them with the `..Self::default()` in this struct
pub struct StyleImportantPopup {
    /// Important Popup (like Error or Delete) foreground color (text)
    pub foreground_color: ColorTermusic,
    /// Important Popup (like Error or Delete) background color (background)
    pub background_color: ColorTermusic,
    /// Important Popup (like Error or Delete) border color (always)
    pub border_color: ColorTermusic,
}

impl Default for StyleImportantPopup {
    fn default() -> Self {
        Self {
            foreground_color: ColorTermusic::Yellow,
            background_color: ColorTermusic::Reset,
            border_color: ColorTermusic::Yellow,
        }
    }
}

/// Generic is when there is no specific config entry for it, like the `AskQuit` popup
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default)] // allow missing fields and fill them with the `..Self::default()` in this struct
pub struct StyleFallback {
    /// Generic foreground color (text)
    pub foreground_color: ColorTermusic,
    /// Generic background color (background)
    pub background_color: ColorTermusic,
    /// Generic border color (always)
    pub border_color: ColorTermusic,
    /// Generic Highlight color
    pub highlight_color: ColorTermusic,
}

impl Default for StyleFallback {
    fn default() -> Self {
        Self {
            foreground_color: ColorTermusic::Foreground,
            background_color: ColorTermusic::Reset,
            border_color: ColorTermusic::Blue,
            highlight_color: ColorTermusic::LightYellow,
        }
    }
}

mod v1_interop {
    use super::{
        ColorTermusic, StyleFallback, StyleImportantPopup, StyleLibrary, StyleLyric, StylePlaylist,
        StyleProgress, Styles,
    };
    use crate::config::v1;

    impl From<v1::ColorTermusic> for ColorTermusic {
        fn from(value: v1::ColorTermusic) -> Self {
            match value {
                v1::ColorTermusic::Reset => Self::Reset,
                v1::ColorTermusic::Foreground => Self::Foreground,
                v1::ColorTermusic::Background => Self::Background,
                v1::ColorTermusic::Black => Self::Black,
                v1::ColorTermusic::Red => Self::Red,
                v1::ColorTermusic::Green => Self::Green,
                v1::ColorTermusic::Yellow => Self::Yellow,
                v1::ColorTermusic::Blue => Self::Blue,
                v1::ColorTermusic::Magenta => Self::Magenta,
                v1::ColorTermusic::Cyan => Self::Cyan,
                v1::ColorTermusic::White => Self::White,
                v1::ColorTermusic::LightBlack => Self::LightBlack,
                v1::ColorTermusic::LightRed => Self::LightRed,
                v1::ColorTermusic::LightGreen => Self::LightGreen,
                v1::ColorTermusic::LightYellow => Self::LightYellow,
                v1::ColorTermusic::LightBlue => Self::LightBlue,
                v1::ColorTermusic::LightMagenta => Self::LightMagenta,
                v1::ColorTermusic::LightCyan => Self::LightCyan,
                v1::ColorTermusic::LightWhite => Self::LightWhite,
            }
        }
    }

    impl From<&v1::StyleColorSymbol> for StyleLibrary {
        fn from(value: &v1::StyleColorSymbol) -> Self {
            Self {
                foreground_color: value.library_foreground.into(),
                background_color: value.library_background.into(),
                border_color: value.library_border.into(),
                highlight_color: value.library_highlight.into(),

                highlight_symbol: value.library_highlight_symbol.clone(),
            }
        }
    }

    impl From<&v1::Settings> for StylePlaylist {
        fn from(value: &v1::Settings) -> Self {
            let use_loop_mode_symbol = value.playlist_display_symbol;
            let value = &value.style_color_symbol;
            Self {
                foreground_color: value.playlist_foreground.into(),
                background_color: value.playlist_background.into(),
                border_color: value.playlist_border.into(),
                highlight_color: value.playlist_highlight.into(),
                highlight_symbol: value.playlist_highlight_symbol.clone(),
                current_track_symbol: value.currently_playing_track_symbol.clone(),
                use_loop_mode_symbol,
            }
        }
    }

    impl From<&v1::StyleColorSymbol> for StyleLyric {
        fn from(value: &v1::StyleColorSymbol) -> Self {
            Self {
                foreground_color: value.lyric_foreground.into(),
                background_color: value.lyric_background.into(),
                border_color: value.lyric_border.into(),
            }
        }
    }

    impl From<&v1::StyleColorSymbol> for StyleProgress {
        fn from(value: &v1::StyleColorSymbol) -> Self {
            Self {
                foreground_color: value.progress_foreground.into(),
                background_color: value.progress_background.into(),
                border_color: value.progress_border.into(),
            }
        }
    }

    impl From<&v1::StyleColorSymbol> for StyleImportantPopup {
        fn from(value: &v1::StyleColorSymbol) -> Self {
            Self {
                foreground_color: value.important_popup_foreground.into(),
                background_color: value.important_popup_background.into(),
                border_color: value.important_popup_border.into(),
            }
        }
    }

    impl From<&v1::StyleColorSymbol> for StyleFallback {
        fn from(value: &v1::StyleColorSymbol) -> Self {
            Self {
                foreground_color: value.fallback_foreground.into(),
                background_color: value.fallback_background.into(),
                border_color: value.fallback_border.into(),
                highlight_color: value.fallback_highlight.into(),
            }
        }
    }

    impl From<&v1::Settings> for Styles {
        fn from(value: &v1::Settings) -> Self {
            let playlist = value.into();
            let value = &value.style_color_symbol;
            Self {
                library: value.into(),
                playlist,
                lyric: value.into(),
                progress: value.into(),
                important_popup: value.into(),
                fallback: value.into(),
            }
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn should_convert_default_without_error() {
            let converted: Styles = (&v1::Settings::default()).into();

            let expected_library = StyleLibrary {
                foreground_color: ColorTermusic::Foreground,
                background_color: ColorTermusic::Reset,
                border_color: ColorTermusic::Blue,
                highlight_color: ColorTermusic::LightYellow,

                highlight_symbol: "🦄".into(),
            };
            assert_eq!(converted.library, expected_library);

            let expected_playlist = StylePlaylist {
                foreground_color: ColorTermusic::Foreground,
                background_color: ColorTermusic::Reset,
                border_color: ColorTermusic::Blue,
                highlight_color: ColorTermusic::LightYellow,

                highlight_symbol: "🚀".into(),
                current_track_symbol: "►".into(),
                use_loop_mode_symbol: true,
            };
            assert_eq!(converted.playlist, expected_playlist);

            let expected_lyric = StyleLyric {
                foreground_color: ColorTermusic::Foreground,
                background_color: ColorTermusic::Reset,
                border_color: ColorTermusic::Blue,
            };
            assert_eq!(converted.lyric, expected_lyric);

            let expected_progress = StyleProgress {
                foreground_color: ColorTermusic::LightBlack,
                background_color: ColorTermusic::Reset,
                border_color: ColorTermusic::Blue,
            };
            assert_eq!(converted.progress, expected_progress);

            let expected_important_popup = StyleImportantPopup {
                foreground_color: ColorTermusic::Yellow,
                background_color: ColorTermusic::Reset,
                border_color: ColorTermusic::Yellow,
            };
            assert_eq!(converted.important_popup, expected_important_popup);

            let expected_fallback = StyleFallback {
                foreground_color: ColorTermusic::Foreground,
                background_color: ColorTermusic::Reset,
                border_color: ColorTermusic::Blue,
                highlight_color: ColorTermusic::LightYellow,
            };
            assert_eq!(converted.fallback, expected_fallback);

            assert_eq!(
                converted,
                Styles {
                    library: expected_library,
                    playlist: expected_playlist,
                    lyric: expected_lyric,
                    progress: expected_progress,
                    important_popup: expected_important_popup,
                    fallback: expected_fallback
                }
            );
        }
    }
}