ghtui 0.2.0

A keyboard-driven terminal client for GitHub, with a pull request diff viewer that goes further than GitHub's
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
//! A conversation's timeline: comments, reviews with the threads they
//! started, and GitHub's events, in order. Events are one line each, and a
//! run of the same kind by the same person reads as one.

use std::collections::HashSet;

use super::*;
use crate::api::commit_comments::CommitComment;
use crate::api::model::NodeId;
use crate::api::project::InProject;
use crate::api::timeline::{Closer, Event, Item, Latest, Linked, Requested, Review, What};
use crate::ui::page::ReviewLink;

/// Files' lines at commits, by commit and path.
pub type LinesAt<'a> = std::collections::HashMap<(String, String), &'a [String]>;

/// What's marked on a timeline, and what's unfolded.
#[derive(Debug, Clone, Copy)]
pub struct Marks<'a> {
    /// Comments that came in since you started looking, by GitHub's
    /// number.
    pub comments: &'a HashSet<u64>,
    /// Reviews likewise.
    pub reviews: &'a HashSet<u64>,
    /// Review threads and their comments likewise.
    pub threads: &'a HashSet<NodeId>,
    /// Long reviews you unfolded, by their anchors.
    pub unfolded: &'a HashSet<String>,
    /// You: your pending review is yours.
    pub viewer: Option<&'a str>,
    /// Files' lines at commits (by commit and path), read from git for
    /// the threads on them.
    pub lines: &'a LinesAt<'a>,
}

impl Marks<'static> {
    /// Nothing marked or unfolded.
    pub fn none() -> Self {
        static NUMBERS: std::sync::LazyLock<HashSet<u64>> = std::sync::LazyLock::new(HashSet::new);
        static IDS: std::sync::LazyLock<HashSet<NodeId>> = std::sync::LazyLock::new(HashSet::new);
        static NAMES: std::sync::LazyLock<HashSet<String>> = std::sync::LazyLock::new(HashSet::new);
        static LINES: std::sync::LazyLock<LinesAt<'static>> =
            std::sync::LazyLock::new(LinesAt::new);
        Marks {
            comments: &NUMBERS,
            reviews: &NUMBERS,
            threads: &IDS,
            unfolded: &NAMES,
            viewer: None,
            lines: &LINES,
        }
    }
}

/// A review with more threads than this folds the rest away.
pub const FOLD_AFTER: usize = 3;

/// Where the timeline is: the repository its links are in, and the pull
/// request if it's one.
pub(super) struct Place<'a> {
    pub(super) repo: &'a RepoId,
    pub(super) pr: Option<&'a PrRef>,
    /// The pull request's review threads, which its reviews started.
    pub(super) threads: &'a [ReviewThread],
    pub(super) icons: Icons,
}

/// The timeline's items, oldest first, each after a connector.
pub(super) fn entries(
    page: &mut Page,
    talk: &Conversation<'_>,
    place: &Place<'_>,
    items: &[Item],
    marks: Marks<'_>,
) {
    let mut i = 0;
    while let Some(item) = items.get(i) {
        match item {
            Item::Comment(c) => {
                connector(page);
                let new = c.id.is_some_and(|id| marks.comments.contains(&id));
                talk.comment(page, c, new);
                i += 1;
            }
            Item::Review(r) => {
                connector(page);
                review(page, talk, place, r, marks);
                i += 1;
            }
            Item::Event(first) => {
                // The run of events like it that follow.
                let rest = items.get(i + 1..).unwrap_or_default().iter();
                let run: Vec<&Event> = std::iter::once(first)
                    .chain(rest.map_while(|it| match it {
                        Item::Event(e) if same_run(first, e) => Some(e),
                        _ => None,
                    }))
                    .collect();
                i += run.len();
                connector(page);
                events(page, talk, place, &run);
            }
        }
    }
}

/// Whether `e` reads on one line with `first`: the same kind of event
/// by the same person (commits by anyone).
fn same_run(first: &Event, e: &Event) -> bool {
    first.what.kind() == e.what.kind()
        && (first.actor == e.actor || matches!(first.what, What::Committed(_)))
        && !matches!(
            first.what,
            What::CrossReferenced { .. }
                | What::Closed { .. }
                | What::Merged { .. }
                | What::Renamed { .. }
                | What::ForcePushed { .. }
                | What::ReviewDismissed { .. }
                | What::Referenced { .. }
        )
}

/// An event's line: its icon, who, what (wrapped), and when.
fn event_line(page: &mut Page, (icon, role): (&str, Role), segs: Vec<Seg>) {
    let prefix = Seg::new(format!("{icon}  "), role);
    let start = page.lines.len();
    hanging(page, prefix, segs, Frame::None);
    for line in page.lines.iter_mut().skip(start) {
        line.indent = line.indent.saturating_add(2);
    }
}

fn meta(text: impl Into<String>) -> Seg {
    Seg::new(text.into(), Role::Meta)
}

/// `#12 Title`, linked, with its state's icon.
fn linked_segs(page: &mut Page, icons: Icons, here: &RepoId, l: &Linked) -> Vec<Seg> {
    let (icon, role) = issue_icon(icons, l.status());
    let name = link_seg(page, l.name(here), l.url(), Role::Link);
    vec![
        Seg::new(format!("{icon} "), role),
        name,
        meta(format!(" {}", l.title)),
    ]
}

/// The link of a commit: in the pull request's commits, if it's one.
fn commit_link(place: &Place<'_>, oid: &str) -> String {
    match place.pr {
        Some(pr) => format!("{}/commits/{oid}", url::pull(pr)),
        None => url::commit(place.repo, oid),
    }
}

/// A run of events as one line (commits, a line each under it).
fn events(page: &mut Page, talk: &Conversation<'_>, place: &Place<'_>, run: &[&Event]) {
    let (Some(first), Some(last)) = (run.first(), run.last()) else {
        return;
    };
    let here = place.repo;
    let who = login_seg(page, &first.actor, Role::Strong);
    let when = meta(format!(" {}", time::ago_iso(&last.at, talk.now)));
    let mut link: Option<Link> = None;
    let mut segs = vec![who];
    let icon: (&str, Role);
    match &first.what {
        What::Closed {
            by,
            reason,
            duplicate,
        } => {
            icon = match reason {
                Some(IssueState::NotPlanned) => ("⊘", Role::Meta),
                _ if place.pr.is_some() => ("✗", Role::Error),
                _ => ("✓", Role::Accent),
            };
            let how = match (reason, duplicate) {
                (_, true) => " closed this as a duplicate",
                (Some(IssueState::NotPlanned), _) => " closed this as not planned",
                (Some(_), _) => " closed this as completed",
                (None, _) => " closed this",
            };
            segs.push(meta(how));
            match by {
                Some(Closer::Commit(oid)) => {
                    segs.push(meta(" in "));
                    let target = url::commit(here, oid);
                    segs.push(link_seg(page, short_sha(oid), target, Role::Link));
                }
                Some(Closer::Pr(pr)) => {
                    segs.push(meta(" in"));
                    segs.push(space());
                    segs.extend(linked_segs(page, place.icons, here, pr));
                }
                None => {}
            }
        }
        What::Reopened => {
            icon = ("↺", Role::Success);
            segs.push(meta(" reopened this"));
        }
        What::Merged { commit, into } => {
            icon = ("⑃", Role::Accent);
            segs.push(meta(" merged commit "));
            if let Some(oid) = commit {
                segs.push(link_seg(
                    page,
                    short_sha(oid),
                    url::commit(here, oid),
                    Role::Link,
                ));
            }
            segs.push(meta(" into "));
            segs.push(chip(into, Bg::PrimaryContainer));
        }
        What::CrossReferenced { from, will_close } => {
            icon = ("↗", Role::Meta);
            let what = if *will_close {
                match place.pr {
                    Some(_) => " linked an issue this will close",
                    None if from.is_pr() => " linked a pull request that will close this issue",
                    None => " mentioned this in an issue that will close it",
                }
            } else {
                " mentioned this in"
            };
            segs.push(meta(what));
            segs.push(space());
            segs.extend(linked_segs(page, place.icons, here, from));
            link = Some(Link::Url(from.url()));
        }
        What::Referenced { commit, repo } => {
            icon = ("◆", Role::Meta);
            segs.push(meta(" added a commit that references this "));
            let repo = repo.as_ref().unwrap_or(here);
            let target = url::commit(repo, commit);
            segs.push(link_seg(
                page,
                short_sha(commit),
                target.clone(),
                Role::Link,
            ));
            link = Some(Link::Url(target));
        }
        What::Labeled { .. } => {
            icon = ("◈", Role::Meta);
            let (added, removed): (Vec<&&Event>, Vec<&&Event>) = run
                .iter()
                .partition(|e| matches!(&e.what, What::Labeled { added, .. } if *added));
            for (verb, list) in [(" added", &added), (" removed", &removed)] {
                if list.is_empty() {
                    continue;
                }
                if verb == " removed" && !added.is_empty() {
                    segs.push(meta(" and"));
                }
                segs.push(meta(verb));
                for e in list {
                    if let What::Labeled { label, .. } = &e.what {
                        segs.push(space());
                        segs.push(Seg::new(
                            format!(" {} ", label.name),
                            Role::Label(label.color.clone()),
                        ));
                    }
                }
            }
        }
        What::Assigned { .. } => {
            icon = ("◉", Role::Meta);
            let names = |added: bool| -> Vec<&str> {
                run.iter()
                    .filter_map(|e| match &e.what {
                        What::Assigned { who, added: a } if *a == added => Some(who.as_str()),
                        _ => None,
                    })
                    .collect()
            };
            let (on, off) = (names(true), names(false));
            let mut said = false;
            for (verb, list) in [(" assigned", on), (" unassigned", off)] {
                if list.is_empty() {
                    continue;
                }
                if said {
                    segs.push(meta(" and"));
                }
                said = true;
                if list == [first.actor.as_str()] {
                    segs.push(meta(if verb == " assigned" {
                        " self-assigned this"
                    } else {
                        " removed their assignment"
                    }));
                    continue;
                }
                segs.push(meta(verb));
                for (n, who) in list.into_iter().enumerate() {
                    segs.push(meta(if n == 0 { " " } else { ", " }));
                    segs.push(login_seg(page, who, Role::Link));
                }
            }
        }
        What::Milestoned { .. } => {
            icon = ("◷", Role::Meta);
            let mut said = false;
            for e in run {
                if let What::Milestoned { title, added } = &e.what {
                    if said {
                        segs.push(meta(" and"));
                    }
                    said = true;
                    let (verb, to) = if *added {
                        (" added this to the ", " milestone")
                    } else {
                        (" removed this from the ", " milestone")
                    };
                    segs.push(meta(verb));
                    segs.push(Seg::new(title.clone(), Role::Strong));
                    segs.push(meta(to));
                }
            }
        }
        What::Project { .. } | What::ProjectStatus { .. } => {
            icon = ("▦", Role::Meta);
            let Some(e) = run.last() else { return };
            match &e.what {
                What::Project { added: true } => segs.push(meta(" added this to a project")),
                What::Project { added: false } => {
                    segs.push(meta(" removed this from a project"));
                }
                What::ProjectStatus { from, to } if from.is_empty() => {
                    segs.push(meta(" set its status in a project to "));
                    segs.push(Seg::new(to.clone(), Role::Strong));
                }
                What::ProjectStatus { from, to } => {
                    segs.push(meta(" moved this in a project from "));
                    segs.push(Seg::new(from.clone(), Role::Strong));
                    segs.push(meta(" to "));
                    segs.push(Seg::new(to.clone(), Role::Strong));
                }
                _ => {}
            }
        }
        What::ReviewRequested { .. } => {
            icon = ("◎", Role::Meta);
            let mut said = false;
            for added in [true, false] {
                let who: Vec<&Reviewer> = (run.iter())
                    .filter_map(|e| match &e.what {
                        What::ReviewRequested {
                            who: Some(w),
                            added: a,
                        } if *a == added => Some(w),
                        _ => None,
                    })
                    .collect();
                if who.is_empty() {
                    continue;
                }
                if said {
                    segs.push(meta(" and"));
                }
                said = true;
                segs.push(meta(if added {
                    " requested a review from"
                } else {
                    " removed the request for review from"
                }));
                for (n, w) in who.into_iter().enumerate() {
                    segs.push(meta(if n == 0 { " " } else { ", " }));
                    let (name, target) = reviewer_link(w);
                    segs.push(link_seg(page, name, target, Role::Link));
                }
            }
            if !said {
                segs.push(meta(" changed who is asked to review"));
            }
        }
        What::ReviewDismissed { of, message } => {
            icon = ("○", Role::Meta);
            match of {
                Some(of) => {
                    segs.push(meta(" dismissed "));
                    segs.push(login_seg(page, of, Role::Link));
                    segs.push(meta("’s review"));
                }
                None => segs.push(meta(" dismissed a review")),
            }
            if !message.trim().is_empty() {
                segs.push(meta(format!(": {}", message.trim())));
            }
        }
        What::Renamed { from, to } => {
            icon = ("✎", Role::Meta);
            segs.push(meta(" changed the title "));
            segs.push(Seg::new(from.clone(), Role::Strike));
            segs.push(space());
            segs.push(Seg::new(to.clone(), Role::Strong));
        }
        What::ReadyForReview | What::ConvertedToDraft => {
            icon = ("◌", Role::Meta);
            let Some(e) = run.last() else { return };
            segs.push(meta(match e.what {
                What::ReadyForReview => " marked this pull request as ready for review",
                _ => " marked this pull request as draft",
            }));
        }
        What::Committed(_) => {
            commits(page, talk, place, run);
            return;
        }
        What::ForcePushed {
            base,
            before,
            after,
        } => {
            icon = ("⇡", Role::Meta);
            segs.push(meta(if *base {
                " force-pushed the base branch"
            } else {
                " force-pushed the branch"
            }));
            if let (Some(before), Some(after)) = (before, after) {
                segs.push(meta(" from "));
                segs.push(Seg::new(short_sha(before).to_owned(), Role::Code));
                segs.push(meta(" to "));
                segs.push(Seg::new(short_sha(after).to_owned(), Role::Code));
                if crate::git::Oid::parse(before).is_ok() && crate::git::Oid::parse(after).is_ok() {
                    let target = url::files(&url::compare(here, before, after));
                    segs.push(meta(" · "));
                    segs.push(link_seg(page, "Compare", target.clone(), Role::Link));
                    link = Some(Link::Url(target));
                }
            }
        }
        What::BranchDeleted { name } => {
            icon = ("⌥", Role::Meta);
            segs.push(meta(" deleted the "));
            segs.push(chip(name, Bg::PrimaryContainer));
            segs.push(meta(" branch"));
        }
        What::BranchRestored => {
            icon = ("⌥", Role::Meta);
            segs.push(meta(" restored the head branch"));
        }
        What::BaseChanged { from, to } => {
            icon = ("⌥", Role::Meta);
            segs.push(meta(" changed the base branch from "));
            segs.push(chip(from, Bg::PrimaryContainer));
            segs.push(meta(" to "));
            segs.push(chip(to, Bg::PrimaryContainer));
        }
        What::Locked { reason } => {
            icon = ("⚿", Role::Meta);
            segs.push(meta(match reason {
                Some(r) => format!(" locked as {r} and limited conversation to collaborators"),
                None => " locked and limited conversation to collaborators".to_owned(),
            }));
        }
        What::Unlocked => {
            icon = ("⚿", Role::Meta);
            segs.push(meta(" unlocked this conversation"));
        }
        What::Pinned { .. } => {
            icon = ("⚲", Role::Meta);
            let Some(e) = run.last() else { return };
            segs.push(meta(match e.what {
                What::Pinned { pinned: true } => " pinned this issue",
                _ => " unpinned this issue",
            }));
        }
        What::Transferred { from } => {
            icon = ("⇄", Role::Meta);
            segs.push(meta(match from {
                Some(from) => format!(" transferred this issue from {from}"),
                None => " transferred this issue".to_owned(),
            }));
        }
        What::MarkedDuplicate { of, undone } => {
            icon = ("⧉", Role::Meta);
            match (of, undone) {
                (_, true) => segs.push(meta(" marked this as not a duplicate")),
                (Some(of), false) => {
                    segs.push(meta(" marked this as a duplicate of"));
                    segs.push(space());
                    segs.extend(linked_segs(page, place.icons, here, of));
                    link = Some(Link::Url(of.url()));
                }
                (None, false) => segs.push(meta(" marked this as a duplicate")),
            }
        }
        What::AutoMerge { .. } => {
            icon = ("⑃", Role::Meta);
            let Some(e) = run.last() else { return };
            segs.push(meta(match e.what {
                What::AutoMerge { on: true } => " enabled auto-merge",
                _ => " disabled auto-merge",
            }));
        }
        What::MergeQueue { .. } => {
            icon = ("⑃", Role::Meta);
            let Some(e) = run.last() else { return };
            match &e.what {
                What::MergeQueue { added: true, .. } => {
                    segs.push(meta(" added this to the merge queue"));
                }
                What::MergeQueue {
                    reason: Some(r), ..
                } => segs.push(meta(format!(" removed this from the merge queue: {r}"))),
                _ => segs.push(meta(" removed this from the merge queue")),
            }
        }
        What::SubIssue { .. } | What::Parent { .. } => {
            icon = ("⊶", Role::Meta);
            let mut said = false;
            for e in run {
                let (verb, issue) = match &e.what {
                    What::SubIssue { issue, added: true } => (" added a sub-issue", issue),
                    What::SubIssue { issue, .. } => (" removed a sub-issue", issue),
                    What::Parent { issue, added: true } => (" added a parent issue", issue),
                    What::Parent { issue, .. } => (" removed a parent issue", issue),
                    _ => continue,
                };
                if said {
                    segs.push(meta(" and"));
                }
                said = true;
                segs.push(meta(verb));
                if let Some(issue) = issue {
                    segs.push(space());
                    segs.extend(linked_segs(page, place.icons, here, issue));
                    link.get_or_insert_with(|| Link::Url(issue.url()));
                }
            }
        }
        What::IssueType { from, to } => {
            icon = ("◇", Role::Meta);
            match (from, to) {
                (Some(from), Some(to)) => {
                    segs.push(meta(" changed the issue type from "));
                    segs.push(Seg::new(from.clone(), Role::Strong));
                    segs.push(meta(" to "));
                    segs.push(Seg::new(to.clone(), Role::Strong));
                }
                (None, Some(to)) => {
                    segs.push(meta(" added the issue type "));
                    segs.push(Seg::new(to.clone(), Role::Strong));
                }
                (Some(from), None) => {
                    segs.push(meta(" removed the issue type "));
                    segs.push(Seg::new(from.clone(), Role::Strong));
                }
                (None, None) => segs.push(meta(" changed the issue type")),
            }
        }
        What::ConvertedToDiscussion => {
            icon = ("◌", Role::Meta);
            segs.push(meta(" converted this issue into a discussion"));
        }
    }
    segs.push(when);
    match link {
        Some(target) => item(page, target, |page, _| event_line(page, icon, segs)),
        None => event_line(page, icon, segs),
    }
}

/// A run of commits: who added how many, then a line for each, each
/// opening the commit (among the pull request's).
fn commits(page: &mut Page, talk: &Conversation<'_>, place: &Place<'_>, run: &[&Event]) {
    let infos: Vec<&CommitInfo> = (run.iter())
        .filter_map(|e| match &e.what {
            What::Committed(c) => Some(c),
            _ => None,
        })
        .collect();
    let (Some(first), Some(last)) = (run.first(), run.last()) else {
        return;
    };
    let one = run.iter().all(|e| e.actor == first.actor) && !first.actor.is_empty();
    let mut segs = Vec::new();
    if one {
        segs.push(login_seg(page, &first.actor, Role::Strong));
        segs.push(meta(format!(
            " added {}",
            plural(infos.len() as u64, "commit")
        )));
    } else {
        segs.push(meta(format!(
            "{} added",
            plural(infos.len() as u64, "commit")
        )));
    }
    segs.push(meta(format!(" {}", time::ago_iso(&last.at, talk.now))));
    event_line(page, ("◆", Role::Meta), segs);
    for c in infos {
        item(page, commit_link(place, &c.oid), |page, link| {
            let room = usize::from(page.room(Frame::None, 5)).saturating_sub(9);
            let headline = crate::ui::text::truncate(&c.headline, room);
            page.add(
                Frame::None,
                5,
                vec![
                    Seg::linked(headline, Role::Body, link),
                    meta("  "),
                    Seg::new(short_sha(&c.oid).to_owned(), Role::Code),
                ],
                Vec::new(),
            );
        });
    }
}

/// Who a reviewer is, and their page.
pub(super) fn reviewer_link(who: &Reviewer) -> (String, String) {
    match who {
        Reviewer::User(login) => (login.clone(), url::user(login)),
        Reviewer::Team(team) => {
            let (org, slug) = team.split_once('/').unwrap_or(("", team));
            (
                team.clone(),
                format!("{}/orgs/{org}/teams/{slug}", url::base()),
            )
        }
    }
}

/// How a review's state reads: its mark, and what its author did.
pub(super) fn review_said(state: ReviewState) -> ((&'static str, Role), &'static str) {
    match state {
        ReviewState::Approved => (("✓", Role::Success), "approved these changes"),
        ReviewState::ChangesRequested => (("✗", Role::Error), "requested changes"),
        ReviewState::Dismissed => (("○", Role::Meta), "reviewed (dismissed)"),
        ReviewState::Commented => (("◉", Role::Meta), "reviewed"),
        ReviewState::Pending => (("◌", Role::Accent), "started a review"),
    }
}

/// A review's state as a chip.
fn state_chip_of(state: ReviewState) -> Seg {
    match state {
        ReviewState::Approved => chip("Approved", Bg::SuccessContainer),
        ReviewState::ChangesRequested => chip("Changes requested", Bg::ErrorContainer),
        ReviewState::Dismissed => chip("Dismissed", Bg::ContainerHigh),
        ReviewState::Commented => chip("Commented", Bg::SecondaryContainer),
        ReviewState::Pending => chip("Pending", Bg::TertiaryContainer),
    }
}

/// The threads a review started, in file order.
pub(super) fn started_by<'a>(
    threads: &'a [ReviewThread],
    review: &Review,
) -> Vec<&'a ReviewThread> {
    let mut out: Vec<&ReviewThread> = (threads.iter())
        .filter(|t| review.id.is_some() && t.comments.first().and_then(|c| c.review) == review.id)
        .collect();
    out.sort_by_key(|t| (&t.path, t.line.or(t.original_line)));
    out
}

/// The threads a review replied in without starting them.
fn replied_in<'a>(threads: &'a [ReviewThread], review: &Review) -> Vec<&'a ReviewThread> {
    (threads.iter())
        .filter(|t| {
            review.id.is_some()
                && t.comments.first().and_then(|c| c.review) != review.id
                && t.comments.iter().skip(1).any(|c| c.review == review.id)
        })
        .collect()
}

/// A review: its state and summary, then each thread it started with
/// the lines it's on, its comments and replies. A long one folds after
/// its first threads, until unfolded.
pub(super) fn review(
    page: &mut Page,
    talk: &Conversation<'_>,
    place: &Place<'_>,
    r: &Review,
    marks: Marks<'_>,
) {
    if let Some(anchor) = r.anchor() {
        page.anchor(anchor);
    }
    let mine = marks.viewer == Some(r.author.as_str());
    let pending = r.state == ReviewState::Pending;
    let started = started_by(place.threads, r);
    let replied = replied_in(place.threads, r);
    let new = r.id.is_some_and(|id| marks.reviews.contains(&id));
    let (icon, what) = review_said(r.state);
    // A review that only decided: one line.
    if r.body.trim().is_empty() && started.is_empty() && replied.is_empty() && !pending {
        let who = login_seg(page, &r.author, Role::Strong);
        let ago = time::ago_iso(&r.at, talk.now);
        let mut segs = vec![who, meta(format!(" {what} {ago}"))];
        if new {
            segs.push(space());
            segs.push(chip("New", Bg::TertiaryContainer));
        }
        event_line(page, icon, segs);
        return;
    }
    let start = page.lines.len();
    let who = login_seg(page, &r.author, Role::Strong);
    let ago = time::ago_iso(&r.at, talk.now);
    let what = if pending && mine {
        format!(" started a review {ago} · not submitted")
    } else {
        format!(" {what} {ago}")
    };
    let mut right = Vec::new();
    if new {
        right.push(chip("New", Bg::TertiaryContainer));
        right.push(space());
    }
    right.push(state_chip_of(r.state));
    page.box_top(
        vec![Seg::new(format!("{}  ", icon.0), icon.1), who, meta(what)],
        right,
    );
    if pending && mine {
        let comments = plural(r.comments, "comment");
        let text = format!(
            "Your pending review on GitHub, with {comments}: submitting your review in Files changed sends it; deleting it here throws it away."
        );
        page.wrapped(vec![meta(text)], 0, Frame::Body);
        page.box_gap();
    }
    if !r.body.trim().is_empty() {
        markdown::render(page, &r.body, Some(&talk.base), Frame::Body);
    }
    reaction_chips(page, &r.editable.reactions);
    // The summary (and the box's top) is the review: `>` quotes it, `,e`
    // edits it.
    let quote = page.link(Link::Quote {
        author: r.author.clone(),
        at: r.at.clone(),
        body: r.body.clone(),
    });
    page.item(start, quote);
    let anchor = r.anchor().unwrap_or_default();
    let folds = started.len() > FOLD_AFTER && !marks.unfolded.contains(&anchor);
    let shown = if folds { FOLD_AFTER - 1 } else { started.len() };
    for t in started.iter().take(shown) {
        page.box_rule();
        thread(page, place, t, marks, talk.now);
    }
    if folds {
        page.box_rule();
        let hidden = started.len().saturating_sub(shown) as u64;
        let link = ReviewLink::Fold(anchor);
        item(page, Link::Review(link), |page, link| {
            body(
                page,
                vec![
                    Seg::linked(
                        format!("▸ {hidden} more {} in this review", noun(hidden, "thread")),
                        Role::Link,
                        link,
                    ),
                    meta(" · ↵ shows them"),
                ],
            );
        });
    } else if started.len() > FOLD_AFTER {
        page.box_rule();
        item(
            page,
            Link::Review(ReviewLink::Fold(anchor)),
            |page, link| {
                body(
                    page,
                    vec![
                        Seg::linked("▾ Fold this review's threads", Role::Link, link),
                        meta(" · ↵"),
                    ],
                );
            },
        );
    }
    if !replied.is_empty() {
        page.box_rule();
        for t in replied {
            let at = super::review::place(&t.path, t.line.or(t.original_line));
            let target = t
                .comments
                .first()
                .map(|c| c.url.clone())
                .unwrap_or_default();
            item(page, target, |page, link| {
                body(
                    page,
                    vec![meta("↳ replied in "), Seg::linked(at, Role::Link, link)],
                );
            });
        }
    }
    page.box_bottom();
}

/// A thread inside a review: where it is (opening it in Files changed),
/// whether it's resolved or outdated, the lines it's on, and its
/// comments. A resolved one shows only its first line, as GitHub folds it.
fn thread(page: &mut Page, place: &Place<'_>, t: &ReviewThread, marks: Marks<'_>, now: u64) {
    let Some(first) = t.comments.first() else {
        return;
    };
    let line = if t.file_level {
        None
    } else {
        t.line.or(t.original_line)
    };
    let at = super::review::place(&t.path, line);
    let target = Some(first.url.clone()).filter(|u| !u.is_empty());
    let target = target.unwrap_or_else(|| match place.pr {
        Some(pr) => url::diff_line(pr, &t.path, line.map(|l| ('R', l))),
        None => url::repo(place.repo),
    });
    item(page, target, |page, link| {
        let mut segs = vec![Seg::linked(at, Role::Link, link)];
        let mut tag = |text: &str, bg| {
            segs.push(space());
            segs.push(chip(text, bg));
        };
        if t.outdated {
            tag("Outdated", Bg::ContainerHigh);
        }
        if t.resolved {
            tag("Resolved", Bg::SuccessContainer);
        }
        if first.pending {
            tag("Pending", Bg::TertiaryContainer);
        }
        if t.comments.iter().any(|c| marks.threads.contains(&c.id)) {
            tag("New", Bg::TertiaryContainer);
        }
        let more = t.comments.total().saturating_sub(1);
        let right = (t.resolved && more > 0).then(|| meta(plural(more, "reply")));
        page.box_line(segs, Vec::from_iter(right), 0);
        if t.resolved {
            let said = super::review::first_line(&first.body, page.room(Frame::Body, 2));
            page.box_line(
                vec![
                    Seg::new(format!("{}: ", first.author), Role::Strong),
                    meta(said),
                ],
                Vec::new(),
                2,
            );
            return;
        }
        if !t.file_level && !git_lines(page, t, marks.lines) {
            hunk_lines(page, t);
        }
        for (n, c) in t.comments.iter().enumerate() {
            if n == 1 && t.comments.left_out() > 0 {
                let text = left_out_text(t.comments.left_out(), "more reply", "more replies");
                page.box_line(vec![meta(text)], Vec::new(), 2);
            }
            let verb = if n == 0 { "" } else { " replied" };
            let mut segs = vec![
                Seg::new(c.author.clone(), Role::Strong),
                meta(format!("{verb} {}", time::ago_iso(&c.created_at, now))),
            ];
            if n > 0 && marks.threads.contains(&c.id) {
                segs.push(space());
                segs.push(chip("New", Bg::TertiaryContainer));
            }
            page.box_line(segs, Vec::new(), 2);
            let body_start = page.lines.len();
            markdown::render_comment(page, &c.body, None);
            for l in page.lines.iter_mut().skip(body_start) {
                l.frame = Frame::Body;
                l.indent = l.indent.saturating_add(2);
            }
        }
    });
}

/// The lines a thread is on, as GitHub kept them when it was started:
/// the end of its diff hunk, up to four lines (or the lines it spans).
/// Lines before a one-line thread's line, for context.
const CONTEXT: u32 = 3;

/// The lines a thread is on, numbered, as the file was at the commit its
/// first comment was made on, read from git; a few lines before a
/// one-line thread's for context. False when git hasn't read them (no
/// clone, or not yet), or the thread is on the old side.
fn git_lines(page: &mut Page, t: &ReviewThread, lines: &LinesAt<'_>) -> bool {
    let (Some(end), Some(first)) = (t.original_line, t.comments.first()) else {
        return false;
    };
    if t.side != crate::api::model::Side::Right {
        return false;
    }
    let Some(commit) = &first.original_commit else {
        return false;
    };
    let Some(text) = lines.get(&(commit.clone(), t.path.clone())) else {
        return false;
    };
    let on = t.original_start_line.unwrap_or(end)..=end;
    let start = if on.start() == on.end() {
        end.saturating_sub(CONTEXT).max(1)
    } else {
        *on.start()
    };
    let width = end.to_string().len();
    let room = usize::from(page.room(Frame::Body, 2)).saturating_sub(width + 3);
    for n in start..=end {
        let Some(code) = usize::try_from(n.saturating_sub(1))
            .ok()
            .and_then(|i| text.get(i))
        else {
            return n > start;
        };
        let tone = if on.contains(&n) {
            crate::ui::page::Tone::Marked
        } else {
            crate::ui::page::Tone::Code
        };
        let code = crate::ui::text::truncate(&code.replace('\t', "    "), room);
        page.push(PageLine {
            segs: vec![
                Seg::new(format!("{n:>width$} │ "), Role::Meta),
                Seg::new(code, Role::Code),
            ],
            indent: 2,
            right: Vec::new(),
            frame: Frame::Body,
            tone,
            fixed: 1,
        });
    }
    true
}

fn hunk_lines(page: &mut Page, t: &ReviewThread) {
    let lines: Vec<&str> = (t.diff_hunk.lines())
        .filter(|l| !l.starts_with("@@"))
        .collect();
    let span = match (t.original_start_line, t.original_line) {
        (Some(start), Some(end)) if end >= start => (end - start + 1) as usize,
        _ => 1,
    };
    let keep = span.clamp(4, 12);
    let room = usize::from(page.room(Frame::Body, 2));
    for l in lines.iter().skip(lines.len().saturating_sub(keep)) {
        let tone = match l.chars().next() {
            Some('+') => crate::ui::page::Tone::Added,
            Some('-') => crate::ui::page::Tone::Removed,
            _ => crate::ui::page::Tone::Code,
        };
        let text = crate::ui::text::truncate(&l.replace('\t', "    "), room);
        page.push(PageLine {
            segs: vec![Seg::new(text, Role::Code)],
            indent: 2,
            right: Vec::new(),
            frame: Frame::Body,
            tone,
            fixed: 0,
        });
    }
}

// ---- sidebars ---------------------------------------------------------------------

/// The sidebar's reviewers: each one's latest review (an approval of an
/// older head marked stale), then who is asked and hasn't answered,
/// code owners marked.
pub(super) fn reviewers_aside(
    page: &mut Page,
    pr: &PrRef,
    head: &str,
    latest: &Capped<Latest>,
    requested: &Capped<Requested>,
    author: &str,
) {
    aside_heading(page, "Reviewers");
    let latest: Vec<&Latest> = latest.iter().filter(|l| l.author != author).collect();
    if latest.is_empty() && requested.is_empty() {
        page.wrapped(vec![meta("No reviews")], 0, Frame::None);
    }
    for l in latest {
        let ((mark, role), what) = match l.state {
            ReviewState::Approved => (("✓", Role::Success), "approved"),
            ReviewState::ChangesRequested => (("✗", Role::Error), "requested changes"),
            ReviewState::Dismissed => (("○", Role::Meta), "dismissed"),
            ReviewState::Commented => (("◉", Role::Meta), "commented"),
            ReviewState::Pending => (("◌", Role::Accent), "pending"),
        };
        let target = match l.id {
            Some(id) => format!("{}#pullrequestreview-{id}", url::pull(pr)),
            None => url::user(&l.author),
        };
        let who = link_seg(page, l.author.clone(), target, Role::Link);
        let mut segs = vec![
            Seg::new(format!("{mark} "), role),
            who,
            meta(format!(" {what}")),
        ];
        let stale = l.state == ReviewState::Approved
            && l.commit
                .as_deref()
                .is_some_and(|c| !head.is_empty() && c != head);
        if stale {
            segs.push(meta(" · before the last push"));
        }
        for team in &l.for_teams {
            segs.push(meta(format!(" · for {team}")));
        }
        page.wrapped(segs, 2, Frame::None);
    }
    for r in requested {
        let (name, target) = reviewer_link(&r.who);
        let who = link_seg(page, name, target, Role::Link);
        let mut segs = vec![Seg::new("● ", Role::Accent), who, meta(" asked")];
        if r.code_owner {
            segs.push(meta(" · code owner"));
        }
        page.wrapped(segs, 2, Frame::None);
    }
    if requested.left_out() > 0 {
        let more = format!("and {} more asked, unnamed", requested.left_out());
        page.wrapped(vec![meta(more)], 0, Frame::None);
    }
}

/// A list of issues or pull requests in a sidebar, each linked with its
/// state's icon.
pub(super) fn linked_aside(
    page: &mut Page,
    icons: Icons,
    here: &RepoId,
    heading: &str,
    list: &Capped<Linked>,
    none: &str,
) {
    aside_heading(page, heading);
    if list.is_empty() {
        page.wrapped(vec![meta(none)], 0, Frame::None);
    }
    for l in list {
        let segs = linked_segs(page, icons, here, l);
        page.wrapped(segs, 2, Frame::None);
    }
    if list.left_out() > 0 {
        let more = left_out_text(list.left_out(), "more", "more");
        page.wrapped(vec![meta(more)], 0, Frame::None);
    }
}

/// An issue's place among others: its type, its parent, its sub-issues
/// and how far along they are, and the pull requests and branches for it
/// (GitHub's "Development").
pub(super) fn relations_aside(page: &mut Page, icons: Icons, d: &IssueDetail) {
    if let Some(t) = &d.issue_type {
        aside_heading(page, "Type");
        page.wrapped(vec![chip(t, Bg::SecondaryContainer)], 0, Frame::None);
    }
    aside_heading(page, "Development");
    if d.closed_by.is_empty() && d.branches.is_empty() {
        page.wrapped(vec![meta("No pull requests or branches")], 0, Frame::None);
    }
    for l in &d.closed_by {
        let segs = linked_segs(page, icons, &d.repo, l);
        page.wrapped(segs, 2, Frame::None);
    }
    for b in &d.branches {
        let target = url::tree(&d.repo, b, "");
        let branch = link_seg(page, b.clone(), target, Role::Link);
        page.wrapped(vec![meta("⌥ "), branch], 2, Frame::None);
    }
    if let Some(parent) = &d.parent {
        aside_heading(page, "Parent issue");
        let segs = linked_segs(page, icons, &d.repo, parent);
        page.wrapped(segs, 2, Frame::None);
    }
    if !d.sub_issues.is_empty() || d.progress.is_some() {
        let heading = match d.progress {
            Some(p) => format!("Sub-issues · {} of {} done", p.done, p.total),
            None => "Sub-issues".to_owned(),
        };
        linked_aside(page, icons, &d.repo, &heading, &d.sub_issues, "None");
    }
    if !d.blocked_by.is_empty() {
        linked_aside(page, icons, &d.repo, "Blocked by", &d.blocked_by, "None");
    }
    if !d.blocking.is_empty() {
        linked_aside(page, icons, &d.repo, "Blocking", &d.blocking, "None");
    }
}

/// A commit's comments, under its page: each on the commit as a whole,
/// or on a line of its diff (which opens there), then the box to write
/// one.
pub fn commit_comments(
    page: &mut Page,
    repo: &RepoId,
    d: &CommitDetail,
    comments: Fetched<'_, Capped<CommitComment>>,
    cx: PageCtx<'_>,
) {
    let loading = |page: &mut Page| {
        page.blank();
        page.line(vec![meta("Loading the comments…")]);
    };
    let Some(comments) = comments.show_or(page, "the commit's comments", loading) else {
        return;
    };
    let talk = Conversation {
        base: LinkBase::new(repo, &d.oid, ""),
        op: "",
        now: cx.now,
        unfolded: Marks::none().unfolded,
    };
    let diff = crate::git::Oid::parse(&d.oid).is_ok();
    page.blank();
    let title = match comments.total() {
        0 => "No comments yet".to_owned(),
        n => format!("Comments · {n}"),
    };
    page.line(vec![Seg::new(title, Role::Strong)]);
    for c in comments {
        connector(page);
        let at = c.path.as_ref().map(|path| {
            let target = if diff {
                let line = c.line.map(|l| format!("R{l}")).unwrap_or_default();
                url::diff_file(&url::commit(repo, &d.oid), path, &line)
            } else {
                url::commit(repo, &d.oid)
            };
            (super::review::place(path, c.line), target)
        });
        let verb = match &at {
            Some((place, _)) => format!("commented on {place}"),
            None => "commented".to_owned(),
        };
        talk.said_at(
            page,
            Some(c.anchor()),
            &c.author,
            &verb,
            &c.created_at,
            &c.body,
            None,
            &[],
        );
        if let Some((place, target)) = at {
            item(page, target, |page, link| {
                page.add(
                    Frame::None,
                    5,
                    vec![
                        meta("↳ "),
                        Seg::linked(format!("Open {place} in the diff"), Role::Link, link),
                    ],
                    Vec::new(),
                );
            });
        }
    }
    if comments.left_out() > 0 {
        page.wrapped(vec![meta("… more comments on GitHub")], 2, Frame::None);
    }
    add_comment(page, cx.keys);
}

/// A repository's pinned issues, above its list of issues, each opening
/// the issue; nothing where none are pinned.
pub fn pinned_issues(page: &mut Page, repo: &RepoId, pinned: &Capped<Linked>, icons: Icons) {
    if pinned.is_empty() {
        return;
    }
    page.box_top(vec![Seg::new("Pinned issues", Role::Strong)], Vec::new());
    for l in pinned {
        item(page, l.url(), |page, _| {
            let segs = linked_segs(page, icons, repo, l);
            hanging(page, Seg::new("⚲ ", Role::Meta), segs, Frame::Body);
        });
    }
    page.box_bottom();
    page.blank();
}

/// The projects an issue or pull request is in, with its status and its
/// other fields' values in each (`,f` sets one here; each opens the
/// project); or why they can't be read (a token without read:project).
pub(super) fn projects_aside(page: &mut Page, projects: Fetched<'_, Capped<InProject>>) {
    aside_heading(page, "Projects");
    #[expect(clippy::disallowed_methods, reason = "said in the sidebar's row")]
    match projects.text("the projects") {
        Ok(list) if list.is_empty() => page.wrapped(vec![meta("None yet")], 0, Frame::None),
        Ok(list) => {
            for p in list {
                let title = link_seg(page, p.title.clone(), p.url.clone(), Role::Link);
                let mut segs = vec![meta("▦ "), title];
                if let Some(status) = p.status() {
                    segs.push(meta(" · "));
                    segs.push(Seg::new(status.to_owned(), Role::Strong));
                }
                page.wrapped(segs, 2, Frame::None);
                let others: Vec<String> = (p.others())
                    .map(|(name, v)| format!("{name}: {}", v.text()))
                    .collect();
                if !others.is_empty() {
                    page.wrapped(vec![meta(others.join(" · "))], 2, Frame::None);
                }
            }
            if list.left_out() > 0 {
                let more = left_out_text(list.left_out(), "more", "more");
                page.wrapped(vec![meta(more)], 0, Frame::None);
            }
        }
        Err(why) => page.wrapped(vec![meta(why)], 0, Frame::None),
    }
}

/// The projects on a page too narrow for a sidebar.
pub(super) fn projects_inline(page: &mut Page, projects: Fetched<'_, Capped<InProject>>) {
    // Said once loaded, or once it failed; while it loads, nothing.
    #[expect(clippy::disallowed_methods, reason = "a failure is said in its row")]
    let list = match projects.text("the projects") {
        Ok(list) => list,
        Err(why) if why.starts_with("Loading") => return,
        Err(why) => return inline(page, "Projects", vec![meta(why)]),
    };
    let mut segs = Vec::new();
    for p in list {
        if !segs.is_empty() {
            segs.push(meta(", "));
        }
        segs.push(link_seg(page, p.title.clone(), p.url.clone(), Role::Link));
        let said: Vec<String> = (p.status().map(str::to_owned).into_iter())
            .chain(p.others().map(|(name, v)| format!("{name}: {}", v.text())))
            .collect();
        if !said.is_empty() {
            segs.push(meta(format!(" ({})", said.join(" · "))));
        }
    }
    inline(page, "Projects", segs);
}

/// `Heading: ` then `segs`, wrapped under it: a sidebar's section on a
/// page too narrow for one.
fn inline(page: &mut Page, heading: &str, segs: Vec<Seg>) {
    if segs.is_empty() {
        return;
    }
    hanging(page, meta(format!("{heading}: ")), segs, Frame::None);
}

/// A pull request's sidebar on a narrow page: its reviewers, and the
/// issues it closes.
pub(super) fn pr_inline(page: &mut Page, icons: Icons, pr: &PrRef, d: &PrDetail, a: &PrActivity) {
    let mut segs = Vec::new();
    let sep = |segs: &mut Vec<Seg>| {
        if !segs.is_empty() {
            segs.push(meta(" · "));
        }
    };
    for l in a.latest.iter().filter(|l| l.author != d.author) {
        sep(&mut segs);
        let ((mark, role), _) = review_said(l.state);
        segs.push(Seg::new(format!("{mark} "), role));
        segs.push(login_seg(page, &l.author, Role::Link));
        let stale = l.state == ReviewState::Approved
            && l.commit.as_deref().is_some_and(|c| c != d.head_oid);
        if stale {
            segs.push(meta(" (before the last push)"));
        }
    }
    for r in &a.requested {
        sep(&mut segs);
        let (name, target) = reviewer_link(&r.who);
        segs.push(Seg::new("● ", Role::Accent));
        segs.push(link_seg(page, name, target, Role::Link));
        segs.push(meta(if r.code_owner {
            " asked (code owner)"
        } else {
            " asked"
        }));
    }
    inline(page, "Reviewers", segs);
    let mut closes = Vec::new();
    for l in &a.closing {
        if !closes.is_empty() {
            closes.push(meta(", "));
        }
        closes.extend(linked_segs(page, icons, &pr.repo, l));
    }
    inline(page, "Closes", closes);
    if let Some(oid) = &a.merge_commit {
        let sha = link_seg(page, short_sha(oid), url::commit(&pr.repo, oid), Role::Link);
        inline(page, "Merge commit", vec![sha]);
    }
}

/// An issue's place among others on a narrow page.
pub(super) fn relations_inline(page: &mut Page, icons: Icons, d: &IssueDetail) {
    if let Some(t) = &d.issue_type {
        inline(page, "Type", vec![chip(t, Bg::SecondaryContainer)]);
    }
    if let Some(parent) = &d.parent {
        let segs = linked_segs(page, icons, &d.repo, parent);
        inline(page, "Parent", segs);
    }
    if let Some(p) = d.progress {
        let mut segs = vec![meta(format!("{} of {} done", p.done, p.total))];
        for l in &d.sub_issues {
            segs.push(meta(" · "));
            segs.extend(linked_segs(page, icons, &d.repo, l));
        }
        inline(page, "Sub-issues", segs);
    }
    for (name, list) in [("Blocked by", &d.blocked_by), ("Blocking", &d.blocking)] {
        if list.is_empty() {
            continue;
        }
        let mut segs = Vec::new();
        for l in list {
            if !segs.is_empty() {
                segs.push(meta(", "));
            }
            segs.extend(linked_segs(page, icons, &d.repo, l));
        }
        if list.left_out() > 0 {
            segs.push(meta(format!(" and {} more", list.left_out())));
        }
        inline(page, name, segs);
    }
    let mut dev = Vec::new();
    for l in &d.closed_by {
        if !dev.is_empty() {
            dev.push(meta(", "));
        }
        dev.extend(linked_segs(page, icons, &d.repo, l));
    }
    for b in &d.branches {
        if !dev.is_empty() {
            dev.push(meta(", "));
        }
        let target = url::tree(&d.repo, b, "");
        dev.push(meta("⌥ "));
        dev.push(link_seg(page, b.clone(), target, Role::Link));
    }
    inline(page, "Development", dev);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::timeline::Event;

    fn ev(actor: &str, what: What) -> Event {
        Event {
            actor: actor.into(),
            at: "2026-01-01T00:00:00Z".into(),
            what,
        }
    }

    /// Labels by one person run together; another's, or a closing, don't.
    #[test]
    fn runs_are_one_kind_by_one_person() {
        let label = |added| What::Labeled {
            label: Label {
                name: "bug".into(),
                color: "ff0000".into(),
            },
            added,
        };
        let a = ev("alice", label(true));
        assert!(same_run(&a, &ev("alice", label(false))));
        assert!(!same_run(&a, &ev("bob", label(true))));
        let closed = What::Closed {
            by: None,
            reason: None,
            duplicate: false,
        };
        assert!(!same_run(
            &ev("alice", closed.clone()),
            &ev("alice", closed)
        ));
    }
}