blubat 0.4.0

Bluetooth battery monitor for macOS: one-shot CLI readings, JSON output and a live TUI
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
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
//! Drawing the dashboard, as a set of small widgets over `App`.
//!
//! Layout only: nothing here decides anything, it just places what the state
//! already says. Every function takes the state borrowed and returns a widget,
//! so a view can be drawn into a test buffer without a terminal.

use blubat_core::{ChargeState, Device, Thresholds};
use ratatui::Frame;
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Cell, Clear, Padding, Paragraph, Row, Table, TableState};

use super::app::{App, Binding, DETAIL_KEYS, KEYMAP, Mode, NOTES, Notice};
use super::columns::{self, Column};
use super::detail;
use super::theme::{self, Palette};
use super::view::Rows;

/// Kept in front of every name so rows stay aligned whatever the gutter holds.
const GUTTER: &str = "  ";

/// What separates two pieces of the status line, and the budget they cost.
const GAP: &str = "   ";

/// The selected row's gutter marker, which colour alone could not carry.
const MARKER: &str = "\u{258e} ";

/// A critical device's gutter marker, which sits where the name would start.
const ALERT: &str = "\u{25b2} ";

/// Where the next typed character will land in the filter.
const CURSOR: &str = "\u{2588}";

/// Cells the alert is given, and the width below which it is not drawn at all.
const ALERT_WIDTH: u16 = 14;

/// Draws one frame of `app` into `table`, which carries the scroll offset.
///
/// The offset is the one piece of state a frame leaves behind: ratatui scrolls
/// the table only as far as it takes to bring the selection back into view, so
/// handing it a fresh state each frame would pin the selection to the last
/// visible row.
pub fn render(frame: &mut Frame, app: &App, table: &mut TableState) {
    let screen = frame.area();

    // The detail view replaces the dashboard rather than covering it, and it
    // draws one device, so a mode holding no device falls back to the table.
    if let (Mode::Detail, Some(device)) = (app.mode, app.current()) {
        detail::render(frame, app, device, screen);
        return;
    }

    // The notice takes a line only while there is one, so the dashboard keeps
    // the layout it usually has.
    let [status, notice, filter, devices, footer] = Layout::vertical([
        Constraint::Length(1),
        Constraint::Length(u16::from(app.notice.is_some())),
        Constraint::Length(1),
        Constraint::Min(0),
        Constraint::Length(1),
    ])
    .horizontal_margin(1)
    .areas(screen);

    let palette = app.look.palette;
    let rows = app.rows();

    render_status(frame, app, status);

    if let Some(said) = &app.notice {
        frame.render_widget(notice_line(said, palette), notice);
    }

    frame.render_widget(filter_line(app, &rows), filter);
    render_devices(frame, app, &rows, devices, table);
    frame.render_widget(keys_footer(app), footer);

    if app.mode == Mode::Keymap {
        render_keymap(frame, screen, palette);
    }
}

/// What the dashboard has to say about itself, in the colour of how it went.
fn notice_line(notice: &Notice, palette: Palette) -> Line<'static> {
    let colour = if notice.problem {
        palette.alert
    } else {
        palette.accent
    };

    Line::from(Span::styled(notice.text.clone(), colour))
}

/// The one line of context above the table, with the alert count on the right.
///
/// The alert is drawn only where it fits whole: cut short it would read as a
/// different count, which is the one thing on this line that must not be wrong.
fn render_status(frame: &mut Frame, app: &App, area: Rect) {
    let [left, right] =
        Layout::horizontal([Constraint::Min(0), Constraint::Length(ALERT_WIDTH)]).areas(area);

    frame.render_widget(status_line(app, left.width), left);

    if right.width == ALERT_WIDTH {
        frame.render_widget(alert_line(app.critical(), app.look.palette), right);
    }
}

fn status_line(app: &App, width: u16) -> Line<'static> {
    const NAME: &str = "blubat";

    let palette = app.look.palette;
    let degraded = degraded(app.degraded(), palette);
    let warnings = warnings(app.warnings().len(), palette);
    let spent = NAME.len()
        + GAP.len()
        + degraded.content.chars().count()
        + warnings.content.chars().count();
    let room = usize::from(width).saturating_sub(spent);

    Line::from(vec![
        Span::styled(
            NAME,
            Style::new().fg(palette.accent).add_modifier(Modifier::BOLD),
        ),
        Span::styled(format!("{GAP}{}", summary(app, room)), palette.dim),
        degraded,
        warnings,
    ])
}

/// How much the dashboard knows, how it is ordered, and when it reads next.
///
/// Assembled a piece at a time and cut short at `room` rather than truncated,
/// so a narrow terminal loses the last detail whole instead of mid word.
fn summary(app: &App, room: usize) -> String {
    let poll = format!("poll {}", seconds(app.interval));
    let pieces = app.next_poll_in().map_or_else(
        || vec!["waiting for the first reading".to_string(), poll.clone()],
        |next| {
            vec![
                format!("{} active", app.connected().count()),
                format!("sort {}", app.view.sort.label()),
                poll.clone(),
                format!("next {}", seconds(next)),
            ]
        },
    );

    pieces.into_iter().fold(String::new(), |line, piece| {
        let extended = if line.is_empty() {
            piece
        } else {
            format!("{line}{GAP}{piece}")
        };

        if extended.chars().count() <= room {
            extended
        } else {
            line
        }
    })
}

/// Says the reading is standing in for one a source could not give.
///
/// Its own marker rather than a warning count, because it is a different claim:
/// a warning is one device blubat could not read, and this is every device the
/// slow source knows about being as old as its last good answer.
fn degraded(degraded: bool, palette: Palette) -> Span<'static> {
    if degraded {
        Span::styled(format!("{GAP}degraded"), palette.low)
    } else {
        Span::raw("")
    }
}

/// Warnings are counted rather than printed: the reading is still usable, and
/// the count is the cue that something in it could not be read.
fn warnings(count: usize, palette: Palette) -> Span<'static> {
    match count {
        0 => Span::raw(""),
        count => Span::styled(format!("{GAP}{}", counted(count, "warning")), palette.low),
    }
}

/// Only connected devices can be critical, so the inactive section never shows here.
fn alert_line(critical: usize, palette: Palette) -> Line<'static> {
    match critical {
        0 => Line::from(Span::styled("all ok", palette.dim)).right_aligned(),
        count => Line::from(Span::styled(
            format!("{ALERT}{count} critical"),
            palette.critical,
        ))
        .right_aligned(),
    }
}

/// The filter, on the line that is otherwise the spacer above the table.
///
/// Present only while it is being typed or narrowing something, so the dense
/// layout keeps its breathing room the rest of the time.
fn filter_line(app: &App, rows: &Rows<'_>) -> Line<'static> {
    let filter = &app.view.filter;
    let typing = app.mode == Mode::Filtering;

    if !typing && !filter.narrows() {
        return Line::default();
    }

    let palette = app.look.palette;
    let cursor = if typing { CURSOR } else { "" };
    let colour = if typing { palette.accent } else { palette.dim };

    Line::from(vec![
        Span::styled(format!("/{}{cursor}", filter.query), colour),
        Span::styled(
            format!("{GAP}{}", counted(rows.len(), "match")),
            palette.dim,
        ),
    ])
}

fn render_devices(
    frame: &mut Frame,
    app: &App,
    rows: &Rows<'_>,
    area: Rect,
    table: &mut TableState,
) {
    if rows.is_empty() {
        frame.render_widget(nothing_to_show(app), area);
        return;
    }

    table.select(Some(table_row(rows, app.selected)));

    frame.render_stateful_widget(device_table(app, rows, area.width), area, table);
}

/// Where the selected device sits among the table's rows.
///
/// The inactive heading is a row of its own, so everything under it is one
/// further down than the selection, which only counts devices.
fn table_row(rows: &Rows<'_>, selected: usize) -> usize {
    if selected < rows.active.len() {
        selected
    } else {
        selected + 1
    }
}

/// The device table: who, how full, and what they are doing.
fn device_table<'a>(app: &'a App, rows: &Rows<'a>, width: u16) -> Table<'a> {
    let palette = app.look.palette;
    let columns = columns::fitting(width);
    let header = Row::new(
        columns
            .iter()
            .map(|column| header_cell(*column))
            .collect::<Vec<_>>(),
    )
    .style(palette.dim);
    let widths = columns
        .iter()
        .map(|column| column.constraint())
        .collect::<Vec<_>>();

    let mut table = rows
        .active
        .iter()
        .map(|device| device_row(app, device, &columns, Section::Active))
        .collect::<Vec<_>>();

    if !rows.inactive.is_empty() {
        table.push(section_row(rows.inactive.len(), palette));
        table.extend(
            rows.inactive
                .iter()
                .map(|device| device_row(app, device, &columns, Section::Inactive)),
        );
    }

    Table::new(table, widths)
        .header(header)
        .column_spacing(1)
        .row_highlight_style(Style::new().bg(palette.selection))
        .highlight_symbol(Span::styled(MARKER, Style::new().fg(palette.accent)))
}

fn header_cell(column: Column) -> Cell<'static> {
    match column {
        Column::Name => Cell::from(format!("{GUTTER}{}", column.header())),
        Column::Level => Cell::from(Line::from(column.header()).right_aligned()),
        other => Cell::from(other.header()),
    }
}

/// Which half of the table a row is in, and what that does to its colours.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Section {
    Active,
    Inactive,
}

impl Section {
    /// `colour` in the live section, and the one dim colour below it.
    ///
    /// Disconnected devices render uniformly dim: their level is what macOS
    /// last persisted, so nothing there competes with a live reading.
    fn tint(self, colour: Color, palette: Palette) -> Color {
        match self {
            Section::Active => colour,
            Section::Inactive => palette.dim,
        }
    }
}

fn device_row<'a>(
    app: &'a App,
    device: &'a Device,
    columns: &[Column],
    section: Section,
) -> Row<'a> {
    let palette = app.look.palette;
    let critical = section == Section::Active
        && theme::is_critical(device.active_level(), app.thresholds(device));
    let cells = columns
        .iter()
        .map(|column| cell(app, device, *column, section, critical))
        .collect::<Vec<_>>();

    Row::new(cells).style(section.tint(palette.text, palette))
}

fn cell<'a>(
    app: &'a App,
    device: &'a Device,
    column: Column,
    section: Section,
    critical: bool,
) -> Cell<'a> {
    let palette = app.look.palette;
    let level = device.levels.lowest();
    let thresholds = app.thresholds(device);

    match column {
        Column::Name => Cell::from(name(device, section, critical, palette)),
        Column::Kind => Cell::from(Span::styled(
            device.kind.as_deref().unwrap_or(theme::UNKNOWN),
            section.tint(palette.dim, palette),
        )),
        Column::Bar => Cell::from(bar(level, section, palette, thresholds)),
        Column::Level => Cell::from(
            Line::from(Span::styled(
                theme::percent(level),
                Style::new()
                    .fg(section.tint(palette.level(level, thresholds), palette))
                    .add_modifier(Modifier::BOLD),
            ))
            .right_aligned(),
        ),
        Column::State => {
            let (text, colour) = state(app, device, critical);

            Cell::from(Span::styled(text, section.tint(colour, palette)))
        }
        Column::Trend => Cell::from(Span::styled(
            theme::sparkline(&recent_levels(app, device)),
            section.tint(
                if critical {
                    palette.critical
                } else {
                    palette.text
                },
                palette,
            ),
        )),
        Column::LastSeen => Cell::from(Span::styled(
            theme::age(app.now.unix().saturating_sub(device.read_at.unix())),
            section.tint(palette.dim, palette),
        )),
    }
}

/// The levels behind one device's sparkline, oldest first.
///
/// Taken from the newest end, since the line only has room for the most recent
/// few and it is the recent ones the trend is about.
fn recent_levels(app: &App, device: &Device) -> Vec<u8> {
    let mut levels: Vec<u8> = app
        .history
        .samples(&device.address)
        .rev()
        .take(theme::SPARK_WIDTH)
        .map(|sample| sample.level)
        .collect();
    levels.reverse();

    levels
}

/// The name, behind the gutter that a critical device puts its mark in.
fn name(device: &Device, section: Section, critical: bool, palette: Palette) -> Line<'_> {
    let marker = if critical {
        Span::styled(ALERT, palette.critical)
    } else {
        Span::raw(GUTTER)
    };
    let colour = if critical {
        palette.alert
    } else {
        section.tint(palette.strong, palette)
    };

    Line::from(vec![marker, Span::styled(device.name.as_str(), colour)])
}

/// The filled run in the level colour, the trough behind it dim.
fn bar(
    level: Option<u8>,
    section: Section,
    palette: Palette,
    thresholds: Thresholds,
) -> Line<'static> {
    let (filled, trough) = theme::battery_bar(level);

    Line::from(vec![
        Span::styled(
            filled,
            section.tint(palette.level(level, thresholds), palette),
        ),
        Span::styled(trough, palette.dim),
    ])
}

/// What a device is doing, which is not always something it is doing.
///
/// A device no source has a level for is unreported rather than absent; one
/// that has stopped reporting is stale, which is the same rule the `stale`
/// event is raised by; and a disconnected one's level is labelled last seen,
/// since macOS keeps reporting it with no timestamp long after the device went
/// away.
fn state(app: &App, device: &Device, critical: bool) -> (String, Color) {
    let palette = app.look.palette;

    if !device.has_battery() {
        ("unreported".to_string(), palette.dim)
    } else if app.is_stale(device) {
        ("stale".to_string(), palette.low)
    } else if !device.connected {
        ("last seen".to_string(), palette.dim)
    } else if device.charge == ChargeState::Charging {
        (
            format!("{} charging", app.look.glyphs.charging),
            palette.charging,
        )
    } else if critical {
        (device.charge.to_string(), palette.alert)
    } else {
        (device.charge.to_string(), palette.text)
    }
}

/// Announces the disconnected devices, a line clear of the live ones.
fn section_row<'a>(inactive: usize, palette: Palette) -> Row<'a> {
    Row::new(vec![Cell::from(Span::styled(
        format!("inactive ({inactive})"),
        palette.dim,
    ))])
    .top_margin(1)
}

/// Stands in for the table whenever it has no rows, saying which kind of none.
fn nothing_to_show(app: &App) -> Paragraph<'static> {
    let message = if app.reading.is_none() {
        "waiting for the first reading"
    } else if app.devices().is_empty() {
        "no Bluetooth devices reported"
    } else if app.view.filter.narrows() {
        "no device matches the filter"
    } else {
        "every device is hidden; press H to show them"
    };

    Paragraph::new(message).style(app.look.palette.dim)
}

/// The keys live in the current view, which is what makes the footer contextual.
pub(super) fn keys_footer(app: &App) -> Line<'static> {
    let palette = app.look.palette;
    let spans = app
        .keys()
        .iter()
        .flat_map(|binding| {
            [
                Span::styled(binding.keys, palette.text),
                Span::styled(format!(" {}  ", binding.label), palette.dim),
            ]
        })
        .collect::<Vec<_>>();

    Line::from(spans)
}

/// The full keymap, centred over the dashboard rather than replacing it.
///
/// It lists the detail view's keys as well as the dashboard's, since the
/// overlay is the one place both sets can be read at once: inside the detail
/// view only its own footer is on screen.
fn render_keymap(frame: &mut Frame, screen: Rect, palette: Palette) {
    let height = KEYMAP.len() + DETAIL_KEYS.len() + NOTES.len() + 5;
    let area = centred(screen, 68, u16::try_from(height).unwrap_or(u16::MAX));

    frame.render_widget(Clear, area);
    frame.render_widget(keymap(palette), area);
}

fn keymap(palette: Palette) -> Paragraph<'static> {
    let bound = |bindings: &'static [Binding]| {
        bindings.iter().map(move |binding| {
            Line::from(vec![
                Span::styled(format!("{:>9}  ", binding.keys), palette.accent),
                Span::raw(binding.label),
            ])
        })
    };
    let heading = Line::from(Span::styled("  in the detail view", palette.dim));
    let notes = NOTES
        .iter()
        .map(|note| Line::from(Span::styled(*note, palette.dim)));
    let lines = bound(&KEYMAP)
        .chain([Line::default(), heading])
        .chain(bound(&DETAIL_KEYS))
        .chain([Line::default()])
        .chain(notes)
        .collect::<Vec<_>>();

    Paragraph::new(lines).block(
        Block::bordered()
            .title(" keys ")
            .padding(Padding::horizontal(1)),
    )
}

/// A box of at most `width` by `height` in the middle of `area`.
fn centred(area: Rect, width: u16, height: u16) -> Rect {
    let [_, band, _] = Layout::vertical([
        Constraint::Fill(1),
        Constraint::Length(height.min(area.height)),
        Constraint::Fill(1),
    ])
    .areas(area);
    let [_, centre, _] = Layout::horizontal([
        Constraint::Fill(1),
        Constraint::Length(width.min(area.width)),
        Constraint::Fill(1),
    ])
    .areas(band);

    centre
}

/// `1 device` and `2 devices`, since both read on the status line.
fn counted(count: usize, noun: &str) -> String {
    let plural = if count == 1 { "" } else { "s" };

    format!("{count} {noun}{plural}")
}

fn seconds(duration: std::time::Duration) -> String {
    format!("{}s", duration.as_secs())
}

#[cfg(test)]
mod tests {
    use blubat_core::{ChargeState, Levels, Raised, Snapshot, Timestamp};
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::buffer::{Buffer, Cell as Drawn};

    use super::super::app::tests::{
        READ_AT, app, config, device, loaded, press, reading, three_devices,
    };
    use super::super::app::{Event, Key, update};
    use super::*;

    /// The same dashboard under a `[theme]` table, which is the only thing that
    /// ever changes what it draws in.
    fn looking(app: App, theme: &str) -> App {
        let theme = config(&format!("[theme]\n{theme}\n")).theme;
        let look = app.look.reloaded(&theme);

        App { look, ..app }
    }

    /// The buffer left behind after drawing each of `apps` in turn.
    ///
    /// One table state throughout, as the loop keeps one, so a test can see
    /// where the previous frame's scroll offset leaves the next one.
    fn buffer_of(apps: &[&App], width: u16, height: u16) -> Buffer {
        let mut terminal = Terminal::new(TestBackend::new(width, height)).expect("a test terminal");
        let mut table = TableState::new();

        for app in apps {
            terminal
                .draw(|frame| render(frame, app, &mut table))
                .expect("a rendered frame");
        }

        terminal.backend().buffer().clone()
    }

    /// A buffer as one string per row.
    ///
    /// Trailing blanks are cut: they are invisible on screen and unreadable in
    /// an expected frame, and every difference that shows still shows.
    fn rows_of(buffer: &Buffer) -> Vec<String> {
        buffer
            .content()
            .chunks(usize::from(buffer.area.width))
            .map(|row| {
                row.iter()
                    .map(Drawn::symbol)
                    .collect::<String>()
                    .trim_end()
                    .to_string()
            })
            .collect()
    }

    /// What a real terminal of this size would show, one string per row.
    fn drawn(app: &App, width: u16, height: u16) -> Vec<String> {
        rows_of(&buffer_of(&[app], width, height))
    }

    /// The cell drawing the first character of `needle`, for asserting the
    /// colours and weights a frame compared as text cannot carry.
    fn cell_of<'a>(buffer: &'a Buffer, needle: &str) -> &'a Drawn {
        let symbols: Vec<&str> = buffer.content().iter().map(Drawn::symbol).collect();
        let wanted: Vec<String> = needle
            .chars()
            .map(|character| character.to_string())
            .collect();
        let start = symbols
            .windows(wanted.len())
            .position(|run| run.iter().zip(&wanted).all(|(drawn, want)| drawn == want))
            .unwrap_or_else(|| panic!("`{needle}` is nowhere on screen"));

        &buffer.content()[start]
    }

    /// The whole screen as one string, for asserting a line is somewhere on it.
    fn screen(app: &App) -> String {
        drawn(app, 100, 30).join("\n")
    }

    fn line_containing(app: &App, needle: &str) -> String {
        drawn(app, 100, 30)
            .into_iter()
            .find(|line| line.contains(needle))
            .unwrap_or_else(|| panic!("no line contains `{needle}`"))
    }

    fn typed(name: &str, kind: &str, address: &str, level: Option<u8>) -> Device {
        Device {
            kind: Some(kind.to_string()),
            ..device(name, address, level)
        }
    }

    /// The dashboard with one of everything: a charging device, a critical one,
    /// an ordinary one, and an inactive section holding a last seen level and a
    /// device no source has ever had a level for.
    fn dashboard() -> App {
        let devices = vec![
            Device {
                charge: ChargeState::Charging,
                ..typed("Magic Trackpad", "trackpad", "30-82-16-f2-24-90", Some(23))
            },
            typed("MX Keys M Mac", "keyboard", "de-df-38-f0-46-9b", Some(67)),
            typed("Soundcore Liberty", "audio", "d0-03-4b-0b-e6-4e", Some(8)),
            Device {
                connected: false,
                read_at: Timestamp::from_unix(READ_AT.unix() - 10_800),
                ..typed("AirPods Pro", "audio", "74-15-f5-02-8e-38", Some(45))
            },
            Device {
                connected: false,
                levels: Levels::default(),
                read_at: Timestamp::from_unix(READ_AT.unix() - 172_800),
                ..typed("MX Master 3S", "mouse", "aa-bb-cc-dd-ee-ff", None)
            },
        ];

        // Five earlier readings a minute apart, each five points fuller, so the
        // sparkline has a full run to draw and every frame below is reproducible.
        let earlier = |step: u8| Snapshot {
            read_at: minutes_before(step),
            devices: devices
                .iter()
                .map(|device| Device {
                    levels: Levels {
                        main: device
                            .levels
                            .main
                            .map(|level| level.saturating_add(5 * step)),
                        ..device.levels
                    },
                    read_at: if device.connected {
                        minutes_before(step)
                    } else {
                        device.read_at
                    },
                    ..device.clone()
                })
                .collect(),
            degraded: false,
            warnings: Vec::new(),
        };

        let app = (1..=5).rev().fold(app(), |app, step| {
            update(app, Event::Reading(earlier(step)))
        });

        update(app, Event::Reading(reading(devices)))
    }

    fn minutes_before(minutes: u8) -> Timestamp {
        Timestamp::from_unix(READ_AT.unix() - i64::from(minutes) * 60)
    }

    /// One device's readings over the two hours before now, a quarter of an
    /// hour apart and `step` points fuller each time.
    ///
    /// An injected clock and an injected history throughout: the chart, the
    /// rate and the estimate the detail view draws are all derived from these
    /// stamps, so nothing below depends on when or where it is run.
    fn charging_up(device: &Device, step: u8) -> App {
        let at = |quarter: i64| Timestamp::from_unix(READ_AT.unix() - quarter * 900);
        let earlier = |quarter: i64| Snapshot {
            read_at: at(quarter),
            devices: vec![Device {
                levels: Levels {
                    main: device.levels.main.map(|level| {
                        level.saturating_sub(step * u8::try_from(quarter).unwrap_or(0))
                    }),
                    ..device.levels
                },
                read_at: at(quarter),
                ..device.clone()
            }],
            degraded: false,
            warnings: Vec::new(),
        };

        let app = (1..=8).rev().fold(app(), |app, quarter| {
            update(app, Event::Reading(earlier(quarter)))
        });

        update(app, Event::Reading(reading(vec![device.clone()])))
    }

    fn raised(device: &Device, event: blubat_core::Event, level: u8, seconds_ago: i64) -> Raised {
        Raised {
            event,
            device: device.name.clone(),
            address: device.address.clone(),
            level: Some(level),
            previous: None,
            charge: device.charge,
            source: device.source,
            threshold: Some(20),
            cycle: 0,
            at: Timestamp::from_unix(READ_AT.unix() - seconds_ago),
        }
    }

    /// A charging trackpad on its own detail view, with two hours of history
    /// behind it and the two events that history raised.
    fn detail() -> App {
        let trackpad = Device {
            charge: ChargeState::Charging,
            ..typed(
                "Paul\u{2019}s Magic Trackpad",
                "trackpad",
                "30-82-16-f2-24-90",
                Some(23),
            )
        };
        let app = charging_up(&trackpad, 2);
        let app = update(
            app,
            Event::Raised(vec![
                raised(&trackpad, blubat_core::Event::LowBattery, 19, 3_600),
                raised(&trackpad, blubat_core::Event::CriticalBattery, 9, 1_800),
            ]),
        );

        update(app, Event::Key(Key::Enter))
    }

    /// The same view over a device that reports three batteries, which is what
    /// the sub level rows exist for.
    fn airpods() -> App {
        let airpods = Device {
            levels: Levels {
                main: None,
                left: Some(100),
                right: Some(97),
                case: Some(68),
            },
            ..typed("AirPods Pro", "audio", "74-15-f5-02-8e-38", None)
        };

        update(
            update(app(), Event::Reading(reading(vec![airpods]))),
            Event::Key(Key::Enter),
        )
    }

    /// More devices than a test terminal can show at once, each one named
    /// distinctly enough to say which of them is on screen.
    fn crowd(count: u8) -> App {
        let devices = (0..count)
            .map(|n| {
                device(
                    &format!("Device {n:02}"),
                    &format!("aa-bb-cc-dd-ee-{n:02x}"),
                    Some(50),
                )
            })
            .collect();

        update(app(), Event::Reading(reading(devices)))
    }

    #[test]
    fn the_dashboard_draws_the_frame_it_is_specified_to_draw() {
        let expected = " blubat   3 active   sort level   poll 5s   next 5s                                    ▲ 1 critical

     Device                   Type         Battery         % State       Trend  Last seen
 ▎ ▲ Soundcore Liberty        audio        █░░░░░░░░░░░   8% on battery  █▇▅▄▂▁ now
     Magic Trackpad           trackpad     ███░░░░░░░░░  23% + charging  █▇▅▄▂▁ now
     MX Keys M Mac            keyboard     ████████░░░░  67% on battery  █▇▅▄▂▁ now

   inactive (2)
     AirPods Pro              audio        █████░░░░░░░  45% stale       ······ 3h ago
     MX Master 3S             mouse        ░░░░░░░░░░░░   -- unreported  ······ 2d ago



















 q quit  j/k move  enter detail  s sort  / filter  h hide  H show hidden  r reload  ? help";
        assert_frame(&dashboard(), 100, 30, expected);
    }

    #[test]
    fn a_narrow_terminal_drops_columns_rather_than_the_reading() {
        let expected = " blubat   3 active   sort level   poll 5s      ▲ 1 critical

     Device                   Battery         % State
 ▎ ▲ Soundcore Liberty        █░░░░░░░░░░░   8% on battery
     Magic Trackpad           ███░░░░░░░░░  23% + charging
     MX Keys M Mac            ████████░░░░  67% on battery

   inactive (2)
     AirPods Pro              █████░░░░░░░  45% stale
     MX Master 3S             ░░░░░░░░░░░░   -- unreported









 q quit  j/k move  enter detail  s sort  / filter  h hide";
        assert_frame(&dashboard(), 60, 20, expected);
    }

    /// Fails with the frame that was drawn, since that is what has to be read
    /// to decide whether a change was the point or a regression.
    fn assert_frame(app: &App, width: u16, height: u16, expected: &str) {
        let drawn = drawn(app, width, height).join("\n");

        assert_eq!(drawn, expected, "\nas drawn at {width}x{height}:\n{drawn}");
    }

    #[test]
    fn the_dashboard_shows_the_devices_it_was_handed() {
        let screen = screen(&loaded());

        assert!(screen.contains("blubat"), "{screen}");
        for name in ["Magic Trackpad", "MX Keys M Mac", "Soundcore Liberty"] {
            assert!(screen.contains(name), "{name} is missing from\n{screen}");
        }
        assert!(screen.contains("85%") && screen.contains("42%"), "{screen}");
        assert!(screen.contains("--"), "an unread level is still a row");
        assert!(screen.contains("on battery"), "{screen}");
    }

    #[test]
    fn the_status_line_says_how_it_is_ordered_and_when_it_reads_next() {
        let app = loaded();
        let ticked = update(
            app.clone(),
            Event::Tick(Timestamp::from_unix(app.now.unix() + 3)),
        );

        assert!(line_containing(&app, "blubat").contains("3 active"));
        assert!(line_containing(&app, "blubat").contains("sort level"));
        assert!(line_containing(&app, "blubat").contains("poll 5s"));
        assert!(line_containing(&app, "blubat").contains("next 5s"));
        assert!(
            line_containing(&ticked, "blubat").contains("next 2s"),
            "the countdown moves on a tick alone"
        );
        assert!(
            line_containing(&press(app, "s"), "blubat").contains("sort name"),
            "and follows the order the table is in"
        );
    }

    #[test]
    fn the_footer_carries_the_keys_of_the_view_on_screen() {
        let dashboard = drawn(&loaded(), 100, 30);
        let footer = dashboard.last().expect("a footer row").clone();

        for key in [
            "q quit",
            "j/k move",
            "enter detail",
            "s sort",
            "/ filter",
            "h hide",
            "H show hidden",
            "r reload",
            "? help",
        ] {
            assert!(footer.contains(key), "{key} is missing from `{footer}`");
        }
    }

    /// The overlay covers what is under it, which only a whole frame can say:
    /// the box, its border, the keys of both views it lists and the dashboard
    /// rows it hides are one assertion rather than four substrings on a screen
    /// that already contains them.
    #[test]
    fn the_keymap_overlay_covers_the_dashboard_and_lists_both_views_keys() {
        let expected = " blubat   3 active   sort level   poll 5s   next 5s                                    ▲ 1 critical

     Device                   Type         Battery         % State       Trend  Last seen
 ▎ ▲ Soundcore Liberty        audio        █░░░░░░░░░░░   8% on battery  █▇▅▄▂▁ now
     Magic Trackpad           trackpad     ███░░░░░░░░░  23% + charging  █▇▅▄▂▁ now
     MX Keys M M┌ keys ────────────────────────────────────────────────────────────┐
                │         q  quit                                                  │
   inactive (2) │       j/k  move                                                  │
     AirPods Pro│     enter  detail                                                │go
     MX Master 3│         s  sort                                                  │go
                │         /  filter                                                │
                │         h  hide                                                  │
                │         H  show hidden                                           │
                │         r  reload                                                │
                │         ?  help                                                  │
                │                                                                  │
                │   in the detail view                                             │
                │ esc/enter  back                                                  │
                │         q  quit                                                  │
                │                                                                  │
                │ the detail chart is this run only; a restart starts it empty.    │
                │ h lasts: it is the one write blubat makes to the config file.    │
                │ a hidden device is hidden here only, never unpaired from macOS.  │
                │ r re-reads the config file; one it cannot read changes nothing.  │
                └──────────────────────────────────────────────────────────────────┘




 ? close  q quit";
        assert_frame(
            &update(dashboard(), Event::Key(Key::Char('?'))),
            100,
            30,
            expected,
        );
    }

    #[test]
    fn the_selected_row_is_marked_in_the_gutter() {
        let selected = update(loaded(), Event::Key(Key::Char('j')));

        assert!(
            line_containing(&selected, "Magic Trackpad").contains(MARKER),
            "the second row is marked"
        );
        assert!(
            !line_containing(&selected, "MX Keys M Mac").contains(MARKER),
            "and the first one is not"
        );
    }

    #[test]
    fn the_selection_marker_skips_the_inactive_heading() {
        let app = press(dashboard(), "jjj");

        assert!(line_containing(&app, "AirPods Pro").contains(MARKER));
        assert!(!line_containing(&app, "inactive (2)").contains(MARKER));
    }

    #[test]
    fn an_empty_dashboard_says_which_kind_of_empty_it_is() {
        assert!(screen(&app()).contains("waiting for the first reading"));

        let empty = update(app(), Event::Reading(reading(Vec::new())));
        assert!(screen(&empty).contains("no Bluetooth devices reported"));

        let filtered = press(loaded(), "/nothing here");
        assert!(screen(&filtered).contains("no device matches the filter"));

        let all_hidden = press(loaded(), "hhh");
        assert!(screen(&all_hidden).contains("every device is hidden"));
    }

    /// The two claims are separate: one device blubat could not parse is a
    /// warning, and a source standing in for its last good answer is degraded.
    #[test]
    fn a_degraded_reading_says_so_beside_the_warnings_it_counts() {
        let both = update(
            app(),
            Event::Reading(Snapshot {
                degraded: true,
                warnings: vec!["system_profiler exited with 1".to_string()],
                ..three_devices()
            }),
        );
        let neither = update(app(), Event::Reading(three_devices()));

        assert!(line_containing(&both, "blubat").contains("degraded"));
        assert!(line_containing(&both, "blubat").contains("1 warning"));
        assert!(!line_containing(&neither, "blubat").contains("degraded"));
        assert!(!line_containing(&neither, "blubat").contains("warning"));
    }

    #[test]
    fn a_disconnected_device_sits_under_a_counted_inactive_heading() {
        let rows = drawn(&dashboard(), 100, 30);
        let heading = rows
            .iter()
            .position(|line| line.contains("inactive (2)"))
            .expect("an inactive heading");
        let airpods = rows
            .iter()
            .position(|line| line.contains("AirPods Pro"))
            .expect("a disconnected device");

        assert!(heading < airpods, "the heading introduces the section");
        assert!(
            rows[..heading].iter().any(|line| line.contains("MX Keys")),
            "and the live devices come first"
        );
    }

    #[test]
    fn a_device_no_source_has_a_level_for_is_shown_as_unreported() {
        assert!(line_containing(&dashboard(), "MX Master 3S").contains("unreported"));
    }

    /// The one staleness rule reaching the table: the same window the `stale`
    /// event is raised by decides which rows say so.
    #[test]
    fn a_device_that_has_stopped_reporting_is_marked_stale_in_the_table() {
        let fresh = update(
            dashboard(),
            Event::Reading(reading(vec![Device {
                connected: false,
                ..typed("AirPods Pro", "audio", "74-15-f5-02-8e-38", Some(45))
            }])),
        );

        assert!(
            line_containing(&dashboard(), "AirPods Pro").contains("stale"),
            "three hours is well past the ten minute window"
        );
        assert!(
            line_containing(&fresh, "AirPods Pro").contains("last seen"),
            "a disconnected reading taken just now is not stale"
        );

        let patient = App {
            config: config("[poll]\nstale_after = \"6h\"\n"),
            ..dashboard()
        };
        assert!(line_containing(&patient, "AirPods Pro").contains("last seen"));
    }

    #[test]
    fn only_a_live_low_reading_is_marked_critical() {
        let screen = screen(&dashboard());

        assert!(line_containing(&dashboard(), "Soundcore").contains(ALERT));
        assert!(screen.contains(&format!("{ALERT}1 critical")));
        assert!(
            !line_containing(&dashboard(), "AirPods Pro").contains(ALERT),
            "a disconnected 45% is history rather than an alert"
        );
    }

    #[test]
    fn a_dashboard_with_nothing_low_says_so() {
        assert!(screen(&loaded()).contains("all ok"));
    }

    #[test]
    fn the_filter_is_drawn_while_it_is_typed_and_while_it_narrows() {
        let typing = press(loaded(), "/key");
        let kept = update(typing.clone(), Event::Key(Key::Enter));

        assert!(line_containing(&typing, "/key").contains(CURSOR));
        assert!(line_containing(&typing, "/key").contains("1 match"));
        assert!(!line_containing(&kept, "/key").contains(CURSOR));
        assert!(
            drawn(&loaded(), 100, 30)[1].is_empty(),
            "and nowhere to be seen otherwise"
        );
    }

    #[test]
    fn the_footer_follows_the_keys_the_filter_binds() {
        let typing = press(loaded(), "/key");
        let footer = drawn(&typing, 100, 30).last().expect("a footer").clone();

        assert!(footer.contains("esc clear"), "{footer}");
        assert!(footer.contains("enter keep"), "{footer}");
    }

    #[test]
    fn the_charging_glyph_is_the_one_the_dashboard_was_given() {
        let ascii = dashboard();
        let nerd_font = looking(dashboard(), "charging_glyph = \"\u{f0e7}\"");

        assert!(line_containing(&ascii, "Magic Trackpad").contains("+ charging"));
        assert!(
            line_containing(&nerd_font, "Magic Trackpad").contains("\u{f0e7} charging"),
            "a Nerd Font terminal gets the bolt"
        );
    }

    #[test]
    fn no_size_a_terminal_can_be_panics_the_render() {
        let open = update(loaded(), Event::Key(Key::Char('?')));
        let states = [
            app(),
            loaded(),
            open.clone(),
            press(dashboard(), "/a"),
            dashboard(),
        ];

        for app in &states {
            // Densest under the minimum width, which is the band the guarantee
            // is about: a column can be half a cell wide and a line can have
            // nowhere at all to put itself.
            for width in 1..=40 {
                for height in 1..=8 {
                    drawn(app, width, height);
                }
            }
            for width in 1..=120 {
                for height in [1, 3, 30] {
                    drawn(app, width, height);
                }
            }
        }
        for (width, height) in [(1, 1), (5, 5), (1, 30), (20, 3), (40, 10), (200, 60)] {
            drawn(&dashboard(), width, height);
            drawn(&open, width, height);
        }
    }

    #[test]
    fn the_alert_is_drawn_whole_or_not_at_all() {
        let whole = format!("{ALERT}1 critical");

        for width in 1..=120 {
            let screen = drawn(&dashboard(), width, 30).join("\n");

            assert!(
                screen.contains(&whole) || !screen.contains("critical"),
                "a cut alert reads as a different count, at {width}:\n{screen}"
            );
        }
    }

    #[test]
    fn a_table_taller_than_the_terminal_scrolls_with_the_selection() {
        let top = crowd(40);
        let bottom = press(top.clone(), &"j".repeat(39));
        let up = press(bottom.clone(), "k");

        let at_the_end = rows_of(&buffer_of(&[&top, &bottom], 100, 14)).join("\n");
        assert!(
            at_the_end.contains("Device 39"),
            "the table scrolls to the selection\n{at_the_end}"
        );
        assert!(!at_the_end.contains("Device 00"), "{at_the_end}");

        let rows = rows_of(&buffer_of(&[&top, &bottom, &up], 100, 14));
        let marked = rows
            .iter()
            .find(|line| line.contains(MARKER))
            .expect("a marked row");

        assert!(marked.contains("Device 38"), "{marked}");
        assert!(
            rows.iter().any(|line| line.contains("Device 39")),
            "and moving up does not drag the rows above it along\n{}",
            rows.join("\n")
        );
    }

    #[test]
    fn a_dashboard_with_nothing_connected_selects_through_the_inactive_section() {
        let asleep = update(
            app(),
            Event::Reading(reading(
                [
                    ("Magic Trackpad", "30-82-16-f2-24-90"),
                    ("MX Keys M Mac", "de-df-38-f0-46-9b"),
                ]
                .into_iter()
                .map(|(name, address)| Device {
                    connected: false,
                    ..device(name, address, Some(50))
                })
                .collect(),
            )),
        );

        assert!(line_containing(&asleep, "blubat").contains("0 active"));
        assert!(!line_containing(&asleep, "inactive (2)").contains(MARKER));
        assert!(line_containing(&asleep, "Magic Trackpad").contains(MARKER));
        assert!(line_containing(&press(asleep, "j"), "MX Keys M Mac").contains(MARKER));
    }

    /// Colour and weight carry as much of this layout as the glyphs do: the
    /// dimmed inactive section, the level scale, the selection tint and the
    /// critical red are all invisible to a frame compared as text.
    #[test]
    fn the_palette_reaches_the_buffer_it_is_drawn_into() {
        let buffer = buffer_of(&[&press(dashboard(), "j")], 100, 30);
        let cell = |needle| cell_of(&buffer, needle);
        let dark = Palette::DARK;

        assert_eq!(cell("blubat").fg, dark.accent);
        assert!(cell("blubat").modifier.contains(Modifier::BOLD));

        assert_eq!(cell("Soundcore").fg, dark.alert, "a critical name");
        assert_eq!(cell("8%").fg, dark.critical, "and the level under it");
        assert!(cell("8%").modifier.contains(Modifier::BOLD));
        assert_eq!(cell("67%").fg, dark.ok);

        assert_eq!(
            cell("Magic Trackpad").bg,
            dark.selection,
            "the selected row is tinted rather than inverted"
        );
        assert_eq!(cell("MX Keys").bg, Color::Reset);

        assert_eq!(
            cell("AirPods Pro").fg,
            dark.dim,
            "the inactive section is dim throughout"
        );
        assert_eq!(cell("45%").fg, dark.dim);
    }

    /// The whole point of `[theme]`: a scheme and its overrides have to reach
    /// the cells rather than a palette nothing draws with.
    #[test]
    fn the_configured_scheme_and_its_overrides_reach_the_cells() {
        let themed = looking(
            press(dashboard(), "j"),
            "scheme = \"light\"\naccent = \"#39c5cf\"\ncritical = \"#f47067\"",
        );
        let buffer = buffer_of(&[&themed], 100, 30);
        let cell = |needle| cell_of(&buffer, needle);
        let overridden = Color::Rgb(0xf4, 0x70, 0x67);

        assert_eq!(
            cell("blubat").fg,
            Color::Rgb(0x39, 0xc5, 0xcf),
            "the accent"
        );
        assert_eq!(cell("8%").fg, overridden, "the bottom band of the scale");
        assert_eq!(
            cell("Soundcore").fg,
            overridden,
            "and the brighter variant paired with it"
        );
        assert_eq!(
            cell("67%").fg,
            Palette::LIGHT.ok,
            "unwritten, so the scheme's"
        );
        assert_eq!(cell("AirPods Pro").fg, Palette::LIGHT.dim);
        assert_eq!(cell("Magic Trackpad").bg, Palette::LIGHT.selection);
    }

    #[test]
    fn a_notice_takes_a_line_of_its_own_only_while_there_is_one() {
        let quiet = drawn(&loaded(), 100, 30);
        let said = update(
            loaded(),
            Event::Note(Notice::problem("config.toml: expected `=` at line 3")),
        );

        assert!(quiet[1].is_empty(), "no line is spent on nothing to say");
        assert!(
            line_containing(&said, "expected `=`").contains("config.toml"),
            "and the problem is on screen rather than on stderr"
        );
        assert_eq!(
            cell_of(&buffer_of(&[&said], 100, 30), "config.toml").fg,
            Palette::DARK.alert,
            "in the colour of how it went"
        );
        assert_eq!(
            cell_of(
                &buffer_of(
                    &[&update(
                        loaded(),
                        Event::Note(Notice::said("config reloaded"))
                    )],
                    100,
                    30
                ),
                "config reloaded"
            )
            .fg,
            Palette::DARK.accent
        );
    }

    #[test]
    fn every_terminal_wide_enough_keeps_the_name_and_the_level() {
        for width in 20..=120 {
            let screen = drawn(&dashboard(), width, 30).join("\n");

            assert!(screen.contains("8%"), "at {width}:\n{screen}");
        }
    }

    /// The whole detail view at the size the mock was approved at: the panels,
    /// the chart drawn over an injected history, the rate and the estimate
    /// derived from it, the thresholds it is judged by, the event log and the
    /// footer of the keys that work here. One assertion, because the point of
    /// this view is that all of it is on screen at once.
    #[test]
    fn the_detail_view_draws_the_frame_it_is_specified_to_draw() {
        let expected = "╭ blubat | Paul’s Magic Trackpad ──────────────────────────────────────────────────────────────────╮
│╭ power ─────────────────────────────────────────────────────────────────────────────────────────╮│
││ 23%  █████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░  charging, est. 9h 38m to full                     ││
││connected  ·  iokit  ·  last reading now                                                        ││
│╰────────────────────────────────────────────────────────────────────────────────────────────────╯│
│╭ battery, last 2h ─────────────────────────────────────────────╮╭ stats ────────────────────────╮│
││100  │                                                         ││charge rate              8.0%/h││
││     │                                                         ││trend                    rising││
││     │                                                         ││to full                  9h 38m││
││     │                                                         ││                               ││
││     │                                                         ││low                         20%││
││     │                                                         ││critical                    10%││
││50   │                                                         ││charged at                 100%││
││     │                                                         ││                               ││
││     │                                                         ││address       30-82-16-f2-24-90││
││     │••••••••••••••••••••••••••••••••⢀⣀⣀⣀⣀⣀⣀⡠⠤⠤⠤⠤⠤⠤⠔⠒⠒⠒⠒⠒⠒⠊⠉⠉⠉││source                    iokit││
││     │    ⣀⣀⣀⣀⣀⣀⣀⠤⠤⠤⠤⠤⠤⠤⠒⠒⠒⠒⠒⠒⠒⠉⠉⠉⠉⠉⠉⠉⠁                        ││type                   trackpad││
││0    │⠉⠉⠉⠉                                                     ││samples                       9││
││     └─────────────────────────────────────────────────────────││                               ││
││2h ago                                                      now││                               ││
│╰───────────────────────────────────────────────────────────────╯╰───────────────────────────────╯│
│╭ recent events ─────────────────────────────────────────────────────────────────────────────────╮│
││30m ago   critical_battery  at 9%, threshold 20%                                                ││
││1h ago    low_battery       at 19%, threshold 20%                                               ││
││                                                                                                ││
││                                                                                                ││
││                                                                                                ││
│╰────────────────────────────────────────────────────────────────────────────────────────────────╯│
│esc/enter back  q quit                                                                            │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯";
        assert_frame(&detail(), 100, 30, expected);
    }

    /// Each battery a device reports gets a row of its own, under the one level
    /// every threshold is applied to.
    #[test]
    fn a_multi_battery_device_lists_every_battery_it_reports() {
        let rows = drawn(&airpods(), 100, 30);
        let row = |needle: &str| {
            rows.iter()
                .find(|line| line.contains(needle))
                .unwrap_or_else(|| panic!("no row for `{needle}`"))
                .clone()
        };

        assert!(
            row(" 68%").contains("on battery"),
            "the emptiest part leads"
        );
        assert!(row("left").contains("100%"));
        assert!(row("right").contains("97%"));
        assert!(row("case").contains("68%"));
        assert!(
            drawn(&detail(), 100, 30)
                .iter()
                .all(|line| !line.contains("main")),
            "and a single battery device has no sub rows at all"
        );
    }

    #[test]
    fn a_device_with_nothing_to_extrapolate_from_says_so_rather_than_guessing() {
        let unread = airpods();

        assert!(
            drawn(&unread, 100, 30)
                .join("\n")
                .contains("no history yet")
        );
        for label in ["rate ", "trend ", "estimate"] {
            assert!(
                line_containing(&unread, label).contains(theme::UNKNOWN),
                "`{label}` is guessed at rather than left unknown"
            );
        }
    }

    /// The same staleness rule as the table, on the view opened from it.
    #[test]
    fn a_stale_device_is_marked_in_the_detail_view_as_well_as_the_table() {
        let quiet = update(
            press(dashboard(), "jjj"),
            Event::Tick(Timestamp::from_unix(READ_AT.unix() + 3_600)),
        );
        let open = update(quiet.clone(), Event::Key(Key::Enter));

        assert!(line_containing(&open, "last reading").contains("stale"));
        assert!(
            line_containing(&open, "last seen level").contains("45%"),
            "and its level is still labelled as the last seen one"
        );
        assert!(
            !line_containing(&update(dashboard(), Event::Key(Key::Enter)), "last reading")
                .contains("stale"),
            "a reading taken this moment is not"
        );
        assert!(line_containing(&quiet, "AirPods Pro").contains("stale"));
    }

    /// Colour carries the detail view as much as the glyphs do: the accented
    /// panel titles, the level scale on the bar, the chart against its
    /// threshold line and the band each event belongs to are all invisible to a
    /// frame compared as text.
    #[test]
    fn the_palette_reaches_the_detail_view_it_is_drawn_into() {
        let buffer = buffer_of(&[&detail()], 100, 30);
        let cell = |needle| cell_of(&buffer, needle);
        let dark = Palette::DARK;

        assert_eq!(cell("blubat | Paul").fg, dark.accent, "the panel titles");
        assert_eq!(cell("power").fg, dark.accent);
        assert_eq!(cell("recent events").fg, dark.accent);

        assert_eq!(cell(" 23%").fg, dark.low, "the level, on its own scale");
        assert!(cell("23%").modifier.contains(Modifier::BOLD));
        assert_eq!(cell("rising").fg, dark.charging);
        assert_eq!(cell("low ").fg, dark.dim, "a stats label");

        assert_eq!(cell("critical_battery").fg, dark.critical);
        assert_eq!(cell("low_battery").fg, dark.low);
        assert_eq!(cell("30m ago").fg, dark.dim);
    }

    #[test]
    fn no_size_a_terminal_can_be_panics_the_detail_view() {
        for app in [&detail(), &airpods()] {
            for width in 1..=60 {
                for height in 1..=10 {
                    drawn(app, width, height);
                }
            }
            for (width, height) in [(1, 1), (100, 30), (200, 60), (1, 60), (200, 1)] {
                drawn(app, width, height);
            }
        }
    }
}