mirador 1.19.2

An opinionated personal dashboard for your terminal: world clocks, a calendar, weather, tasks, notes, a market watchlist, and live CPU and network graphs.
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
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
//! The agenda: what is actually next, out of a local `.ics` file.
//!
//! The gap this fills was named early and left open for a long time: tasks are
//! self-paced, and a meeting is not. A dashboard that can tell you four things
//! and none of them is "you are in a call in ten minutes" is missing the one
//! with a deadline attached.
//!
//! Deliberately **offline**. mirador does not sign into a calendar server, and
//! is not going to — that is an account, a token to refresh, and a background
//! process with opinions about your credentials. It reads a file. Whatever put
//! the file there is your business: an export, a `vdirsyncer` run, a cron job
//! with `curl`, a symlink into a synced folder.
//!
//! The `calendar` widget beside this one is a date grid and stays that way. One
//! answers "what is the date", the other "what is next", and squeezing both
//! into one panel does neither well.

use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use jiff::civil::Date;
use jiff::tz::TimeZone;
use jiff::{Span, Zoned};
use ratatui::Frame;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind};
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span as TextSpan};
use ratatui::widgets::{List, ListItem, Paragraph};

use crate::config::AgendaConfig;
use crate::frame::{Binding, FRAME_HEIGHT, FRAME_WIDTH};
use crate::ical;
use crate::keymap::{KeysConfig, Meta, PanelKeymap};
use crate::panel::{KeyOutcome, Panel, RenderContext, describe_age};

/// The status shown while a reload is in flight.
///
/// Named rather than repeated because `tick` has to recognise it: a reload that
/// has landed must take its own message down, and only its own. A path put up
/// by `o` has to survive the next background read.
const RELOADING: &str = "reloading…";

/// What the agenda's keys do. The file dialog keeps its own keys.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AgendaAction {
    File,
    Reload,
    Up,
    Down,
    First,
    Last,
    PageUp,
    PageDown,
    ShowPath,
}

/// Every key the panel responds to, under `[agenda.keys]`. The border hint, the
/// status bar and the help overlay are derived from it, so a key the panel
/// reads is a key it advertises.
pub const ACTIONS: &[Meta<AgendaAction>] = &[
    Meta {
        action: AgendaAction::File,
        name: "file",
        defaults: &[(KeyCode::Char('f'), KeyModifiers::NONE)],
        label: "file",
        primary: true,
        joins: false,
        about: "choose the calendar file",
    },
    Meta {
        action: AgendaAction::Reload,
        name: "reload",
        defaults: &[(KeyCode::Char('r'), KeyModifiers::NONE)],
        label: "reload",
        primary: true,
        joins: false,
        about: "read the calendar file again",
    },
    Meta {
        action: AgendaAction::Up,
        name: "up",
        defaults: &[
            (KeyCode::Up, KeyModifiers::NONE),
            (KeyCode::Char('k'), KeyModifiers::NONE),
        ],
        label: "scroll",
        primary: false,
        joins: false,
        about: "scroll up",
    },
    Meta {
        action: AgendaAction::Down,
        name: "down",
        defaults: &[
            (KeyCode::Down, KeyModifiers::NONE),
            (KeyCode::Char('j'), KeyModifiers::NONE),
        ],
        label: "scroll",
        primary: false,
        joins: true,
        about: "scroll down",
    },
    Meta {
        action: AgendaAction::First,
        name: "first",
        defaults: &[
            (KeyCode::Char('g'), KeyModifiers::NONE),
            (KeyCode::Home, KeyModifiers::NONE),
        ],
        label: "first",
        primary: false,
        joins: false,
        about: "scroll to the first event",
    },
    Meta {
        action: AgendaAction::Last,
        name: "last",
        defaults: &[
            (KeyCode::Char('G'), KeyModifiers::NONE),
            (KeyCode::End, KeyModifiers::NONE),
        ],
        label: "last",
        primary: false,
        joins: true,
        about: "scroll to the last event",
    },
    Meta {
        action: AgendaAction::PageUp,
        name: "page_up",
        defaults: &[(KeyCode::PageUp, KeyModifiers::NONE)],
        label: "scroll ten rows",
        primary: false,
        joins: false,
        about: "scroll ten rows up",
    },
    Meta {
        action: AgendaAction::PageDown,
        name: "page_down",
        defaults: &[(KeyCode::PageDown, KeyModifiers::NONE)],
        label: "scroll ten rows",
        primary: false,
        joins: true,
        about: "scroll ten rows down",
    },
    Meta {
        action: AgendaAction::ShowPath,
        name: "show_path",
        defaults: &[(KeyCode::Char('o'), KeyModifiers::NONE)],
        label: "show file path",
        primary: false,
        joins: false,
        about: "show which calendar file is read",
    },
];

/// `[agenda.keys]` laid over [`ACTIONS`], or why it cannot be.
pub fn keymap(keys: &KeysConfig) -> Result<PanelKeymap<AgendaAction>, String> {
    PanelKeymap::new("agenda", ACTIONS, keys)
}

/// The keys the panel starts with, until `build` hands it the config's
/// through [`Panel::set_keys`].
fn default_keys() -> PanelKeymap<AgendaAction> {
    PanelKeymap::defaults("agenda", ACTIONS)
}

/// How close an event has to be before it is worth interrupting for.
///
/// Ten minutes is about the time it takes to finish a thought, find the link
/// and get there. Much longer and the signal is lit for a large part of a
/// working day, which is the failure this whole feature is built to avoid.
const IMMINENT: std::time::Duration = std::time::Duration::from_mins(10);

/// Width at which the panel stops gaining anything: a time, a generous summary
/// and a location beside it.
const USEFUL_WIDTH: u16 = 52;

/// Why the panel has no events.
///
/// Separated because the two want opposite treatments. A panel nobody has
/// pointed at a file is not broken — it is a panel nobody has set up, the same
/// standing as an empty task list — and painting it red teaches the reader to
/// ignore red.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Trouble {
    /// `[agenda].file` names nothing that exists.
    NoFile,
    /// The file is there and reading it went wrong.
    Unreadable(String),
}

/// What the reader thread has produced.
#[derive(Debug, Default)]
struct State {
    events: Vec<ical::Event>,
    /// Why there is nothing to show, if there is nothing to show.
    error: Option<Trouble>,
    /// Lines the parser could not make sense of, so a half-read calendar says
    /// so rather than looking merely empty.
    skipped: usize,
    /// When this was read.
    read_at: Option<Instant>,
    /// The day the window was built around, so a dashboard left open overnight
    /// notices that "today" moved.
    built_for: Option<Date>,
}

#[derive(Debug)]
pub struct AgendaPanel {
    /// `[agenda.keys]` over the defaults.
    keys: PanelKeymap<AgendaAction>,
    state: Arc<Mutex<State>>,
    /// Set to ask the reader thread for an immediate re-read.
    reload: Arc<Mutex<bool>>,
    generation: Arc<AtomicU64>,
    seen: u64,
    stop: Arc<AtomicBool>,
    /// Shared with the reader thread, which re-reads it every cycle, so
    /// pointing the panel at a different calendar is a swap here and a reload
    /// rather than tearing the thread down and starting another.
    path: Arc<Mutex<PathBuf>>,
    days: u16,
    show_location: bool,
    /// The first row on screen, moved by the scroll keys and the wheel.
    offset: usize,
    /// How far `offset` may go, as of the last draw: the rows that did not fit.
    ///
    /// Bounded by what was drawn, the rule `news` and `watchlog` follow,
    /// because only `render` knows the panel's height. It was a `ListState`
    /// whose selection the keys moved and the list was never drawn with, so
    /// the agenda never scrolled at all.
    max_offset: usize,
    status: Option<String>,
    /// The `f` dialog, while it is open.
    asking: Option<crate::prompt::Prompt>,
    /// What the calendar held at the last read, so a new entry can be spotted.
    ///
    /// `None` until the first successful read. That is what stops the whole
    /// calendar being announced at startup: on a first read there is nothing to
    /// have changed *from*, and a log opening with forty entries is a log
    /// nobody reads twice.
    known: Option<std::collections::HashSet<String>>,
    /// Events waiting to be drained by the watch log.
    pending: Vec<crate::watch::Event>,
    /// What the reader thread had published at the last tick.
    ///
    /// Copied once when the generation moves, rather than on every draw. The
    /// panel used to call `snapshot` from `render` and from two key handlers —
    /// two of those cloning the entire event list only to read its `len` —
    /// which measured at 210,000 event clones in thirty idle seconds against a
    /// calendar of three hundred daily meetings. A recurring rule expands, so
    /// the list is far longer than the file suggests.
    shown: State,
}

impl Drop for AgendaPanel {
    /// See `Drop for StocksPanel`: the picker can drop a panel without calling
    /// `shutdown`, and a reader thread with no way to end is a leak.
    fn drop(&mut self) {
        self.stop.store(true, Ordering::Relaxed);
    }
}

impl AgendaPanel {
    pub fn new(config: &AgendaConfig, path: PathBuf) -> Self {
        let state = Arc::new(Mutex::new(State::default()));
        let reload = Arc::new(Mutex::new(false));
        let stop = Arc::new(AtomicBool::new(false));
        let generation = Arc::new(AtomicU64::new(0));

        let days = config.days.clamp(1, 365);
        // A local file is cheap to read, but not free — a year of a shared work
        // calendar is megabytes. The floor keeps a hand-edited `0` from
        // spinning the thread.
        let interval = Duration::from_secs(config.refresh_secs.max(5));

        let shared = (
            Arc::clone(&state),
            Arc::clone(&reload),
            Arc::clone(&stop),
            Arc::clone(&generation),
        );
        let path = Arc::new(Mutex::new(path));
        let thread_path = Arc::clone(&path);
        std::thread::Builder::new()
            .name("mirador-agenda".into())
            .spawn(move || {
                let (state, reload, stop, generation) = shared;
                read_loop(
                    &thread_path,
                    days,
                    interval,
                    &state,
                    &reload,
                    &stop,
                    &generation,
                );
            })
            .expect("spawning the agenda thread");

        Self {
            keys: default_keys(),
            state,
            reload,
            generation,
            seen: 0,
            stop,
            path,
            days,
            show_location: config.show_location,
            offset: 0,
            max_offset: 0,
            status: None,
            asking: None,
            known: None,
            pending: Vec::new(),
            shown: State::default(),
        }
    }

    /// Deal with a keypress while the file prompt is open.
    ///
    /// The answer is checked before it is taken. A path that cannot be read is
    /// almost always a typo, and accepting it would replace a working calendar
    /// with an error message and no way back to what was there — so the prompt
    /// stays open with the text in it and says what went wrong.
    ///
    /// An empty answer is allowed through, and means "no calendar": that is how
    /// you undo this without having to remember the path you started with.
    fn handle_prompt_key(&mut self, key: KeyEvent) {
        let Some(prompt) = self.asking.as_mut() else {
            return;
        };
        match prompt.handle_key(key) {
            // `Chose` cannot arise: this prompt offers no list. It is grouped
            // with `Editing` rather than given its own empty arm because an
            // arm that does nothing invites someone to make it do something.
            crate::prompt::Outcome::Editing | crate::prompt::Outcome::Chose { .. } => {}
            crate::prompt::Outcome::Cancelled => self.asking = None,
            crate::prompt::Outcome::Submitted(answer) => {
                let path = crate::prompt::expand_tilde(&answer);
                if !answer.is_empty()
                    && let Err(e) = std::fs::metadata(&path)
                {
                    prompt.reject(format!("{e}"));
                    return;
                }
                self.set_path(path);
                self.asking = None;
                self.status = Some(RELOADING.into());
            }
        }
    }

    /// Record anything in the calendar that was not there at the last read.
    ///
    /// This is the clearest case the watch log has: an entry you did not add,
    /// which appeared because somebody else put it in a calendar you sync. It
    /// is worth knowing whether or not you were looking at this panel, which is
    /// the whole test for whether something belongs in the log.
    fn note_new_entries(&mut self) {
        let state = &self.shown;
        // A failed read publishes no events; treating that as "everything was
        // cancelled" and then "everything is new" would fill the log with a
        // network blip.
        if state.error.is_some() {
            return;
        }

        let current: std::collections::HashSet<String> = state
            .events
            .iter()
            .map(|event| format!("{}@{}", event.summary, event.start.timestamp()))
            .collect();

        if let Some(known) = self.known.take() {
            for event in &state.events {
                let key = format!("{}@{}", event.summary, event.start.timestamp());
                if !known.contains(&key) {
                    self.pending.push(crate::watch::Event::new(
                        "agenda",
                        format!(
                            "{} appeared in your calendar, {}",
                            event.summary,
                            event.start.strftime("%a %d %b at %H:%M")
                        ),
                    ));
                }
            }
        }
        self.known = Some(current);
    }

    /// Point the panel at a different calendar and read it now.
    pub fn set_path(&mut self, to: PathBuf) {
        match self.path.lock() {
            Ok(mut guard) => *guard = to,
            Err(poisoned) => *poisoned.into_inner() = to,
        }
        // Another calendar starts at its top, not wherever this one was left.
        self.offset = 0;
        self.ask_for_reload();
    }

    fn snapshot(&self) -> State {
        match self.state.lock() {
            Ok(guard) => State {
                events: guard.events.clone(),
                error: guard.error.clone(),
                skipped: guard.skipped,
                read_at: guard.read_at,
                built_for: guard.built_for,
            },
            Err(poisoned) => {
                let guard = poisoned.into_inner();
                State {
                    events: guard.events.clone(),
                    error: guard.error.clone(),
                    skipped: guard.skipped,
                    read_at: guard.read_at,
                    built_for: guard.built_for,
                }
            }
        }
    }

    fn ask_for_reload(&self) {
        match self.reload.lock() {
            Ok(mut flag) => *flag = true,
            Err(poisoned) => *poisoned.into_inner() = true,
        }
    }

    /// The rows to draw: a heading per day, then that day's events.
    ///
    /// A flat sequence rather than a tree because a heading and an event
    /// scroll together, one row each, which is all the offset needs.
    ///
    /// An iterator over borrowed events, not a `Vec` of cloned ones. `render`
    /// counts it to bound the scroll and then builds lines for the rows on
    /// screen only, so a frame costs what fits in the panel rather than what is
    /// in the calendar — it used to clone and format every event in the window,
    /// once a second.
    fn rows<'a>(state: &'a State, now: &'a Zoned) -> impl Iterator<Item = Row<'a>> + 'a {
        let mut current: Option<Date> = None;
        state.events.iter().flat_map(move |event| {
            let day = event.start.date();
            let heading = (current != Some(day)).then(|| {
                current = Some(day);
                Row::Day(day)
            });
            heading.into_iter().chain(std::iter::once(Row::Event {
                event,
                in_progress: event.contains(now),
            }))
        })
    }

    fn scroll_down(&mut self, rows: usize) {
        self.offset = self.offset.saturating_add(rows).min(self.max_offset);
    }

    fn scroll_up(&mut self, rows: usize) {
        self.offset = self.offset.saturating_sub(rows);
    }
}

enum Row<'a> {
    Day(Date),
    Event {
        event: &'a ical::Event,
        in_progress: bool,
    },
}

/// How a day is introduced. "Today" and "Tomorrow" beat a date you have to
/// work out, and the date is still there for everything else.
fn day_label(day: Date, today: Date) -> String {
    let delta = (day - today).get_days();
    match delta {
        0 => "TODAY".to_string(),
        1 => "TOMORROW".to_string(),
        _ => format!("{} {}", weekday_name(day), day.strftime("%-d %b")),
    }
}

fn weekday_name(day: Date) -> &'static str {
    match day.weekday() {
        jiff::civil::Weekday::Monday => "MONDAY",
        jiff::civil::Weekday::Tuesday => "TUESDAY",
        jiff::civil::Weekday::Wednesday => "WEDNESDAY",
        jiff::civil::Weekday::Thursday => "THURSDAY",
        jiff::civil::Weekday::Friday => "FRIDAY",
        jiff::civil::Weekday::Saturday => "SATURDAY",
        jiff::civil::Weekday::Sunday => "SUNDAY",
    }
}

/// Largest `.ics` that will be read.
///
/// The network side of this program is bounded — `ureq` stops at 10MB — and the
/// local side was not bounded at all. A calendar is a file somebody else's
/// software writes, it grows without anyone deciding to, and reading it costs
/// more than its size: unfolding turns it into a `Vec<String>`, and a recurring
/// rule expands further still. Matching the network figure keeps one number to
/// remember.
///
/// A year of a busy calendar is a few megabytes, so this refuses nothing real.
const MAX_CALENDAR: u64 = 10 * 1024 * 1024;

/// Read the calendar, refusing one too large to be a calendar.
///
/// Checked before reading rather than after: the point is not to notice that
/// something enormous was loaded, it is not to load it.
fn read_calendar(path: &std::path::Path) -> std::io::Result<String> {
    let size = std::fs::metadata(path)?.len();
    if size > MAX_CALENDAR {
        return Err(std::io::Error::other(format!(
            "the calendar is {} MB, over the {} MB limit — mirador reads a \
             calendar into memory, so it will not open one this large",
            size / (1024 * 1024),
            MAX_CALENDAR / (1024 * 1024)
        )));
    }
    std::fs::read_to_string(path)
}

/// The path as it stands, which the panel may have changed since the last pass.
fn current_path(path: &Arc<Mutex<PathBuf>>) -> PathBuf {
    match path.lock() {
        Ok(guard) => guard.clone(),
        Err(poisoned) => poisoned.into_inner().clone(),
    }
}

/// Read the file, parse it, and publish — then wait, and do it again.
fn read_loop(
    path: &Arc<Mutex<PathBuf>>,
    days: u16,
    interval: Duration,
    state: &Arc<Mutex<State>>,
    reload: &Arc<Mutex<bool>>,
    stop: &Arc<AtomicBool>,
    generation: &Arc<AtomicU64>,
) {
    while !stop.load(Ordering::Relaxed) {
        let tz = TimeZone::system();
        let today = ical::today(&tz);
        let from = ical::local_midnight(today, &tz);
        let until = today
            .checked_add(Span::new().days(i64::from(days)))
            .ok()
            .and_then(|d| ical::local_midnight(d, &tz));

        let next = match (from, until) {
            (Some(from), Some(until)) => match read_calendar(&current_path(path)) {
                Ok(text) => {
                    let calendar = ical::parse(&text, &tz, &from, &until);
                    State {
                        events: calendar.events,
                        error: None,
                        skipped: calendar.skipped.len(),
                        read_at: Some(Instant::now()),
                        built_for: Some(today),
                    }
                }
                // A missing file is the unconfigured case, not a failure.
                // Anything else — a permission problem, a directory where a
                // file should be — keeps the message the OS gave, which is the
                // one that says what to do about it.
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => State {
                    error: Some(Trouble::NoFile),
                    read_at: Some(Instant::now()),
                    built_for: Some(today),
                    ..State::default()
                },
                Err(e) => State {
                    error: Some(Trouble::Unreadable(format!("{e}"))),
                    read_at: Some(Instant::now()),
                    built_for: Some(today),
                    ..State::default()
                },
            },
            _ => State {
                error: Some(Trouble::Unreadable(
                    "could not work out today's date".into(),
                )),
                read_at: Some(Instant::now()),
                built_for: Some(today),
                ..State::default()
            },
        };

        match state.lock() {
            Ok(mut guard) => *guard = next,
            Err(poisoned) => *poisoned.into_inner() = next,
        }
        generation.fetch_add(1, Ordering::Release);

        let woke = crate::poll::wait(interval, stop, || match reload.lock() {
            Ok(mut flag) => std::mem::replace(&mut *flag, false),
            Err(poisoned) => std::mem::replace(&mut *poisoned.into_inner(), false),
        });
        if woke == crate::poll::Wake::Stop {
            return;
        }
    }
}

impl Panel for AgendaPanel {
    fn title(&self) -> String {
        "Agenda".to_string()
    }

    fn counter(&self) -> Option<String> {
        // Reads the cache, not the mutex. `counter` is called on *every* frame
        // by the frame renderer, with nothing guarding it — cloning an expanded
        // recurring calendar here was the most expensive of the three.
        let state = &self.shown;
        match &state.error {
            Some(Trouble::NoFile) => return Some("not set up".into()),
            Some(Trouble::Unreadable(_)) => return Some("unreadable".into()),
            None => {}
        }
        let today = state.built_for?;
        let n = state
            .events
            .iter()
            .filter(|e| e.start.date() == today)
            .count();
        Some(match n {
            0 => "clear".to_string(),
            1 => "1 today".to_string(),
            n => format!("{n} today"),
        })
    }

    fn bindings(&self) -> &[Binding] {
        self.keys.bindings()
    }

    fn set_keys(&mut self, config: &crate::config::Config) {
        self.keys = PanelKeymap::or_defaults("agenda", ACTIONS, &config.agenda.keys);
    }

    fn max_width(&self) -> Option<u16> {
        // Past this the summaries stop being the constraint and the row just
        // grows a gap in the middle; the graphs next door can use it.
        Some(USEFUL_WIDTH + FRAME_WIDTH)
    }

    fn refresh_interval(&self) -> Duration {
        // The reader thread owns the real cadence. This only decides how
        // quickly a completed read reaches the screen, and how often the
        // in-progress marker is re-evaluated.
        Duration::from_secs(20)
    }

    fn tick(&mut self) -> bool {
        // Two things can change without a keypress: a read landing, and an
        // event starting or ending. The first is a counter; the second is why
        // this returns true on the day rolling over even when nothing was read.
        let now = self.generation.load(Ordering::Acquire);
        let moved = now != self.seen;
        self.seen = now;
        if moved {
            // One copy, at the one moment the data can have changed.
            let before = self.shown.built_for;
            self.shown = self.snapshot();
            // A new day is a new agenda. A scroll left over from yesterday
            // would hide the top of today's — the events happening now — on a
            // dashboard left open overnight.
            if self.shown.built_for != before {
                self.offset = 0;
            }
            self.note_new_entries();
            // The generation only moves when a read lands, so this is where a
            // reload finishes. Taking the message down here rather than on the
            // next keypress matters because a dashboard is read without being
            // touched: left up, an idle panel claims to be mid-operation, which
            // reads as a hang rather than as a stale label.
            //
            // Only its own message. A path put up by `o` has to survive the
            // next background read.
            if self.status.as_deref() == Some(RELOADING) {
                self.status = None;
            }
        }
        moved
    }

    fn alert(&self) -> Option<crate::panel::Alert> {
        let now = Zoned::now();

        // Reads the events under the lock rather than through `snapshot`, and
        // the difference is not stylistic. `snapshot` clones the whole event
        // list, and this runs on every draw — measured at 39 calls in thirty
        // idle seconds, so a twelve-event calendar cloned 456 events, each with
        // one or two `String`s, for nothing. A real week of meetings would be
        // several times that, for ever. Panel::alert's own documentation says
        // it must not allocate when it has nothing to say, which is nearly
        // always; this is that promise kept.
        let guard = match self.state.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        if guard.error.is_some() {
            return None;
        }

        // The nearest event that has not started and starts within the window.
        // An event already under way is deliberately not an alert: you are
        // either in it or you have missed it, and a dashboard telling you about
        // a meeting you are sitting in is noise.
        let (until, event) = guard
            .events
            .iter()
            .filter(|event| !event.all_day)
            .filter_map(|event| {
                let until = event.start.duration_since(&now);
                let until = std::time::Duration::try_from(until).ok()?;
                (until <= IMMINENT).then_some((until, event))
            })
            .min_by_key(|(until, _)| *until)?;

        let minutes = until.as_secs() / 60;
        let when = if minutes == 0 {
            "now".to_string()
        } else {
            format!("in {minutes}m")
        };
        Some(match &event.location {
            Some(place) => crate::panel::Alert::soon(format!("{} {when} · {place}", event.summary)),
            None => crate::panel::Alert::soon(format!("{} {when}", event.summary)),
        })
    }

    fn events(&mut self) -> Vec<crate::watch::Event> {
        std::mem::take(&mut self.pending)
    }

    fn overlay(&self) -> Option<&crate::prompt::Prompt> {
        self.asking.as_ref()
    }

    fn captures_input(&self) -> bool {
        self.asking.is_some()
    }

    fn handle_key(&mut self, key: KeyEvent) -> KeyOutcome {
        if self.asking.is_some() {
            self.handle_prompt_key(key);
            return KeyOutcome::Consumed;
        }

        self.status = None;
        let Some(action) = self.keys.action(key) else {
            return KeyOutcome::Ignored;
        };
        match action {
            AgendaAction::File => {
                self.asking = Some(crate::prompt::Prompt::new(
                    "AGENDA FILE",
                    "Tab completes · Enter saves · Esc cancels",
                    &current_path(&self.path).display().to_string(),
                    crate::prompt::Completion::Paths,
                ));
            }
            AgendaAction::Reload => {
                self.ask_for_reload();
                self.status = Some(RELOADING.into());
            }
            AgendaAction::ShowPath => {
                self.status = Some(current_path(&self.path).display().to_string());
            }
            AgendaAction::Down => self.scroll_down(1),
            AgendaAction::Up => self.scroll_up(1),
            AgendaAction::PageDown => self.scroll_down(10),
            AgendaAction::PageUp => self.scroll_up(10),
            AgendaAction::Last => self.scroll_down(usize::MAX),
            AgendaAction::First => self.scroll_up(usize::MAX),
        }
        KeyOutcome::Consumed
    }

    fn handle_mouse(&mut self, event: MouseEvent, _area: Rect) -> KeyOutcome {
        match event.kind {
            MouseEventKind::ScrollDown => self.scroll_down(1),
            MouseEventKind::ScrollUp => self.scroll_up(1),
            _ => return KeyOutcome::Ignored,
        }
        KeyOutcome::Consumed
    }

    fn render(&mut self, frame: &mut Frame, area: Rect, ctx: RenderContext<'_>) {
        let theme = ctx.theme;
        if area.width == 0 || area.height == 0 {
            return;
        }

        let state = &self.shown;
        let now = Zoned::now();

        // Notices take rows from the bottom, and the list is given what is
        // left. Drawing the list across the whole panel and painting over it
        // afterwards is what put `1 entry could not be readndred` on screen:
        // the notice landed on top of an event instead of beside it.
        let mut notices: Vec<Line<'static>> = Vec::new();
        if state.skipped > 0 {
            notices.push(Line::from(TextSpan::styled(
                format!(
                    "{} entr{} could not be read",
                    state.skipped,
                    if state.skipped == 1 { "y" } else { "ies" }
                ),
                Style::default().fg(theme.error),
            )));
        }
        if let Some(message) = &self.status {
            notices.push(Line::from(TextSpan::styled(
                message.clone(),
                Style::default().fg(theme.muted),
            )));
        }

        let (list_area, notice_area) =
            split_for_notices(area, u16::try_from(notices.len()).unwrap_or(0));

        // Bound the scroll against this frame's rows and height before drawing:
        // a re-read can shorten the calendar and a resize can lengthen the
        // panel, and either leaves the old offset showing blank rows.
        let rows = if state.error.is_some() {
            0
        } else {
            Self::rows(state, &now).count()
        };
        self.max_offset = rows.saturating_sub(usize::from(list_area.height));
        self.offset = self.offset.min(self.max_offset);
        self.draw_list(frame, list_area, state, &now, theme);

        if notice_area.height > 0 {
            frame.render_widget(Paragraph::new(notices), notice_area);
        }
    }

    fn shutdown(&mut self) {
        self.stop.store(true, Ordering::Relaxed);
    }

    fn remember(&self, state: &mut crate::state::UiState) {
        state.agenda_file = Some(current_path(&self.path).display().to_string());
    }
}

impl AgendaPanel {
    fn draw_list(
        &self,
        frame: &mut Frame,
        area: Rect,
        state: &State,
        now: &Zoned,
        theme: &crate::theme::Theme,
    ) {
        if let Some(trouble) = &state.error {
            let muted = Style::default().fg(theme.muted);
            let mut lines = match trouble {
                // Not an error, and not red. This is a panel nobody has set up
                // yet, which is an ordinary state — the same standing as an
                // empty task list.
                Trouble::NoFile => {
                    let mut lines = vec![
                        Line::from(TextSpan::styled(
                            "No agenda file",
                            Style::default().fg(theme.text).add_modifier(Modifier::BOLD),
                        )),
                        Line::from(TextSpan::styled("Nothing to show.", muted)),
                        Line::from(""),
                    ];
                    lines.extend(wrapped_lines(
                        "Set [agenda].file to an .ics you already have — an export, \
                         or whatever your calendar syncs to, or press f.",
                        area.width,
                        muted,
                    ));
                    lines.push(Line::from(""));
                    // Wrapped, not truncated. This line exists to tell you
                    // where to put the file, and a path cut off at the panel
                    // edge — `Looked in /var/folders/zj/blsvny` — fails at the
                    // one job the message has.
                    lines.extend(wrapped_lines(
                        &format!("Looked in {}", current_path(&self.path).display()),
                        area.width,
                        muted,
                    ));
                    lines
                }
                // This one *is* a fault: the file is there and something went
                // wrong reading it.
                Trouble::Unreadable(why) => {
                    let mut lines = vec![Line::from(TextSpan::styled(
                        "Cannot read the agenda file",
                        Style::default()
                            .fg(theme.error)
                            .add_modifier(Modifier::BOLD),
                    ))];
                    // The reason comes off the filesystem and can be any
                    // length; the path is the reader's own and can be deep.
                    // Neither is ours to truncate.
                    lines.extend(wrapped_lines(why, area.width, muted));
                    lines.push(Line::from(""));
                    lines.extend(wrapped_lines(
                        &current_path(&self.path).display().to_string(),
                        area.width,
                        muted,
                    ));
                    lines
                }
            };
            if let Some(age) = state.read_at.map(|at| describe_age(at.elapsed())) {
                lines.push(Line::from(TextSpan::styled(
                    format!("checked {age}"),
                    muted,
                )));
            }
            frame.render_widget(Paragraph::new(lines), area);
            return;
        }

        if state.events.is_empty() {
            let horizon = if self.days == 1 {
                "today".to_string()
            } else {
                format!("the next {} days", self.days)
            };
            // Wrapped, not hand-broken. Two lines written to fit a panel of the
            // author's imagination lose a word each in a narrower one, and the
            // pair still reads as a whole sentence — `Nothing schedule` above
            // `in the next 7 da` looks like a rendering glitch, where the same
            // words wrapped honestly just take four rows.
            let mut lines = wrapped_lines(
                "Nothing scheduled",
                area.width,
                Style::default().fg(theme.text).add_modifier(Modifier::BOLD),
            );
            lines.extend(wrapped_lines(
                &format!("in {horizon}."),
                area.width,
                Style::default().fg(theme.muted),
            ));
            frame.render_widget(Paragraph::new(lines), area);
            return;
        }

        self.draw_events(frame, area, state, now, theme);
    }

    /// The events themselves, a heading per day, from the scroll offset down.
    fn draw_events(
        &self,
        frame: &mut Frame,
        area: Rect,
        state: &State,
        now: &Zoned,
        theme: &crate::theme::Theme,
    ) {
        let today = state.built_for.unwrap_or_else(|| now.date());

        // Scrolled into a day, its heading stays on top, covering the row that
        // has just scrolled under it: an event on its own says `09:00` and not
        // which of seven days it is. Covering rather than pushing down keeps
        // every press moving the view, and keeps the last row reachable at the
        // same offset as without it.
        //
        // Not when the row below is the next day's heading. The covered event
        // was that day's last, and its heading over nothing would say the day
        // is empty, under a border counting its events; the event shows bare
        // for that one step instead. Nor in a one-row list, where the heading
        // would be all there ever was.
        let height = usize::from(area.height);
        let mut rows = Self::rows(state, now).skip(self.offset).peekable();
        let top = rows.next().map(|row| match row {
            Row::Event { event, .. }
                if self.offset > 0
                    && height >= 2
                    && matches!(rows.peek(), Some(Row::Event { .. })) =>
            {
                Row::Day(event.start.date())
            }
            row => row,
        });
        let items: Vec<ListItem> = top
            .into_iter()
            .chain(rows.take(height.saturating_sub(1)))
            .map(|row| match row {
                Row::Day(day) => ListItem::new(Line::from(TextSpan::styled(
                    crate::grid::truncate(
                        &crate::glyphs::utility(&day_label(day, today)),
                        usize::from(area.width),
                    ),
                    Style::default()
                        .fg(theme.label)
                        .add_modifier(Modifier::BOLD),
                ))),
                Row::Event { event, in_progress } => ListItem::new(event_line(
                    event,
                    in_progress,
                    self.show_location,
                    area.width,
                    theme,
                )),
            })
            .collect();

        frame.render_widget(List::new(items), area);
    }
}

/// Divide the panel between the list and the notices below it.
///
/// The two never overlap and together cover `area`. Getting that wrong does not
/// look like a layout bug — the notice lands on top of the last event and the
/// tail of it shows through, which reads as a corrupt terminal:
///
/// ```text
///   20:00  Meeting at 20 hundred
/// 1 entry could not be readndred
/// ```
///
/// The list always keeps at least one row: a panel showing only a complaint has
/// hidden the thing the complaint is about.
fn split_for_notices(area: Rect, notices: u16) -> (Rect, Rect) {
    let reserved = notices.min(area.height.saturating_sub(1));
    let list = Rect {
        height: area.height - reserved,
        ..area
    };
    let notice = Rect {
        y: area.y + list.height,
        height: reserved,
        ..area
    };
    (list, notice)
}

/// One event, as a row.
fn event_line(
    event: &ical::Event,
    in_progress: bool,
    show_location: bool,
    width: u16,
    theme: &crate::theme::Theme,
) -> Line<'static> {
    // A fixed-width time column, so the summaries line up whether or not the
    // day mixes all-day entries with timed ones.
    const TIME_WIDTH: usize = 6;

    let time = if event.all_day {
        "all day".to_string()
    } else {
        event.start.strftime("%H:%M").to_string()
    };

    let marker = if in_progress { "▸ " } else { "  " };
    let time_style = if in_progress {
        Style::default()
            .fg(theme.accent)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(theme.muted)
    };
    let summary_style = if in_progress {
        Style::default().fg(theme.text).add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(theme.text)
    };

    let used = marker.len() + TIME_WIDTH.max(crate::grid::display_width(&time)) + 1;
    let room = usize::from(width).saturating_sub(used);

    let mut text = event.summary.clone();
    if show_location && let Some(location) = &event.location {
        // Only when there is genuinely room: a location that squeezes the
        // summary to nothing has cost more than it gave.
        let combined = format!("{text}  ·  {location}");
        if crate::grid::display_width(&combined) <= room {
            text = combined;
        }
    }

    // The summary is already cut to the room left beside the time; the time
    // and its marker are a value of their own, and below nine columns they
    // drop whole rather than leaving `09:1` for the terminal to finish.
    crate::grid::assemble(
        vec![
            vec![
                TextSpan::styled(marker.to_string(), time_style),
                TextSpan::styled(format!("{time:<TIME_WIDTH$} "), time_style),
            ],
            vec![TextSpan::styled(
                crate::grid::truncate(&text, room),
                summary_style,
            )],
        ],
        width,
    )
}

/// One styled `Line` per row of `text` wrapped to `width`.
///
/// The empty-state messages used to be hand-wrapped into fixed lines and the
/// path printed as one long line, so both were cut at the panel edge by the
/// renderer. A message that says where to put your calendar file is worth
/// nothing if you cannot read the path, and prose broken for a 40-cell panel
/// reads badly in a 30-cell one.
fn wrapped_lines(text: &str, width: u16, style: Style) -> Vec<Line<'static>> {
    crate::grid::wrap(text, usize::from(width))
        .into_iter()
        .map(|row| Line::from(TextSpan::styled(row, style)))
        .collect()
}

/// Rows the frame costs, re-exported so `max_height` reads the same as the
/// other panels even though this one does not declare a maximum.
const _: u16 = FRAME_HEIGHT;

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

    /// Every key in the map works and is advertised; see
    /// [`crate::keymap::assert_every_key_works`].
    #[test]
    fn every_key_in_the_map_works_and_is_advertised() {
        crate::keymap::assert_every_key_works(&default_keys(), |event| {
            idle_panel().handle_key(event)
        });
    }
    use jiff::civil::date;

    /// `f` asks for a path in place. A path that does not exist is refused
    /// *inside the prompt*, keeping what was typed; Esc backs out; a path
    /// that exists is taken and a reload is asked for. None of
    /// `handle_prompt_key`, `set_path` or `ask_for_reload` had been executed
    /// by a test.
    #[test]
    fn f_asks_for_a_path_and_a_missing_file_is_refused_where_it_was_typed() {
        let dir =
            std::env::temp_dir().join(format!("mirador-agenda-prompt-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let ics = dir.join("cal.ics");
        let mut panel = AgendaPanel::new(&AgendaConfig::default(), ics.clone());
        let key = |code| KeyEvent::new(code, ratatui::crossterm::event::KeyModifiers::NONE);

        panel.handle_key(key(KeyCode::Char('f')));
        assert!(panel.asking.is_some(), "`f` opens the prompt");
        panel.handle_key(key(KeyCode::Enter));
        let prompt = panel
            .asking
            .as_ref()
            .expect("a missing file keeps the prompt open");
        assert_eq!(
            prompt.value(),
            ics.display().to_string(),
            "and keeps what was typed"
        );
        panel.handle_key(key(KeyCode::Esc));
        assert!(panel.asking.is_none(), "Esc backs out");

        std::fs::write(&ics, "BEGIN:VCALENDAR\nEND:VCALENDAR\n").unwrap();
        panel.handle_key(key(KeyCode::Char('f')));
        panel.handle_key(key(KeyCode::Enter));
        assert!(panel.asking.is_none(), "a file that exists is taken");
        assert_eq!(
            panel.status.as_deref(),
            Some(RELOADING),
            "and a reload is under way"
        );
        assert_eq!(current_path(&panel.path), ics);
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// The scroll keys and the wheel have to move what is on screen. Until
    /// 1.19.2 they moved a `ListState` the list was never drawn with, and the
    /// test that covered them asserted that state rather than the screen — so
    /// it passed with nothing moving, and every event below the bottom of the
    /// panel was out of reach. This one reads the rendered rows.
    #[test]
    fn scrolling_moves_what_is_on_screen_and_stops_at_both_ends() {
        let mut panel =
            AgendaPanel::new(&AgendaConfig::default(), PathBuf::from("/nonexistent.ics"));
        let today = Zoned::now().date();
        let tomorrow = today.tomorrow().expect("a tomorrow");
        // Two headings and twelve events: fourteen rows in a panel of six.
        panel.shown.events = (0..12)
            .map(|i: i8| {
                let day = if i < 6 { today } else { tomorrow };
                event(day, 8 + i % 6, &format!("event {i:02}"), false)
            })
            .collect();
        panel.shown.built_for = Some(today);
        let key = |panel: &mut AgendaPanel, code| {
            panel.handle_key(KeyEvent::from(code));
        };
        let wheel = |panel: &mut AgendaPanel, kind| {
            let event = MouseEvent {
                kind,
                column: 1,
                row: 1,
                modifiers: ratatui::crossterm::event::KeyModifiers::NONE,
            };
            panel.handle_mouse(event, Rect::new(0, 0, 40, 6));
        };

        let first = screen(&mut panel, 40, 6);
        assert!(first[1].contains("event 00"), "{first:#?}");
        assert!(
            !first.iter().any(|row| row.contains("event 05")),
            "{first:#?}"
        );

        key(&mut panel, KeyCode::Char('j'));
        let moved = screen(&mut panel, 40, 6);
        assert!(
            moved[0].contains("TODAY"),
            "the day stays on top: {moved:#?}"
        );
        assert!(
            moved[1].contains("event 01"),
            "j moves the view a row: {moved:#?}"
        );
        assert!(moved[5].contains("event 05"), "{moved:#?}");

        for _ in 0..50 {
            wheel(&mut panel, MouseEventKind::ScrollDown);
        }
        let bottom = screen(&mut panel, 40, 6);
        assert!(
            bottom[5].contains("event 11"),
            "the last event is reachable: {bottom:#?}"
        );
        assert!(
            bottom[0].contains("TOMORROW"),
            "under its day's heading: {bottom:#?}"
        );
        assert!(
            bottom[1].contains("event 07"),
            "and the view stops there: {bottom:#?}"
        );

        key(&mut panel, KeyCode::Char('j'));
        assert_eq!(
            screen(&mut panel, 40, 6),
            bottom,
            "past the end, nothing moves"
        );

        key(&mut panel, KeyCode::Char('g'));
        assert_eq!(screen(&mut panel, 40, 6), first, "g returns to the top");

        key(&mut panel, KeyCode::PageDown);
        assert_eq!(
            screen(&mut panel, 40, 6),
            bottom,
            "ten rows is past the end here"
        );

        for _ in 0..50 {
            wheel(&mut panel, MouseEventKind::ScrollUp);
        }
        assert_eq!(
            screen(&mut panel, 40, 6),
            first,
            "and the wheel stops at the top"
        );

        key(&mut panel, KeyCode::Char('G'));
        assert_eq!(
            screen(&mut panel, 40, 6),
            bottom,
            "G goes to the last event"
        );
    }

    /// At every step from the top to the bottom, no heading sits directly on
    /// another. A day's heading kept on top over its last event would have read
    /// as "nothing today" above `TOMORROW`, under a border counting today's
    /// events; at that one step the event shows instead.
    #[test]
    fn a_heading_never_stands_over_nothing_while_scrolling() {
        let mut panel =
            AgendaPanel::new(&AgendaConfig::default(), PathBuf::from("/nonexistent.ics"));
        let today = Zoned::now().date();
        let tomorrow = today.tomorrow().expect("a tomorrow");
        panel.shown.events = (0..8)
            .map(|i: i8| {
                let day = if i < 4 { today } else { tomorrow };
                event(day, 8 + i % 4, &format!("event {i:02}"), false)
            })
            .collect();
        panel.shown.built_for = Some(today);
        let heading = |row: &str| row.contains("TODAY") || row.contains("TOMORROW");

        for height in 2..=6 {
            panel.handle_key(KeyEvent::from(KeyCode::Char('g')));
            for step in 0..12 {
                let rows = screen(&mut panel, 40, height);
                for pair in rows.windows(2) {
                    assert!(
                        !(heading(&pair[0]) && heading(&pair[1])),
                        "height {height}, step {step}: {rows:#?}"
                    );
                }
                panel.handle_key(KeyEvent::from(KeyCode::Char('j')));
            }
        }
    }

    /// A scroll is bounded again on every draw, because the panel can grow and
    /// the calendar can shrink under it. Left alone, an offset past the new
    /// end shows blank rows with the top of the agenda out of reach of `k`.
    #[test]
    fn a_taller_panel_or_a_shorter_calendar_pulls_the_scroll_back() {
        let mut panel =
            AgendaPanel::new(&AgendaConfig::default(), PathBuf::from("/nonexistent.ics"));
        let today = Zoned::now().date();
        panel.shown.events = (0..12)
            .map(|i: i8| event(today, 8 + i, &format!("event {i:02}"), false))
            .collect();
        panel.shown.built_for = Some(today);
        screen(&mut panel, 40, 6);
        panel.handle_key(KeyEvent::from(KeyCode::Char('G')));
        screen(&mut panel, 40, 6);

        let taller = screen(&mut panel, 40, 13);
        assert!(taller[0].contains("TODAY"), "{taller:#?}");
        assert!(
            taller[1].contains("event 00"),
            "all of it fits again: {taller:#?}"
        );

        panel.handle_key(KeyEvent::from(KeyCode::Char('G')));
        screen(&mut panel, 40, 6);
        panel.shown.events.truncate(3);
        let shorter = screen(&mut panel, 40, 6);
        assert!(shorter[1].contains("event 00"), "{shorter:#?}");
        assert!(shorter[3].contains("event 02"), "{shorter:#?}");
    }

    /// The panel drawn at `width` by `height`, one string per row.
    fn screen(panel: &mut AgendaPanel, width: u16, height: u16) -> Vec<String> {
        use crate::panel::RenderContext;
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let theme = crate::theme::Theme::default();
        let gradients = theme.gradients();
        let mut terminal = Terminal::new(TestBackend::new(width, height)).expect("backend");
        terminal
            .draw(|f| {
                panel.render(
                    f,
                    Rect::new(0, 0, width, height),
                    RenderContext {
                        theme: &theme,
                        gradients: &gradients,
                        focused: true,
                        watch: &crate::watch::WatchLog::default(),
                    },
                );
            })
            .expect("draws");
        let buffer = terminal.backend().buffer();
        (0..height)
            .map(|y| (0..width).map(|x| buffer[(x, y)].symbol()).collect())
            .collect()
    }

    fn tz() -> TimeZone {
        TimeZone::get("America/New_York").unwrap()
    }

    fn event(day: Date, hour: i8, summary: &str, all_day: bool) -> ical::Event {
        let start = day.at(hour, 0, 0, 0).to_zoned(tz()).unwrap();
        ical::Event {
            summary: summary.to_string(),
            location: None,
            end: start.checked_add(Span::new().hours(1)).ok(),
            start,
            all_day,
        }
    }

    /// The empty-state message exists to say where to put your calendar, so a
    /// path cut off at the panel edge fails at the only job it has. It used to
    /// be one long `Line` and the renderer clipped it — the dashboard showed
    /// `Looked in /var/folders/zj/blsvny` and stopped.
    ///
    /// Asserted on the drawn buffer rather than the lines, because clipping
    /// happens at draw time: a test that inspected the `Line` would pass with
    /// the defect in place, which is a trap this repository has fallen into
    /// twice.
    #[test]
    fn the_path_in_the_empty_message_is_wrapped_rather_than_cut_off() {
        use crate::panel::RenderContext;
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;

        let dir = std::env::temp_dir().join(format!("mirador-agendapath-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).expect("temp dir");
        let missing = dir.join("no-such-calendar.ics");

        let theme = crate::theme::Theme::default();
        let gradients = theme.gradients();

        for width in [24u16, 30, 40, 60] {
            let mut panel =
                AgendaPanel::new(&crate::config::AgendaConfig::default(), missing.clone());
            // Let the reader thread notice the file is absent.
            for _ in 0..50 {
                if panel.tick() {
                    break;
                }
                std::thread::sleep(std::time::Duration::from_millis(20));
            }

            let mut terminal = Terminal::new(TestBackend::new(width, 24)).expect("backend");
            terminal
                .draw(|f| {
                    panel.render(
                        f,
                        Rect::new(0, 0, width, 24),
                        RenderContext {
                            theme: &theme,
                            gradients: &gradients,
                            focused: true,
                            watch: &crate::watch::WatchLog::default(),
                        },
                    );
                })
                .expect("draws");

            let buffer = terminal.backend().buffer();
            let rows: Vec<String> = (0..24)
                .map(|y| {
                    (0..width)
                        .map(|x| buffer[(x, y)].symbol())
                        .collect::<String>()
                })
                .collect();
            let screen = rows.join("\n");

            // Nothing may be drawn wider than the panel.
            for row in &rows {
                assert!(
                    crate::grid::display_width(row.trim_end()) <= usize::from(width),
                    "a row overflowed at width {width}: {row:?}"
                );
            }

            // The end of the path has to survive. A clipped path stops before
            // its filename, which is exactly the part that tells you what to
            // create.
            let joined: String = rows.iter().map(|r| r.trim_end()).collect();
            assert!(
                joined.contains("no-such-calendar.ics"),
                "the path was cut before its filename at width {width}:\n{screen}"
            );
        }

        let _ = std::fs::remove_dir_all(&dir);
    }

    /// A calendar is written by somebody else's software and grows without
    /// anyone deciding to. Reading it costs more than its size — unfolding
    /// makes a `Vec<String>` of it and a recurring rule expands further — so
    /// the size is checked before the read rather than regretted after it.
    #[test]
    fn a_calendar_too_large_to_be_a_calendar_is_refused_before_it_is_read() {
        let dir = std::env::temp_dir().join(format!("mirador-ics-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).expect("test directory");
        let path = dir.join("big.ics");

        // Sparse where the filesystem allows it, so this costs no real disk.
        let file = std::fs::File::create(&path).expect("create");
        file.set_len(MAX_CALENDAR + 1).expect("grow");
        drop(file);

        let err = read_calendar(&path).expect_err("must refuse");
        let message = err.to_string();
        assert!(message.contains("limit"), "says why: {message}");
        assert!(
            message.contains("10 MB"),
            "and what the limit is: {message}"
        );

        // And one of an ordinary size is read.
        let small = dir.join("small.ics");
        std::fs::write(&small, "BEGIN:VCALENDAR\nEND:VCALENDAR\n").expect("write");
        assert!(read_calendar(&small).is_ok());

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn a_notice_never_lands_on_top_of_an_event() {
        // The bug this exists for, seen on screen before it was found in the
        // code: the notice was painted over the last row and the tail of the
        // event showed through it — `1 entry could not be readndred`.
        for height in 0u16..12 {
            for notices in 0u16..4 {
                let area = Rect::new(3, 5, 40, height);
                let (list, notice) = split_for_notices(area, notices);

                assert_eq!(
                    list.height + notice.height,
                    height,
                    "{height} rows, {notices} notices: the split loses rows"
                );
                assert_eq!(
                    notice.y,
                    list.y + list.height,
                    "{height} rows, {notices} notices: the notice overlaps the list"
                );
                if height > 0 {
                    assert!(
                        list.height >= 1,
                        "{height} rows, {notices} notices: the list was squeezed out"
                    );
                }
            }
        }
    }

    #[test]
    fn today_and_tomorrow_are_named_rather_than_dated() {
        let today = date(2026, 8, 1);
        assert_eq!(day_label(today, today), "TODAY");
        assert_eq!(day_label(date(2026, 8, 2), today), "TOMORROW");
        // Anything further out gets a weekday and a date, because "in 4 days"
        // is arithmetic the reader should not have to do either way.
        assert_eq!(day_label(date(2026, 8, 5), today), "WEDNESDAY 5 Aug");
    }

    #[test]
    fn each_day_gets_one_heading_however_many_events_it_has() {
        let state = State {
            events: vec![
                event(date(2026, 8, 1), 9, "a", false),
                event(date(2026, 8, 1), 11, "b", false),
                event(date(2026, 8, 2), 9, "c", false),
            ],
            ..State::default()
        };
        let now = date(2026, 8, 1).at(8, 0, 0, 0).to_zoned(tz()).unwrap();
        let rows: Vec<Row> = AgendaPanel::rows(&state, &now).collect();

        let headings = rows.iter().filter(|r| matches!(r, Row::Day(_))).count();
        assert_eq!(headings, 2, "one heading per day, not per event");
        assert_eq!(rows.len(), 5);
    }

    #[test]
    fn an_event_happening_now_is_marked() {
        let state = State {
            events: vec![event(date(2026, 8, 1), 9, "standup", false)],
            ..State::default()
        };
        let during = date(2026, 8, 1).at(9, 30, 0, 0).to_zoned(tz()).unwrap();
        let after = date(2026, 8, 1).at(10, 30, 0, 0).to_zoned(tz()).unwrap();

        let marked = |now: &Zoned| match AgendaPanel::rows(&state, now).nth(1) {
            Some(Row::Event { in_progress, .. }) => in_progress,
            _ => unreachable!("row 1 is the event"),
        };
        assert!(marked(&during), "the meeting you are in must stand out");
        assert!(!marked(&after));
    }

    #[test]
    fn an_all_day_event_says_so_instead_of_showing_midnight() {
        let theme = crate::theme::Theme::default();
        let e = event(date(2026, 8, 1), 0, "Holiday", true);
        let line = event_line(&e, false, true, 60, &theme);
        let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
        assert!(text.contains("all day"), "got `{text}`");
        assert!(!text.contains("00:00"), "midnight is not a time here");
    }

    #[test]
    fn a_row_never_outgrows_the_panel() {
        // Invariant 9: the budget is display cells, and a CJK summary is the
        // case that catches a `chars()` count.
        let theme = crate::theme::Theme::default();
        for summary in [
            "short",
            "an extremely long summary that will not fit in a narrow panel at all",
            "日本語のとても長い予定のタイトルです",
        ] {
            for width in [10u16, 20, 40, 80] {
                let mut e = event(date(2026, 8, 1), 9, summary, false);
                e.location = Some("Room 12, second floor".into());
                let line = event_line(&e, false, true, width, &theme);
                let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
                assert!(
                    crate::grid::display_width(&text) <= usize::from(width),
                    "{width} cells: `{text}` is {}",
                    crate::grid::display_width(&text)
                );
            }
        }
    }

    #[test]
    fn a_location_is_dropped_rather_than_squeezing_the_summary_out() {
        let theme = crate::theme::Theme::default();
        let mut e = event(date(2026, 8, 1), 9, "Design review", false);
        e.location = Some("The very long name of a meeting room".into());
        let narrow: String = event_line(&e, false, true, 30, &theme)
            .spans
            .iter()
            .map(|s| s.content.as_ref())
            .collect();
        assert!(narrow.contains("Design"), "the summary lost: `{narrow}`");
        assert!(!narrow.contains("very long name"), "got `{narrow}`");

        let wide: String = event_line(&e, false, true, 70, &theme)
            .spans
            .iter()
            .map(|s| s.content.as_ref())
            .collect();
        assert!(wide.contains("meeting room"), "got `{wide}`");
    }

    /// A panel pointed at nothing. The reader thread finds no file and says so,
    /// which is all these two need — they are about the status line, not the
    /// calendar.
    fn idle_panel() -> AgendaPanel {
        AgendaPanel::new(&AgendaConfig::default(), PathBuf::from("/nonexistent.ics"))
    }

    /// Pretend a read has just landed, the way the reader thread does.
    fn land_a_read(panel: &mut AgendaPanel) {
        panel
            .generation
            .store(panel.seen + 1, std::sync::atomic::Ordering::Release);
        panel.tick();
    }

    /// The bug: `reloading…` was set when the reload was *asked for* and cleared
    /// only by the next keypress, so a panel nobody touched went on claiming to
    /// be mid-operation after the reload had landed. Measured at 83 seconds in a
    /// real terminal, with the reloaded events already on screen above it.
    ///
    /// This is the whole point of a dashboard you leave open: the reader is not
    /// pressing keys, so "cleared on the next keypress" is "never".
    #[test]
    fn a_landed_reload_takes_its_own_message_down() {
        let mut panel = idle_panel();
        panel.status = Some(RELOADING.into());
        land_a_read(&mut panel);
        assert_eq!(
            panel.status, None,
            "a reload that has landed must stop saying it is reloading"
        );
    }

    /// The other half, and the reason `tick` matches on the message rather than
    /// clearing whatever is there: `o` puts the calendar's path up, and a
    /// background re-read must not wipe it out from under the reader.
    #[test]
    fn a_path_shown_by_o_survives_a_background_read() {
        let mut panel = idle_panel();
        panel.status = Some("/home/someone/calendar.ics".into());
        land_a_read(&mut panel);
        assert_eq!(
            panel.status.as_deref(),
            Some("/home/someone/calendar.ics"),
            "a read landing must not clear a status it did not put up"
        );
    }
}