gtm 0.2.83

Feature rich and cross platform terminal audio player
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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
// Copyright (c) 2026
// Author: prjctimg <prjctimg@outlook.com>
// Footer bar: modular keybinding/system/playback status display.
//
// This is free software released under the GPL-3.0 license.

use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use std::borrow::Cow;

use gtm_core::state::PlaybackStatus;

use crate::app::App;

fn darken(c: Color, factor: f64) -> Color {
    match c {
        Color::Rgb(r, g, b) => Color::Rgb(
            (r as f64 * factor) as u8,
            (g as f64 * factor) as u8,
            (b as f64 * factor) as u8,
        ),
        _ => c,
    }
}

fn bg_luminance(c: Color) -> f64 {
    match c {
        Color::Rgb(r, g, b) => 0.299 * r as f64 + 0.587 * g as f64 + 0.114 * b as f64,
        _ => 128.0,
    }
}

/// A footer module. Each module renders a small run of text with its own
/// accent-coloured background (brand-badge style).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FooterModule {
    Playback,
    Title,
    Volume,
    Repeat,
    Shuffle,
    Progress,
    Queue,
    KeyAction,
    Backend,
    System,
    EqPreset,
    SleepTimer,
    Notification,
    Time,
}

impl FooterModule {
    /// Stable display name; also used to parse user presets from TOML.
    pub fn as_str(self) -> &'static str {
        match self {
            FooterModule::Playback => "Playback",
            FooterModule::Title => "Title",
            FooterModule::Volume => "Volume",
            FooterModule::Repeat => "Repeat",
            FooterModule::Shuffle => "Shuffle",
            FooterModule::Progress => "Progress",
            FooterModule::Queue => "Queue",
            FooterModule::KeyAction => "KeyAction",
            FooterModule::Backend => "Backend",
            FooterModule::System => "System",
            FooterModule::EqPreset => "EqPreset",
            FooterModule::SleepTimer => "SleepTimer",
            FooterModule::Notification => "Notification",
            FooterModule::Time => "Time",
        }
    }

    /// Parse a module name from a TOML footer preset file. Unknown names are
    /// dropped by the caller so typos don't break the whole preset.
    pub fn from_str_lossy(s: &str) -> Option<Self> {
        Some(match s.trim() {
            "Playback" => FooterModule::Playback,
            "Title" => FooterModule::Title,
            "Volume" => FooterModule::Volume,
            "Repeat" => FooterModule::Repeat,
            "Shuffle" => FooterModule::Shuffle,
            "Progress" => FooterModule::Progress,
            "Queue" => FooterModule::Queue,
            "KeyAction" => FooterModule::KeyAction,
            "Backend" => FooterModule::Backend,
            "System" => FooterModule::System,
            "EqPreset" => FooterModule::EqPreset,
            "SleepTimer" => FooterModule::SleepTimer,
            "Notification" => FooterModule::Notification,
            "Time" => FooterModule::Time,
            _ => return None,
        })
    }
}

/// A footer layout preset. `left` modules hug the left edge and `right`
/// modules hug the right edge (lualine-style `a,b,c` / `x,y,z` layout with no
/// centred middle group).
#[derive(Debug, Clone)]
pub struct FooterPreset {
    pub name: Cow<'static, str>,
    pub left: Vec<FooterModule>,
    pub right: Vec<FooterModule>,
}

/// Built-in presets, intended to mirror the left/right/middle layout
/// (historically mirrored the archived gtm.nim status-bar ordering where
/// playback/queue sit at an extreme end and system/device info at the other).
pub fn presets() -> Vec<FooterPreset> {
    vec![
        FooterPreset {
            name: Cow::Borrowed("Default"),
            left: vec![
                FooterModule::Playback,
                FooterModule::Title,
                FooterModule::Repeat,
                FooterModule::Shuffle,
                FooterModule::Volume,
                FooterModule::EqPreset,
                FooterModule::KeyAction,
                FooterModule::Notification,
                FooterModule::SleepTimer,
            ],
            right: vec![
                FooterModule::Queue,
                FooterModule::Time,
                FooterModule::System,
                FooterModule::Backend,
            ],
        },
        // Bare minimum for termux or very small viewports.
        FooterPreset {
            name: Cow::Borrowed("Minimal"),
            left: vec![
                FooterModule::Playback,
                FooterModule::Volume,
                FooterModule::KeyAction,
                FooterModule::SleepTimer,
            ],
            right: vec![FooterModule::Time, FooterModule::Backend],
        },
        FooterPreset {
            name: Cow::Borrowed("Full"),
            left: vec![
                FooterModule::Playback,
                FooterModule::Title,
                FooterModule::Repeat,
                FooterModule::Shuffle,
                FooterModule::Volume,
                FooterModule::EqPreset,
                FooterModule::Progress,
                FooterModule::KeyAction,
                FooterModule::SleepTimer,
            ],
            right: vec![
                FooterModule::Queue,
                FooterModule::Time,
                FooterModule::System,
                FooterModule::Backend,
            ],
        },
    ]
}

// ─── User presets (TOML) ──────────────────────────────────────────────

pub fn user_presets_path() -> std::path::PathBuf {
    let config = std::env::var("XDG_CONFIG_HOME")
        .map(std::path::PathBuf::from)
        .unwrap_or_else(|_| {
            let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".into());
            std::path::PathBuf::from(home).join(".config")
        });
    config.join("gtm").join("footer.toml")
}

#[derive(serde::Deserialize, Default)]
struct UserPresetsFile {
    #[serde(default)]
    preset: Vec<UserPreset>,
}

#[derive(serde::Deserialize, Default)]
struct UserPreset {
    name: String,
    #[serde(default)]
    left: Vec<String>,
    #[serde(default, alias = "middle")]
    _legacy_middle: Vec<String>,
    #[serde(default)]
    right: Vec<String>,
}

fn parse_module_list(names: &[String]) -> Vec<FooterModule> {
    names
        .iter()
        .filter_map(|s| FooterModule::from_str_lossy(s))
        .collect()
}

/// Load user-defined presets from `~/.config/gtm/footer.toml`. Unparseable
/// files are skipped so a malformed file never breaks the TUI.
pub fn load_user_presets() -> Vec<FooterPreset> {
    let Ok(text) = std::fs::read_to_string(user_presets_path()) else {
        return Vec::new();
    };
    let Ok(parsed) = toml::from_str::<UserPresetsFile>(&text) else {
        return Vec::new();
    };
    parsed
        .preset
        .into_iter()
        .map(|p| FooterPreset {
            name: Cow::Owned(p.name),
            left: parse_module_list(&p.left),
            right: parse_module_list(&p.right),
        })
        .collect()
}

/// Built-in presets followed by user presets; user presets replace built-ins
/// on name collision.
pub fn merged_presets() -> Vec<FooterPreset> {
    let mut v = presets();
    for up in load_user_presets() {
        if let Some(existing) = v.iter_mut().find(|p| p.name == up.name) {
            *existing = up;
        } else {
            v.push(up);
        }
    }
    v
}

// ─── Rendering ────────────────────────────────────────────────────────

/// One rendered footer group: a styled line, its background, and its width
/// in terminal cells.
#[derive(Clone)]
pub struct FooterGroup {
    pub line: Line<'static>,
    pub bg: Color,
    pub width: u16,
}

/// The full output of a footer render: left and right module groups plus the
/// background used for the unfilled trailing area on the right edge.
#[derive(Clone)]
pub struct FooterRenderOutput {
    pub left: Vec<FooterGroup>,
    pub right: Vec<FooterGroup>,
    pub right_bg: Color,
}

/// Cached footer render used to suppress refresh during tab transitions.
#[derive(Default)]
pub struct FooterCache {
    pub last: Option<FooterRenderOutput>,
    pub suppress_refresh: bool,
}

/// Render the current footer preset into left/right module groups plus the
/// trailing-area background. Returns `None` when every module would be empty
/// (e.g. no track loaded and no key action pending).
pub fn render(app: &App) -> Option<FooterRenderOutput> {
    let preset = app
        .footer_presets
        .get(app.footer_preset)
        .or_else(|| app.footer_presets.first())?;

    let mut out_left: Vec<FooterGroup> = Vec::new();
    let mut out_right: Vec<FooterGroup> = Vec::new();
    for (is_left, m) in preset
        .left
        .iter()
        .map(|m| (true, *m))
        .chain(preset.right.iter().map(|m| (false, *m)))
    {
        let Some(text) = module_text(m, app) else {
            continue;
        };
        if text.is_empty() {
            continue;
        }
        // Brand-badge styling: a solid per-module accent background with the
        // readable foreground and a bold weight, exactly like the "gtm" badge.
        let bg = module_color(m, &app.theme);
        let fg = crate::theme::readable_fg(app.theme.fg, bg);
        let span = Span::styled(
            format!(" {} ", text),
            Style::default().fg(fg).add_modifier(Modifier::BOLD),
        );
        let group = FooterGroup {
            width: span.width() as u16,
            line: Line::from(span),
            bg,
        };
        if is_left {
            out_left.push(group);
        } else {
            out_right.push(group);
        }
    }

    if out_left.is_empty() && out_right.is_empty() {
        return None;
    }
    Some(FooterRenderOutput {
        left: out_left,
        right: out_right,
        right_bg: if app.transparent_bg {
            Color::Reset
        } else if bg_luminance(app.theme.bg) > 180.0 {
            darken(app.theme.bg, 0.85)
        } else {
            app.theme.border
        },
    })
}

/// Draw a previously-computed [`FooterRenderOutput`] into `area`.
///
/// Left groups are laid out left-to-right hugging the left edge (`a,b,c`) and
/// right groups are laid out right-to-left hugging the right edge (`x,y,z`),
/// following the lualine approach with no centred middle group. The whole
/// strip is painted with the trailing background first so gaps between groups
/// stay transparent-aware.
pub fn draw(f: &mut Frame, area: Rect, out: &FooterRenderOutput) {
    if area.width == 0 {
        return;
    }
    let left_w: u16 = out.left.iter().map(|g| g.width).sum();
    let right_w: u16 = out.right.iter().map(|g| g.width).sum();
    if left_w + right_w == 0 {
        return;
    }

    f.render_widget(
        Paragraph::new("").style(Style::default().bg(out.right_bg)),
        area,
    );

    let render_at = |f: &mut Frame, group: &FooterGroup, x: u16, w: u16| {
        f.render_widget(
            Paragraph::new(group.line.clone()).style(Style::default().bg(group.bg)),
            Rect {
                x,
                y: area.y,
                width: w,
                height: area.height,
            },
        );
    };

    if left_w + right_w > area.width {
        // Not enough room: render left groups first, then as many right groups
        // as fit, truncating overflow at the edges.
        let mut x = area.x;
        for group in &out.left {
            if x >= area.x + area.width {
                break;
            }
            let avail = area.x + area.width - x;
            render_at(f, group, x, group.width.min(avail));
            x += group.width;
            if x >= area.x + area.width {
                break;
            }
        }
        let mut x = area.x + area.width;
        for group in out.right.iter().rev() {
            if x <= area.x {
                break;
            }
            let avail = x - area.x;
            let w = group.width.min(avail);
            render_at(f, group, x - w, w);
            x -= group.width;
            if x <= area.x {
                break;
            }
        }
        return;
    }

    // Fit: left groups hug the left edge, right groups hug the right edge.
    let mut x = area.x;
    for group in &out.left {
        render_at(f, group, x, group.width);
        x += group.width;
    }
    let mut x = area.x + area.width;
    for group in out.right.iter().rev() {
        let w = group.width;
        render_at(f, group, x - w, w);
        x -= w;
    }
}

// ─── Module dispatch ───────────────────────────────────────────────────

/// Per-module accent colour used as the group background (brand-badge style).
fn module_color(m: FooterModule, theme: &crate::theme::AppTheme) -> Color {
    match m {
        FooterModule::Playback => theme.accent,
        FooterModule::Title => theme.secondary_accent,
        FooterModule::Volume => theme.tertiary_accent,
        FooterModule::Repeat => theme.accent,
        FooterModule::Shuffle => theme.tertiary_accent,
        FooterModule::Progress => theme.secondary_accent,
        FooterModule::Queue => theme.accent,
        FooterModule::KeyAction => theme.tertiary_accent,
        FooterModule::Backend => theme.secondary_accent,
        FooterModule::System => theme.accent,
        FooterModule::EqPreset => theme.secondary_accent,
        FooterModule::SleepTimer => theme.accent,
        FooterModule::Notification => theme.fg_bright,
        FooterModule::Time => theme.tertiary_accent,
    }
}

fn module_text(m: FooterModule, app: &App) -> Option<String> {
    match m {
        FooterModule::Playback => Some(render_playback(app)),
        FooterModule::Title => render_title(app),
        FooterModule::Volume => Some(render_volume(app)),
        FooterModule::Repeat => render_repeat(app),
        FooterModule::Shuffle => render_shuffle(app),
        FooterModule::Progress => render_progress(app),
        FooterModule::Queue => render_queue(app),
        FooterModule::KeyAction => render_keyaction(app),
        FooterModule::Backend => Some(render_backend(app)),
        FooterModule::System => Some(render_system()),
        FooterModule::EqPreset => render_eq_preset(app),
        FooterModule::SleepTimer => render_sleep_timer(app),
        FooterModule::Notification => render_footer_notification(app),
        FooterModule::Time => render_time(app),
    }
}

fn render_footer_notification(app: &App) -> Option<String> {
    let (msg, expires) = app.footer_notification.as_ref()?;
    if std::time::Instant::now() >= *expires {
        return None;
    }
    Some(msg.clone())
}

fn render_playback(app: &App) -> String {
    match app.state.status {
        PlaybackStatus::Playing => {
            if crate::ui::use_nerd_fonts() {
                "\u{f040a}".into()
            } else {
                "\u{25b6}".into()
            }
        }
        PlaybackStatus::Paused => {
            if crate::ui::use_nerd_fonts() {
                "\u{f03e4}".into()
            } else {
                "\u{23f8}".into()
            }
        }
        PlaybackStatus::Stopped => {
            if crate::ui::use_nerd_fonts() {
                "\u{f04db}".into()
            } else {
                "\u{25a0}".into()
            }
        }
    }
}

fn render_title(app: &App) -> Option<String> {
    let raw = app
        .state
        .current_track
        .as_ref()
        .map_or_else(String::new, |t| {
            // Show the track title with the artist concatenated after it.
            if t.artist.is_empty() {
                t.title.clone()
            } else {
                format!("{} \u{2013} {}", t.title, t.artist)
            }
        });
    if raw.is_empty() {
        return None;
    }
    const MAX: usize = 30;
    const SPEED: usize = 6;
    const HOLD_STEPS: usize = 50; // ~5s hold at 60fps / SPEED
    let char_count = raw.chars().count();
    if char_count > MAX {
        // Char-based modulo so multibyte UTF-8 never splits mid-sequence.
        // Scroll one full loop, then hold still before animating again.
        let chars: Vec<char> = raw.chars().collect();
        let step = app.footer_title_scroll / SPEED;
        let pos = step % (char_count + HOLD_STEPS);
        let offset = if pos < char_count { pos } else { 0 };
        let s: String = chars.iter().cycle().skip(offset).take(MAX).collect();
        Some(format!("{} \u{2026}", s))
    } else {
        Some(raw)
    }
}

fn render_volume(app: &App) -> String {
    if app.state.mute {
        "MUTE".into()
    } else {
        format!("{:>3}%", app.state.volume)
    }
}

fn render_repeat(app: &App) -> Option<String> {
    match app.state.repeat {
        gtm_core::state::RepeatMode::Off => None,
        gtm_core::state::RepeatMode::One => Some(if crate::ui::use_nerd_fonts() {
            "\u{f0458}".into()
        } else {
            "1".into()
        }),
        gtm_core::state::RepeatMode::All => Some(if crate::ui::use_nerd_fonts() {
            "\u{f0456}".into()
        } else {
            "A".into()
        }),
    }
}

fn render_shuffle(app: &App) -> Option<String> {
    if app.state.shuffle {
        Some(if crate::ui::use_nerd_fonts() {
            "\u{f049d}".into()
        } else {
            "S".into()
        })
    } else {
        None
    }
}

fn render_eq_preset(app: &App) -> Option<String> {
    if app.state.eq_enabled {
        let icon = if crate::ui::use_nerd_fonts() {
            "\u{f062e} "
        } else {
            "EQ:"
        };
        Some(format!("{icon}{}", app.state.eq_preset.label()))
    } else {
        None
    }
}

fn render_sleep_timer(app: &App) -> Option<String> {
    if let Some(secs) = app.state.sleep_timer {
        let m = secs / 60;
        let s = secs % 60;
        let icon = if crate::ui::use_nerd_fonts() {
            "\u{f04b2} "
        } else {
            "zzz "
        };
        Some(format!("{icon}{}:{:02}", m, s))
    } else {
        None
    }
}

fn render_progress(app: &App) -> Option<String> {
    let track = app.state.current_track.as_ref()?;
    let pos = app.display_position as u64;
    let dur = if app.state.duration > 0.0 {
        app.state.duration as u64
    } else {
        track.duration as u64
    };
    if dur == 0 {
        return None;
    }
    let ratio = (pos as f64 / dur as f64).clamp(0.0, 1.0);
    let time_str = format!("{} / {}", format_duration(pos), format_duration(dur));
    let bar_w = 16;
    let progress = crate::ui::Render::progress_variant(ratio, bar_w, app);
    Some(format!("{} {}", progress, time_str))
}

pub fn format_duration(secs: u64) -> String {
    let h = secs / 3600;
    let m = (secs % 3600) / 60;
    let s = secs % 60;
    if h > 0 {
        format!("{}:{:02}:{:02}", h, m, s)
    } else {
        format!("{}:{:02}", m, s)
    }
}

pub fn format_uptime(secs: f64) -> String {
    let total = secs as u64;
    let d = total / 86400;
    let h = (total % 86400) / 3600;
    let m = (total % 3600) / 60;
    let s = total % 60;
    if d > 0 {
        format!("{}d {}h {}m {}s", d, h, m, s)
    } else if h > 0 {
        format!("{}h {}m {}s", h, m, s)
    } else if m > 0 {
        format!("{}m {}s", m, s)
    } else {
        format!("{}s", s)
    }
}

fn render_queue(app: &App) -> Option<String> {
    let len = app.queue_cache.len();
    if len == 0 {
        return None;
    }
    let cursor = app.queue_cursor;
    Some(format!("{}/{}", cursor + 1, len))
}

fn render_keyaction(app: &App) -> Option<String> {
    if let Some((ref action, expires)) = app.last_action_name
        && std::time::Instant::now() < expires
    {
        return Some(format!("[{}]", action));
    }
    None
}

fn render_backend(app: &App) -> String {
    let name = app
        .health_report
        .as_ref()
        .and_then(|h| {
            h.components
                .iter()
                .find(|c| c.name == "audio_backend")
                .and_then(|c| c.message.as_deref())
        })
        .unwrap_or("unknown");
    name.to_string()
}

fn render_system() -> String {
    // Just the OS name; the detailed arch/memory/CPU breakdown lives in the
    // About window so the footer stays clean and cosmetic.
    std::env::consts::OS.to_string()
}

/// Render the current wall-clock time using the user's strftime-style format.
fn render_time(app: &App) -> Option<String> {
    if app.footer_time_format.is_empty() {
        return None;
    }
    Some(strftime(&app.footer_time_format))
}

/// Minimal strftime-style formatter covering the specifiers users realistically
/// put in a status bar. Unknown specifiers are passed through verbatim.
fn strftime(fmt: &str) -> String {
    use std::time::{SystemTime, UNIX_EPOCH};

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs() as i64;
    let days = now.div_euclid(86400);
    let secs_of_day = now.rem_euclid(86400);
    let mut year = 1970i64;
    let mut remaining = days;
    loop {
        let leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
        let dim = if leap { 366 } else { 365 };
        if remaining >= dim {
            remaining -= dim;
            year += 1;
        } else {
            break;
        }
    }
    let (mut month, mut day) = (1u32, 1u32);
    for (m, dims) in [
        (1u32, 31u32),
        (
            2,
            if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 {
                29
            } else {
                28
            },
        ),
        (3, 31),
        (4, 30),
        (5, 31),
        (6, 30),
        (7, 31),
        (8, 31),
        (9, 30),
        (10, 31),
        (11, 30),
        (12, 31),
    ] {
        if remaining >= dims as i64 {
            remaining -= dims as i64;
        } else {
            month = m;
            day = (remaining + 1) as u32;
            break;
        }
    }
    let hour = secs_of_day / 3600;
    let minute = (secs_of_day % 3600) / 60;
    let second = secs_of_day % 60;

    let weekday = ((days + 4).rem_euclid(7)) as usize; // 1970-01-01 was a Thursday
    const WEEKDAY_NAMES: [&str; 7] = [
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
    ];
    const WEEKDAY_SHORT: [&str; 7] = ["Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed"];
    const MONTH_NAMES: [&str; 12] = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    ];
    const MONTH_SHORT: [&str; 12] = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    ];

    let mut out = String::new();
    let mut chars = fmt.chars();
    while let Some(c) = chars.next() {
        if c != '%' {
            out.push(c);
            continue;
        }
        match chars.next() {
            Some('H') => out.push_str(&format!("{:02}", hour)),
            Some('I') => {
                let h12 = if hour % 12 == 0 { 12 } else { hour % 12 };
                out.push_str(&format!("{:02}", h12));
            }
            Some('M') => out.push_str(&format!("{:02}", minute)),
            Some('S') => out.push_str(&format!("{:02}", second)),
            Some('p') => out.push_str(if hour < 12 { "AM" } else { "PM" }),
            Some('P') => out.push_str(if hour < 12 { "am" } else { "pm" }),
            Some('Y') => out.push_str(&format!("{:04}", year)),
            Some('y') => out.push_str(&format!("{:02}", year % 100)),
            Some('m') => out.push_str(&format!("{:02}", month)),
            Some('d') => out.push_str(&format!("{:02}", day)),
            Some('e') => out.push_str(&format!("{:2}", day)),
            Some('b') => out.push_str(MONTH_SHORT[(month - 1) as usize]),
            Some('B') => out.push_str(MONTH_NAMES[(month - 1) as usize]),
            Some('a') => out.push_str(WEEKDAY_SHORT[weekday]),
            Some('A') => out.push_str(WEEKDAY_NAMES[weekday]),
            Some('j') => {
                // Day of year (1-366).
                let doy = crate::footer::day_of_year(year, month, day);
                out.push_str(&format!("{:03}", doy));
            }
            Some('%') => out.push('%'),
            Some(other) => {
                out.push('%');
                out.push(other);
            }
            None => out.push('%'),
        }
    }
    out
}

fn day_of_year(year: i64, month: u32, day: u32) -> i64 {
    const CUM: [i64; 12] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
    let leap_adj = if month > 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        1
    } else {
        0
    };
    CUM[(month - 1) as usize] + day as i64 + leap_adj
}

pub(crate) fn read_process_memory_kb() -> Option<u64> {
    let status = std::fs::read_to_string("/proc/self/status").ok()?;
    for line in status.lines() {
        if let Some(rest) = line.strip_prefix("VmRSS:") {
            let kb: u64 = rest.split_whitespace().next()?.parse().ok()?;
            return Some(kb);
        }
    }
    None
}

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

    #[test]
    fn format_duration_hours() {
        assert_eq!(format_duration(3661), "1:01:01");
    }

    #[test]
    fn format_duration_minutes() {
        assert_eq!(format_duration(125), "2:05");
    }

    #[test]
    fn module_round_trip() {
        for m in [
            FooterModule::Playback,
            FooterModule::Title,
            FooterModule::Volume,
            FooterModule::Repeat,
            FooterModule::Shuffle,
            FooterModule::Progress,
            FooterModule::Queue,
            FooterModule::KeyAction,
            FooterModule::Backend,
            FooterModule::System,
            FooterModule::EqPreset,
            FooterModule::SleepTimer,
            FooterModule::Notification,
        ] {
            let s = m.as_str();
            assert_eq!(FooterModule::from_str_lossy(s), Some(m));
        }
        assert!(FooterModule::from_str_lossy("NoSuchModule").is_none());
    }

    #[test]
    fn presets_have_unique_names() {
        crate::theme::assert_unique_names(presets().iter().map(|p| p.name.as_ref()), "preset");
    }

    #[test]
    fn parse_module_list_drops_unknowns() {
        let names = vec!["Playback".into(), "Bogus".into(), "Volume".into()];
        let parsed = parse_module_list(&names);
        assert_eq!(parsed, vec![FooterModule::Playback, FooterModule::Volume]);
    }

    #[test]
    fn user_presets_round_trip() {
        let toml_text = r#"
            [[preset]]
            name = "Custom"
            left = ["Playback", "Queue"]
            middle = ["KeyAction"]
            right = ["Volume"]
        "#;
        let parsed: UserPresetsFile = toml::from_str(toml_text).unwrap();
        assert_eq!(parsed.preset.len(), 1);
        let preset = &parsed.preset[0];
        assert_eq!(preset.name, "Custom");
        assert_eq!(preset.left, vec!["Playback", "Queue"]);
        let built = parse_module_list(&preset.left);
        assert_eq!(built, vec![FooterModule::Playback, FooterModule::Queue]);
    }
}