radio-cli 0.10.0

A lightweight terminal radio with local favorites and recordings
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
//! Offline map rendering and bounded Explore state.
use crate::{
    geo::{GeoBounds, WORLD_HEIGHT, WORLD_WIDTH},
    model::{Facet, Filter},
};
use ratatui::{
    Frame,
    layout::Rect,
    style::{Color, Modifier, Style},
    widgets::{Block, Borders, Paragraph},
};
use std::{sync::Arc, time::Instant};
use unicode_width::UnicodeWidthStr;

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct Request {
    pub filter: Filter,
    pub bounds: GeoBounds,
    pub columns: usize,
    pub rows: usize,
    pub countries: bool,
}
#[derive(Clone)]
pub(super) struct Image {
    pub request: Request,
    pub revision: u64,
    pub land: Arc<Vec<char>>,
    pub counts: Vec<u32>,
    pub countries: Vec<Facet>,
}
pub(super) struct State {
    pub bounds: GeoBounds,
    pub zoom: u8,
    pub cursor: (i64, i64),
    pub area: Rect,
    pub size: (usize, usize),
    pub deadline: Option<Instant>,
    pub submitted: Option<(Request, u64)>,
    pub refresh_requested: bool,
    pub initialized: bool,
    pub chooser: bool,
    pub country_index: usize,
    pub country_origin: Option<Option<String>>,
}
impl Default for State {
    fn default() -> Self {
        Self {
            bounds: GeoBounds::default(),
            zoom: 0,
            cursor: (180_000_000, 90_000_000),
            area: Rect::default(),
            size: (64, 16),
            deadline: None,
            submitted: None,
            refresh_requested: false,
            initialized: false,
            chooser: false,
            country_index: 0,
            country_origin: None,
        }
    }
}
impl State {
    pub fn cell(&self) -> GeoBounds {
        let (col, row) = self
            .bounds
            .project(self.cursor.0, self.cursor.1, self.size.0, self.size.1)
            .unwrap_or((0, 0));
        self.bounds.cell(col, row, self.size.0, self.size.1)
    }
    pub fn resize(&mut self, area: Rect) {
        self.area = area;
        if area.width > 0 && area.height > 0 {
            self.size = (area.width as usize, area.height as usize);
        }
    }
    pub fn zoom(&mut self, inward: bool, anchor: (i64, i64)) {
        let next = if inward {
            self.zoom.saturating_add(1).min(3)
        } else {
            self.zoom.saturating_sub(1)
        };
        let dx = (anchor.0 - self.bounds.x).rem_euclid(WORLD_WIDTH);
        let dy = anchor.1 - self.bounds.y;
        let width = WORLD_WIDTH >> next;
        let height = WORLD_HEIGHT >> next;
        self.bounds = GeoBounds {
            x: (anchor.0 - dx * width / self.bounds.width).rem_euclid(WORLD_WIDTH),
            y: (anchor.1 - dy * height / self.bounds.height).clamp(0, WORLD_HEIGHT - height),
            width,
            height,
        };
        if next == 0 {
            self.bounds = GeoBounds::default();
        }
        self.zoom = next;
        self.cursor = anchor;
    }
    pub fn step(&mut self, dx: i64, dy: i64) {
        let (col, row) = self
            .bounds
            .project(self.cursor.0, self.cursor.1, self.size.0, self.size.1)
            .unwrap_or((0, 0));
        let columns = self.size.0 as i64;
        let rows = self.size.1 as i64;
        let mut col = col as i64 + dx;
        let row = row as i64 + dy;
        if self.bounds.width == WORLD_WIDTH {
            col = col.rem_euclid(columns);
        } else if !(0..columns).contains(&col) {
            let step = (self.bounds.width + columns - 1) / columns;
            self.bounds.x = (self.bounds.x + dx * step).rem_euclid(WORLD_WIDTH);
        }
        if !(0..rows).contains(&row) {
            let step = (self.bounds.height + rows - 1) / rows;
            self.bounds.y = (self.bounds.y + dy * step).clamp(0, WORLD_HEIGHT - self.bounds.height);
        }
        // Move by cells, whose widths can differ by one microdegree, instead
        // of accumulating a truncated distance that can leave the cursor behind.
        let cell = self.bounds.cell(
            col.clamp(0, columns - 1) as usize,
            row.clamp(0, rows - 1) as usize,
            self.size.0,
            self.size.1,
        );
        self.cursor = (
            (cell.x + cell.width / 2) % WORLD_WIDTH,
            cell.y + cell.height / 2,
        );
    }
    pub fn point(&self, col: u16, row: u16) -> (i64, i64) {
        let cell = self.bounds.cell(
            (col - self.area.x) as usize,
            (row - self.area.y) as usize,
            self.size.0,
            self.size.1,
        );
        (
            (cell.x + cell.width / 2) % WORLD_WIDTH,
            cell.y + cell.height / 2,
        )
    }
}

/// Scanline even-odd fill of all polygon rings. Geometry stays in read-only
/// binary storage; scratch space is bounded by vertex count and viewport size.
pub(super) fn raster(bounds: GeoBounds, columns: usize, rows: usize) -> Vec<char> {
    let data = include_bytes!("map/land.bin");
    let width = columns * 2;
    let height = rows * 4;
    let mut masks = vec![0u8; columns * rows];
    let mut crossings = Vec::with_capacity(256);
    let bits = [[1u8, 8], [2, 16], [4, 32], [64, 128]];
    for py in 0..height {
        crossings.clear();
        let lat = 90_000_000.0
            - (bounds.y as f64 + (py as f64 + 0.5) * bounds.height as f64 / height as f64);
        let mut offset = 0;
        while offset < data.len() {
            let count = u32::from_le_bytes(data[offset..offset + 4].try_into().unwrap()) as usize;
            offset += 4;
            let point = |i: usize| {
                let at = offset + i * 8;
                (
                    i32::from_le_bytes(data[at..at + 4].try_into().unwrap()) as f64,
                    i32::from_le_bytes(data[at + 4..at + 8].try_into().unwrap()) as f64,
                )
            };
            let mut previous = point(count - 1);
            for i in 0..count {
                let current = point(i);
                if (current.1 > lat) != (previous.1 > lat) {
                    crossings.push(
                        current.0
                            + (lat - current.1) * (previous.0 - current.0)
                                / (previous.1 - current.1),
                    );
                }
                previous = current;
            }
            offset += count * 8;
        }
        crossings.sort_unstable_by(f64::total_cmp);
        for span in crossings.as_chunks::<2>().0 {
            for wrap in [-1, 0, 1] {
                let left =
                    span[0] + 180_000_000.0 + wrap as f64 * WORLD_WIDTH as f64 - bounds.x as f64;
                let right =
                    span[1] + 180_000_000.0 + wrap as f64 * WORLD_WIDTH as f64 - bounds.x as f64;
                let start = (left * width as f64 / bounds.width as f64 - 0.5)
                    .ceil()
                    .clamp(0.0, width as f64) as usize;
                let end = (right * width as f64 / bounds.width as f64 - 0.5)
                    .ceil()
                    .clamp(0.0, width as f64) as usize;
                for px in start..end {
                    masks[(py / 4) * columns + px / 2] |= bits[py % 4][px % 2];
                }
            }
        }
    }
    masks
        .into_iter()
        .map(|b| {
            if b == 0 {
                ' '
            } else {
                char::from_u32(0x2800 + b as u32).unwrap()
            }
        })
        .collect()
}

// Fixed count bands keep a color's meaning stable across pans and filters;
// a busy city must not wash out every other marker through max normalization.
const DENSITY_LABELS: [&str; 4] = ["1–9", "10–49", "50–199", "200+"];

fn density_band(count: u32) -> usize {
    match count {
        0..=9 => 0,
        10..=49 => 1,
        50..=199 => 2,
        _ => 3,
    }
}

fn density_styles(theme: crate::config::Theme) -> [Style; 4] {
    use crate::config::Theme;
    // Blue -> teal -> amber -> red. Darker counterparts remain legible on
    // the light theme's white background; Mono preserves NO_COLOR behavior.
    let colors = match theme {
        Theme::Default => [
            Color::Indexed(75),
            Color::Indexed(80),
            Color::Indexed(215),
            Color::Indexed(203),
        ],
        Theme::Light => [
            Color::Indexed(25),
            Color::Indexed(30),
            Color::Indexed(130),
            Color::Indexed(160),
        ],
        Theme::Mono => [Color::Reset; 4],
    };
    colors.map(|color| {
        let style = Style::default().fg(color);
        if theme == Theme::Mono {
            style.add_modifier(Modifier::UNDERLINED)
        } else {
            style
        }
    })
}

fn draw_density_legend(
    frame: &mut Frame,
    area: Rect,
    styles: [Style; 4],
    theme: crate::config::Theme,
) {
    use super::styles::{Role, style};
    let label = crate::t!("explore_density");
    let keys_width: usize = DENSITY_LABELS
        .iter()
        .map(|label| label.width() + 4)
        .sum::<usize>()
        - 2;
    let mut x = area.x;
    let buffer = frame.buffer_mut();
    if label.width() + 2 + keys_width <= usize::from(area.width) {
        buffer.set_string(x, area.y, label, style(theme, Role::Muted));
        x += label.width() as u16 + 2;
    }
    for (label, style) in DENSITY_LABELS.into_iter().zip(styles) {
        // Never paint a partial key that could misidentify its color band.
        let width = label.width() as u16 + 2;
        if x + width > area.right() {
            break;
        }
        buffer[(x, area.y)].set_char('⠿').set_style(style);
        buffer.set_string(
            x + 2,
            area.y,
            label,
            super::styles::style(theme, Role::Muted),
        );
        x += width + 2;
    }
}

pub(super) fn draw(
    frame: &mut Frame,
    state: &mut State,
    image: Option<&Arc<Image>>,
    error: Option<&str>,
    rect: Rect,
    focused: bool,
    theme: crate::config::Theme,
) {
    use super::styles::{Role, style};
    let block = Block::default()
        .borders(Borders::ALL)
        .title(crate::t!("explore_title", "zoom" => 1<<state.zoom))
        .border_style(style(
            theme,
            if focused { Role::Focus } else { Role::Border },
        ));
    let inner = block.inner(rect);
    frame.render_widget(block, rect);
    if inner.width < 8 || inner.height < 3 {
        state.area = Rect::default();
        frame.render_widget(Paragraph::new(crate::t!("explore_small")), inner);
        return;
    }
    if state.chooser {
        state.area = Rect::default();
        let countries = image.map(|i| i.countries.as_slice()).unwrap_or_default();
        let start = state
            .country_index
            .saturating_sub(inner.height.saturating_sub(2) as usize);
        let mut lines = vec![crate::t!("explore_countries").to_string()];
        if image.is_none_or(|image| !image.request.countries) {
            lines.push(
                error
                    .map(crate::clean_text)
                    .unwrap_or_else(|| crate::t!("explore_loading").into()),
            );
        }
        lines.extend(
            countries
                .iter()
                .enumerate()
                .skip(start)
                .take(inner.height.saturating_sub(1) as usize)
                .map(|(i, c)| {
                    format!(
                        "{} {} ({})",
                        if i == state.country_index { ">" } else { " " },
                        if c.value.is_empty() {
                            crate::t!("explore_unknown").to_string()
                        } else {
                            crate::clean_text(&crate::territories::display_in(
                                crate::messages::locale(),
                                &c.value,
                                &c.value,
                            ))
                        },
                        c.count
                    )
                }),
        );
        frame.render_widget(Paragraph::new(lines.join("\n")), inner);
        return;
    }
    let height = (inner.height - 2).min(80).min(inner.width / 4).max(1);
    let width = inner.width.min(height * 4).min(320);
    let area = Rect::new(inner.x + (inner.width - width) / 2, inner.y, width, height);
    state.resize(area);
    let matching = image.filter(|i| {
        i.request.bounds == state.bounds && (i.request.columns, i.request.rows) == state.size
    });
    let densities = density_styles(theme);
    let land_style = Style::default().fg(match theme {
        crate::config::Theme::Default => Color::Indexed(240),
        crate::config::Theme::Light => Color::Indexed(246),
        crate::config::Theme::Mono => Color::Reset,
    });
    if let Some(image) = matching {
        let cursor =
            state
                .bounds
                .project(state.cursor.0, state.cursor.1, state.size.0, state.size.1);
        for y in 0..height as usize {
            for x in 0..width as usize {
                let i = y * width as usize + x;
                let selected = cursor == Some((x, y));
                let count = image.counts[i];
                // Highlight only the selected cell with a large circle; other
                // cells retain their original 2x4 Braille mask and density color.
                let symbol = if selected {
                    '●'
                } else if image.land[i] == ' ' && count > 0 {
                    '⠐'
                } else {
                    image.land[i]
                };
                let marker_style = if count > 0 {
                    densities[density_band(count)]
                } else if selected {
                    style(theme, Role::Focus)
                } else {
                    land_style
                };
                let cell = &mut frame.buffer_mut()[(area.x + x as u16, area.y + y as u16)];
                cell.set_char(symbol).set_style(marker_style);
            }
        }
    }
    draw_density_legend(
        frame,
        Rect::new(inner.x, inner.bottom() - 2, inner.width, 1),
        densities,
        theme,
    );
    let cell = state.cell();
    let count = matching.and_then(|i| {
        state
            .bounds
            .project(state.cursor.0, state.cursor.1, state.size.0, state.size.1)
            .map(|(x, y)| i.counts[y * state.size.0 + x])
    });
    let text = if let Some(error) = error {
        crate::clean_text(error)
    } else if let Some(count) = count {
        format!(
            "{} · {:.2}°, {:.2}° · {:.2}° × {:.2}°",
            crate::t!("explore_count","count"=>count),
            90.0 - state.cursor.1 as f64 / 1e6,
            state.cursor.0 as f64 / 1e6 - 180.0,
            cell.width as f64 / 1e6,
            cell.height as f64 / 1e6
        )
    } else {
        crate::t!("explore_loading").into()
    };
    frame.render_widget(
        Paragraph::new(text).style(Style::default()),
        Rect::new(inner.x, inner.bottom() - 1, inner.width, 1),
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn arrows_advance_exactly_one_cell_on_uneven_grids() {
        for (columns, rows) in [(68, 17), (76, 19), (319, 79)] {
            let mut state = State {
                size: (columns, rows),
                ..State::default()
            };
            let start = state
                .bounds
                .project(state.cursor.0, state.cursor.1, columns, rows)
                .unwrap();
            for step in 1..=columns * 2 {
                state.step(1, 0);
                assert_eq!(
                    state
                        .bounds
                        .project(state.cursor.0, state.cursor.1, columns, rows),
                    Some(((start.0 + step) % columns, start.1))
                );
            }
            for step in 1..=rows {
                state.step(0, 1);
                assert_eq!(
                    state
                        .bounds
                        .project(state.cursor.0, state.cursor.1, columns, rows),
                    Some((start.0, (start.1 + step).min(rows - 1)))
                );
            }
            state.step(-1, -1);
            assert_eq!(
                state
                    .bounds
                    .project(state.cursor.0, state.cursor.1, columns, rows),
                Some(((start.0 + columns - 1) % columns, rows - 2))
            );
        }
    }
    #[test]
    fn density_colors_preserve_braille_subpixels_in_every_theme() {
        use crate::config::Theme;
        use ratatui::{Terminal, backend::TestBackend};
        for theme in [Theme::Default, Theme::Light, Theme::Mono] {
            let rect = Rect::new(0, 0, 82, 24);
            let mut terminal = Terminal::new(TestBackend::new(rect.width, rect.height)).unwrap();
            let mut state = State::default();
            terminal
                .draw(|frame| draw(frame, &mut state, None, None, rect, true, theme))
                .unwrap();
            let (columns, rows) = state.size;
            let mut counts = vec![0; columns * rows];
            counts[..8].copy_from_slice(&[1, 9, 10, 49, 50, 199, 200, u32::MAX]);
            let request = Request {
                filter: Filter::default(),
                bounds: state.bounds,
                columns,
                rows,
                countries: false,
            };
            let shapes = ['⠁', '⠂', '⠃', '⠿', '⣀', '⣿', '⣤', '⣷', '⡇'];
            let mut land = vec!['⠿'; columns * rows];
            land[..9].copy_from_slice(&shapes);
            land[9..12].fill(' ');
            counts[9] = 10; // Offshore station, absent from the land mask.
            let image = Arc::new(Image {
                request,
                revision: 0,
                land: Arc::new(land),
                counts,
                countries: vec![],
            });
            let selected = state.bounds.cell(4, 0, columns, rows);
            state.cursor = (
                selected.x + selected.width / 2,
                selected.y + selected.height / 2,
            );
            terminal
                .draw(|frame| {
                    frame
                        .buffer_mut()
                        .set_style(rect, super::super::styles::base(theme));
                    draw(frame, &mut state, Some(&image), None, rect, true, theme);
                })
                .unwrap();
            let buffer = terminal.backend().buffer();
            assert_eq!(buffer[(state.area.x + 9, state.area.y)].symbol(), "⠐");
            assert_eq!(buffer[(state.area.x + 11, state.area.y)].symbol(), " ");
            let marker = |column: u16| &buffer[(state.area.x + column, state.area.y)];
            for column in 0..8 {
                let expected = if column == 4 {
                    '●'
                } else {
                    shapes[column as usize]
                };
                assert_eq!(marker(column).symbol(), expected.to_string());
                assert!(!marker(column).modifier.contains(Modifier::BOLD));
                assert_eq!(
                    marker(column).modifier.contains(Modifier::UNDERLINED),
                    theme == Theme::Mono
                );
                assert!(!marker(column).modifier.contains(Modifier::REVERSED));
            }
            assert_eq!(marker(8).symbol(), "⡇");
            if theme != Theme::Mono {
                assert_ne!(marker(8).fg, marker(0).fg);
            }
            for column in [0, 2, 4, 6] {
                assert_eq!(marker(column).fg, marker(column + 1).fg);
            }
            if theme == Theme::Mono {
                for cell in &buffer.content {
                    assert_eq!((cell.fg, cell.bg), (Color::Reset, Color::Reset));
                }
            } else {
                for column in [0, 2, 4] {
                    assert_ne!(marker(column).fg, marker(column + 2).fg);
                }
                if theme == Theme::Light {
                    for column in 0..8 {
                        assert_ne!(marker(column).fg, marker(column).bg);
                    }
                }
            }
            let legend: String = (rect.x..rect.right())
                .map(|x| buffer[(x, rect.bottom() - 3)].symbol())
                .collect();
            for label in DENSITY_LABELS {
                assert!(legend.contains(label));
            }
            // Moving selection restores the old mask and marks even an empty
            // sea cell with a large circle, without a fictitious density.
            let empty = state.bounds.cell(10, 0, columns, rows);
            state.cursor = (empty.x + empty.width / 2, empty.y + empty.height / 2);
            terminal
                .draw(|frame| draw(frame, &mut state, Some(&image), None, rect, true, theme))
                .unwrap();
            let cell = &terminal.backend().buffer()[(state.area.x + 10, state.area.y)];
            assert_eq!(cell.symbol(), "●");
            assert!(!cell.modifier.contains(Modifier::REVERSED));
            assert_eq!(
                terminal.backend().buffer()[(state.area.x + 4, state.area.y)].symbol(),
                "⣀"
            );
        }
    }

    #[test]
    fn offline_raster_has_land_and_ocean_in_known_locations() {
        let land = raster(GeoBounds::default(), 180, 45);
        for (lat, lon, expected) in [
            (48.0, 10.0, true),
            (25.0, 15.0, true),
            (0.0, -140.0, false),
            (-25.0, -120.0, false),
        ] {
            let (x, y) = crate::geo::coordinates(Some(lat), Some(lon)).unwrap();
            let (x, y) = GeoBounds::default().project(x, y, 180, 45).unwrap();
            assert_eq!(land[y * 180 + x] != ' ', expected, "{lat},{lon}");
        }
    }
    #[test]
    fn zoom_keeps_pointer_anchor_and_pan_wraps_longitude() {
        let mut s = State {
            cursor: (1, 1),
            ..State::default()
        };
        let anchor = (350_000_000, 30_000_000);
        let before = s.bounds.project(anchor.0, anchor.1, 100, 25);
        s.zoom(true, anchor);
        assert_eq!(s.bounds.project(anchor.0, anchor.1, 100, 25), before);
        for _ in 0..20 {
            s.zoom(true, s.cursor);
        }
        assert_eq!(s.zoom, 3);
        for _ in 0..1000 {
            s.step(1, -1);
            assert!(
                s.bounds
                    .project(s.cursor.0, s.cursor.1, s.size.0, s.size.1)
                    .is_some()
            );
        }
        for _ in 0..20 {
            s.zoom(false, s.cursor);
        }
        assert_eq!(s.bounds, GeoBounds::default());
    }
    #[test]
    #[ignore = "release map raster timing"]
    fn map_raster_benchmark() {
        for (columns, rows) in [(100, 25), (320, 80)] {
            let mut samples = vec![];
            for _ in 0..20 {
                let start = Instant::now();
                std::hint::black_box(raster(GeoBounds::default(), columns, rows));
                samples.push(start.elapsed());
            }
            samples.sort();
            eprintln!(
                "raster {columns}x{rows}: P50 {:?}, P95 {:?}",
                samples[10], samples[18]
            );
            let request = Request {
                filter: Filter::default(),
                bounds: GeoBounds::default(),
                columns,
                rows,
                countries: false,
            };
            let image = Arc::new(Image {
                land: Arc::new(raster(request.bounds, columns, rows)),
                revision: 0,
                counts: vec![1; columns * rows],
                countries: vec![],
                request,
            });
            let mut state = State::default();
            let rect = Rect::new(0, 0, columns as u16 + 2, rows as u16 + 4);
            let mut terminal =
                ratatui::Terminal::new(ratatui::backend::TestBackend::new(rect.width, rect.height))
                    .unwrap();
            let mut samples = vec![];
            for _ in 0..20 {
                state.cursor.0 = (state.cursor.0 + WORLD_WIDTH / columns as i64) % WORLD_WIDTH;
                let start = Instant::now();
                terminal
                    .draw(|frame| {
                        draw(
                            frame,
                            &mut state,
                            Some(&image),
                            None,
                            rect,
                            true,
                            crate::config::Theme::Default,
                        )
                    })
                    .unwrap();
                samples.push(start.elapsed());
            }
            samples.sort();
            eprintln!(
                "cached frame {columns}x{rows}: P50 {:?}, P95 {:?}",
                samples[10], samples[18]
            );
        }
    }
}