gtm 0.1.7

GTM music player client - TUI and CLI modes
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
// Copyright (c) 2025 - present
// Author: prjctimg <prjctimg@outlook.com>
// Footer bar: system info, keybindings, and playback status.
//
// This is free software released under the GPL-3.0 license.

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

use gtm_core::state::PlaybackStatus;

use crate::app::App;

/// Darken an RGB color by the given factor (0.0 = black, 1.0 = unchanged).
/// Used to create readable background colors from bright accent colors.
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,
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FooterModule {
    Playback,
    Title,
    Volume,
    Repeat,
    Shuffle,
    Progress,
    Queue,
    KeyAction,
    Backend,
    System,
    Device,
    EqPreset,
    SleepTimer,
}

impl FooterModule {
    /// Stable display name; also used to parse user presets from TOML.
    #[allow(dead_code)]
    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::Device => "Device",
            FooterModule::EqPreset => "EqPreset",
            FooterModule::SleepTimer => "SleepTimer",
        }
    }

    /// 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,
            "Device" => FooterModule::Device,
            "EqPreset" => FooterModule::EqPreset,
            "SleepTimer" => FooterModule::SleepTimer,
            _ => return None,
        })
    }
}

/// A footer layout preset. Unlike the previous a/b/c/x/y/z slots (which were
/// silently re-flattened into 3 groups), `left`/`middle`/`right` map directly
/// to the on-screen groups.
#[derive(Debug, Clone)]
pub struct FooterPreset {
    pub name: Cow<'static, str>,
    pub left: Vec<FooterModule>,
    pub middle: Vec<FooterModule>,
    pub right: Vec<FooterModule>,
}

/// Built-in presets inspired by lualine.nvim groups.
pub fn presets() -> Vec<FooterPreset> {
    vec![
        FooterPreset {
            name: Cow::Borrowed("Default"),
            left: vec![
                FooterModule::Playback,
                FooterModule::Queue,
                FooterModule::Repeat,
                FooterModule::Shuffle,
                FooterModule::Volume,
                FooterModule::EqPreset,
            ],
            middle: vec![FooterModule::KeyAction, FooterModule::SleepTimer],
            right: vec![],
        },
        // Bare minimum for termux or very small viewports.
        FooterPreset {
            name: Cow::Borrowed("Minimal"),
            left: vec![FooterModule::Playback, FooterModule::EqPreset],
            middle: vec![FooterModule::KeyAction, FooterModule::SleepTimer],
            right: vec![],
        },
        FooterPreset {
            name: Cow::Borrowed("Full"),
            left: vec![
                FooterModule::Playback,
                FooterModule::Title,
                FooterModule::Volume,
                FooterModule::Repeat,
                FooterModule::Shuffle,
                FooterModule::EqPreset,
                FooterModule::Progress,
            ],
            middle: vec![FooterModule::KeyAction, FooterModule::SleepTimer],
            right: vec![],
        },
    ]
}

// ─── 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)]
    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),
            middle: parse_module_list(&p.middle),
            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: per-group lines plus the background
/// used for the unfilled trailing area on the right edge.
#[derive(Clone)]
pub struct FooterRenderOutput {
    pub groups: 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 a list of group widgets plus the
/// trailing-area background. Returns `None` when every group 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 is_playing = app.state.status == PlaybackStatus::Playing;

    // Each footer section gets a distinct background from the theme's accent
    // colors, darkened for readability.  Left = accent (playing) or
    // secondary_accent (paused); Middle = secondary_accent; Right = tertiary_accent.
    let left_bg = if is_playing {
        darken(app.theme.accent, 0.25)
    } else {
        darken(app.theme.secondary_accent, 0.25)
    };
    let middle_bg = darken(app.theme.secondary_accent, 0.20);
    let right_bg = darken(app.theme.tertiary_accent, 0.20);

    let slots: [(&[FooterModule], Color); 3] = [
        (&preset.left, left_bg),
        (&preset.middle, middle_bg),
        (&preset.right, right_bg),
    ];

    let mut out_groups: Vec<FooterGroup> = Vec::new();
    for (modules, bg) in &slots {
        let parts = render_modules(modules, app);
        if parts.is_empty() {
            continue;
        }
        let mut spans: Vec<Span<'static>> = Vec::with_capacity(parts.len() * 2 + 2);
        spans.push(Span::raw(" "));
        let mut width: u32 = 1;
        for (j, (text, color)) in parts.iter().enumerate() {
            if j > 0 {
                spans.push(Span::raw(" "));
                width += 1;
            }
            let fg = crate::ui::readable_fg(*color, *bg);
            let span = Span::styled(text.clone(), Style::default().fg(fg));
            width += span.width() as u32;
            spans.push(span);
        }
        spans.push(Span::raw(" "));
        width += 1;
        out_groups.push(FooterGroup {
            line: Line::from(spans),
            bg: *bg,
            width: width as u16,
        });
    }

    if out_groups.is_empty() {
        return None;
    }
    Some(FooterRenderOutput {
        groups: out_groups,
        right_bg: app.theme.border,
    })
}

/// Draw a previously-computed [`FooterRenderOutput`] into `area`.
pub fn draw(f: &mut Frame, area: Rect, out: &FooterRenderOutput) {
    let total: u16 = out.groups.iter().map(|g| g.width).sum();
    if total == 0 {
        return;
    }
    let mut constraints: Vec<Constraint> = out
        .groups
        .iter()
        .map(|g| Constraint::Length(g.width))
        .collect();
    constraints.push(Constraint::Min(0));
    let chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints(constraints)
        .split(area);

    for (i, group) in out.groups.iter().enumerate() {
        if chunks[i].x >= area.x + area.width || chunks[i].width == 0 {
            break;
        }
        f.render_widget(
            Paragraph::new(group.line.clone()).style(Style::default().bg(group.bg)),
            chunks[i],
        );
    }
    // Fill remaining space on the right edge with the trailing background.
    if let Some(trail) = chunks.last() {
        if trail.x < area.x + area.width && trail.width > 0 {
            f.render_widget(
                Paragraph::new("").style(Style::default().bg(out.right_bg)),
                *trail,
            );
        }
    }
}

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

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.fg_dim,
        FooterModule::KeyAction => theme.tertiary_accent,
        FooterModule::Backend => theme.fg_dim,
        FooterModule::System => theme.fg_dim,
        FooterModule::Device => theme.fg_dim,
        FooterModule::EqPreset => theme.secondary_accent,
        FooterModule::SleepTimer => theme.accent,
    }
}

fn render_modules(modules: &[FooterModule], app: &App) -> Vec<(String, Color)> {
    let mut parts = Vec::new();
    for m in modules {
        if let Some(text) = module_text(*m, app) {
            if !text.is_empty() {
                parts.push((text, module_color(*m, &app.theme)));
            }
        }
    }
    parts
}

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()),
        FooterModule::System => Some(render_system()),
        FooterModule::Device => render_device(app),
        FooterModule::EqPreset => render_eq_preset(app),
        FooterModule::SleepTimer => render_sleep_timer(app),
    }
}

fn render_playback(app: &App) -> String {
    match app.state.status {
        PlaybackStatus::Playing => "\u{25b6}".into(),
        PlaybackStatus::Paused => "\u{23f8}".into(),
        PlaybackStatus::Stopped => "\u{25a0}".into(),
    }
}

fn render_title(app: &App) -> Option<String> {
    let raw = app
        .state
        .current_track
        .as_ref()
        .map_or_else(String::new, |t| {
            if t.artist.is_empty() {
                t.title.clone()
            } else {
                format!("{} \u{2013} {}", t.artist, t.title)
            }
        });
    if raw.is_empty() {
        return None;
    }
    const MAX: usize = 30;
    let char_count = raw.chars().count();
    if char_count > MAX {
        // Char-based modulo so multibyte UTF-8 never splits mid-sequence.
        let chars: Vec<char> = raw.chars().collect();
        let offset = app.footer_title_scroll % char_count;
        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("1".into()),
        gtm_core::state::RepeatMode::All => Some("A".into()),
    }
}

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

fn render_eq_preset(app: &App) -> Option<String> {
    if app.state.eq_enabled {
        Some(format!("EQ:{}", 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;
        Some(format!("zzz {}:{: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 = 14usize;
    let progress = crate::ui::render_progress_variant(ratio, bar_w, app);
    Some(format!("{} {}", progress, time_str))
}

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)
    }
}

fn render_queue(app: &App) -> Option<String> {
    let len = app.queue_cache.len();
    if len == 0 {
        return None;
    }
    let cursor = app.queue_cursor;
    let next = if cursor + 1 < len {
        format!(
            " \u{2192} {}",
            app.queue_cache[cursor + 1]
                .title
                .chars()
                .take(20)
                .collect::<String>()
        )
    } else {
        String::new()
    };
    Some(format!("[{}/{}]{}", cursor + 1, len, next))
}

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

fn render_backend() -> String {
    let rust_ver = option_env!("VERGEN_RUSTC_SEMVER").unwrap_or("?");
    let crate_ver = option_env!("CARGO_PKG_VERSION").unwrap_or("?");
    format!("rust {} \u{2022} v{}", rust_ver, crate_ver)
}

fn render_system() -> String {
    let os = std::env::consts::OS;
    let arch = std::env::consts::ARCH;
    let cpus = std::thread::available_parallelism()
        .map(|n| n.get())
        .unwrap_or(1);
    let mem = read_process_memory_kb();
    if let Some(kb) = mem {
        if kb > 1024 * 1024 {
            format!(
                "{} {} \u{2022} {}GB {}CPU",
                os,
                arch,
                kb / (1024 * 1024),
                cpus
            )
        } else {
            format!("{} {} \u{2022} {}MB {}CPU", os, arch, kb / 1024, cpus)
        }
    } else {
        format!("{} {} \u{2022} {}CPU", os, arch, cpus)
    }
}

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
}

fn render_device(app: &App) -> Option<String> {
    let track_count = app.tracks_cache.len();
    if track_count == 0 {
        return None;
    }
    let total_dur: u64 = app.tracks_cache.iter().map(|t| t.duration as u64).sum();
    let hours = total_dur / 3600;
    let mins = (total_dur % 3600) / 60;
    Some(format!(
        "{} tracks \u{2022} {}h{}m",
        track_count, hours, mins
    ))
}

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

    #[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::Device,
            FooterModule::EqPreset,
            FooterModule::SleepTimer,
        ] {
            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() {
        let all = presets();
        let names: Vec<&str> = all.iter().map(|p| p.name.as_ref()).collect();
        let mut sorted = names.clone();
        sorted.sort();
        sorted.dedup();
        assert_eq!(sorted.len(), names.len());
    }

    #[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]);
    }

    #[test]
    fn render_title_handles_multibyte_without_panic() {
        // Construct an App-like minimal context isn't trivial, so just verify
        // the char-based scroll math: a 40-char title scrolls at offsets that
        // are valid character boundaries.
        let raw = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 26 fullwidth chars
        let chars: Vec<char> = raw.chars().collect();
        let n = chars.len();
        let max = 10;
        let mut produced = Vec::new();
        for offset in 0..n {
            let s: String = chars.iter().cycle().skip(offset).take(max).collect();
            produced.push(s);
        }
        assert_eq!(produced.len(), n);
        // Every produced string is exactly `max` chars wide (fullwidth chars
        // advance one usize each in `chars().take`).
        for s in &produced {
            assert_eq!(s.chars().count(), max);
        }
    }
}