sonos-cli 0.5.0

CLI and TUI for controlling Sonos speakers
Documentation
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
//! TUI color theme system.
//!
//! Every widget references `app.theme` — no hardcoded colors or characters.
//! Styles and glyphs are pre-computed in the constructor so render functions
//! pay zero allocation cost.

use ratatui::style::{Color, Modifier, Style};

/// Check whether two `Color` values are the same variant and value.
pub(crate) fn colors_equal(a: Color, b: Color) -> bool {
    match (a, b) {
        (Color::Rgb(r1, g1, b1), Color::Rgb(r2, g2, b2)) => r1 == r2 && g1 == g2 && b1 == b2,
        _ => std::mem::discriminant(&a) == std::mem::discriminant(&b),
    }
}

/// UI characters used by the TUI. Themes declare both styles and glyphs,
/// so custom themes can swap characters alongside colors.
#[derive(Clone, Debug)]
pub struct Glyphs {
    // Playback state icons
    pub playing: &'static str,
    pub paused: &'static str,
    pub stopped: &'static str,

    // Speaker list tree connectors
    pub connector_branch: &'static str,
    pub connector_last: &'static str,

    // Cursor / selection indicator
    pub cursor: &'static str,

    // Dot leader fill character
    pub leader_char: char,

    // Model separator in speaker rows
    pub model_separator: &'static str,

    // Group divider
    pub divider_left: &'static str,
    pub divider_fill: &'static str,
    pub divider_right: &'static str,

    // Media controls
    pub control_prev: &'static str,
    pub control_next: &'static str,

    // Header / tabs
    pub logo: &'static str,
    pub tab_active_left: &'static str,
    pub tab_active_right: &'static str,
    pub tab_active_indicator: &'static str,

    // Settings
    pub dropdown_indicator: &'static str,
    pub dropdown_active: &'static str,
    pub settings_cursor: &'static str,

    // Separator line
    pub separator: char,

    // Progress bar cursor
    pub progress_cursor: &'static str,

    // Toast notification
    pub toast_prefix: &'static str,

    // Album art placeholder
    pub music_note: &'static str,
}

impl Glyphs {
    pub fn default_glyphs() -> Self {
        Self {
            playing: "\u{25b6}", // ▶
            paused: "\u{23f8}",  // ⏸
            stopped: "\u{25a0}", // ■

            connector_branch: "\u{251c}\u{2500} ", // ├─
            connector_last: "\u{2514}\u{2500} ",   // └─

            cursor: "\u{276f}", // ❯

            leader_char: ':',

            model_separator: " \u{2022} ", // •

            divider_left: "+",
            divider_fill: "\u{2500}", // ─
            divider_right: "+",

            control_prev: "\u{23ee}", // ⏮
            control_next: "\u{23ed}", // ⏭

            logo: "\u{266a}  S O N O S", // ♪  S O N O S

            tab_active_left: "[",
            tab_active_right: "]",
            tab_active_indicator: "\u{25b8}", // ▸

            dropdown_indicator: "\u{25bc}", // ▼
            dropdown_active: "\u{25b8}",    // ▸
            settings_cursor: "\u{25c0}",    // ◀

            separator: '\u{2500}', // ─

            progress_cursor: "\u{25cf}", // ●

            toast_prefix: "\u{25cf}", // ●

            music_note: "\u{266a}", // ♪
        }
    }
}

/// Semantic styles used by the TUI. Grows as screens need new roles.
#[derive(Clone, Debug)]
pub struct Theme {
    // Layout chrome
    pub header: Style,
    pub legend: Style,
    pub muted: Style,

    // Track info
    pub track_info: Style,
    pub bottom_bar_controls: Style,

    // Playback state icons
    pub playing_icon: Style,
    pub paused_icon: Style,
    pub stopped_icon: Style,

    // Volume bar
    pub volume_filled: Style,
    pub volume_empty: Style,

    // Progress bar
    pub progress_filled: Style,
    pub progress_empty: Style,
    pub progress_cursor: Style,
    pub progress_time: Style,

    // Speakers tab
    pub group_header: Style,
    pub speaker_cursor: Style,
    pub speaker_name: Style,
    pub leader: Style,

    // Pickup mode
    pub picked_up: Style,

    // General
    pub accent: Style,
    pub accent_secondary: Style,
    pub error: Style,

    // Progress bar gradient (start == end means no gradient)
    pub progress_gradient_start: Color,
    pub progress_gradient_end: Color,

    // UI characters
    pub glyphs: Glyphs,
}

impl Theme {
    /// Resolve a theme by name from config. Unknown names fall back to default.
    pub fn from_name(name: &str) -> Self {
        match name {
            "bw" => Self::bw(),
            "minimal" => Self::minimal(),
            "dance_party" => Self::dance_party(),
            _ => Self::default_theme(),
        }
    }

    pub fn default_theme() -> Self {
        Self {
            header: Style::new().fg(Color::White).add_modifier(Modifier::BOLD),
            legend: Style::new().fg(Color::DarkGray),
            muted: Style::new().fg(Color::DarkGray),

            track_info: Style::new().fg(Color::Gray),
            bottom_bar_controls: Style::new().fg(Color::White),

            playing_icon: Style::new().fg(Color::Green),
            paused_icon: Style::new().fg(Color::Yellow),
            stopped_icon: Style::new().fg(Color::DarkGray),

            volume_filled: Style::new().fg(Color::Cyan),
            volume_empty: Style::new().fg(Color::DarkGray),

            progress_filled: Style::new().fg(Color::Gray),
            progress_empty: Style::new().fg(Color::DarkGray),
            progress_cursor: Style::new().fg(Color::White),
            progress_time: Style::new().fg(Color::DarkGray),

            group_header: Style::new().fg(Color::White).add_modifier(Modifier::BOLD),
            speaker_cursor: Style::new().fg(Color::Cyan),
            speaker_name: Style::new().fg(Color::Gray),
            leader: Style::new().fg(Color::DarkGray),

            picked_up: Style::new().bg(Color::DarkGray),

            accent: Style::new().fg(Color::Cyan),
            accent_secondary: Style::new().fg(Color::Blue),
            error: Style::new().fg(Color::Red),

            progress_gradient_start: Color::Gray,
            progress_gradient_end: Color::Gray,

            glyphs: Glyphs::default_glyphs(),
        }
    }

    pub fn bw() -> Self {
        Self {
            header: Style::new().fg(Color::White).add_modifier(Modifier::BOLD),
            legend: Style::new().fg(Color::DarkGray),
            muted: Style::new().fg(Color::DarkGray),

            track_info: Style::new().fg(Color::Gray),
            bottom_bar_controls: Style::new().fg(Color::White),

            playing_icon: Style::new()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD),
            paused_icon: Style::new().fg(Color::Gray),
            stopped_icon: Style::new().fg(Color::DarkGray),

            volume_filled: Style::new().fg(Color::White),
            volume_empty: Style::new().fg(Color::DarkGray),

            progress_filled: Style::new().fg(Color::White),
            progress_empty: Style::new().fg(Color::DarkGray),
            progress_cursor: Style::new()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD),
            progress_time: Style::new().fg(Color::DarkGray),

            group_header: Style::new()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
            speaker_cursor: Style::new()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD),
            speaker_name: Style::new().fg(Color::Gray),
            leader: Style::new().fg(Color::DarkGray),

            picked_up: Style::new().bg(Color::DarkGray),

            accent: Style::new().fg(Color::White),
            accent_secondary: Style::new().fg(Color::Gray),
            error: Style::new()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD | Modifier::UNDERLINED),

            progress_gradient_start: Color::White,
            progress_gradient_end: Color::White,

            glyphs: Glyphs::default_glyphs(),
        }
    }

    pub fn minimal() -> Self {
        Self {
            header: Style::new().fg(Color::Gray),
            legend: Style::new().fg(Color::DarkGray),
            muted: Style::new().fg(Color::DarkGray),

            track_info: Style::new().fg(Color::DarkGray),
            bottom_bar_controls: Style::new().fg(Color::Gray),

            playing_icon: Style::new().fg(Color::Green),
            paused_icon: Style::new().fg(Color::Yellow),
            stopped_icon: Style::new().fg(Color::DarkGray),

            volume_filled: Style::new().fg(Color::Gray),
            volume_empty: Style::new().fg(Color::DarkGray),

            progress_filled: Style::new().fg(Color::DarkGray),
            progress_empty: Style::new().fg(Color::DarkGray),
            progress_cursor: Style::new().fg(Color::Gray),
            progress_time: Style::new().fg(Color::DarkGray),

            group_header: Style::new()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD),
            speaker_cursor: Style::new().fg(Color::Gray),
            speaker_name: Style::new().fg(Color::DarkGray),
            leader: Style::new().fg(Color::DarkGray),

            picked_up: Style::new().bg(Color::DarkGray),

            accent: Style::new().fg(Color::Gray),
            accent_secondary: Style::new().fg(Color::DarkGray),
            error: Style::new().fg(Color::Red),

            progress_gradient_start: Color::DarkGray,
            progress_gradient_end: Color::DarkGray,

            glyphs: Glyphs {
                connector_branch: "   ",
                connector_last: "   ",
                leader_char: ' ',
                divider_left: "",
                divider_fill: " ",
                divider_right: "",
                model_separator: "  ",
                cursor: "\u{203a}",              // ›
                progress_cursor: "\u{2022}",     // •
                logo: "sonos",
                tab_active_left: "",
                tab_active_right: "",
                tab_active_indicator: "",
                ..Glyphs::default_glyphs()
            },
        }
    }

    pub fn dance_party() -> Self {
        let hot_pink = Color::Rgb(255, 100, 255);
        Self {
            header: Style::new().fg(hot_pink).add_modifier(Modifier::BOLD),
            legend: Style::new().fg(Color::Rgb(100, 100, 180)),
            muted: Style::new().fg(Color::Rgb(90, 70, 120)),

            track_info: Style::new().fg(Color::Rgb(255, 200, 100)),
            bottom_bar_controls: Style::new().fg(Color::Rgb(100, 255, 255)),

            playing_icon: Style::new().fg(Color::Rgb(50, 255, 50)),
            paused_icon: Style::new().fg(Color::Rgb(255, 255, 50)),
            stopped_icon: Style::new().fg(Color::Rgb(120, 50, 150)),

            volume_filled: Style::new().fg(Color::Rgb(255, 50, 150)),
            volume_empty: Style::new().fg(Color::Rgb(60, 30, 80)),

            progress_filled: Style::new().fg(Color::Rgb(255, 50, 150)),
            progress_empty: Style::new().fg(Color::Rgb(40, 20, 60)),
            progress_cursor: Style::new().fg(Color::Rgb(255, 255, 100)),
            progress_time: Style::new().fg(Color::Rgb(100, 100, 180)),

            group_header: Style::new()
                .fg(Color::Rgb(255, 150, 50))
                .add_modifier(Modifier::BOLD),
            speaker_cursor: Style::new().fg(hot_pink),
            speaker_name: Style::new().fg(Color::Rgb(200, 150, 255)),
            leader: Style::new().fg(Color::Rgb(60, 30, 80)),

            picked_up: Style::new().bg(Color::Rgb(80, 30, 100)),

            accent: Style::new().fg(hot_pink),
            accent_secondary: Style::new().fg(Color::Rgb(100, 255, 200)),
            error: Style::new().fg(Color::Rgb(255, 80, 80)),

            progress_gradient_start: Color::Rgb(255, 50, 150),
            progress_gradient_end: Color::Rgb(50, 200, 255),

            glyphs: Glyphs {
                playing: "\u{266b}",             // ♫
                paused: "\u{1f4a4}",             // 💤
                stopped: "\u{2716}",             // ✖
                cursor: "\u{2605}",              // ★
                music_note: "\u{266b}",          // ♫
                logo: "\u{2605} D A N C E  P A R T Y \u{2605}",
                progress_cursor: "\u{25c6}",     // ◆
                toast_prefix: "\u{2605}",        // ★
                control_prev: "\u{25c4}\u{25c4}", // ◄◄
                control_next: "\u{25ba}\u{25ba}", // ►►
                ..Glyphs::default_glyphs()
            },
        }
    }
}

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

    #[test]
    fn from_name_unknown_falls_back_to_default() {
        let theme = Theme::from_name("nonexistent");
        let default = Theme::default_theme();
        assert_eq!(theme.header, default.header);
    }

    #[test]
    fn from_name_resolves_all_themes() {
        let _ = Theme::from_name("default");
        let _ = Theme::from_name("bw");
        let _ = Theme::from_name("minimal");
        let _ = Theme::from_name("dance_party");
    }

    #[test]
    fn colors_equal_same_rgb() {
        assert!(colors_equal(Color::Rgb(1, 2, 3), Color::Rgb(1, 2, 3)));
    }

    #[test]
    fn colors_equal_different_rgb() {
        assert!(!colors_equal(Color::Rgb(1, 2, 3), Color::Rgb(4, 5, 6)));
    }

    #[test]
    fn colors_equal_named() {
        assert!(colors_equal(Color::White, Color::White));
        assert!(!colors_equal(Color::White, Color::Cyan));
    }
}