omni-dev 0.46.0

AI-powered git commit rewriter, PR generator, and MCP server for Jira, Confluence, Datadog, Gmail, and Drive.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
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
//! The action-menu and confirm-modal popups (issue #1585 Phase 2, extended
//! by #1602 Phase 1 into the context-menu machinery).
//!
//! Two things here are load-bearing for #1602:
//!
//! * **Placement is a pure function** ([`place`]) of anchor, content size and
//!   frame size, so the flip-up/flip-left/clamp rules are unit-tested with no
//!   terminal at all.
//! * **The renderer produces the hit map.** [`draw_menu`] returns the
//!   [`PopupRegion`] describing what it just drew, so `RegionMap` and the
//!   frame cannot disagree about where an item is — the same invariant
//!   `GroupRegion::tab_spans` already keeps for tab strips.

use std::path::PathBuf;

use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Wrap};
use ratatui::Frame;
use unicode_width::UnicodeWidthStr;

use super::actions::{ActionKind, ConfirmPrompt};
use super::glyph::{Glyph, GlyphMode};
use super::keys::ChromeKey;
use super::mouse::{PopupRegion, SubmenuRegion};

/// What invoking a menu entry does.
///
/// Three kinds, because a menu entry is not always an action. The tree-row
/// menu dispatches [`ActionKind`]s; the tab-strip and terminal-grid menus
/// (#1602 Phase 3) mostly re-use chords the chrome already implements; and a
/// few entries exist only in a menu and have no key at all.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MenuCommand {
    /// A selection action — the same list the `a` menu and `:` palette use.
    Action(ActionKind),
    /// A chord the chrome already handles, invoked from a menu instead of
    /// from the keyboard. Routing these through the *existing* `ChromeKey`
    /// dispatch rather than reimplementing them is what keeps a menu entry
    /// and its shortcut from drifting apart.
    Chrome(ChromeKey),
    /// An entry with no chord behind it.
    Ui(UiAction),
}

/// Menu-only commands: the ones with no [`ChromeKey`] and no [`ActionKind`].
///
/// Deliberately small. Anything here is reachable *only* from a menu, so each
/// variant is a decision to add a capability the keyboard cannot reach — the
/// opposite of the rest of this surface, where the menu is a second route to
/// something that already had one.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UiAction {
    /// Close every tab in this group except the clicked one.
    CloseOtherTabs,
    /// Close the tabs after the clicked one in this group.
    CloseTabsToRight,
    /// Select the whole scrollback of the focused grid.
    SelectAll,
    /// Drop the grid's selection without copying it.
    ClearSelection,
    /// Jump the grid back to the live edge.
    ScrollToBottom,
    /// Close the pane group above the splitter that was clicked.
    CloseGroupAbove,
    /// Close the pane group below the splitter that was clicked.
    CloseGroupBelow,
    /// Quit, via the same confirm gate `q` uses.
    Quit,
}

/// One selectable entry — the command it dispatches and its display label.
///
/// `enabled: false` renders dimmed and refuses invocation. The tree menu never
/// uses it (`applicable_actions` *hides* rather than greys out, and that
/// filter is what keeps *Close Worktree* off a main working tree); it exists
/// for the Phase 3 grid menu, where *Copy* must be visible-but-inert with no
/// selection so its keyboard shortcut stays discoverable.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MenuItem {
    pub command: MenuCommand,
    pub label: &'static str,
    pub enabled: bool,
}

impl MenuItem {
    /// An enabled item — the common case.
    pub fn new(command: MenuCommand, label: &'static str) -> Self {
        Self {
            command,
            label,
            enabled: true,
        }
    }

    /// An item dispatching `action`, enabled.
    pub fn action(action: ActionKind, label: &'static str) -> Self {
        Self::new(MenuCommand::Action(action), label)
    }
}

/// A row of a menu. Separators carry no command and are skipped by keyboard
/// navigation and by hit-testing alike, so neither route can select one.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MenuEntry {
    Item(MenuItem),
    Separator,
    /// One level of submenu — *Row Colour ▸*. Deliberately holds a flat
    /// `Vec<MenuItem>` and not `Vec<MenuEntry>`: arbitrary nesting was
    /// rejected in #1602, and a flat list makes "one level only" structural
    /// rather than a convention someone can quietly break.
    Submenu {
        label: &'static str,
        items: Vec<MenuItem>,
    },
}

impl MenuEntry {
    fn item(&self) -> Option<&MenuItem> {
        match self {
            Self::Item(item) => Some(item),
            Self::Separator | Self::Submenu { .. } => None,
        }
    }

    /// Whether the keyboard and the mouse can land on this row. A submenu
    /// row is selectable but carries no command of its own — activating it
    /// opens the submenu instead.
    fn is_selectable(&self) -> bool {
        !matches!(self, Self::Separator)
    }

    fn label(&self) -> &'static str {
        match self {
            Self::Item(item) => item.label,
            Self::Submenu { label, .. } => label,
            Self::Separator => "",
        }
    }
}

/// Where a popup wants to be.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Anchor {
    /// Centred in the frame — the `a` menu and both relocation pickers.
    #[default]
    Centered,
    /// Top-left corner at this screen cell — a context menu at the pointer.
    At { col: u16, row: u16 },
}

/// Places a `width`x`height` popup for `anchor` inside `area`.
///
/// Anchored placement follows the universal convention: prefer down-and-right
/// of the anchor, **flip** to the other side of it when that would overflow,
/// and **clamp** into the frame when even the flip does not fit (a popup
/// taller or wider than the frame, which scrolling then handles). Pure: no
/// frame, no terminal, no state.
pub fn place(anchor: Anchor, width: u16, height: u16, area: Rect) -> Rect {
    let width = width.min(area.width);
    let height = height.min(area.height);
    match anchor {
        Anchor::Centered => Rect {
            x: area.x + (area.width.saturating_sub(width)) / 2,
            y: area.y + (area.height.saturating_sub(height)) / 2,
            width,
            height,
        },
        Anchor::At { col, row } => {
            // Flip rather than merely clamp, so a menu opened near the right
            // or bottom edge still sits beside the pointer instead of under
            // it.
            let x = if col.saturating_add(width) <= area.right() {
                col
            } else {
                col.saturating_sub(width)
            };
            let y = if row.saturating_add(height) <= area.bottom() {
                row
            } else {
                row.saturating_sub(height)
            };
            // `width <= area.width` above guarantees `right() - width >= x`,
            // so these clamps can never invert.
            Rect {
                x: x.clamp(area.x, area.right().saturating_sub(width)),
                y: y.clamp(area.y, area.bottom().saturating_sub(height)),
                width,
                height,
            }
        }
    }
}

/// A menu popup: the `a` action menu, the `c` row-colour picker, and (from
/// Phase 2) the per-surface context menus.
pub struct ActionMenu {
    pub title: &'static str,
    pub entries: Vec<MenuEntry>,
    /// Index into `entries`. Always on an item, never a separator, provided
    /// the menu has at least one item.
    pub selected: usize,
    /// First entry rendered — non-zero only when the menu is taller than the
    /// frame allows.
    pub scroll: usize,
    pub anchor: Anchor,
    /// Whether a press has begun *inside* this menu.
    ///
    /// A context menu opens on `Down(Right)` at the pointer, so the matching
    /// `Up(Right)` lands on the menu a moment later. Without this latch that
    /// release would invoke whatever item the menu happened to open under.
    /// Only a press that starts inside the menu arms it, and only an armed
    /// menu can be invoked by a release.
    pub armed: bool,
    /// The tree row the menu was opened from, when it was opened from one.
    ///
    /// A context menu names a *subject*, so it must not outlive it. The tree
    /// is live — a daemon push can remove the worktree the menu is acting on
    /// while it is open — and a menu still offering *Close Worktree* for a
    /// row that has gone would act on whatever row slid into its place.
    /// `None` for menus with no row subject (the `c` picker).
    pub origin_row: Option<PathBuf>,
    /// The entry index of the open submenu, if any. At most one, ever.
    pub open_submenu: Option<usize>,
    /// Index into the open submenu's items.
    pub submenu_selected: usize,
}

impl ActionMenu {
    /// A centred menu of plain items — the `a` menu, the `c` picker and the
    /// palette's post-filter menu.
    pub fn new(items: Vec<MenuItem>) -> Self {
        Self::with_entries("Actions", items.into_iter().map(MenuEntry::Item).collect())
    }

    pub fn with_entries(title: &'static str, entries: Vec<MenuEntry>) -> Self {
        let mut menu = Self {
            title,
            entries,
            selected: 0,
            scroll: 0,
            anchor: Anchor::Centered,
            armed: false,
            origin_row: None,
            open_submenu: None,
            submenu_selected: 0,
        };
        // Opening on a separator would make the first `Enter` do nothing.
        if menu.entry_is_selectable(0) {
            return menu;
        }
        menu.selected = menu.next_selectable(0, 1).unwrap_or(0);
        menu
    }

    fn entry_is_selectable(&self, index: usize) -> bool {
        self.entries
            .get(index)
            .is_some_and(MenuEntry::is_selectable)
    }

    /// The open submenu's items, if one is open.
    fn submenu_items(&self) -> Option<&[MenuItem]> {
        match self.entries.get(self.open_submenu?)? {
            MenuEntry::Submenu { items, .. } => Some(items),
            MenuEntry::Item(_) | MenuEntry::Separator => None,
        }
    }

    /// Opens the submenu on the selected row, if it is one. `→`, and what
    /// `Enter` does on a submenu row.
    pub fn open_selected_submenu(&mut self) -> bool {
        if matches!(
            self.entries.get(self.selected),
            Some(MenuEntry::Submenu { .. })
        ) {
            self.open_submenu = Some(self.selected);
            self.submenu_selected = 0;
            return true;
        }
        false
    }

    /// Closes the open submenu, returning whether one was open — `←`, and
    /// what `Esc` does *first*, so one `Esc` never closes both levels.
    pub fn close_submenu(&mut self) -> bool {
        self.open_submenu.take().is_some()
    }

    /// The next selectable index from `from` walking by `step`, or `None`
    /// when there is none in that direction.
    fn next_selectable(&self, from: usize, step: isize) -> Option<usize> {
        let mut index = from as isize;
        loop {
            index += step;
            if index < 0 || index as usize >= self.entries.len() {
                return None;
            }
            if self.entry_is_selectable(index as usize) {
                return Some(index as usize);
            }
        }
    }

    /// Moves the selection by `delta` items, skipping separators and stopping
    /// at either end rather than wrapping. Drives the open submenu when there
    /// is one, since that is what the eye is on.
    pub fn move_selection(&mut self, delta: isize) {
        if let Some(len) = self.submenu_items().map(<[MenuItem]>::len) {
            if len == 0 {
                return;
            }
            let max = (len - 1) as isize;
            let current = (self.submenu_selected.min(len - 1)) as isize;
            self.submenu_selected = (current + delta).clamp(0, max) as usize;
            return;
        }
        let step = if delta < 0 { -1 } else { 1 };
        for _ in 0..delta.abs() {
            match self.next_selectable(self.selected, step) {
                Some(next) => self.selected = next,
                None => break,
            }
        }
    }

    /// Selects the first/last item — `Home`/`End`. Walks inward from just
    /// outside the range, so the end entry itself is considered and a
    /// trailing separator is skipped.
    ///
    /// Drives the open submenu when there is one, for the same reason
    /// [`Self::move_selection`] does: the eye is on the submenu, and
    /// [`Self::selected_command`] reads from it. Moving the *parent*
    /// highlight here instead would leave `Enter` dispatching a submenu item
    /// while a different row is the one lit up.
    pub fn select_end(&mut self, last: bool) {
        if let Some(len) = self.submenu_items().map(<[MenuItem]>::len) {
            if len > 0 {
                self.submenu_selected = if last { len - 1 } else { 0 };
            }
            return;
        }
        let (from, step) = if last {
            (self.entries.len() as isize, -1)
        } else {
            (-1, 1)
        };
        let mut index = from;
        loop {
            index += step;
            if index < 0 || index as usize >= self.entries.len() {
                return;
            }
            if self.entry_is_selectable(index as usize) {
                self.selected = index as usize;
                return;
            }
        }
    }

    /// Selects entry `index` if it is a selectable item. Used by the mouse:
    /// hover moves the highlight, a click invokes what is under it.
    pub fn select_index(&mut self, index: usize) -> bool {
        if self.entry_is_selectable(index) {
            self.selected = index;
            return true;
        }
        false
    }

    /// The command the selection would dispatch — `None` on an empty menu, a
    /// disabled item, or a submenu row (which opens rather than dispatches).
    /// That `None` is what makes a disabled entry inert to `Enter` as well as
    /// to the mouse.
    pub fn selected_command(&self) -> Option<MenuCommand> {
        if let Some(items) = self.submenu_items() {
            return items
                .get(self.submenu_selected)
                .filter(|item| item.enabled)
                .map(|item| item.command);
        }
        self.entries
            .get(self.selected)?
            .item()
            .filter(|item| item.enabled)
            .map(|item| item.command)
    }

    /// What `Enter` (or a click) does: opens the submenu on a submenu row,
    /// otherwise yields the command to dispatch. Keeping this one method
    /// stops the key path and the mouse path from disagreeing about which
    /// rows dispatch and which expand.
    pub fn activate(&mut self) -> Option<MenuCommand> {
        if self.open_submenu.is_none() && self.open_selected_submenu() {
            return None;
        }
        self.selected_command()
    }

    /// Scrolls so the selection is visible in a window `rows` tall.
    fn sync_scroll(&mut self, rows: usize) {
        if rows == 0 {
            return;
        }
        if self.selected < self.scroll {
            self.scroll = self.selected;
        } else if self.selected >= self.scroll + rows {
            self.scroll = self.selected + 1 - rows;
        }
        let max_scroll = self.entries.len().saturating_sub(rows);
        self.scroll = self.scroll.min(max_scroll);
    }
}

/// The confirm modal — renders a [`ConfirmPrompt`] verbatim (body, then
/// risks, then info), with the yes/no footer hint.
pub struct ConfirmModal {
    pub prompt: ConfirmPrompt,
}

/// Width of a menu popup. Fixed rather than content-derived so a menu does
/// not resize as its filtered contents change under the palette.
const MENU_WIDTH: u16 = 50;

/// Narrowest a submenu is allowed to get, so a list of short labels still
/// reads as a menu rather than a sliver.
const MIN_SUBMENU_WIDTH: u16 = 14;

/// A submenu is sized to its content, not to [`MENU_WIDTH`].
///
/// The parent is a fixed 50 columns wide precisely so it does not resize as
/// its contents change, but a *submenu* inheriting that width cannot fit
/// beside its parent in an 80-column terminal — and a 50-wide box for eleven
/// colour names would be mostly empty even where it fits. Measured in display
/// cells, like every other width in this UI.
fn submenu_width(items: &[MenuItem]) -> u16 {
    let longest = items
        .iter()
        .map(|item| UnicodeWidthStr::width(item.label))
        .max()
        .unwrap_or(0);
    // +2 for the borders.
    u16::try_from(longest.saturating_add(2))
        .unwrap_or(MENU_WIDTH)
        .clamp(MIN_SUBMENU_WIDTH, MENU_WIDTH)
}

/// Where a `width`-wide submenu goes relative to its already-placed parent.
///
/// Right of the parent, flipping to its left when that overflows, and only
/// clamping into the frame when neither side fits. The clamp is last on
/// purpose: it is the one case that *overlaps* the parent, which hides the
/// very row the submenu belongs to.
fn submenu_col(parent: Rect, width: u16, area: Rect) -> u16 {
    if parent.right().saturating_add(width) <= area.right() {
        parent.right()
    } else if parent.x >= area.x.saturating_add(width) {
        parent.x - width
    } else {
        area.right().saturating_sub(width)
    }
}

/// Draws `menu` and returns the region describing what was drawn, for
/// `RegionMap`.
pub fn draw_menu(
    frame: &mut Frame<'_>,
    area: Rect,
    menu: &mut ActionMenu,
    glyphs: GlyphMode,
) -> PopupRegion {
    let wanted_height = menu.entries.len().saturating_add(2).min(u16::MAX as usize) as u16;
    let popup_area = place(menu.anchor, MENU_WIDTH, wanted_height, area);
    let rows = usize::from(popup_area.height.saturating_sub(2));
    menu.sync_scroll(rows);

    let truncated = menu.entries.len() > rows;
    let mut lines: Vec<ListItem> = Vec::with_capacity(rows);
    let mut items: Vec<(usize, Rect)> = Vec::new();
    // The screen row of the open submenu's parent, once we reach it — the
    // submenu is drawn beside it, so it must be laid out after the parent.
    let mut submenu_row: Option<u16> = None;
    let inner_x = popup_area.x + 1;
    let inner_width = popup_area.width.saturating_sub(2);

    for offset in 0..rows {
        let index = menu.scroll + offset;
        let row_y = popup_area.y + 1 + offset as u16;
        // The first and last visible rows become `…` affordances when there
        // is more menu beyond them, rather than silently truncating.
        let more_above = truncated && offset == 0 && menu.scroll > 0;
        let more_below = truncated && offset + 1 == rows && menu.scroll + rows < menu.entries.len();
        if more_above || more_below {
            lines.push(ListItem::new(Line::from(Span::styled(
                "  …",
                Style::default().fg(Color::DarkGray),
            ))));
            continue;
        }
        let Some(entry) = menu.entries.get(index) else {
            break;
        };
        match entry {
            MenuEntry::Separator => lines.push(ListItem::new(Line::from(Span::styled(
                "─".repeat(usize::from(inner_width)),
                Style::default().fg(Color::DarkGray),
            )))),
            MenuEntry::Item(_) | MenuEntry::Submenu { .. } => {
                let enabled = entry.item().is_none_or(|item| item.enabled);
                let style = if !enabled {
                    Style::default().fg(Color::DarkGray)
                } else if index == menu.selected {
                    Style::default().add_modifier(Modifier::REVERSED)
                } else {
                    Style::default()
                };
                // A submenu row is marked with a trailing arrow, right-aligned
                // so the column reads down the menu.
                let text = match entry {
                    MenuEntry::Submenu { label, .. } => {
                        let arrow = Glyph::SubmenuArrow.as_str(glyphs);
                        let pad =
                            usize::from(inner_width).saturating_sub(label.chars().count() + 1);
                        format!("{label}{}{arrow}", " ".repeat(pad))
                    }
                    _ => entry.label().to_string(),
                };
                lines.push(ListItem::new(Line::from(Span::styled(text, style))));
                items.push((
                    index,
                    Rect {
                        x: inner_x,
                        y: row_y,
                        width: inner_width,
                        height: 1,
                    },
                ));
                if menu.open_submenu == Some(index) {
                    submenu_row = Some(row_y);
                }
            }
        }
    }

    frame.render_widget(Clear, popup_area);
    let list = List::new(lines).block(
        Block::default()
            .borders(Borders::ALL)
            .title(menu.title.to_string()),
    );
    frame.render_widget(list, popup_area);

    // A submenu whose parent row is not on screen has nothing to hang off,
    // and leaving `open_submenu` set would let `selected_command` dispatch an
    // item nobody can see. Closing it here keeps "what is open is what is
    // drawn" true by construction rather than by the callers being careful.
    if submenu_row.is_none() {
        menu.open_submenu = None;
    }

    // The submenu is drawn last so it sits above the parent.
    let submenu = submenu_row.and_then(|row| {
        let items = menu.submenu_items()?;
        let height = (items.len() as u16).saturating_add(2);
        let width = submenu_width(items).min(area.width);
        let col = submenu_col(popup_area, width, area);
        let rect = place(Anchor::At { col, row }, width, height, area);
        frame.render_widget(Clear, rect);
        let rows: Vec<ListItem> = items
            .iter()
            .enumerate()
            .map(|(i, item)| {
                let style = if !item.enabled {
                    Style::default().fg(Color::DarkGray)
                } else if i == menu.submenu_selected {
                    Style::default().add_modifier(Modifier::REVERSED)
                } else {
                    Style::default()
                };
                ListItem::new(Line::from(Span::styled(item.label, style)))
            })
            .collect();
        frame.render_widget(
            List::new(rows).block(Block::default().borders(Borders::ALL)),
            rect,
        );
        let item_rects = (0..items.len())
            .map(|i| {
                (
                    i,
                    Rect {
                        x: rect.x + 1,
                        y: rect.y + 1 + i as u16,
                        width: rect.width.saturating_sub(2),
                        height: 1,
                    },
                )
            })
            .filter(|(_, r)| r.y < rect.bottom().saturating_sub(1))
            .collect();
        Some(SubmenuRegion {
            rect,
            items: item_rects,
        })
    });

    PopupRegion {
        rect: popup_area,
        items,
        submenu,
    }
}

/// A generic "pick one of these labelled items" popup — the Move/Copy-Claude-
/// Session-Here flow's session and destination pickers. Kept generic over
/// `&str` labels rather than tied to [`ActionKind`] since those pick a
/// session id / a worktree path, not an action. Always centred, and inert to
/// the mouse (see `app::popup_mouse_enabled`).
pub fn draw_list_popup(
    frame: &mut Frame<'_>,
    area: Rect,
    title: &str,
    items: &[&str],
    selected: usize,
) {
    let popup_height = (items.len() as u16 + 2).min(area.height);
    let popup_area = place(Anchor::Centered, MENU_WIDTH, popup_height, area);
    frame.render_widget(Clear, popup_area);
    let list_items: Vec<ListItem> = items
        .iter()
        .enumerate()
        .map(|(i, label)| {
            let style = if i == selected {
                Style::default().add_modifier(Modifier::REVERSED)
            } else {
                Style::default()
            };
            ListItem::new(Line::from(Span::styled(*label, style)))
        })
        .collect();
    let list = List::new(list_items).block(Block::default().borders(Borders::ALL).title(title));
    frame.render_widget(list, popup_area);
}

/// A one-line text prompt — the scrollback find (`alt-f`) and the command
/// palette (`:`). `hint` names what the input does; `status` carries the
/// last result ("no match", the filtered command count).
pub fn draw_prompt(frame: &mut Frame<'_>, area: Rect, title: &str, input: &str, status: &str) {
    let width = area.width.saturating_sub(8).clamp(20, 70);
    let rect = Rect {
        x: area.x + (area.width.saturating_sub(width)) / 2,
        y: area.y + area.height / 3,
        width,
        height: 3,
    };
    frame.render_widget(Clear, rect);
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan))
        .title(title.to_string());
    let inner = block.inner(rect);
    frame.render_widget(block, rect);
    let line = if status.is_empty() {
        Line::from(vec![
            Span::raw(input.to_string()),
            Span::styled("_", Style::default().add_modifier(Modifier::REVERSED)),
        ])
    } else {
        Line::from(vec![
            Span::raw(input.to_string()),
            Span::styled("_", Style::default().add_modifier(Modifier::REVERSED)),
            Span::styled(format!("   {status}"), Style::default().fg(Color::DarkGray)),
        ])
    };
    frame.render_widget(Paragraph::new(line), inner);
}

pub fn draw_confirm_modal(frame: &mut Frame<'_>, area: Rect, modal: &ConfirmModal) {
    // Body, plus a header+lines block each for risks/info only when
    // non-empty, plus a blank line and the footer hint, plus the block's
    // own top/bottom border rows.
    let risk_block = if modal.prompt.risk_lines.is_empty() {
        0
    } else {
        1 + modal.prompt.risk_lines.len()
    };
    let info_block = if modal.prompt.info_lines.is_empty() {
        0
    } else {
        1 + modal.prompt.info_lines.len()
    };
    let content_lines = modal.prompt.body_lines.len() + risk_block + info_block + 2 + 2;
    let popup_area = place(
        Anchor::Centered,
        70,
        (content_lines as u16).min(area.height),
        area,
    );
    frame.render_widget(Clear, popup_area);

    let mut lines: Vec<Line> = Vec::new();
    for line in &modal.prompt.body_lines {
        lines.push(Line::from(line.as_str()));
    }
    if !modal.prompt.risk_lines.is_empty() {
        lines.push(Line::from(Span::styled(
            "risks:",
            Style::default().add_modifier(Modifier::BOLD).fg(Color::Red),
        )));
        for line in &modal.prompt.risk_lines {
            lines.push(Line::from(Span::styled(
                format!("  {line}"),
                Style::default().fg(Color::Red),
            )));
        }
    }
    if !modal.prompt.info_lines.is_empty() {
        lines.push(Line::from(Span::styled(
            "info:",
            Style::default().add_modifier(Modifier::BOLD),
        )));
        for line in &modal.prompt.info_lines {
            lines.push(Line::from(format!("  {line}")));
        }
    }
    lines.push(Line::from(""));
    lines.push(Line::from("y/enter confirm   n/esc cancel"));

    let paragraph = Paragraph::new(lines).wrap(Wrap { trim: false }).block(
        Block::default()
            .borders(Borders::ALL)
            .title(modal.prompt.title.as_str()),
    );
    frame.render_widget(paragraph, popup_area);
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use ratatui::backend::TestBackend;
    use ratatui::Terminal;

    use super::*;

    fn buffer_text(terminal: &Terminal<TestBackend>) -> String {
        terminal
            .backend()
            .buffer()
            .content
            .iter()
            .map(ratatui::buffer::Cell::symbol)
            .collect()
    }

    fn frame() -> Rect {
        Rect::new(0, 0, 80, 24)
    }

    #[test]
    fn anchored_placement_puts_the_popup_at_the_pointer() {
        let at = place(Anchor::At { col: 10, row: 4 }, 20, 6, frame());
        assert_eq!(at, Rect::new(10, 4, 20, 6));
    }

    #[test]
    fn placement_flips_up_at_the_bottom_edge_and_left_at_the_right_edge() {
        // Bottom: 20 + 6 > 24, so the popup sits *above* the pointer.
        let up = place(Anchor::At { col: 0, row: 20 }, 20, 6, frame());
        assert_eq!(up, Rect::new(0, 14, 20, 6));
        // Right: 70 + 20 > 80, so it sits *left* of the pointer.
        let left = place(Anchor::At { col: 70, row: 0 }, 20, 6, frame());
        assert_eq!(left, Rect::new(50, 0, 20, 6));
        // Both at once.
        let both = place(Anchor::At { col: 78, row: 23 }, 20, 6, frame());
        assert_eq!(both, Rect::new(58, 17, 20, 6));
    }

    #[test]
    fn placement_clamps_instead_of_flipping_when_the_popup_exceeds_the_frame() {
        let small = Rect::new(0, 0, 10, 4);
        // Wider and taller than the frame: clamped to it, never negative.
        let placed = place(Anchor::At { col: 8, row: 3 }, 40, 20, small);
        assert_eq!(placed, small);
    }

    #[test]
    fn centered_placement_is_unchanged_by_the_anchor_parameter() {
        assert_eq!(
            place(Anchor::Centered, 20, 6, frame()),
            Rect::new(30, 9, 20, 6)
        );
    }

    #[test]
    fn navigation_skips_separators_and_clamps_at_both_ends() {
        let mut menu = ActionMenu::with_entries(
            "t",
            vec![
                MenuEntry::Item(MenuItem::action(ActionKind::Focus, "a")),
                MenuEntry::Separator,
                MenuEntry::Item(MenuItem::action(ActionKind::CopyDirectory, "b")),
            ],
        );
        assert_eq!(menu.selected, 0);
        menu.move_selection(1);
        assert_eq!(menu.selected, 2, "the separator is stepped over");
        menu.move_selection(5);
        assert_eq!(menu.selected, 2, "clamps at the end");
        menu.move_selection(-5);
        assert_eq!(menu.selected, 0, "clamps at the start");
    }

    #[test]
    fn a_menu_opening_on_a_separator_selects_the_first_real_item() {
        let menu = ActionMenu::with_entries(
            "t",
            vec![
                MenuEntry::Separator,
                MenuEntry::Item(MenuItem::action(ActionKind::Focus, "a")),
            ],
        );
        assert_eq!(menu.selected, 1);
        assert_eq!(
            menu.selected_command(),
            Some(MenuCommand::Action(ActionKind::Focus))
        );
    }

    #[test]
    fn home_and_end_land_on_real_items() {
        let mut menu = ActionMenu::with_entries(
            "t",
            vec![
                MenuEntry::Item(MenuItem::action(ActionKind::Focus, "a")),
                MenuEntry::Item(MenuItem::action(ActionKind::CopyDirectory, "b")),
                MenuEntry::Separator,
            ],
        );
        menu.select_end(true);
        assert_eq!(menu.selected, 1, "End skips the trailing separator");
        menu.select_end(false);
        assert_eq!(menu.selected, 0);
    }

    #[test]
    fn a_separator_cannot_be_selected_by_index_and_a_disabled_item_is_inert() {
        let mut menu = ActionMenu::with_entries(
            "t",
            vec![
                MenuEntry::Item(MenuItem::action(ActionKind::Focus, "a")),
                MenuEntry::Separator,
                MenuEntry::Item(MenuItem {
                    command: MenuCommand::Action(ActionKind::CopyDirectory),
                    label: "b",
                    enabled: false,
                }),
            ],
        );
        assert!(!menu.select_index(1), "separator");
        assert_eq!(menu.selected, 0);
        assert!(
            menu.select_index(2),
            "a disabled item still takes the highlight"
        );
        assert_eq!(
            menu.selected_command(),
            None,
            "but dispatches nothing, by key or by mouse"
        );
    }

    #[test]
    fn selected_command_is_none_when_empty() {
        let menu = ActionMenu::new(Vec::new());
        assert_eq!(menu.selected_command(), None);
    }

    /// A plain item beside a *Row Colour* submenu, mirroring `app.rs`'s
    /// real context-menu shape.
    fn submenu_entries() -> Vec<MenuEntry> {
        vec![
            MenuEntry::Item(MenuItem::action(ActionKind::Focus, "Open Worktree")),
            MenuEntry::Submenu {
                label: "Row Colour",
                items: vec![
                    MenuItem::action(ActionKind::SetRowColor("red"), "red"),
                    MenuItem::action(ActionKind::SetRowColor("blue"), "blue"),
                ],
            },
        ]
    }

    #[test]
    fn activate_on_a_submenu_row_opens_it_and_a_second_activate_dispatches_the_selected_item() {
        let mut menu = ActionMenu::with_entries("t", submenu_entries());
        menu.selected = 1;
        assert_eq!(menu.activate(), None, "opening must not dispatch");
        assert_eq!(menu.open_submenu, Some(1));
        assert_eq!(menu.submenu_selected, 0);
        assert_eq!(
            menu.activate(),
            Some(MenuCommand::Action(ActionKind::SetRowColor("red"))),
            "the second activate dispatches the selected submenu item"
        );
    }

    #[test]
    fn home_and_end_drive_the_open_submenu_rather_than_the_parent() {
        let mut menu = ActionMenu::with_entries("t", submenu_entries());
        menu.selected = 1;
        menu.open_selected_submenu();
        menu.submenu_selected = 1;

        menu.select_end(false); // Home
        assert_eq!(menu.submenu_selected, 0, "Home moves inside the submenu");
        assert_eq!(
            menu.selected, 1,
            "and leaves the parent highlight on the submenu's own row"
        );
        // The bug this guards: moving the *parent* highlight here would leave
        // `Enter` dispatching a submenu item while another row is lit up.
        assert_eq!(
            menu.selected_command(),
            Some(MenuCommand::Action(ActionKind::SetRowColor("red"))),
            "what Enter dispatches is what is highlighted"
        );

        menu.select_end(true); // End
        assert_eq!(menu.submenu_selected, 1);
        assert_eq!(menu.selected, 1);

        // With nothing open, both drive the parent again.
        menu.close_submenu();
        menu.select_end(false);
        assert_eq!(menu.selected, 0);
    }

    #[test]
    fn close_submenu_reports_whether_one_was_open() {
        let mut menu = ActionMenu::with_entries("t", submenu_entries());
        menu.selected = 1;
        assert!(!menu.close_submenu(), "nothing open yet");
        menu.open_selected_submenu();
        assert!(menu.close_submenu(), "one was open");
        assert!(!menu.close_submenu(), "already closed");
    }

    #[test]
    fn move_selection_drives_the_submenu_while_open_and_the_parent_once_closed() {
        let mut menu = ActionMenu::with_entries("t", submenu_entries());
        menu.selected = 1;
        menu.open_selected_submenu();
        menu.move_selection(1);
        assert_eq!(menu.submenu_selected, 1, "submenu selection moved");
        assert_eq!(menu.selected, 1, "parent selection untouched while open");
        menu.close_submenu();
        menu.move_selection(-1);
        assert_eq!(
            menu.selected, 0,
            "with nothing open, the parent moves again"
        );
    }

    #[test]
    fn selected_command_is_none_for_a_disabled_item_inside_an_open_submenu() {
        let mut menu = ActionMenu::with_entries(
            "t",
            vec![MenuEntry::Submenu {
                label: "Row Colour",
                items: vec![MenuItem {
                    command: MenuCommand::Action(ActionKind::SetRowColor("red")),
                    label: "red",
                    enabled: false,
                }],
            }],
        );
        menu.selected = 0;
        menu.open_selected_submenu();
        assert_eq!(
            menu.selected_command(),
            None,
            "disabled items are inert inside a submenu too"
        );
    }

    #[test]
    fn home_and_end_are_inert_with_nothing_selectable() {
        let mut menu =
            ActionMenu::with_entries("t", vec![MenuEntry::Separator, MenuEntry::Separator]);
        assert_eq!(menu.selected, 0, "no item to land on when opening");
        menu.select_end(true);
        assert_eq!(menu.selected, 0, "End finds nothing selectable either");
        menu.select_end(false);
        assert_eq!(menu.selected, 0, "nor Home");
    }

    #[test]
    fn draw_menu_reports_one_rect_per_visible_item_and_none_for_separators() {
        let backend = TestBackend::new(80, 24);
        let mut terminal = Terminal::new(backend).unwrap();
        let mut menu = ActionMenu::with_entries(
            "Actions",
            vec![
                MenuEntry::Item(MenuItem::action(ActionKind::Focus, "Open Worktree")),
                MenuEntry::Separator,
                MenuEntry::Item(MenuItem::action(
                    ActionKind::CloseWorktree,
                    "Close Worktree",
                )),
            ],
        );
        menu.anchor = Anchor::At { col: 4, row: 2 };
        let mut region = None;
        terminal
            .draw(|frame| {
                region = Some(draw_menu(
                    frame,
                    frame.area(),
                    &mut menu,
                    GlyphMode::Unicode,
                ));
            })
            .unwrap();
        let region = region.unwrap();
        assert_eq!(region.rect, Rect::new(4, 2, MENU_WIDTH, 5));
        // Two items, at entry indices 0 and 2; the separator has no rect.
        let indices: Vec<usize> = region.items.iter().map(|(i, _)| *i).collect();
        assert_eq!(indices, vec![0, 2]);
        assert_eq!(region.items[0].1.y, 3, "first item is below the border");
        assert_eq!(region.items[1].1.y, 5);
        let text = buffer_text(&terminal);
        assert!(text.contains("Open Worktree"));
        assert!(text.contains("Close Worktree"));
    }

    #[test]
    fn draw_menu_marks_a_submenu_row_with_the_arrow_and_reports_its_item_rects() {
        let backend = TestBackend::new(80, 24);
        let mut terminal = Terminal::new(backend).unwrap();
        let mut menu = ActionMenu::with_entries("Actions", submenu_entries());
        menu.selected = 1;
        menu.open_selected_submenu();
        menu.anchor = Anchor::At { col: 4, row: 2 };
        let mut region = None;
        terminal
            .draw(|frame| {
                region = Some(draw_menu(
                    frame,
                    frame.area(),
                    &mut menu,
                    GlyphMode::Unicode,
                ));
            })
            .unwrap();
        let region = region.unwrap();
        let submenu = region.submenu.expect("the submenu row is open");
        assert_eq!(submenu.items.len(), 2, "one rect per submenu item");
        let text = buffer_text(&terminal);
        assert!(text.contains('▸'), "the submenu row is arrow-marked");
        assert!(text.contains("red"));
        assert!(text.contains("blue"));
    }

    #[test]
    fn a_submenu_is_sized_to_its_content_and_never_covers_its_parent() {
        // 80 columns is the case that used to break: the parent is a fixed
        // 50 wide, so a submenu inheriting that width had nowhere to go but
        // on top of it, wiping out the very row it belongs to.
        let backend = TestBackend::new(80, 24);
        let mut terminal = Terminal::new(backend).unwrap();
        let mut menu = ActionMenu::with_entries("Actions", submenu_entries());
        menu.selected = 1;
        menu.open_selected_submenu();
        menu.anchor = Anchor::At { col: 4, row: 2 };
        let mut region = None;
        terminal
            .draw(|frame| {
                region = Some(draw_menu(
                    frame,
                    frame.area(),
                    &mut menu,
                    GlyphMode::Unicode,
                ));
            })
            .unwrap();
        let region = region.unwrap();
        let sub = region.submenu.expect("the submenu row is open").rect;
        assert_eq!(
            sub.width, MIN_SUBMENU_WIDTH,
            "sized to content, floored at the minimum"
        );
        assert!(
            sub.x >= region.rect.right(),
            "beside the parent ({sub:?} vs {:?}), not on top of it",
            region.rect
        );
        assert!(sub.right() <= 80, "and inside the frame");
        let text = buffer_text(&terminal);
        assert!(
            text.contains("Row Colour"),
            "the parent row it belongs to is still legible"
        );
    }

    #[test]
    fn a_submenu_flips_left_when_there_is_no_room_to_the_right() {
        let area = Rect::new(0, 0, 80, 24);
        let parent = Rect::new(28, 2, MENU_WIDTH, 6); // right() == 78
        assert_eq!(
            submenu_col(parent, 20, area),
            8,
            "no room right of 78, so it goes left of the parent"
        );
    }

    #[test]
    fn a_submenu_clamps_into_the_frame_only_when_neither_side_fits() {
        let area = Rect::new(0, 0, 30, 24);
        let parent = Rect::new(4, 2, 24, 6); // right() == 28
        assert_eq!(
            submenu_col(parent, 20, area),
            10,
            "neither side fits, so it abuts the frame's right edge"
        );
    }

    #[test]
    fn submenu_width_is_the_longest_label_plus_borders_clamped_both_ways() {
        let short = [MenuItem::action(ActionKind::SetRowColor("red"), "red")];
        assert_eq!(submenu_width(&short), MIN_SUBMENU_WIDTH, "floored");
        let long = [MenuItem::action(
            ActionKind::Focus,
            "a label far longer than fifty columns could ever hope to be",
        )];
        assert_eq!(submenu_width(&long), MENU_WIDTH, "capped at the parent's");
    }

    #[test]
    fn a_submenu_whose_parent_row_scrolled_out_of_view_is_closed_not_left_open() {
        let backend = TestBackend::new(60, 6);
        let mut terminal = Terminal::new(backend).unwrap();
        let mut entries: Vec<MenuEntry> = (0..20)
            .map(|_| MenuEntry::Item(MenuItem::action(ActionKind::Focus, "Open Worktree")))
            .collect();
        entries.push(MenuEntry::Submenu {
            label: "Row Colour",
            items: vec![MenuItem::action(ActionKind::SetRowColor("red"), "red")],
        });
        let mut menu = ActionMenu::with_entries("Actions", entries);
        menu.selected = 20;
        menu.open_selected_submenu();
        // Scroll the parent row out from under the open submenu.
        menu.scroll = 0;
        menu.selected = 0;
        let mut region = None;
        terminal
            .draw(|frame| {
                region = Some(draw_menu(
                    frame,
                    frame.area(),
                    &mut menu,
                    GlyphMode::Unicode,
                ));
            })
            .unwrap();
        assert!(region.unwrap().submenu.is_none(), "nothing was drawn");
        assert_eq!(menu.open_submenu, None, "so nothing is left open");
        assert_eq!(
            menu.selected_command(),
            Some(MenuCommand::Action(ActionKind::Focus)),
            "and Enter dispatches the visible row, not the vanished submenu"
        );
    }

    #[test]
    fn a_menu_taller_than_the_frame_scrolls_and_shows_the_affordances() {
        let backend = TestBackend::new(60, 6);
        let mut terminal = Terminal::new(backend).unwrap();
        let entries: Vec<MenuEntry> = (0..20)
            .map(|_| MenuEntry::Item(MenuItem::action(ActionKind::Focus, "row")))
            .collect();
        let mut menu = ActionMenu::with_entries("Actions", entries);
        menu.selected = 19;
        let mut region = None;
        terminal
            .draw(|frame| {
                region = Some(draw_menu(
                    frame,
                    frame.area(),
                    &mut menu,
                    GlyphMode::Unicode,
                ));
            })
            .unwrap();
        let region = region.unwrap();
        assert_eq!(
            region.rect.height, 6,
            "clamped to the frame, not overflowed"
        );
        assert!(menu.scroll > 0, "scrolled to reveal the selection");
        assert!(buffer_text(&terminal).contains('…'), "affordance drawn");
        // Every reported rect lies inside the popup.
        for (_, rect) in &region.items {
            assert!(rect.y > region.rect.y && rect.y < region.rect.bottom() - 1);
        }

        // Moving the selection back above the current scroll window (Home,
        // in effect) must scroll back up to follow it, not leave it hidden
        // above the visible range.
        let scrolled_down_to = menu.scroll;
        menu.selected = 0;
        terminal
            .draw(|frame| {
                draw_menu(frame, frame.area(), &mut menu, GlyphMode::Unicode);
            })
            .unwrap();
        assert!(
            menu.scroll < scrolled_down_to,
            "scrolled back up to follow the selection"
        );
        assert_eq!(menu.scroll, 0);
    }

    #[test]
    fn a_disabled_entry_renders_dimmed_rather_than_highlighted() {
        let backend = TestBackend::new(60, 10);
        let mut terminal = Terminal::new(backend).unwrap();
        let mut menu = ActionMenu::with_entries(
            "Actions",
            vec![MenuEntry::Item(MenuItem {
                command: MenuCommand::Action(ActionKind::CopyDirectory),
                label: "b",
                enabled: false,
            })],
        );
        // Force the selection onto the disabled entry: disabled items still
        // take the highlight (see `select_index`), so the dimmed style must
        // win over the "selected" reversed style.
        menu.selected = 0;
        let mut region = None;
        terminal
            .draw(|frame| {
                region = Some(draw_menu(
                    frame,
                    frame.area(),
                    &mut menu,
                    GlyphMode::Unicode,
                ));
            })
            .unwrap();
        let region = region.unwrap();
        let (_, rect) = region.items[0];
        let cell = &terminal.backend().buffer()[(rect.x, rect.y)];
        assert_eq!(cell.fg, Color::DarkGray, "dimmed, not reversed");
        assert!(!cell.modifier.contains(Modifier::REVERSED));
    }

    #[test]
    fn draw_list_popup_renders_a_custom_title_and_labels_with_the_selection_highlighted() {
        let backend = TestBackend::new(60, 20);
        let mut terminal = Terminal::new(backend).unwrap();
        terminal
            .draw(|frame| {
                draw_list_popup(
                    frame,
                    frame.area(),
                    "Move to which worktree?",
                    &["/repo/wt-1", "/repo/wt-2"],
                    1,
                );
            })
            .unwrap();
        let text = buffer_text(&terminal);
        assert!(text.contains("Move to which worktree?"));
        assert!(text.contains("/repo/wt-1"));
        assert!(text.contains("/repo/wt-2"));
    }

    #[test]
    fn draw_confirm_modal_renders_body_risks_and_info_verbatim() {
        let backend = TestBackend::new(80, 24);
        let mut terminal = Terminal::new(backend).unwrap();
        let modal = ConfirmModal {
            prompt: ConfirmPrompt {
                title: "Close 2 worktrees?".to_string(),
                body_lines: vec!["Worktree: /repo/wt-1".to_string()],
                risk_lines: vec!["/repo/wt-1: [dirty] uncommitted changes".to_string()],
                info_lines: vec!["/repo/wt-1: [unpushed] 2 unpushed commits".to_string()],
            },
        };
        terminal
            .draw(|frame| draw_confirm_modal(frame, frame.area(), &modal))
            .unwrap();
        let text = buffer_text(&terminal);
        assert!(text.contains("Close 2 worktrees?"));
        assert!(text.contains("Worktree: /repo/wt-1"));
        assert!(text.contains("dirty"));
        assert!(text.contains("uncommitted changes"));
        assert!(text.contains("unpushed"));
        assert!(text.contains("confirm"));
    }
}