bunflared 0.8.1

One command, one throwaway link for your localhost, several ports behind it. Chat with your client from the terminal, send them to a page, follow their cursor, catch their reactions 🔥 Or let your coding agent run the session over MCP.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
use ratatui::Frame;
use ratatui::buffer::Buffer;
use ratatui::layout::{Constraint, Layout, Margin, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Clear, Paragraph};

use super::art::{self, Frame3};
use super::fx::{self, put};
use super::scenes::wrap;
use super::{App, Ask, Focus, LANE_TRIP, Qr, Replay, Row, Session, Theme, plural};
use crate::live::REACTIONS;
use crate::proxy::{CAPTURE, Capture};

const MIN_WIDTH: u16 = 64;
const MIN_HEIGHT: u16 = 20;
const QR_MIN_MAIN: u16 = 56;
const ECG: [char; 16] = [
    '▁', '▁', '▁', '▂', '▁', '▁', '▇', '▃', '▁', '▁', '▁', '▁', '▁', '▁', '▁', '▁',
];
const BARS: [char; 8] = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
const PANIC_FOR: f32 = 3.0;
const HOP_FOR: f32 = 1.2;
const KEYS: [(&str, &str); 8] = [
    ("c", "copy"),
    ("o", "open"),
    ("r", "qr"),
    ("m", "message"),
    ("g", "go"),
    ("x", "clear"),
    ("?", "help"),
    ("q", "quit"),
];
const HELP: [&str; 19] = [
    "c    copy the link",
    "o    open it in your browser",
    "r    big QR code for phones",
    "f    fireworks",
    "",
    "↑ ↓  pick a visitor",
    "tab  switch to the requests",
    "     enter: its details, p: replay it",
    "esc  pick nobody: everyone again",
    "m    message them",
    "g    send them to a page",
    "R    reload their page",
    "e    send them a reaction",
    "x    clear the chat",
    "",
    "?    this help",
    "q    stop sharing",
    "psst: there are secrets.",
    "the bunny knows a few codes.",
];
const HELP_WIDTH: u16 = 40;

fn hms(secs: u64) -> String {
    format!("{:02}:{:02}:{:02}", secs / 3600, secs / 60 % 60, secs % 60)
}

/// A panel whose border shows it has the keyboard focus.
fn focusable<'a>(app: &App, title: &str, focus: Focus) -> Block<'a> {
    let block = panel(app, title);
    if app.focus == Some(focus) {
        block.border_style(app.theme.fg(fx::PINK))
    } else {
        block
    }
}

/// The first line to draw so that the selected one stays in view.
fn scroll(selected: Option<usize>, height: usize) -> usize {
    selected.map_or(0, |i| (i + 1).saturating_sub(height))
}

fn highlight(line: Line<'_>, selected: bool) -> Line<'_> {
    if selected {
        line.style(Style::new().add_modifier(Modifier::REVERSED))
    } else {
        line
    }
}

fn panel<'a>(app: &App, title: &str) -> Block<'a> {
    let border = if app.disco {
        fx::rainbow(app.elapsed() * 150.0 + title.len() as f32 * 45.0)
    } else {
        fx::DIM
    };
    Block::bordered()
        .border_type(BorderType::Rounded)
        .border_style(app.theme.fg(border))
        .title(Span::styled(
            format!(" {title} "),
            app.theme.fg(fx::PINK).add_modifier(Modifier::BOLD),
        ))
}

pub fn render(app: &mut App, frame: &mut Frame) {
    let area = frame.area();
    if area.width < MIN_WIDTH || area.height < MIN_HEIGHT {
        return compact(app, frame, area);
    }
    let [header, body, footer] = Layout::vertical([
        Constraint::Length(3),
        Constraint::Min(0),
        Constraint::Length(1),
    ])
    .areas(area);
    draw_header(app, frame, header);

    let qr_size = app.qr_code.as_ref().map_or(0, |qr| qr.size as u16);
    let (qr_width, qr_height) = (qr_size + 2, qr_size.div_ceil(2) + 2);
    let fits = qr_size > 0 && body.width >= qr_width + QR_MIN_MAIN && body.height >= qr_height;
    let mut visitors = None;
    let main = if fits {
        let [main, side] =
            Layout::horizontal([Constraint::Min(0), Constraint::Length(qr_width)]).areas(body);
        let [side, below] =
            Layout::vertical([Constraint::Length(qr_height), Constraint::Min(0)]).areas(side);
        visitors = (below.height >= 4).then_some(below);
        let block = panel(app, "scan me");
        let inner = block.inner(side);
        frame.render_widget(block, side);
        app.spots.qr = Some(side);
        if let Some(qr) = &app.qr_code {
            draw_qr(
                frame.buffer_mut(),
                &app.theme,
                qr,
                inner.x as i32,
                inner.y as i32,
            );
        }
        app.keep_clear = Some(inner);
        main
    } else {
        body
    };

    let [lane, middle, log] = Layout::vertical([
        Constraint::Length(6),
        Constraint::Length(6),
        Constraint::Min(3),
    ])
    .areas(main);
    let (ports, stats) = if visitors.is_none() {
        // No room under the QR code: the visitors take a third of the middle row.
        let [ports, stats, right] = Layout::horizontal([Constraint::Ratio(1, 3); 3]).areas(middle);
        visitors = Some(right);
        (ports, stats)
    } else {
        let [ports, stats] =
            Layout::horizontal([Constraint::Percentage(48), Constraint::Percentage(52)])
                .areas(middle);
        (ports, stats)
    };
    draw_lane(app, frame, lane);
    draw_ports(app, frame, ports);
    draw_stats(app, frame, stats);
    if let Some(area) = visitors {
        draw_visitors(app, frame, area);
    }
    app.spots.visitors = visitors;
    let (log, side) = split_side(app, log);
    app.spots.log = Some(log);
    draw_log(app, frame, log);
    if let Some(side) = side {
        draw_side(app, frame, side);
    }
    draw_footer(app, frame, footer);
}

fn draw_header(app: &App, frame: &mut Frame, area: Rect) {
    let e = app.elapsed();
    let title: Vec<Span> = " bunflared "
        .chars()
        .enumerate()
        .map(|(i, ch)| {
            let color = fx::rainbow(i as f32 * 30.0 + e * 90.0);
            Span::styled(
                ch.to_string(),
                app.theme.fg(color).add_modifier(Modifier::BOLD),
            )
        })
        .collect();
    let dot = if app.theme.calm || (e * 2.0) as i32 % 2 == 0 {
        fx::RED
    } else {
        fx::PINK
    };
    let live = Line::from(vec![
        Span::styled(" ●", app.theme.fg(dot)),
        Span::styled(
            format!(" LIVE {} ", hms(app.live_for())),
            app.theme.fg(fx::FG).add_modifier(Modifier::BOLD),
        ),
    ])
    .right_aligned();
    let border = if app.disco {
        fx::rainbow(e * 150.0)
    } else {
        fx::DIM
    };
    let block = Block::bordered()
        .border_type(BorderType::Rounded)
        .border_style(app.theme.fg(border))
        .title(Line::from(title))
        .title_top(live);
    let inner = block.inner(area);
    frame.render_widget(block, area);
    let url = app.url.clone().unwrap_or_default();
    let (note, color) = if app.copied {
        ("✓ in your clipboard", fx::GREEN)
    } else {
        ("press c to copy", fx::DIM)
    };
    let line = Line::from(vec![
        Span::styled(
            url,
            app.theme
                .fg(fx::CYAN)
                .add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
        ),
        Span::raw("   "),
        Span::styled(note, app.theme.fg(color)),
    ]);
    frame.render_widget(Paragraph::new(line), inner);
}

struct Mood {
    label: &'static str,
    pose: &'static Frame3,
    jitter: i32,
    color: Color,
}

fn mood(app: &App) -> Mood {
    let e = app.elapsed();
    let alt = |hz: f32| !app.theme.calm && (e * hz) as i32 % 2 == 1;
    let recent = |at: Option<std::time::Instant>, window: f32| {
        at.is_some_and(|at| (app.now - at).as_secs_f32() < window)
    };
    if app.disco {
        let pose = if alt(4.0) {
            &art::DANCE_B
        } else {
            &art::DANCE_A
        };
        Mood {
            label: "disco!",
            pose,
            jitter: 0,
            color: fx::rainbow(e * 200.0),
        }
    } else if app.worried_until.is_some() {
        let pose = if alt(3.0) {
            &art::WORRIED_B
        } else {
            &art::WORRIED_A
        };
        Mood {
            label: "worried",
            pose,
            jitter: 0,
            color: fx::YELLOW,
        }
    } else if recent(app.last_error, PANIC_FOR) {
        let pose = if alt(7.0) {
            &art::PANIC_B
        } else {
            &art::PANIC_A
        };
        Mood {
            label: "PANICKING",
            pose,
            jitter: if alt(11.0) { 1 } else { 0 },
            color: fx::RED,
        }
    } else if recent(app.last_hit, HOP_FOR) {
        let pose = if alt(5.0) { &art::HOP } else { &art::HAPPY };
        Mood {
            label: "hopping",
            pose,
            jitter: 0,
            color: fx::FG,
        }
    } else if app.sleeping() {
        Mood {
            label: "napping",
            pose: &art::SLEEP,
            jitter: 0,
            color: fx::DIM,
        }
    } else {
        let blink = !app.theme.calm && e % 3.2 < 0.15;
        Mood {
            label: "chilling",
            pose: if blink { &art::BLINK } else { &art::IDLE },
            jitter: 0,
            color: fx::FG,
        }
    }
}

fn draw_lane(app: &mut App, frame: &mut Frame, area: Rect) {
    let mood = mood(app);
    let caption = Line::from(Span::styled(
        format!(" bunny: {} ", mood.label),
        app.theme.fg(mood.color),
    ))
    .right_aligned();
    let mut block = panel(app, "traffic").title_bottom(caption);
    if app.reactions.iter().any(|&n| n > 0) {
        let counts: Vec<String> = REACTIONS
            .iter()
            .zip(app.reactions)
            .map(|((emoji, _), n)| format!("{emoji} {n}"))
            .collect();
        block = block.title_bottom(Span::styled(
            format!(" {} ", counts.join("  ")),
            app.theme.fg(fx::FG),
        ));
    }
    let inner = block.inner(area);
    frame.render_widget(block, area);
    let e = app.elapsed();
    let buf = frame.buffer_mut();
    let theme = &app.theme;
    let (left, right) = (inner.x as i32 + 12, inner.right() as i32 - 2);
    put(
        buf,
        inner.x as i32 + 1,
        inner.y as i32,
        "⌂ your machine",
        theme.fg(fx::DIM),
    );
    put(
        buf,
        right - 13,
        inner.y as i32,
        "the internet ☁",
        theme.fg(fx::DIM),
    );

    let offset = if theme.calm { 0 } else { (e * 10.0) as i32 };
    for lane in 0..3 {
        let y = inner.y as i32 + 1 + lane;
        for x in left..right {
            if (x - offset).rem_euclid(4) == 0 {
                put(
                    buf,
                    x,
                    y,
                    "·",
                    theme.fg(fx::DIM).add_modifier(Modifier::DIM),
                );
            }
        }
    }
    for sprite in &app.sprites {
        let trip = (app.now - sprite.born).as_secs_f32() / LANE_TRIP;
        let x = left + ((right - left) as f32 * trip) as i32;
        let y = inner.y as i32 + 1 + sprite.lane as i32;
        let color = if app.disco {
            fx::rainbow(x as f32 * 8.0)
        } else {
            sprite.color
        };
        for (back, ch) in [(2, "·"), (1, "•")] {
            if x - back >= left {
                put(
                    buf,
                    x - back,
                    y,
                    ch,
                    theme.fg(color).add_modifier(Modifier::DIM),
                );
            }
        }
        put(
            buf,
            x,
            y,
            &sprite.glyph.to_string(),
            theme.fg(color).add_modifier(Modifier::BOLD),
        );
    }
    fx::art(
        buf,
        inner.x as i32 + mood.jitter,
        inner.y as i32 + 1,
        mood.pose,
        theme.fg(mood.color),
    );

    if !theme.calm && mood.label == "napping" && app.rng.range(0.0, 1.0) < app.dt * 1.2 {
        app.particles
            .snore(&mut app.rng, inner.x as f32 + 9.0, inner.y as f32 + 1.0);
    }
}

fn draw_ports(app: &App, frame: &mut Frame, area: Rect) {
    let block = panel(app, "ports");
    let inner = block.inner(area);
    frame.render_widget(block, area);
    let e = app.elapsed();
    let shift = if app.theme.calm {
        0
    } else {
        (e * 8.0) as usize
    };
    let lines: Vec<Line> = app
        .ports
        .iter()
        .enumerate()
        .map(|(i, port)| {
            let target = format!(" {:<12} :{:<5} ", port.route, port.port);
            if port.up {
                let beat = !app.theme.calm && (e * 1.3 + i as f32 * 0.37).fract() < 0.25;
                let heart = if beat {
                    app.theme.fg(fx::RED).add_modifier(Modifier::BOLD)
                } else {
                    app.theme.fg(fx::PINK)
                };
                let ecg: String = (0..10)
                    .map(|j| ECG[(j + shift + i * 5) % ECG.len()])
                    .collect();
                Line::from(vec![
                    Span::styled("♥", heart),
                    Span::styled(target, app.theme.fg(fx::CYAN)),
                    Span::styled(ecg, app.theme.fg(fx::GREEN)),
                    Span::styled(format!(" {}", port.hits), app.theme.fg(fx::DIM)),
                ])
            } else {
                Line::from(vec![
                    Span::styled("✖", app.theme.fg(fx::RED).add_modifier(Modifier::BOLD)),
                    Span::styled(target, app.theme.fg(fx::CYAN)),
                    Span::styled("▁".repeat(10), app.theme.fg(fx::RED)),
                    Span::styled(" down", app.theme.fg(fx::RED).add_modifier(Modifier::BOLD)),
                ])
            }
        })
        .collect();
    frame.render_widget(Paragraph::new(lines), inner);
}

fn draw_stats(app: &App, frame: &mut Frame, area: Rect) {
    let block = panel(app, "stats");
    let inner = block.inner(area);
    frame.render_widget(block, area);
    let theme = &app.theme;
    let average = if app.total == 0 {
        0
    } else {
        app.ms_sum / app.total as u64
    };
    let strong = |value: String, color: Color| {
        Span::styled(value, theme.fg(color).add_modifier(Modifier::BOLD))
    };
    let label = |text: &'static str| Span::styled(text, theme.fg(fx::DIM));
    let mut lines = vec![
        Line::from(vec![
            strong(app.total.to_string(), fx::FG),
            label(" requests  "),
            strong(app.visitors.len().to_string(), fx::FG),
            label(if app.visitors.len() == 1 {
                " visitor  "
            } else {
                " visitors  "
            }),
            strong(app.sockets.to_string(), fx::FG),
            label(" sockets"),
        ]),
        Line::from(vec![
            label("2xx "),
            strong(app.classes[0].to_string(), fx::GREEN),
            label("  3xx "),
            strong(app.classes[1].to_string(), fx::CYAN),
            label("  4xx "),
            strong(app.classes[2].to_string(), fx::YELLOW),
            label("  5xx "),
            strong(app.classes[3].to_string(), fx::RED),
        ]),
        Line::from(vec![
            label("avg "),
            strong(format!("{average} ms"), fx::FG),
            label("  carrots eaten "),
            strong(app.classes[0].to_string(), fx::CARROT),
            label("  feedback "),
            strong(app.feedback.to_string(), fx::PINK),
        ]),
    ];
    let width = inner.width as usize;
    let recent: Vec<u64> = app
        .per_second
        .iter()
        .rev()
        .take(width)
        .rev()
        .copied()
        .collect();
    let peak = recent.iter().copied().max().unwrap_or(0).max(1);
    let spark: Vec<Span> = recent
        .iter()
        .map(|&count| {
            let level = if count == 0 {
                0
            } else {
                1 + (count * 6 / peak) as usize
            };
            Span::styled(
                BARS[level.min(7)].to_string(),
                theme.fg(fx::hsv(140.0 - level as f32 * 18.0, 0.7, 1.0)),
            )
        })
        .collect();
    lines.push(Line::from(spark));
    frame.render_widget(Paragraph::new(lines), inner);
}

/// "active", "tab hidden", "idle 45s" or "left", with its color.
fn presence(app: &App, session: &Session) -> (&'static str, String, Color) {
    let away = app.now.saturating_duration_since(session.seen);
    let status = session.presence.status(away);
    let (glyph, color) = match status.as_str() {
        "left" => ("○", fx::DIM),
        "active" => ("●", fx::GREEN),
        _ => ("◐", fx::YELLOW),
    };
    (glyph, status, color)
}

/// The visitors in the order they are listed: by arrival, those who left last.
pub fn roster(app: &App) -> Vec<&Session> {
    let mut sessions: Vec<&Session> = app.sessions.values().collect();
    sessions.sort_by_key(|s| (presence(app, s).1 == "left", s.first));
    sessions
}

pub fn here(app: &App) -> usize {
    app.sessions
        .values()
        .filter(|s| presence(app, s).1 != "left")
        .count()
}

fn draw_visitors(app: &App, frame: &mut Frame, area: Rect) {
    let block = focusable(
        app,
        &format!("visitors · {} here", here(app)),
        Focus::Visitors,
    );
    let inner = block.inner(area);
    frame.render_widget(block, area);
    let theme = &app.theme;
    if app.sessions.is_empty() {
        let line = Line::from(Span::styled("Nobody on the page yet.", theme.fg(fx::DIM)));
        frame.render_widget(Paragraph::new(line), inner);
        return;
    }
    let sessions = roster(app);
    let at = sessions
        .iter()
        .position(|s| app.selected.as_ref() == Some(&s.presence.sid));
    let lines: Vec<Line> = sessions
        .into_iter()
        .skip(scroll(at, inner.height as usize))
        .take(inner.height as usize)
        .map(|session| {
            let (dot, state, color) = presence(app, session);
            let p = &session.presence;
            let selected = app.selected.as_ref() == Some(&p.sid);
            let line = Line::from(vec![
                Span::styled(format!("{dot} "), theme.fg(color)),
                Span::styled(format!("{}  ", p.device), theme.fg(fx::FG)),
                Span::styled(format!("{}  ", p.page), theme.fg(fx::CYAN)),
                Span::styled(state, theme.fg(color)),
                Span::styled(
                    format!("  {}", plural(p.clicks as usize, "click")),
                    theme.fg(fx::DIM),
                ),
            ]);
            highlight(line, selected)
        })
        .collect();
    frame.render_widget(Paragraph::new(lines), inner);
}

fn status_color(status: u16) -> Color {
    match status {
        0..300 => fx::GREEN,
        300..400 => fx::CYAN,
        400..500 => fx::YELLOW,
        _ => fx::RED,
    }
}

fn draw_log(app: &App, frame: &mut Frame, area: Rect) {
    let block = focusable(app, "requests", Focus::Log);
    let inner = block.inner(area);
    frame.render_widget(block, area);
    let theme = &app.theme;
    if app.rows.is_empty() {
        let waiting = Line::from(Span::styled(
            "No visitors yet. Send the link to someone!",
            theme.fg(fx::DIM),
        ))
        .centered();
        let [_, middle, _] = Layout::vertical([
            Constraint::Min(0),
            Constraint::Length(1),
            Constraint::Min(0),
        ])
        .areas(inner);
        frame.render_widget(Paragraph::new(waiting), middle);
        return;
    }
    let at = app.rows.iter().position(|row| Some(row.n) == app.picked);
    let lines: Vec<Line> = app
        .rows
        .iter()
        .skip(scroll(at, inner.height as usize))
        .take(inner.height as usize)
        .map(|row| {
            let hit = &row.hit;
            let replayed = match row.replayed {
                None => Span::raw(""),
                Some(Replay::Pending) => Span::styled("↻ …  ", theme.fg(fx::DIM)),
                Some(Replay::Done { status, .. }) => Span::styled(
                    format!("↻ {status} "),
                    theme.fg(status_color(status)).add_modifier(Modifier::BOLD),
                ),
                Some(Replay::Failed) => Span::styled("↻ ✖   ", theme.fg(fx::RED)),
            };
            let line = Line::from(vec![
                Span::styled(format!("{} ", row.clock), theme.fg(fx::DIM)),
                Span::styled(format!("{:<7}", hit.method), theme.fg(fx::PURPLE)),
                Span::styled(
                    format!("{} ", hit.status),
                    theme
                        .fg(status_color(hit.status))
                        .add_modifier(Modifier::BOLD),
                ),
                replayed,
                Span::styled(format!("{:>5}ms ", hit.ms), theme.fg(fx::FG)),
                Span::styled(format!(":{:<5} ", hit.port), theme.fg(fx::DIM)),
                Span::styled(hit.path.clone(), theme.fg(fx::FG)),
            ]);
            highlight(line, Some(row.n) == app.picked)
        })
        .collect();
    frame.render_widget(Paragraph::new(lines), inner);
}

/// The radar of the selected visitor and the chat, once someone has talked,
/// take the right of the request log.
fn split_side(app: &App, log: Rect) -> (Rect, Option<Rect>) {
    if app.chat.is_empty() && app.selected.is_none() {
        return (log, None);
    }
    let [log, side] =
        Layout::horizontal([Constraint::Min(0), Constraint::Percentage(42)]).areas(log);
    (log, Some(side))
}

fn draw_side(app: &App, frame: &mut Frame, area: Rect) {
    match (app.selected.is_some(), app.chat.is_empty()) {
        (true, false) => {
            let [radar, chat] =
                Layout::vertical([Constraint::Percentage(50), Constraint::Min(0)]).areas(area);
            draw_radar(app, frame, radar);
            draw_chat(app, frame, chat);
        }
        (true, true) => draw_radar(app, frame, area),
        _ => draw_chat(app, frame, area),
    }
}

/// The selected visitor's viewport, scaled, with their pointer in it.
fn draw_radar(app: &App, frame: &mut Frame, area: Rect) {
    let session = app.selected.as_ref().and_then(|sid| app.sessions.get(sid));
    let device = session.map_or("nobody", |s| s.presence.device.as_str());
    let block = panel(app, &format!("radar · {device}"));
    let inner = block.inner(area);
    frame.render_widget(block, area);
    let theme = &app.theme;
    let Some((pointer, at)) = session.and_then(|s| s.pointer.as_ref()) else {
        let line = Line::from(Span::styled(
            "Their pointer shows here once it moves.",
            theme.fg(fx::DIM),
        ));
        frame.render_widget(Paragraph::new(line), inner);
        return;
    };
    let still = (app.now - *at).as_secs();
    let caption = match still {
        0 => format!("{}×{} · moving", pointer.w, pointer.h),
        secs => format!("{}×{} · still for {secs}s", pointer.w, pointer.h),
    };
    let [room, bottom] = Layout::vertical([Constraint::Min(0), Constraint::Length(1)]).areas(inner);
    frame.render_widget(
        Paragraph::new(Line::from(Span::styled(caption, theme.fg(fx::DIM))).centered()),
        bottom,
    );
    if room.width < 4 || room.height < 3 {
        return;
    }
    // A cell is about twice as tall as it is wide.
    let ratio = pointer.w.max(1.0) / pointer.h.max(1.0);
    let (mut width, mut height) = (room.height as f32 * 2.0 * ratio, room.height as f32);
    if width > room.width as f32 {
        (width, height) = (room.width as f32, room.width as f32 / 2.0 / ratio);
    }
    let width = (width as u16).clamp(4, room.width);
    let height = (height as u16).clamp(3, room.height);
    let screen = Rect::new(room.x + (room.width - width) / 2, room.y, width, height);
    frame.render_widget(Block::bordered().border_style(theme.fg(fx::DIM)), screen);
    let inside = screen.inner(Margin::new(1, 1));
    let spot = |value: f32, size: f32, start: u16, cells: u16| {
        start as i32
            + ((value / size.max(1.0)).clamp(0.0, 1.0) * cells.saturating_sub(1) as f32).round()
                as i32
    };
    let x = spot(pointer.x, pointer.w, inside.x, inside.width);
    let y = spot(pointer.y, pointer.h, inside.y, inside.height);
    let glyph = if still == 0 { "◉" } else { "●" };
    put(
        frame.buffer_mut(),
        x,
        y,
        glyph,
        theme.fg(fx::PINK).add_modifier(Modifier::BOLD),
    );
}

fn draw_chat(app: &App, frame: &mut Frame, area: Rect) {
    let block = panel(app, "chat");
    let inner = block.inner(area);
    frame.render_widget(block, area);
    let theme = &app.theme;
    let width = inner.width.saturating_sub(2).max(8) as usize;
    let mut lines = Vec::new();
    for message in &app.chat {
        let (who, color) = if message.mine {
            (format!("you → {}", message.who), fx::PINK)
        } else {
            (message.who.clone(), fx::CYAN)
        };
        let mut head = vec![
            Span::styled(format!("{} ", message.clock), theme.fg(fx::DIM)),
            Span::styled(who, theme.fg(color).add_modifier(Modifier::BOLD)),
        ];
        if !message.page.is_empty() {
            head.push(Span::styled(
                format!(" {}", message.page),
                theme.fg(fx::DIM),
            ));
        }
        lines.push(Line::from(head));
        for text in message.text.lines().flat_map(|line| wrap(line, width)) {
            lines.push(Line::from(Span::styled(
                format!("  {text}"),
                theme.fg(fx::FG),
            )));
        }
    }
    let newest = lines.split_off(lines.len().saturating_sub(inner.height as usize));
    frame.render_widget(Paragraph::new(newest), inner);
}

fn draw_footer(app: &App, frame: &mut Frame, area: Rect) {
    let mut spans = vec![Span::raw(" ")];
    for (key, label) in KEYS {
        spans.push(Span::styled(
            key,
            app.theme.fg(fx::PINK).add_modifier(Modifier::BOLD),
        ));
        spans.push(Span::styled(format!(" {label}   "), app.theme.fg(fx::DIM)));
    }
    frame.render_widget(Paragraph::new(Line::from(spans)), area);
}

fn compact(app: &App, frame: &mut Frame, area: Rect) {
    let theme = &app.theme;
    let status = if app.ready_at.is_some() {
        format!("● LIVE {}", hms(app.live_for()))
    } else {
        "digging...".into()
    };
    let lines = vec![
        Line::from(vec![
            Span::styled(
                "bunflared ",
                theme.fg(fx::PINK).add_modifier(Modifier::BOLD),
            ),
            Span::styled(status, theme.fg(fx::RED)),
        ]),
        Line::from(Span::styled(
            app.url.clone().unwrap_or_default(),
            theme.fg(fx::CYAN).add_modifier(Modifier::BOLD),
        )),
        Line::from(Span::styled(
            format!("{} here now · {} feedback", here(app), app.feedback),
            theme.fg(fx::FG),
        )),
        Line::from(Span::styled(
            format!(
                "{} · {} · {}",
                plural(app.total as usize, "request"),
                plural(app.visitors.len(), "visitor"),
                plural(app.classes[3] as usize, "error")
            ),
            theme.fg(fx::FG),
        )),
        Line::from(Span::styled(
            "c copy · o open · r qr · m message · q quit",
            theme.fg(fx::DIM),
        )),
    ];
    frame.render_widget(Paragraph::new(lines), area);
}

/// Half blocks with explicit black and white, so phones scan it on any theme.
fn draw_qr(buf: &mut Buffer, theme: &Theme, qr: &Qr, x: i32, y: i32) {
    const BLACK: Color = Color::Rgb(0, 0, 0);
    const WHITE: Color = Color::Rgb(255, 255, 255);
    let dark = |col: usize, row: usize| row < qr.size && qr.dark[row * qr.size + col];
    for row in 0..qr.size.div_ceil(2) {
        for col in 0..qr.size {
            let (top, bottom) = (dark(col, row * 2), dark(col, row * 2 + 1));
            let (cx, cy) = (x + col as i32, y + row as i32);
            if theme.color {
                let fg = if top { BLACK } else { WHITE };
                let bg = if bottom { BLACK } else { WHITE };
                put(buf, cx, cy, "▀", Style::new().fg(fg).bg(bg));
            } else {
                // ponytail: assumes a dark terminal, light modules are drawn.
                let ch = match (top, bottom) {
                    (false, false) => "█",
                    (false, true) => "▀",
                    (true, false) => "▄",
                    (true, true) => " ",
                };
                put(buf, cx, cy, ch, Style::new());
            }
        }
    }
}

const BODY_LINES: usize = 400;

fn kib(bytes: usize) -> String {
    format!("{:.1} KiB", bytes as f32 / 1024.0)
}

/// The start of a body as text, or what it is when it is not text.
fn body_lines(theme: &Theme, headers: &hyper::HeaderMap, capture: &Capture) -> Vec<Line<'static>> {
    let kind = headers
        .get(hyper::header::CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");
    let text = capture.readable(headers);
    let note = |text: String| Line::from(Span::styled(format!("  {text}"), theme.fg(fx::DIM)));
    if capture.total == 0 {
        return vec![note("empty".into())];
    }
    if !text {
        let what = if kind.is_empty() { "binary" } else { kind };
        return vec![note(format!("{what}, {}, not shown", kib(capture.total)))];
    }
    let mut lines: Vec<Line> = String::from_utf8_lossy(&capture.bytes)
        .lines()
        .take(BODY_LINES)
        .map(|line| Line::from(Span::styled(format!("  {line}"), theme.fg(fx::FG))))
        .collect();
    if !capture.complete() {
        lines.push(note(format!(
            "… the first {} KiB of {} are kept",
            CAPTURE / 1024,
            kib(capture.total)
        )));
    }
    lines
}

fn header_lines(theme: &Theme, headers: &hyper::HeaderMap) -> Vec<Line<'static>> {
    headers
        .iter()
        .map(|(name, value)| {
            Line::from(vec![
                Span::styled(format!("  {name}: "), theme.fg(fx::CYAN)),
                Span::styled(
                    String::from_utf8_lossy(value.as_bytes()).into_owned(),
                    theme.fg(fx::FG),
                ),
            ])
        })
        .collect()
}

fn details_lines(app: &App, row: &Row) -> Vec<Line<'static>> {
    let theme = &app.theme;
    let hit = &row.hit;
    let exchange = &hit.exchange;
    let section = |title: &str| {
        Line::from(Span::styled(
            title.to_string(),
            theme.fg(fx::PINK).add_modifier(Modifier::BOLD),
        ))
    };
    let mut lines = vec![Line::from(vec![
        Span::styled(format!("{} ", hit.method), theme.fg(fx::PURPLE)),
        Span::styled(format!("{}  ", hit.path), theme.fg(fx::FG)),
        Span::styled(
            hit.status.to_string(),
            theme
                .fg(status_color(hit.status))
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            format!(
                " · {} ms · :{} · {} · from {}",
                hit.ms, hit.port, row.clock, hit.visitor
            ),
            theme.fg(fx::DIM),
        ),
    ])];
    let replay = match row.replayed {
        Some(Replay::Done { status, ms }) => format!("replayed: {status} in {ms} ms"),
        Some(Replay::Pending) => "replaying…".into(),
        Some(Replay::Failed) => "the replay failed".into(),
        None => hit
            .unreplayable()
            .unwrap_or_else(|| "p replays it to your local server".into()),
    };
    lines.push(Line::from(Span::styled(
        format!("↻ {replay}"),
        theme.fg(fx::DIM),
    )));
    lines.push(Line::raw(""));
    lines.push(section("Request headers"));
    lines.extend(header_lines(theme, &exchange.request_headers));
    lines.push(section("Request body"));
    let request = exchange.request_body.lock().unwrap();
    lines.extend(body_lines(theme, &exchange.request_headers, &request));
    lines.push(Line::raw(""));
    lines.push(section("Response headers"));
    lines.extend(header_lines(theme, &exchange.response_headers));
    lines.push(section("Response body"));
    let response = exchange.response_body.lock().unwrap();
    lines.extend(body_lines(theme, &exchange.response_headers, &response));
    lines
}

/// Enter on a request: everything that went through, scrollable.
fn draw_details(app: &mut App, frame: &mut Frame, area: Rect) {
    let Some(row) = app
        .details
        .and_then(|n| app.rows.iter().find(|row| row.n == n))
    else {
        return;
    };
    let lines = details_lines(app, row);
    let rect = centered_rect(
        area,
        area.width.saturating_sub(8).min(110),
        area.height.saturating_sub(4),
    );
    frame.render_widget(Clear, rect);
    let block = panel(app, "request")
        .border_style(app.theme.fg(fx::PINK))
        .title_bottom(Line::from(" ↑ ↓ scroll · p replay · esc close ").right_aligned());
    let inner = block.inner(rect).inner(Margin::new(1, 0));
    frame.render_widget(block, rect);
    let most = lines.len().saturating_sub(inner.height as usize) as u16;
    app.details_scroll = app.details_scroll.min(most);
    frame.render_widget(Paragraph::new(lines).scroll((app.details_scroll, 0)), inner);
    app.keep_clear = Some(rect);
}

/// The text box of `g`, low on the screen so the panels stay readable.
fn draw_prompt(app: &App, frame: &mut Frame, area: Rect) {
    let Some(prompt) = &app.prompt else {
        return;
    };
    let suggestions = app.suggestions(prompt);
    let (title, hint) = match prompt.ask {
        Ask::Go => (
            format!("send {} to", app.target()),
            " enter send · tab pick · esc cancel ",
        ),
        Ask::Chat => (
            format!("message to {}", app.target()),
            " enter send · esc close ",
        ),
        Ask::React => (
            format!("react to {}", app.target()),
            " 1-4 or tab, enter send · esc cancel ",
        ),
    };
    let typed = prompt.ask != Ask::React;
    let width = 60.min(area.width.saturating_sub(2));
    let height = 2 + u16::from(typed) + suggestions.len() as u16;
    let x = area.x + (area.width - width) / 2;
    let y = area.bottom().saturating_sub(height + 2).max(area.y);
    let rect = Rect::new(x, y, width, height.min(area.height));
    frame.render_widget(Clear, rect);
    let block = panel(app, &title)
        .border_style(app.theme.fg(fx::PINK))
        .title_bottom(Line::from(hint).right_aligned());
    let inner = block.inner(rect);
    frame.render_widget(block, rect);
    let theme = &app.theme;
    let mut lines = Vec::new();
    if typed {
        lines.push(Line::from(vec![
            Span::styled("› ", theme.fg(fx::PINK).add_modifier(Modifier::BOLD)),
            Span::styled(prompt.text.clone(), theme.fg(fx::FG)),
            Span::styled("▏", theme.fg(fx::PINK)),
        ]));
    }
    lines.extend(suggestions.into_iter().enumerate().map(|(i, page)| {
        let line = Line::from(Span::styled(format!("  {page}"), theme.fg(fx::CYAN)));
        highlight(line, prompt.pick == Some(i))
    }));
    frame.render_widget(Paragraph::new(lines), inner);
}

fn centered_rect(area: Rect, width: u16, height: u16) -> Rect {
    let width = width.min(area.width);
    let height = height.min(area.height);
    Rect::new(
        area.x + (area.width - width) / 2,
        area.y + (area.height - height) / 2,
        width,
        height,
    )
}

pub fn overlays(app: &mut App, frame: &mut Frame) {
    let area = frame.area();
    if app.qr
        && let Some(qr) = &app.qr_code
    {
        let rect = centered_rect(area, qr.size as u16 + 4, qr.size.div_ceil(2) as u16 + 4);
        frame.render_widget(Clear, rect);
        let block = panel(app, "scan me").title_bottom(Line::from(" esc to close ").centered());
        let inner = block.inner(rect);
        frame.render_widget(block, rect);
        draw_qr(
            frame.buffer_mut(),
            &app.theme,
            qr,
            inner.x as i32 + 1,
            inner.y as i32 + 1,
        );
        app.keep_clear = Some(rect);
    }
    if app.help {
        let rect = centered_rect(area, HELP_WIDTH, HELP.len() as u16 + 4);
        frame.render_widget(Clear, rect);
        let block = panel(app, "keys");
        let inner = block.inner(rect);
        frame.render_widget(block, rect);
        let lines: Vec<Line> = HELP
            .iter()
            .map(|line| {
                let color = if line.starts_with("psst") || line.starts_with("the bunny") {
                    fx::YELLOW
                } else {
                    fx::FG
                };
                Line::from(Span::styled(format!(" {line}"), app.theme.fg(color)))
            })
            .collect();
        frame.render_widget(
            Paragraph::new(lines),
            inner.inner(ratatui::layout::Margin::new(0, 1)),
        );
    }
    draw_details(app, frame, area);
    draw_prompt(app, frame, area);
    for (i, toast) in app.toasts.iter().enumerate() {
        let width = (Span::raw(toast.title.as_str())
            .width()
            .max(Span::raw(toast.body.as_str()).width()) as u16
            + 4)
        .min(area.width);
        let age = (app.now - toast.born).as_secs_f32();
        let slide = if app.theme.calm {
            0
        } else {
            ((1.0 - (age / 0.25).min(1.0)) * width as f32) as u16
        };
        let x = area.right().saturating_sub(width + 1) + slide;
        // Stacked up from the footer, clear of the QR code at the top right.
        let Some(y) = area.bottom().checked_sub(2 + (i as u16 + 1) * 4) else {
            continue;
        };
        if y < area.y || x >= area.right() {
            continue;
        }
        let rect = Rect::new(x, y, width.min(area.right() - x), 4);
        frame.render_widget(Clear, rect);
        let border = if toast.gold { fx::GOLD } else { fx::CYAN };
        let block = Block::bordered()
            .border_type(BorderType::Rounded)
            .border_style(app.theme.fg(border));
        let inner = block.inner(rect);
        frame.render_widget(block, rect);
        let lines = vec![
            Line::from(Span::styled(
                toast.title.clone(),
                app.theme.fg(border).add_modifier(Modifier::BOLD),
            )),
            Line::from(Span::styled(toast.body.clone(), app.theme.fg(fx::FG))),
        ];
        frame.render_widget(Paragraph::new(lines), inner);
    }
}