ommp 0.1.3

Oh My Music Player — a terminal music player built with ratatui
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
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::widgets::{Block, Borders, Clear};
use ratatui::Frame;

use crate::ui::theme::Theme;
use crate::ui::text::centered_rect;

// Neon colors
const NEON_PINK: Color = Color::Rgb(255, 50, 150);
const NEON_CYAN: Color = Color::Rgb(0, 255, 255);
const BG_DARK: Color = Color::Rgb(10, 5, 30);

// Logo gradient endpoints: hot pink -> cyan (smooth per-character interpolation)
const LOGO_START: (f32, f32, f32) = (255.0, 50.0, 150.0); // hot pink
const LOGO_END: (f32, f32, f32) = (0.0, 255.0, 255.0);    // cyan

// Dim background scatter colors
const DIM_PURPLE: Color = Color::Rgb(60, 20, 80);
const DIM_CYAN: Color = Color::Rgb(20, 60, 80);
const DIM_PINK: Color = Color::Rgb(80, 20, 50);

const NOTES: [char; 4] = ['', '', '', ''];
const DIM_COLORS: [Color; 3] = [DIM_PURPLE, DIM_CYAN, DIM_PINK];

// Waveform pattern for below-logo decoration
const WAVEFORM: &str = "▁▂▃▅▇█▇▅▃▂▁▂▃▅▇█▇▅▃▂▁";

// Unicode block-art logo — each row is 35 display columns
const LOGO: [&str; 5] = [
    " ██████  ██    ██ ██    ██ ██████  ",
    "██    ██ ███  ███ ███  ███ ██   ██ ",
    "██    ██ ██ ██ ██ ██ ██ ██ ██████  ",
    "██    ██ ██    ██ ██    ██ ██      ",
    " ██████  ██    ██ ██    ██ ██      ",
];
const LOGO_DISPLAY_W: u16 = 35;

/// Shown at the bottom of the splash so the skip is discoverable.
const SKIP_HINT: &str = "Press any key to skip";

/// Fill area with dark background + scattered music notes
fn render_background(buf: &mut Buffer, area: Rect) {
    let bg = Style::default().bg(BG_DARK).fg(BG_DARK);
    for y in area.y..area.y + area.height {
        for x in area.x..area.x + area.width {
            buf.set_string(x, y, " ", bg);
        }
    }

    for y in area.y..area.y + area.height {
        for x in area.x..area.x + area.width {
            let hash = (x as u32).wrapping_mul(7).wrapping_add((y as u32).wrapping_mul(13));
            if hash.is_multiple_of(37) {
                let note = NOTES[(hash as usize / 37) % NOTES.len()];
                let color = DIM_COLORS[(hash as usize / 37 + 1) % DIM_COLORS.len()];
                buf.set_string(x, y, note.to_string(), Style::default().fg(color).bg(BG_DARK));
            }
        }
    }
}

/// Render waveform with cyan->purple gradient, centered at given y
fn render_waveform(buf: &mut Buffer, area_x: u16, area_w: u16, y: u16, max_y: u16) {
    if y >= max_y {
        return;
    }
    let chars: Vec<char> = WAVEFORM.chars().collect();
    let total_w = chars.len() as u16;
    let start_x = area_x + area_w.saturating_sub(total_w) / 2;

    for (i, ch) in chars.iter().enumerate() {
        let x = start_x + i as u16;
        if x >= area_x && x < area_x + area_w {
            let frac = i as f32 / chars.len().max(1) as f32;
            let r = (frac * 160.0) as u8;
            let g = (255.0 - frac * 195.0) as u8;
            let b = (255.0 - frac * 15.0) as u8;
            buf.set_string(
                x, y, ch.to_string(),
                Style::default().fg(Color::Rgb(r, g, b)).bg(BG_DARK),
            );
        }
    }
}

/// Lerp a foreground color toward BG_DARK by opacity (0.0 = invisible, 1.0 = full color)
fn fade_color(target: Color, opacity: f32) -> Color {
    match target {
        Color::Rgb(r, g, b) => {
            let (br, bg, bb) = (10.0_f32, 5.0_f32, 30.0_f32);
            Color::Rgb(
                (br + (r as f32 - br) * opacity) as u8,
                (bg + (g as f32 - bg) * opacity) as u8,
                (bb + (b as f32 - bb) * opacity) as u8,
            )
        }
        other => other,
    }
}

/// Helper: center a string of `display_w` columns within `area_w`, returning x offset from area.x
fn center_x(area_x: u16, area_w: u16, display_w: u16) -> u16 {
    area_x + area_w.saturating_sub(display_w) / 2
}

pub fn render_about_modal(frame: &mut Frame, area: Rect, _theme: &Theme) {
    let modal = centered_rect(50, 60, area);
    frame.render_widget(Clear, modal);

    // Background with scattered music notes on entire modal
    render_background(frame.buffer_mut(), modal);

    // Modal border
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(NEON_PINK).bg(BG_DARK))
        .title(" ♪ OMMP ♪ ")
        .title_style(
            Style::default()
                .fg(NEON_PINK)
                .bg(BG_DARK)
                .add_modifier(Modifier::BOLD),
        );

    let inner = block.inner(modal);
    frame.render_widget(block, modal);

    // Re-render background inside inner area (border rendering overwrites)
    render_background(frame.buffer_mut(), inner);

    // === Calculate total content height for vertical centering ===
    // Logo(5) + waveform(1) + 1 + subtitle(1) + tagline(1) + 1 + divider(1) + 1 + info(2) + 1 + links(2) + 1 + divider(1) + 1 + footer(1) = 21
    let content_h: u16 = 21;
    let top_pad = inner.height.saturating_sub(content_h) / 2;
    let mut cur_y = inner.y + top_pad;

    let buf = frame.buffer_mut();

    // --- Logo with smooth horizontal gradient (hot pink -> cyan, per-character RGB lerp) ---
    for (row_idx, row) in LOGO.iter().enumerate() {
        if cur_y < inner.y + inner.height {
            let logo_x = center_x(inner.x, inner.width, LOGO_DISPLAY_W);
            for (col, ch) in row.chars().enumerate() {
                let t = (col as f32 + row_idx as f32 * 3.0)
                    / (LOGO_DISPLAY_W as f32 + 4.0 * 3.0);
                let t = t.clamp(0.0, 1.0);
                let r = (LOGO_START.0 + (LOGO_END.0 - LOGO_START.0) * t) as u8;
                let g = (LOGO_START.1 + (LOGO_END.1 - LOGO_START.1) * t) as u8;
                let b = (LOGO_START.2 + (LOGO_END.2 - LOGO_START.2) * t) as u8;
                let style = Style::default()
                    .fg(Color::Rgb(r, g, b))
                    .bg(BG_DARK)
                    .add_modifier(Modifier::BOLD);
                buf.set_string(logo_x + col as u16, cur_y, ch.to_string(), style);
            }
        }
        cur_y += 1;
    }

    // --- Waveform below logo ---
    render_waveform(buf, inner.x, inner.width, cur_y, inner.y + inner.height);
    cur_y += 2;

    // --- Subtitle ---
    let subtitle = "Oh My Music Player";
    let sub_w = subtitle.len() as u16;
    if cur_y < inner.y + inner.height {
        let x = center_x(inner.x, inner.width, sub_w);
        buf.set_string(
            x, cur_y, subtitle,
            Style::default()
                .fg(NEON_CYAN)
                .bg(BG_DARK)
                .add_modifier(Modifier::BOLD | Modifier::ITALIC),
        );
    }
    cur_y += 1;

    // --- Tagline ---
    let tagline = "Terminal music, your way";
    let tag_w = tagline.len() as u16;
    if cur_y < inner.y + inner.height {
        let x = center_x(inner.x, inner.width, tag_w);
        buf.set_string(x, cur_y, tagline, Style::default().fg(NEON_CYAN).bg(BG_DARK));
    }
    cur_y += 2;

    // --- Divider ---
    let div_w = inner.width.saturating_sub(6) as usize;
    let div_style = Style::default().fg(DIM_PURPLE).bg(BG_DARK);
    if cur_y < inner.y + inner.height {
        buf.set_string(inner.x + 3, cur_y, "".repeat(div_w), div_style);
    }
    cur_y += 2;

    // --- Info rows ---
    let label_style = Style::default()
        .fg(NEON_PINK)
        .bg(BG_DARK)
        .add_modifier(Modifier::BOLD);
    let value_style = Style::default().fg(NEON_CYAN).bg(BG_DARK);

    // Read from Cargo.toml rather than a literal, so the About box cannot claim
    // a version the build is not.
    for (label, value) in &[("Version", env!("CARGO_PKG_VERSION")), ("License", "Apache-2.0")] {
        if cur_y < inner.y + inner.height {
            let lbl = format!("  {:12}", label);
            let val = *value;
            let total_w = (lbl.len() + val.len()) as u16;
            let x = center_x(inner.x, inner.width, total_w);
            buf.set_string(x, cur_y, &lbl, label_style);
            buf.set_string(x + lbl.len() as u16, cur_y, val, value_style);
        }
        cur_y += 1;
    }
    cur_y += 1;

    // --- Links ---
    let link_style = Style::default()
        .fg(Color::Rgb(100, 180, 255))
        .bg(BG_DARK)
        .add_modifier(Modifier::UNDERLINED);
    let icon_style = Style::default()
        .fg(NEON_PINK)
        .bg(BG_DARK)
        .add_modifier(Modifier::BOLD);

    for (icon, url) in &[
        ("\u{F09B} ", "https://github.com/sqzer-x/ommp"),
        ("\u{F004} ", "https://github.com/sponsors/sqzer-x"),
    ] {
        if cur_y < inner.y + inner.height {
            let icon_w = icon.chars().count() as u16;
            let url_w = url.len() as u16;
            let total_w = icon_w + url_w;
            let x = center_x(inner.x, inner.width, total_w);
            buf.set_string(x, cur_y, *icon, icon_style);
            buf.set_string(x + icon_w, cur_y, *url, link_style);
        }
        cur_y += 1;
    }
    cur_y += 1;

    // --- Divider ---
    if cur_y < inner.y + inner.height {
        buf.set_string(inner.x + 3, cur_y, "".repeat(div_w), div_style);
    }
    cur_y += 2;

    // --- Footer ---
    let footer = "g: Open GitHub  s: Open Sponsor  Esc: Close";
    let fw = footer.len() as u16;
    if cur_y < inner.y + inner.height {
        let x = center_x(inner.x, inner.width, fw);
        buf.set_string(
            x, cur_y, footer,
            Style::default().fg(Color::Rgb(120, 120, 140)).bg(BG_DARK),
        );
    }
}

/// Render a full-screen splash screen with fade-in/fade-out.
/// `opacity`: 0.0 = fully transparent (BG_DARK only), 1.0 = full brightness.
pub fn render_splash_screen(frame: &mut Frame, area: Rect, _theme: &Theme, opacity: f32) {
    let opacity = opacity.clamp(0.0, 1.0);
    let buf = frame.buffer_mut();

    // Fill entire screen with dark background
    let bg = Style::default().bg(BG_DARK).fg(BG_DARK);
    for y in area.y..area.y + area.height {
        for x in area.x..area.x + area.width {
            buf.set_string(x, y, " ", bg);
        }
    }

    // Scattered music notes (faded)
    for y in area.y..area.y + area.height {
        for x in area.x..area.x + area.width {
            let hash = (x as u32).wrapping_mul(7).wrapping_add((y as u32).wrapping_mul(13));
            if hash.is_multiple_of(37) {
                let note = NOTES[(hash as usize / 37) % NOTES.len()];
                let color = DIM_COLORS[(hash as usize / 37 + 1) % DIM_COLORS.len()];
                buf.set_string(
                    x, y, note.to_string(),
                    Style::default().fg(fade_color(color, opacity)).bg(BG_DARK),
                );
            }
        }
    }

    // Content height: logo(5) + waveform(1) + gap(1) + subtitle(1) + tagline(1) = 9
    let content_h: u16 = 9;
    let top_pad = area.height.saturating_sub(content_h) / 2;
    let mut cur_y = area.y + top_pad;

    // --- Logo with smooth horizontal gradient (faded) ---
    for (row_idx, row) in LOGO.iter().enumerate() {
        if cur_y < area.y + area.height {
            let logo_x = center_x(area.x, area.width, LOGO_DISPLAY_W);
            for (col, ch) in row.chars().enumerate() {
                let t = (col as f32 + row_idx as f32 * 3.0)
                    / (LOGO_DISPLAY_W as f32 + 4.0 * 3.0);
                let t = t.clamp(0.0, 1.0);
                let r = (LOGO_START.0 + (LOGO_END.0 - LOGO_START.0) * t) as u8;
                let g = (LOGO_START.1 + (LOGO_END.1 - LOGO_START.1) * t) as u8;
                let b = (LOGO_START.2 + (LOGO_END.2 - LOGO_START.2) * t) as u8;
                let style = Style::default()
                    .fg(fade_color(Color::Rgb(r, g, b), opacity))
                    .bg(BG_DARK)
                    .add_modifier(Modifier::BOLD);
                buf.set_string(logo_x + col as u16, cur_y, ch.to_string(), style);
            }
        }
        cur_y += 1;
    }

    // --- Waveform below logo (faded) ---
    if cur_y < area.y + area.height {
        let chars: Vec<char> = WAVEFORM.chars().collect();
        let total_w = chars.len() as u16;
        let start_x = area.x + area.width.saturating_sub(total_w) / 2;
        for (i, ch) in chars.iter().enumerate() {
            let x = start_x + i as u16;
            if x >= area.x && x < area.x + area.width {
                let frac = i as f32 / chars.len().max(1) as f32;
                let r = (frac * 160.0) as u8;
                let g = (255.0 - frac * 195.0) as u8;
                let b = (255.0 - frac * 15.0) as u8;
                buf.set_string(
                    x, cur_y, ch.to_string(),
                    Style::default().fg(fade_color(Color::Rgb(r, g, b), opacity)).bg(BG_DARK),
                );
            }
        }
    }
    cur_y += 2;

    // --- Subtitle (faded) ---
    let subtitle = "Oh My Music Player";
    let sub_w = subtitle.len() as u16;
    if cur_y < area.y + area.height {
        let x = center_x(area.x, area.width, sub_w);
        buf.set_string(
            x, cur_y, subtitle,
            Style::default()
                .fg(fade_color(NEON_CYAN, opacity))
                .bg(BG_DARK)
                .add_modifier(Modifier::BOLD | Modifier::ITALIC),
        );
    }
    cur_y += 1;

    // --- Tagline (faded) ---
    let tagline = "Terminal music, your way";
    if cur_y < area.y + area.height {
        let x = center_x(area.x, area.width, tagline.len() as u16);
        buf.set_string(
            x, cur_y, tagline,
            Style::default().fg(fade_color(NEON_CYAN, opacity)).bg(BG_DARK),
        );
    }

    // --- Skip hint, pinned to the bottom ---
    if area.height > content_h + 2 {
        let y = area.y + area.height - 2;
        let x = center_x(area.x, area.width, SKIP_HINT.len() as u16);
        buf.set_string(
            x, y, SKIP_HINT,
            // Dimmer than the rest so it reads as a footnote, and it fades with
            // everything else rather than popping in at full strength.
            Style::default()
                .fg(fade_color(Color::Rgb(120, 130, 150), opacity))
                .bg(BG_DARK)
                .add_modifier(Modifier::ITALIC),
        );
    }
}


#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::backend::TestBackend;
    use ratatui::Terminal;

    fn rows(w: u16, h: u16) -> Vec<String> {
        let mut terminal = Terminal::new(TestBackend::new(w, h)).unwrap();
        terminal
            .draw(|f| render_splash_screen(f, f.area(), &Theme::default(), 1.0))
            .unwrap();
        let buf = terminal.backend().buffer();
        (0..h)
            .map(|y| {
                (0..w)
                    .map(|x| buf.cell((x, y)).map(|c| c.symbol()).unwrap_or(" "))
                    .collect::<String>()
            })
            .collect()
    }

    #[test]
    fn the_skip_hint_sits_below_the_logo_without_covering_it() {
        let rows = rows(80, 24);
        let hint = rows
            .iter()
            .position(|r| r.contains(SKIP_HINT))
            .expect("hint is drawn");
        let tagline = rows
            .iter()
            .position(|r| r.contains("Terminal music, your way"))
            .expect("tagline is drawn");
        assert!(hint > tagline, "hint must not land on the centred block");
        assert!(hint < rows.len(), "hint stays on screen");
    }

    #[test]
    fn a_terminal_too_short_for_the_hint_drops_it_rather_than_overlapping() {
        // Nine rows of content plus the hint's own two do not fit in eleven.
        assert!(!rows(60, 11).iter().any(|r| r.contains(SKIP_HINT)));
        assert!(rows(60, 12).iter().any(|r| r.contains(SKIP_HINT)));
    }
}