pulsedeck 0.1.6

A cyber-synthwave internet radio player and smart tape recorder for your terminal
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
use super::theme;
use crate::app::{App, LayoutMode, PlaybackState, RecordingState};
use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders, Paragraph};

const CASSETTE_INNER_WIDTH: usize = 44;
#[cfg(test)]
const CASSETTE_REEL_CELL_WIDTH: usize = 10;
const CASSETTE_TAPE_WIDTH: usize = 4;
const CASSETTE_HEIGHT: u16 = 9;

pub fn render(frame: &mut Frame, area: Rect, app: &App) {
    let title_text = match app.active_deck_page {
        0 => " 📼 Tape Deck ",
        _ => " 📼 Tape History ",
    };

    // Outer block with desaturated deep purple border and custom retro neon title
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(theme::border())
        .border_type(ratatui::widgets::BorderType::Rounded)
        .title(Span::styled(title_text, theme::title()));

    let inner_area = block.inner(area);
    frame.render_widget(block, area);

    match app.active_deck_page {
        0 => {
            let full_deck = app.layout_mode == LayoutMode::RightOnly;

            // Split the inner area vertically: stable cassette, status strip, framed visualizer.
            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(CASSETTE_HEIGHT),
                    Constraint::Length(5),
                    Constraint::Min(0),
                ])
                .split(inner_area);

            render_cassette(frame, chunks[0], app);
            render_meta_details(frame, chunks[1], app, full_deck);
            render_oscilloscope(frame, chunks[2], app);
        }
        _ => {
            render_history(frame, inner_area, app);
        }
    }
}

fn render_cassette(frame: &mut Frame, area: Rect, app: &App) {
    let lines = build_cassette_lines(
        CASSETTE_INNER_WIDTH,
        app.tick_count,
        &app.playback,
        app.recording_state,
    );

    let paragraph = Paragraph::new(lines).alignment(Alignment::Center);
    frame.render_widget(paragraph, area);
}

#[derive(Debug, Clone)]
struct CassetteSegment {
    text: String,
    style: Style,
}

fn build_cassette_lines(
    inner_width: usize,
    tick_count: u64,
    playback: &PlaybackState,
    recording_state: RecordingState,
) -> Vec<Line<'static>> {
    let shell_style = theme::dim();
    let reel_style = Style::default()
        .fg(theme::highlight())
        .bg(theme::bg())
        .add_modifier(Modifier::BOLD);

    let mut lines = Vec::with_capacity(CASSETTE_HEIGHT as usize);
    lines.push(cassette_border_line("", "", inner_width, shell_style));
    lines.push(cassette_label_line(
        inner_width,
        tick_count,
        recording_state,
        shell_style,
    ));
    lines.push(shell_text_line(
        "        ────────────────────────────        ",
        inner_width,
        shell_style,
        shell_style,
    ));
    lines.push(shell_text_line(
        "  ╭────────╮        ════        ╭────────╮  ",
        inner_width,
        shell_style,
        shell_style,
    ));

    let (left_reel, right_reel) = reel_cells_for_state(tick_count, playback);
    lines.push(shell_line(
        inner_width,
        shell_style,
        vec![
            segment("  ", shell_style),
            segment(left_reel, reel_style),
            segment("  ────────────────  ", shell_style),
            segment(right_reel, reel_style),
            segment("  ", shell_style),
        ],
    ));

    lines.push(shell_text_line(
        "  ╰────────╯                    ╰────────╯  ",
        inner_width,
        shell_style,
        shell_style,
    ));
    lines.push(shell_text_line(
        "                                            ",
        inner_width,
        shell_style,
        shell_style,
    ));
    lines.push(shell_text_line(
        "       ╲____________________________╱       ",
        inner_width,
        shell_style,
        shell_style,
    ));
    lines.push(cassette_border_line("", "", inner_width, shell_style));

    lines
}

fn cassette_label_line(
    inner_width: usize,
    tick_count: u64,
    recording_state: RecordingState,
    shell_style: Style,
) -> Line<'static> {
    let brand = "P U L S E  D E C K";
    let label_style = Style::default()
        .fg(theme::accent_secondary())
        .bg(theme::bg())
        .add_modifier(Modifier::BOLD);

    let (status, status_style) = cassette_recording_status(tick_count, recording_state);
    let fixed_padding = 4;
    let spacer_width = inner_width
        .saturating_sub(visible_len(brand))
        .saturating_sub(visible_len(status))
        .saturating_sub(fixed_padding);

    shell_line(
        inner_width,
        shell_style,
        vec![
            segment("  ", shell_style),
            segment(brand, label_style),
            segment(" ".repeat(spacer_width), shell_style),
            segment(status, status_style),
            segment("  ", shell_style),
        ],
    )
}

fn cassette_recording_status(
    tick_count: u64,
    recording_state: RecordingState,
) -> (&'static str, Style) {
    match recording_state {
        RecordingState::Active => {
            let status = if tick_count.is_multiple_of(2) {
                "● REC ACTIVE"
            } else {
                "  REC ACTIVE"
            };
            (status, theme::error().add_modifier(Modifier::BOLD))
        }
        RecordingState::Pending => {
            let status = if tick_count.is_multiple_of(2) {
                "● REC PENDING"
            } else {
                "  REC PENDING"
            };
            (
                status,
                Style::default()
                    .fg(theme::warm())
                    .bg(theme::bg())
                    .add_modifier(Modifier::BOLD),
            )
        }
        RecordingState::Off => (
            "A-SIDE",
            Style::default()
                .fg(theme::highlight())
                .bg(theme::bg())
                .add_modifier(Modifier::BOLD),
        ),
    }
}

fn reel_cells_for_state(tick_count: u64, playback: &PlaybackState) -> (String, String) {
    match playback {
        PlaybackState::Playing => {
            let transfer_step = ((tick_count / 6) % 8) as usize;
            let transfer = if transfer_step < 4 {
                transfer_step
            } else {
                7 - transfer_step
            };

            let left_fill = CASSETTE_TAPE_WIDTH.saturating_sub(transfer).max(1);
            let right_fill = (1 + transfer).min(CASSETTE_TAPE_WIDTH);

            (
                reel_cell("", fixed_tape_mass(left_fill, CASSETTE_TAPE_WIDTH), ""),
                reel_cell("", fixed_tape_mass(right_fill, CASSETTE_TAPE_WIDTH), ""),
            )
        }
        PlaybackState::Connecting => {
            let hub = if (tick_count / 4).is_multiple_of(2) {
                ""
            } else {
                ""
            };
            let tape = fixed_tape_mass(1, CASSETTE_TAPE_WIDTH);
            (reel_cell(hub, tape.clone(), hub), reel_cell(hub, tape, hub))
        }
        PlaybackState::Paused => {
            let tape = fixed_tape_mass(2, CASSETTE_TAPE_WIDTH);
            (reel_cell("", tape.clone(), ""), reel_cell("", tape, ""))
        }
        PlaybackState::Error(_) => {
            let tape = fixed_tape_mass(0, CASSETTE_TAPE_WIDTH);
            (reel_cell("×", tape.clone(), "×"), reel_cell("×", tape, "×"))
        }
        PlaybackState::Stopped => {
            let tape = fixed_tape_mass(0, CASSETTE_TAPE_WIDTH);
            (reel_cell("", tape.clone(), ""), reel_cell("", tape, ""))
        }
    }
}

fn fixed_tape_mass(fill: usize, width: usize) -> String {
    let fill = fill.min(width);
    format!("{}{}", "".repeat(fill), "".repeat(width - fill))
}

fn reel_cell(left_hub: &str, tape: String, right_hub: &str) -> String {
    format!("{left_hub}{tape}{right_hub}")
}

fn cassette_border_line(
    left_corner: &str,
    right_corner: &str,
    inner_width: usize,
    style: Style,
) -> Line<'static> {
    Line::from(vec![Span::styled(
        format!("{left_corner}{}{right_corner}", "".repeat(inner_width)),
        style,
    )])
}

fn shell_text_line(
    text: impl Into<String>,
    inner_width: usize,
    shell_style: Style,
    content_style: Style,
) -> Line<'static> {
    shell_line(
        inner_width,
        shell_style,
        vec![segment(text.into(), content_style)],
    )
}

fn shell_line(
    inner_width: usize,
    shell_style: Style,
    parts: Vec<CassetteSegment>,
) -> Line<'static> {
    let mut spans = Vec::with_capacity(parts.len() + 3);
    let mut remaining = inner_width;

    spans.push(Span::styled("", shell_style));

    for part in parts {
        if remaining == 0 {
            break;
        }

        let part_width = visible_len(&part.text);
        let text = if part_width > remaining {
            truncate_to_chars(&part.text, remaining)
        } else {
            part.text
        };
        let text_width = visible_len(&text);

        remaining = remaining.saturating_sub(text_width);
        spans.push(Span::styled(text, part.style));
    }

    if remaining > 0 {
        spans.push(Span::styled(" ".repeat(remaining), shell_style));
    }

    spans.push(Span::styled("", shell_style));
    Line::from(spans)
}

fn segment(text: impl Into<String>, style: Style) -> CassetteSegment {
    CassetteSegment {
        text: text.into(),
        style,
    }
}

fn visible_len(text: &str) -> usize {
    text.chars().count()
}

fn truncate_to_chars(text: &str, max_chars: usize) -> String {
    text.chars().take(max_chars).collect()
}

fn render_meta_details(frame: &mut Frame, area: Rect, app: &App, full_deck: bool) {
    let (status_text, status_style) = match app.playback {
        PlaybackState::Playing => ("PLAYING", theme::playing()),
        PlaybackState::Connecting => (
            "TUNING...",
            Style::default()
                .fg(theme::warm())
                .add_modifier(Modifier::BOLD),
        ),
        PlaybackState::Paused => ("PAUSED", theme::neon()),
        PlaybackState::Error(_) => ("OFFLINE / ERROR", theme::error()),
        PlaybackState::Stopped => ("STOPPED", theme::dim()),
    };

    let station = app.now_playing();
    let genre = station.map(|s| s.genre.as_str()).unwrap_or("N/A");
    let country = station.map(|s| s.country.as_str()).unwrap_or("N/A");

    let filled = (app.buffer_percent / 10) as usize;
    let empty = 10 - filled;
    let bar = format!("{}{}", "".repeat(filled), "".repeat(empty));

    let mut lines = Vec::new();
    lines.push(Line::from(vec![
        Span::styled("", status_style),
        Span::styled(status_text, status_style),
        Span::styled("   GENRE ", theme::dim()),
        Span::styled(genre, theme::cyan()),
        Span::styled("   ORIGIN ", theme::dim()),
        Span::styled(country, theme::cyan()),
    ]));
    lines.push(Line::from(vec![
        Span::styled(" BUFFER ", theme::dim()),
        Span::styled(
            format!("[{}] ", bar),
            Style::default()
                .fg(theme::highlight())
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(format!("{}% ", app.buffer_percent), theme::cyan()),
        Span::styled(format!("({}s)", app.buffer_seconds), theme::dim()),
    ]));

    if app.recording_state == RecordingState::Active {
        if let Some(ref filepath) = app.active_record_filepath {
            let filename = std::path::Path::new(filepath)
                .file_name()
                .and_then(|f| f.to_str())
                .unwrap_or(filepath);
            lines.push(Line::from(vec![
                Span::styled(
                    " ● REC ACTIVE ",
                    theme::error().add_modifier(Modifier::BOLD),
                ),
                Span::styled(format!("capture -> {}", filename), theme::dim()),
            ]));
        }
    }

    let title = if full_deck {
        " SIGNAL / TAPE STATUS "
    } else {
        " SIGNAL "
    };
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(theme::border())
        .border_type(ratatui::widgets::BorderType::Rounded)
        .title(Span::styled(title, theme::title()));

    let paragraph = Paragraph::new(lines).block(block);
    frame.render_widget(paragraph, area);
}

fn render_oscilloscope(frame: &mut Frame, area: Rect, app: &App) {
    if area.width < 3 || area.height < 3 {
        return;
    }

    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(theme::border())
        .border_type(ratatui::widgets::BorderType::Rounded)
        .title(Span::styled(visualizer_title(app), theme::title()));
    let inner = block.inner(area);
    frame.render_widget(block, area);

    render_visualizer_signal(frame, inner, app);
}

fn visualizer_title(app: &App) -> &'static str {
    match app.visualizer_mode {
        0 => " RTA SPECTRUM ",
        1 => " REAL OSC ",
        _ => " SIM OSC ",
    }
}

fn render_visualizer_signal(frame: &mut Frame, area: Rect, app: &App) {
    let width = area.width as usize;
    let height = area.height as usize;

    if height == 0 || width == 0 {
        return;
    }

    // Mode 0: Spectrum Analyzer (renders vertical block bars with neon tri-color gradient)
    if app.playback == PlaybackState::Playing && app.visualizer_mode == 0 {
        render_spectrum_analyzer(frame, area, app);
        return;
    }

    let mut canvas = BrailleCanvas::new(width, height);
    match app.playback {
        PlaybackState::Playing => match app.visualizer_mode {
            1 => {
                let pixel_width = width * 2;
                let pixel_height = height * 4;
                let center_y = pixel_height as f32 * 0.5;
                let amplitude = (app.volume as f32 / 100.0) * (pixel_height as f32 * 0.45);

                let mut samples = Vec::with_capacity(pixel_width);
                if let Ok(buf) = app.sample_buffer.lock() {
                    let n = buf.len();
                    if n >= pixel_width {
                        let start_idx = n - pixel_width;
                        samples.extend(buf.iter().skip(start_idx).take(pixel_width).copied());
                    } else {
                        samples.extend(vec![0.0; pixel_width - n]);
                        samples.extend(buf.iter().copied());
                    }
                } else {
                    samples.extend(vec![0.0; pixel_width]);
                }

                for (x, sample_val) in samples.iter().enumerate().take(pixel_width) {
                    let y_float = center_y - (sample_val * amplitude);
                    let y = y_float.clamp(0.0, (pixel_height - 1) as f32) as usize;
                    canvas.set_pixel(x, y);
                }
            }
            _ => {
                let pixel_width = width * 2;
                let pixel_height = height * 4;
                let center_y = pixel_height as f32 * 0.5;
                let amplitude = (app.volume as f32 / 100.0) * (pixel_height as f32 * 0.4);

                for x in 0..pixel_width {
                    let t = app.tick_count as f32 * 0.15;
                    let bass = (x as f32 * 0.05 + t).sin() * 0.6;
                    let mid = (x as f32 * 0.15 - t * 0.8).cos() * 0.3;
                    let high = (x as f32 * 0.45 + t * 2.0).sin() * 0.1;

                    let wave_sum = bass + mid + high;
                    let y_float = center_y + wave_sum * amplitude;
                    let y = y_float.clamp(0.0, (pixel_height - 1) as f32) as usize;
                    canvas.set_pixel(x, y);
                }
            }
        },
        PlaybackState::Connecting => {
            let pixel_width = width * 2;
            let pixel_height = height * 4;
            let center_y = pixel_height as f32 * 0.5;
            let amplitude = pixel_height as f32 * 0.2;

            for x in 0..pixel_width {
                let t = app.tick_count as f32 * 0.4;
                let carrier = (x as f32 * 0.3 + t).sin();
                let envelope = (x as f32 * 0.04 - t * 0.25).cos().abs();

                let y_float = center_y + carrier * envelope * amplitude;
                let y = y_float.clamp(0.0, (pixel_height - 1) as f32) as usize;
                canvas.set_pixel(x, y);
            }
        }
        PlaybackState::Paused => {
            let pixel_width = width * 2;
            let pixel_height = height * 4;
            let center_y = pixel_height as f32 * 0.5;
            let amplitude = pixel_height as f32 * 0.07;

            for x in 0..pixel_width {
                let t = app.tick_count as f32 * 0.05;
                let ripple = (x as f32 * 0.08 + t).cos();
                let y_float = center_y + ripple * amplitude;
                let y = y_float.clamp(0.0, (pixel_height - 1) as f32) as usize;
                canvas.set_pixel(x, y);
            }
        }
        _ => {}
    }

    let active_style = Style::default()
        .fg(theme::accent_secondary())
        .add_modifier(Modifier::BOLD);
    let lines = canvas.to_lines(active_style, theme::dim());

    let paragraph = Paragraph::new(lines);
    frame.render_widget(paragraph, area);
}

fn render_spectrum_analyzer(frame: &mut Frame, area: Rect, app: &App) {
    let width = area.width as usize;
    let height = area.height as usize;
    let peaks = &app.visualizer_peaks;

    if peaks.is_empty() {
        frame.render_widget(Paragraph::new(Vec::<Line<'static>>::new()), area);
        return;
    }

    let mut lines = Vec::with_capacity(height);
    for row in 0..height {
        let mut spans = Vec::with_capacity(width);
        let y_factor = (height - 1 - row) as f32 / (height.max(2) - 1) as f32;
        let color = if y_factor < 0.35 {
            theme::highlight()
        } else if y_factor < 0.7 {
            theme::accent_secondary()
        } else {
            theme::warm()
        };
        let style = Style::default().fg(color).add_modifier(Modifier::BOLD);

        for col in 0..width {
            let (band, is_spacer) = spectrum_column_slot(col, width, peaks.len());
            let val = if is_spacer { 0.0 } else { peaks[band] };
            let h = val * height.saturating_sub(1).max(1) as f32;
            let height_in_row = h - (height - 1 - row) as f32;

            let char_str = if height_in_row <= 0.0 {
                " "
            } else if height_in_row >= 1.0 {
                ""
            } else {
                let level = (height_in_row * 8.0).round() as usize;
                let blocks = [" ", " ", "", "", "", "", "", "", ""];
                blocks[level.min(8)]
            };

            spans.push(Span::styled(char_str, style));
        }
        lines.push(Line::from(spans));
    }

    frame.render_widget(Paragraph::new(lines), area);
}

fn spectrum_column_slot(col: usize, width: usize, bands: usize) -> (usize, bool) {
    if bands == 0 || width == 0 {
        return (0, false);
    }

    let band = (col * bands / width).min(bands - 1);
    let start = band * width / bands;
    let end = ((band + 1) * width / bands).min(width);
    let band_width = end.saturating_sub(start);
    let is_spacer = band_width >= 3 && col + 1 == end;

    (band, is_spacer)
}

/// A micro-pixel canvas helper that aggregates sub-pixels into Unicode Braille characters (U+2800 - U+28FF)
struct BrailleCanvas {
    width: usize,
    height: usize,
    grid: Vec<u8>,
}

impl BrailleCanvas {
    fn new(width: usize, height: usize) -> Self {
        Self {
            width,
            height,
            grid: vec![0u8; width * height],
        }
    }

    fn set_pixel(&mut self, x: usize, y: usize) {
        let char_x = x / 2;
        let char_y = y / 4;

        if char_x >= self.width || char_y >= self.height {
            return;
        }

        let sub_x = x % 2;
        let sub_y = y % 4;

        // Unicode Braille dot matrix offsets:
        // Left Column: Dot 1 (1), Dot 2 (2), Dot 3 (4), Dot 7 (64)
        // Right Column: Dot 4 (8), Dot 5 (16), Dot 6 (32), Dot 8 (128)
        let bit = match (sub_x, sub_y) {
            (0, 0) => 1,
            (0, 1) => 2,
            (0, 2) => 4,
            (0, 3) => 64,
            (1, 0) => 8,
            (1, 1) => 16,
            (1, 2) => 32,
            (1, 3) => 128,
            _ => 0,
        };

        let idx = char_y * self.width + char_x;
        self.grid[idx] |= bit;
    }

    fn to_lines(&self, active_style: Style, dim_style: Style) -> Vec<Line<'static>> {
        let mut lines = Vec::with_capacity(self.height);
        let center_y = self.height / 2;

        for y in 0..self.height {
            let mut spans = Vec::with_capacity(self.width);
            for x in 0..self.width {
                let idx = y * self.width + x;
                let cell = self.grid[idx];

                if cell == 0 {
                    if y == center_y {
                        spans.push(Span::styled("", dim_style)); // Dotted neon center grid line
                    } else {
                        spans.push(Span::raw(" "));
                    }
                } else {
                    let c = std::char::from_u32(0x2800 + cell as u32).unwrap_or(' ');
                    spans.push(Span::styled(c.to_string(), active_style));
                }
            }
            lines.push(Line::from(spans));
        }
        lines
    }
}

fn render_history(frame: &mut Frame, area: Rect, app: &App) {
    let mut lines = Vec::new();

    lines.push(Line::from(vec![Span::styled(
        "   📼 Captured Session Mixtape ",
        Style::default()
            .fg(theme::accent_secondary())
            .add_modifier(Modifier::BOLD),
    )]));
    lines.push(Line::from(vec![Span::styled(
        "   ════════════════════════════",
        theme::dim(),
    )]));
    lines.push(Line::from(""));

    if app.song_history.is_empty() {
        lines.push(Line::from(vec![Span::styled(
            "   [ No tracks captured yet ]",
            theme::dim(),
        )]));
        lines.push(Line::from(""));
        lines.push(Line::from(vec![Span::styled(
            "   Music playback will record",
            theme::dim(),
        )]));
        lines.push(Line::from(vec![Span::styled(
            "   inline ICY metadata here...",
            theme::dim(),
        )]));
    } else {
        // Render song history in reverse (newest on top)
        let visible_rows = (area.height as usize).saturating_sub(4);

        for (idx, song) in app.song_history.iter().enumerate().rev().take(visible_rows) {
            let track_num = idx + 1;
            let track_tag = format!("   Track {:02}: ", track_num);

            lines.push(Line::from(vec![
                Span::styled(
                    track_tag,
                    Style::default()
                        .fg(theme::highlight())
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled(song.as_str(), theme::text()),
            ]));
        }
    }

    let paragraph = Paragraph::new(lines);
    frame.render_widget(paragraph, area);
}

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

    fn line_width(line: &Line<'_>) -> usize {
        line.spans
            .iter()
            .map(|span| span.content.chars().count())
            .sum()
    }

    #[test]
    fn cassette_rows_have_equal_width() {
        let expected_width = CASSETTE_INNER_WIDTH + 2;
        let lines = build_cassette_lines(
            CASSETTE_INNER_WIDTH,
            0,
            &PlaybackState::Playing,
            RecordingState::Off,
        );

        assert_eq!(lines.len(), CASSETTE_HEIGHT as usize);
        for line in lines {
            assert_eq!(line_width(&line), expected_width);
        }
    }

    #[test]
    fn reel_animation_keeps_constant_cell_width() {
        for tick_count in 0..96 {
            let (left_reel, right_reel) = reel_cells_for_state(tick_count, &PlaybackState::Playing);

            assert_eq!(left_reel.chars().count(), CASSETTE_REEL_CELL_WIDTH);
            assert_eq!(right_reel.chars().count(), CASSETTE_REEL_CELL_WIDTH);
        }
    }

    #[test]
    fn tape_mass_keeps_constant_width() {
        for fill in 0..=CASSETTE_TAPE_WIDTH + 2 {
            assert_eq!(
                fixed_tape_mass(fill, CASSETTE_TAPE_WIDTH).chars().count(),
                CASSETTE_TAPE_WIDTH
            );
        }
    }

    #[test]
    fn spectrum_column_slot_adds_spacer_when_band_is_wide() {
        let (band, spacer) = spectrum_column_slot(3, 160, 40);

        assert_eq!(band, 0);
        assert!(spacer);
    }

    #[test]
    fn spectrum_column_slot_maps_last_column_to_last_band() {
        let (band, _) = spectrum_column_slot(159, 160, 40);

        assert_eq!(band, 39);
    }
}