fux 0.12.0

A minimal trusted Bevy terminal multiplexer
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
//! Viewer-local corner overlays. Captured entities are validated again on execution.
#[cfg(test)]
mod tests;
use crate::{
    actions::{self, Action, Target},
    assets::BindingAction,
    chrome,
    control::{Chooser, Command, Order, Scope, Subject},
    model::*,
    navigation,
    protocol::{Direction, Input, Key, Modifiers, MouseAction},
};
use bevy_ecs::prelude::*;
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize, std_traits::ReflectDefault};
use serde::{Deserialize, Serialize};

#[derive(Clone, Reflect)]
pub struct Entry {
    pub label: String,
    pub run: Run,
}
/// A list row either names a bound action (menus, which may prompt again) or
/// carries the exact command a chooser resolved (never prompts again).
#[derive(Clone, Reflect)]
pub enum Run {
    Action(Action),
    Command(Command),
}
#[derive(Clone, Reflect)]
pub enum Mode {
    List {
        title: String,
        entries: Vec<Entry>,
        selected: usize,
    },
    Confirm {
        command: Command,
    },
    Text {
        action: Action,
        buffer: String,
    },
}
/// The open prefix command column and its selected action row. Present only
/// while the column is open, like `Overlay` and `Selection`.
#[derive(Component, Default, Reflect, Serialize, Deserialize)]
#[reflect(Component, Default, Serialize, Deserialize)]
pub struct Prefix {
    pub scroll: usize,
}

/// A modal viewer owns its input: the prefix column, an overlay or copy mode.
pub(crate) fn modal(world: &World, id: Entity) -> bool {
    world.get_entity(id).is_ok_and(|entity| {
        entity.contains::<Prefix>()
            || entity.contains::<Overlay>()
            || entity.contains::<crate::selection::Selection>()
    })
}

pub(crate) fn close_prefix(world: &mut World, id: Entity) {
    if let Ok(mut entity) = world.get_entity_mut(id) {
        entity.remove::<Prefix>();
    }
}

/// Its enum fields keep their reflect encoding, so instead of serde it
/// registers a `Default` for the reflect insertion fallback: an empty list
/// with a placeholder target, which execution validates like any capture.
#[derive(Component, Clone, Reflect)]
#[reflect(Component, Default)]
#[component(on_insert = crate::paste::overlay_opened)]
pub struct Overlay {
    pub serial: u64,
    pub target: Target,
    pub mode: Mode,
}
impl Default for Overlay {
    fn default() -> Self {
        Self {
            serial: 0,
            target: Target {
                workspace: Entity::PLACEHOLDER,
                tab: None,
                leaf: None,
            },
            mode: Mode::List {
                title: String::new(),
                entries: Vec::new(),
                selected: 0,
            },
        }
    }
}

fn label(world: &World, entity: Entity) -> String {
    let entity = world.get::<PaneView>(entity).map_or(entity, |p| p.pane);
    world
        .get::<Name>(entity)
        .map_or_else(|| entity.to_bits().to_string(), |n| n.to_string())
}
use crate::navigation::workspaces as roots;
fn open(world: &mut World, id: Entity, target: Target, mode: Mode) {
    if world.get::<Viewer>(id).is_none() {
        return;
    }
    // `paste::overlay_opened` assigns the serial on insertion.
    world.entity_mut(id).remove::<Prefix>().insert(Overlay {
        serial: 0,
        target,
        mode,
    });
}

fn notify_error(world: &mut World, id: Entity, message: impl Into<String>) {
    notify(world, id, Notice::error(message));
}

/// Runs a bound action for a viewer: prompts and confirmations open an
/// overlay whose completion produces the command; everything else is a
/// command right away.
pub fn bound(world: &mut World, id: Entity, target: Target, action: Action) -> Result<(), String> {
    use Action::*;
    if let Some(reason) = actions::unavailable(world, target, action) {
        return Err(reason.into());
    }
    match action {
        Close | TabClose | WorkspaceClose => {
            let command = action.command(target).ok_or("no pane")?;
            settle(world, id);
            open(world, id, target, Mode::Confirm { command });
            Ok(())
        }
        RenamePane | RenameTab | RenameWorkspace | SaveLayout | LoadLayout => {
            settle(world, id);
            open(
                world,
                id,
                target,
                Mode::Text {
                    action,
                    buffer: String::new(),
                },
            );
            Ok(())
        }
        _ => crate::server::execute(world, id, action.command(target).ok_or("no pane")?),
    }
}

/// Every command starts from a quiet viewer: no selection, no column, no notice.
pub(crate) fn settle(world: &mut World, id: Entity) {
    if let Ok(mut entity) = world.get_entity_mut(id) {
        entity.remove::<(crate::selection::Selection, Prefix)>();
    }
    notify(world, id, None);
}

/// Opens an interactive list whose rows are resolved commands.
pub(crate) fn choose(
    world: &mut World,
    id: Entity,
    target: Target,
    chooser: Chooser,
) -> Result<(), String> {
    let title = match chooser {
        Chooser::Tab => Action::TabChoose,
        Chooser::Workspace => Action::WorkspaceChoose,
        Chooser::SwapTarget => Action::SwapChoose,
        Chooser::MoveToTab => Action::MoveTab,
        Chooser::MoveToWorkspace => Action::MoveWorkspace,
    };
    let entities = match chooser {
        Chooser::Tab | Chooser::MoveToTab => navigation::tabs(world, target.workspace),
        Chooser::Workspace | Chooser::MoveToWorkspace => roots(world),
        Chooser::SwapTarget => navigation::leaves(world, target.tab.ok_or("no tab")?)
            .into_iter()
            .filter(|e| Some(*e) != target.leaf)
            .collect(),
    };
    let entries = entities
        .into_iter()
        .map(|entity| {
            let command = match chooser {
                Chooser::Tab => Command::Select {
                    scope: Scope::Tab,
                    entity,
                },
                Chooser::Workspace => Command::Select {
                    scope: Scope::Workspace,
                    entity,
                },
                Chooser::SwapTarget => Command::Swap { with: entity },
                Chooser::MoveToTab => Command::Move {
                    to: MoveTo::Tab { tab: entity },
                },
                Chooser::MoveToWorkspace => Command::Move {
                    to: MoveTo::Workspace { workspace: entity },
                },
            };
            Entry {
                label: label(world, entity),
                run: Run::Command(command),
            }
        })
        .collect();
    if let Some(reason) = actions::unavailable(world, target, title) {
        return Err(reason.into());
    }
    settle(world, id);
    open(
        world,
        id,
        target,
        Mode::List {
            title: title.label().into(),
            entries,
            selected: 0,
        },
    );
    Ok(())
}
/// Opens the action menu for a pane, tab or workspace.
pub(crate) fn menu(
    world: &mut World,
    id: Entity,
    mut target: Target,
    subject: Subject,
) -> Result<(), String> {
    use Action::*;
    let (action, group, entity) = match subject {
        Subject::Pane(pane) => {
            target.leaf = Some(pane);
            (PaneMenu, "Panes", pane)
        }
        Subject::Tab(tab) => {
            target.tab = Some(tab);
            target.leaf = None;
            (TabMenu, "Tabs", tab)
        }
        Subject::Workspace(workspace) => {
            target = Target {
                workspace,
                tab: None,
                leaf: None,
            };
            (WorkspaceMenu, "Workspaces", workspace)
        }
    };
    if let Some(reason) = actions::unavailable(world, target, action) {
        return Err(reason.into());
    }
    let entries = actions::ALL
        .iter()
        .copied()
        .filter(|a| {
            (a.group() == group || group == "Workspaces" && matches!(a, SaveLayout | LoadLayout))
                && !matches!(a, PaneMenu | TabMenu | WorkspaceMenu)
                && !matches!(
                    a,
                    TabNext | TabPrevious | TabChoose | WorkspaceNext | WorkspacePrevious
                )
        })
        .map(|a| Entry {
            label: a.label().into(),
            run: Run::Action(a),
        })
        .collect();
    settle(world, id);
    open(
        world,
        id,
        target,
        Mode::List {
            title: format!("{group}: {}", label(world, entity)),
            entries,
            selected: 0,
        },
    );
    Ok(())
}

/// Reorders a tab among its workspace's tabs or a workspace among all workspaces.
pub(crate) fn reorder(world: &mut World, subject: Subject, order: Order) -> Result<(), String> {
    let entity = subject.entity();
    let (mut entities, workspace) = match subject {
        Subject::Tab(tab) => {
            let workspace = world
                .get::<ChildOf>(tab)
                .ok_or("tab has no workspace")?
                .parent();
            (navigation::tabs(world, workspace), Some(workspace))
        }
        Subject::Workspace(_) => (roots(world), None),
        Subject::Pane(_) => return Err("target has the wrong kind for this action".into()),
    };
    let index = entities
        .iter()
        .position(|e| *e == entity)
        .ok_or("target removed")?;
    let other = match order {
        Order::Previous => index.saturating_sub(1),
        Order::Next => (index + 1).min(entities.len() - 1),
    };
    entities.swap(index, other);
    match workspace {
        Some(workspace) => {
            world.entity_mut(workspace).replace_children(&entities);
        }
        None => {
            for (index, entity) in entities.into_iter().enumerate() {
                world
                    .entity_mut(entity)
                    .insert(WorkspaceOrder(index as i64));
            }
        }
    }
    Ok(())
}

pub(crate) fn rename(world: &mut World, subject: Subject, name: String) -> Result<(), String> {
    let entity = match subject {
        Subject::Pane(leaf) => world.get::<PaneView>(leaf).ok_or("pane removed")?.pane,
        other => other.entity(),
    };
    world
        .get_entity_mut(entity)
        .map_err(|_| "target no longer exists")?
        .insert(Name::new(name));
    Ok(())
}

/// The kind a subject names must match the entity, whichever caller built it.
pub(crate) fn check(world: &World, subject: Subject) -> Result<Entity, String> {
    let entity = subject.entity();
    let ok = match subject {
        Subject::Pane(_) => world.get::<PaneView>(entity).is_some(),
        Subject::Tab(_) => world.get::<Tab>(entity).is_some(),
        Subject::Workspace(_) => world.get::<Workspace>(entity).is_some(),
    };
    if world.get_entity(entity).is_err() {
        return Err("target no longer exists".into());
    }
    if !ok {
        return Err("target has the wrong kind for this action".into());
    }
    Ok(entity)
}

/// Where a pane moves to. New containers are created on demand.
#[derive(Reflect, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[reflect(Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "kind")]
pub enum MoveTo {
    Tab { tab: Entity },
    NewTab { name: Option<String> },
    Workspace { workspace: Entity },
    NewWorkspace { name: Option<String> },
}

/// Moves the viewer's focused pane and follows it, without overwriting other
/// viewers' memory.
pub(crate) fn move_pane(
    world: &mut World,
    id: Entity,
    target: Target,
    to: MoveTo,
) -> Result<(), String> {
    let leaf = target.leaf.ok_or("no pane")?;
    let (workspace, tab) = match to {
        MoveTo::NewWorkspace { name } => {
            let order = roots(world)
                .into_iter()
                .filter_map(|e| world.get::<WorkspaceOrder>(e).map(|o| o.0))
                .max()
                .unwrap_or(-1)
                .saturating_add(1);
            let root = world
                .spawn((
                    Workspace,
                    WorkspaceOrder(order),
                    Name::new(name.unwrap_or_else(|| "workspace".into())),
                    tab_node(),
                ))
                .id();
            let tab = world.spawn((Tab, Name::new("main"), ChildOf(root))).id();
            (root, tab)
        }
        MoveTo::NewTab { name } => {
            let tab = world
                .spawn((
                    Tab,
                    Name::new(name.unwrap_or_else(|| "tab".into())),
                    ChildOf(target.workspace),
                ))
                .id();
            (target.workspace, tab)
        }
        MoveTo::Workspace { workspace: root } => {
            if world.get::<Workspace>(root).is_none() {
                return Err("destination workspace removed".into());
            }
            navigation::normalize_workspace(world, root);
            // normalize_workspace spawns a tab when a workspace has none.
            #[expect(clippy::indexing_slicing, reason = "normalized workspace has a tab")]
            let tab = navigation::tabs(world, root)[0];
            (root, tab)
        }
        MoveTo::Tab { tab } => {
            if world.get::<Tab>(tab).is_none() {
                return Err("destination tab removed".into());
            }
            let root = world
                .get::<ChildOf>(tab)
                .ok_or("tab has no workspace")?
                .parent();
            (root, tab)
        }
    };
    if Some(tab) == target.tab {
        return Ok(());
    }
    if world
        .get::<Children>(tab)
        .is_some_and(|children| !children.is_empty())
    {
        // A tab may itself be a native split container. Preserve custom
        // nonzero spacing, adding shared separators to default layouts.
        world.entity_mut(tab).insert(Split);
        if let Some(mut node) = world.get_mut::<bevy_ui::Node>(tab) {
            if node.column_gap == bevy_ui::Val::ZERO {
                node.column_gap = bevy_ui::Val::Px(1.0);
            }
            if node.row_gap == bevy_ui::Val::ZERO {
                node.row_gap = bevy_ui::Val::Px(1.0);
            }
        }
    }
    world.entity_mut(leaf).insert(ChildOf(tab));
    // Following the pane records this viewer's memory through the observers,
    // without touching other viewers'.
    world
        .get_entity_mut(id)
        .map_err(|_| "viewer removed")?
        .insert((Viewing(workspace), OnTab(tab), Focused(leaf)));
    world
        .get_mut::<Viewer>(id)
        .ok_or("viewer removed")?
        .reset_view();
    Ok(())
}

/// Swaps or moves the focused pane toward its nearest neighbour.
pub(crate) fn beside(
    world: &mut World,
    id: Entity,
    target: Target,
    direction: Direction,
    swap_panes: bool,
) -> Result<(), String> {
    let source = target.leaf.ok_or("no pane")?;
    let destination =
        crate::frame::neighbor(world, id, source, direction).ok_or("no pane in that direction")?;
    if swap_panes {
        swap(world, source, destination)?;
    } else {
        move_beside(world, source, destination, direction)?;
    }
    world.get_mut::<Viewer>(id).ok_or("viewer removed")?.zoom = false;
    Ok(())
}

pub fn close(world: &mut World, entity: Entity) {
    let processes: Vec<_> = navigation::leaves(world, entity)
        .into_iter()
        .filter_map(|leaf| world.get::<PaneView>(leaf).map(|p| p.pane))
        .collect();
    world.despawn(entity);
    use bevy_ecs::relationship::RelationshipTarget;
    for process in processes {
        if world
            .get::<PaneViews>(process)
            .is_none_or(|views| views.len() == 0)
        {
            world.despawn(process);
        }
    }
}

fn move_beside(
    world: &mut World,
    source: Entity,
    destination: Entity,
    direction: Direction,
) -> Result<(), String> {
    use bevy_ui::FlexDirection;
    let parent = world
        .get::<ChildOf>(destination)
        .ok_or("destination removed")?
        .parent();
    let index = world
        .get::<Children>(parent)
        .and_then(|children| children.iter().position(|e| e == destination))
        .ok_or("destination removed")?;
    let mut split = world.spawn(Split);
    if matches!(direction, Direction::Up | Direction::Down) {
        split.insert(split_node(FlexDirection::Column));
    }
    let split = split.id();
    let children = if matches!(direction, Direction::Left | Direction::Up) {
        [source, destination]
    } else {
        [destination, source]
    };
    world.entity_mut(parent).insert_children(index, &[split]);
    world.entity_mut(split).add_children(&children);
    Ok(())
}

pub(crate) fn swap(world: &mut World, source: Entity, destination: Entity) -> Result<(), String> {
    if source == destination {
        return Ok(());
    }
    if world.get::<PaneView>(destination).is_none() {
        return Err("swap destination removed".into());
    }
    let a = world
        .get::<ChildOf>(source)
        .ok_or("source has no parent")?
        .parent();
    let b = world
        .get::<ChildOf>(destination)
        .ok_or("destination has no parent")?
        .parent();
    let ai = world
        .get::<Children>(a)
        .and_then(|children| children.iter().position(|e| e == source))
        .ok_or("source has no parent")?;
    let bi = world
        .get::<Children>(b)
        .and_then(|children| children.iter().position(|e| e == destination))
        .ok_or("destination has no parent")?;
    if a == b {
        world
            .get_mut::<Children>(a)
            .ok_or("source has no parent")?
            .swap(ai, bi);
    } else {
        world.entity_mut(a).insert_children(ai, &[destination]);
        world.entity_mut(b).insert_children(bi, &[source]);
    }
    Ok(())
}

/// Command column navigation owns reserved keys before configured prefix bindings.
pub fn command_input(world: &mut World, id: Entity, input: &Input) -> bool {
    let (Some(v), Some(prefix)) = (world.get::<Viewer>(id), world.get::<Prefix>(id)) else {
        return false;
    };
    let (rows, cols, mut scroll) = (v.rows, v.cols, prefix.scroll);
    let settings = world.resource::<crate::assets::Settings>();
    // The prefix itself retains literal forwarding, even for a navigation-key prefix.
    if input.token().as_ref() == Some(&settings.prefix) {
        return false;
    }
    let step = |current, down, page| chrome::scroll(settings, rows, current, down, page);
    let mut execute = None;
    let mut close = false;
    match input {
        Input::Resize { .. } => return false,
        Input::Key {
            key,
            modifiers:
                Modifiers {
                    ctrl: false,
                    alt: false,
                    shift: false,
                },
        } => match key {
            Key::Arrow(Direction::Up) => scroll = step(scroll, false, false),
            Key::PageUp => scroll = step(scroll, false, true),
            Key::Arrow(Direction::Down) => scroll = step(scroll, true, false),
            Key::PageDown => scroll = step(scroll, true, true),
            Key::Arrow(_) => {} // Vertical menus reserve all unmodified arrows.
            Key::Home => scroll = 0,
            Key::End => scroll = chrome::help_limit(settings, rows),
            Key::Enter => {
                execute = chrome::selected_action(settings, rows, cols, scroll).cloned();
                close = true;
            }
            Key::Escape => close = true,
            _ => return false,
        },
        Input::Mouse { action, .. } => match action {
            MouseAction::ScrollUp => scroll = step(scroll, false, false),
            MouseAction::ScrollDown => scroll = step(scroll, true, false),
            _ => {}
        },
        Input::Paste { .. } | Input::PasteBegin => return true,
        _ => return false,
    }
    if close {
        close_prefix(world, id);
    } else if let Some(mut prefix) = world.get_mut::<Prefix>(id) {
        prefix.scroll = scroll;
    }
    match execute {
        Some(binding) => {
            let Some(target) = Target::of(world, id) else {
                return true;
            };
            dispatch_binding(world, id, target, &binding);
        }
        None if close => notify(world, id, None),
        None => {}
    }
    true
}

/// Consume overlay input before the ordinary terminal/prefix observer.
pub fn input(world: &mut World, id: Entity, input: &Input) -> bool {
    let Some(overlay) = world.get::<Overlay>(id).cloned() else {
        return false;
    };
    if !overlay.target.valid(world) {
        world.entity_mut(id).remove::<Overlay>();
        notify_error(world, id, "target changed; action cancelled");
        return !matches!(input, Input::Resize { .. });
    }
    if matches!(input, Input::Resize { .. }) {
        return false;
    }
    let mut next = overlay.clone();
    let mut execute: Option<Run> = None;
    let mut cancel = false;
    match (&mut next.mode, input) {
        (
            _,
            Input::Key {
                key: Key::Escape, ..
            },
        ) => cancel = true,
        (
            Mode::Confirm { command },
            Input::Key {
                key: Key::Char(c),
                modifiers:
                    Modifiers {
                        ctrl: false,
                        alt: false,
                        ..
                    },
            },
        ) => {
            if *c == 'y' {
                execute = Some(Run::Command(command.clone()));
            } else if *c == 'n' {
                cancel = true;
            }
        }
        (Mode::Text { action, buffer }, Input::Key { key, modifiers }) => match key {
            Key::Enter if !buffer.is_empty() => {
                execute = action
                    .with_text(overlay.target, buffer.clone())
                    .map(Run::Command);
            }
            Key::Backspace => {
                buffer.pop();
            }
            Key::Char(c) if !modifiers.ctrl && !modifiers.alt => buffer.push(*c),
            _ => {}
        },
        (Mode::Text { buffer, .. }, Input::Paste { text }) => buffer.push_str(text),
        (
            Mode::List {
                entries, selected, ..
            },
            Input::Key { key, .. },
        ) => {
            let page = list_capacity(world.get::<Viewer>(id).map_or(0, |v| v.rows));
            let last = entries.len().saturating_sub(1);
            match key {
                Key::Arrow(Direction::Up) | Key::Char('k') => {
                    *selected = selected.saturating_sub(1);
                }
                Key::Arrow(Direction::Down) | Key::Char('j') => {
                    *selected = selected.saturating_add(1).min(last);
                }
                Key::PageUp => *selected = selected.saturating_sub(page),
                Key::PageDown => *selected = selected.saturating_add(page).min(last),
                Key::Home => *selected = 0,
                Key::End => *selected = last,
                Key::Enter => {
                    if let Some(entry) = entries.get(*selected) {
                        execute = Some(entry.run.clone());
                    }
                }
                Key::Char('q') => cancel = true,
                _ => {}
            }
        }
        (
            Mode::List {
                entries, selected, ..
            },
            Input::Mouse { action, .. },
        ) => match action {
            MouseAction::ScrollUp => *selected = selected.saturating_sub(1),
            MouseAction::ScrollDown => {
                *selected = selected
                    .saturating_add(1)
                    .min(entries.len().saturating_sub(1));
            }
            _ => {}
        },
        _ => {}
    }
    if cancel || execute.is_some() {
        world.entity_mut(id).remove::<Overlay>();
    } else {
        world.entity_mut(id).insert(next);
    }
    if let Some(run) = execute {
        let result = match run {
            Run::Action(action) => bound(world, id, overlay.target, action),
            Run::Command(command) => crate::server::execute(world, id, command),
        };
        if let Err(message) = result {
            notify_error(world, id, message);
        }
    }
    true
}

/// The one place a configured binding is dispatched. A custom name has always
/// closed the prefix, dropped the selection and reported itself as unknown.
pub fn dispatch_binding(world: &mut World, id: Entity, target: Target, binding: &BindingAction) {
    let result = match binding {
        BindingAction::Known(action) => bound(world, id, target, *action),
        BindingAction::Custom(name) => {
            if !target.valid(world) {
                Err(actions::TARGET_GONE.into())
            } else {
                settle(world, id);
                Err(format!("unknown action {name}"))
            }
        }
    };
    if let Err(message) = result {
        notify_error(world, id, message);
    }
}

fn list_capacity(rows: u16) -> usize {
    if rows >= 5 {
        usize::from(rows - 4)
    } else {
        usize::from(rows.saturating_sub(2).max(1))
    }
}

pub fn lines(world: &World, overlay: &Overlay, rows: u16) -> Vec<(String, &'static str)> {
    let mut lines = Vec::new();
    match &overlay.mode {
        Mode::Confirm { command } => {
            let (kind, entity) = match command {
                Command::Close { subject } => (subject.kind(), subject.entity()),
                _ => ("target", Entity::PLACEHOLDER),
            };
            let named = if world.get_entity(entity).is_ok() {
                format!("{} #{}", label(world, entity), entity.to_bits())
            } else {
                "removed target".into()
            };
            lines.push((format!("Close {kind} {named}?"), "\x1b[1m"));
            lines.push((
                if kind == "pane" {
                    "Remove pane; stop if last view"
                } else {
                    "Remove all contained pane views"
                }
                .into(),
                "",
            ));
            lines.push(("y confirm · n/Esc cancel".into(), ""));
        }
        Mode::Text { action, buffer } => {
            lines.push((action.label().into(), "\x1b[1m"));
            lines.push((format!("{buffer}"), "\x1b[7m"));
            lines.push(("Enter accept · Esc cancel".into(), "\x1b[2m"));
        }
        Mode::List {
            title,
            entries,
            selected,
        } => {
            if rows >= 4 {
                lines.push((title.clone(), "\x1b[1m"));
            }
            let capacity = list_capacity(rows);
            let start = selected.saturating_sub(capacity - 1);
            if start > 0 && rows >= 5 {
                lines.push((format!("{start} more"), "\x1b[2m"));
            }
            for (index, entry) in entries.iter().enumerate().skip(start).take(capacity) {
                let disabled = match &entry.run {
                    Run::Action(action) => {
                        actions::unavailable(world, overlay.target, *action).is_some()
                    }
                    Run::Command(_) => false,
                };
                lines.push((
                    format!(
                        "{} {}",
                        if index == *selected { "" } else { " " },
                        entry.label
                    ),
                    if disabled && index == *selected {
                        "\x1b[2;7m"
                    } else if disabled {
                        "\x1b[2m"
                    } else if index == *selected {
                        "\x1b[7m"
                    } else {
                        ""
                    },
                ));
            }
            if entries.len() > start.saturating_add(capacity) && rows >= 5 {
                lines.push((
                    format!("{} more", entries.len() - start - capacity),
                    "\x1b[2m",
                ));
            }
            if entries.is_empty() {
                lines.push(("No destinations".into(), "\x1b[2m"));
            }
        }
    }
    // Tiny viewers prioritize the actionable/selected row rather than a heading.
    let available = rows.saturating_sub(1) as usize;
    if available < lines.len() && available <= 2 {
        match &overlay.mode {
            Mode::Confirm { .. } => {
                let title = lines.remove(0);
                lines = vec![(format!("y/n {}", title.0), "\x1b[1m")];
            }
            Mode::Text { .. } => {
                lines.remove(0);
            }
            Mode::List { .. } => {}
        }
    }
    lines.truncate(available);
    lines
}