fux 0.7.0

Minimal persistent terminal multiplexer built on bevy_ecs
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
//! Viewer-local interaction modes: history/copy, tab and workspace choosers, rename, confirmed
//! closes, repeated resize and workspace naming. Transient state never leaves this process.

use super::copy::{CopyKey, CopyOutcome, CopySession};
use super::hints::HintPanel;
use crate::commands::Action;
use crate::ids::{PaneId, TabId};
use crate::proto::attach::{MouseEvent, ViewReply};
use crate::proto::control::{Request, TabAction, WorkspaceAction};
use crate::view::{Frame, MouseMode};
use unicode_segmentation::UnicodeSegmentation as _;

const MAX_TEXT_BYTES: usize = 128;
pub const RESIZE_STEP: i16 = 250;

enum Mode {
    Pane,
    Copy(Box<CopySession>),
    LoadingWorkspaces,
    Workspaces {
        names: Vec<String>,
        selected: usize,
    },
    Tabs {
        choices: Vec<(TabId, String)>,
        selected: usize,
    },
    Rename {
        tab: TabId,
        text: String,
    },
    NewWorkspace {
        text: String,
    },
    ClosePane {
        pane: PaneId,
    },
    CloseTab {
        tab: TabId,
        label: String,
        panes: usize,
    },
    Resize {
        pane: PaneId,
    },
}

/// How long a bar notice stays without a key press.
pub const NOTICE_TTL: std::time::Duration = std::time::Duration::from_secs(2);

pub struct Controller {
    mode: Mode,
    escape: Vec<u8>,
    utf8: Vec<u8>,
    paste: bool,
    back: bool,
    error: Option<String>,
    info: Option<String>,
    notice_since: Option<std::time::Instant>,
    copied: Option<String>,
    workspaces_enabled: bool,
    loading_input: Vec<u8>,
}

/// What a mouse report should do.
pub enum MouseDisposition {
    /// Handled locally (history, selection); nothing goes to the server.
    Local,
    /// Forward to the server, which focuses or re-encodes for the application.
    Forward,
    /// Consumed and dropped (a mode that ignores the mouse).
    Ignore,
}

impl Controller {
    #[must_use]
    pub fn new(workspaces_enabled: bool) -> Self {
        Self {
            mode: Mode::Pane,
            escape: Vec::new(),
            utf8: Vec::new(),
            paste: false,
            back: false,
            error: None,
            info: None,
            notice_since: None,
            copied: None,
            workspaces_enabled,
            loading_input: Vec::new(),
        }
    }

    pub fn active(&self) -> bool {
        !matches!(self.mode, Mode::Pane)
    }

    /// A cancelled mode still owns an unfinished paste or sequence; its tail must never be
    /// reinterpreted as commands or forwarded to a pane.
    pub fn owns_input(&self) -> bool {
        self.active() || self.paste || !self.escape.is_empty() || !self.utf8.is_empty()
    }

    pub fn in_copy(&self) -> bool {
        matches!(self.mode, Mode::Copy(_))
    }

    pub fn escape_pending(&self) -> bool {
        !self.escape.is_empty()
            && !self.paste
            && (self.escape.last() == Some(&27)
                || !(self.escape.starts_with(b"\x1b[") || self.escape.starts_with(b"\x1bO")))
    }

    pub fn take_back(&mut self) -> bool {
        std::mem::take(&mut self.back)
    }

    pub fn take_copied(&mut self) -> Option<String> {
        self.copied.take()
    }

    pub fn clear_error(&mut self) {
        self.error = None;
        self.info = None;
        self.notice_since = None;
    }

    /// The bar notice to show at `now`: an error wins over an info line; both live for
    /// [`NOTICE_TTL`] or until the next key.
    pub fn notice(&self, now: std::time::Instant) -> Option<super::render::Notice> {
        let since = self.notice_since?;
        if now.duration_since(since) >= NOTICE_TTL {
            return None;
        }
        if let Some(error) = &self.error {
            return Some(super::render::Notice {
                text: error.clone(),
                error: true,
            });
        }
        self.info.as_ref().map(|info| super::render::Notice {
            text: info.clone(),
            error: false,
        })
    }

    /// When the current notice expires, if one is showing at `now`.
    pub fn notice_deadline(&self, now: std::time::Instant) -> Option<std::time::Instant> {
        let since = self.notice_since?;
        if self.error.is_none() && self.info.is_none() {
            return None;
        }
        let deadline = since + NOTICE_TTL;
        (deadline > now).then_some(deadline)
    }

    /// Drops a notice whose time is up.
    pub fn expire_notice(&mut self, now: std::time::Instant) {
        if self.notice(now).is_none() {
            self.clear_error();
        }
    }

    /// A transient confirmation shown in the bar until the next key or [`NOTICE_TTL`].
    pub fn report_info(&mut self, message: impl Into<String>) {
        self.notice_since = Some(std::time::Instant::now());
        self.info = Some(crate::view::printable(&message.into(), 256));
    }

    pub fn report_error(&mut self, error: impl Into<String>) {
        self.notice_since = Some(std::time::Instant::now());
        self.error = Some(crate::view::printable(&error.into(), 256));
    }

    pub fn error(&self) -> Option<&str> {
        self.error.as_deref()
    }

    /// The copy session's view and selection for the compositor.
    pub fn local_view(&self) -> Option<super::render::LocalView<'_>> {
        match &self.mode {
            Mode::Copy(copy) => Some(super::render::LocalView {
                pane: copy.pane(),
                view: copy.view(),
                cursor: copy.cursor(),
                anchor: copy.anchor(),
            }),
            _ => None,
        }
    }

    pub fn take_read(&mut self) -> Option<(u64, PaneId, u32)> {
        match &mut self.mode {
            Mode::Copy(copy) => copy.take_read(),
            _ => None,
        }
    }

    pub fn awaiting_read(&self) -> bool {
        matches!(&self.mode, Mode::Copy(copy) if copy.awaiting_read())
    }

    pub fn install_view(&mut self, reply: ViewReply) {
        if let Mode::Copy(copy) = &mut self.mode
            && !copy.install(reply)
        {
            self.cancel();
            self.report_error("That pane is no longer available for copying.");
        }
    }

    /// Reconciles the mode with a new frame: stale targets cancel with feedback, live copy views
    /// follow new output.
    pub fn reconcile(&mut self, frame: &Frame) {
        if let Some(message) = &frame.message {
            self.report_error(message.clone());
        }
        let stale = match &mut self.mode {
            Mode::Copy(copy) => match frame.pane(copy.pane()) {
                Some(live) => {
                    copy.refresh_live(live);
                    false
                }
                None => true,
            },
            Mode::ClosePane { pane } | Mode::Resize { pane } => frame.pane(*pane).is_none(),
            Mode::CloseTab { tab, .. } | Mode::Rename { tab, .. } => {
                !frame.tabs.iter().any(|entry| entry.id == *tab)
            }
            Mode::Tabs { .. }
            | Mode::Pane
            | Mode::LoadingWorkspaces
            | Mode::Workspaces { .. }
            | Mode::NewWorkspace { .. } => false,
        };
        if stale {
            self.cancel();
            self.report_error("The target of that command has closed.");
        }
    }

    pub fn loading_workspaces(&mut self) {
        self.loading_input.clear();
        self.mode = Mode::LoadingWorkspaces;
        self.reset_input();
    }

    fn reset_input(&mut self) {
        self.escape.clear();
        self.utf8.clear();
        self.error = None;
    }

    pub fn workspaces_loaded(&mut self, result: anyhow::Result<Vec<String>>, current: &str) {
        if !matches!(self.mode, Mode::LoadingWorkspaces) {
            return;
        }
        match result {
            Ok(names) if !names.is_empty() => {
                self.escape.clear();
                self.utf8.clear();
                self.paste = false;
                let selected = names.iter().position(|name| name == current).unwrap_or(0);
                self.mode = Mode::Workspaces { names, selected };
            }
            result => {
                self.mode = Mode::Pane;
                self.loading_input.clear();
                self.report_error(result.err().map_or_else(
                    || "No workspaces are available".to_owned(),
                    |error| error.to_string(),
                ));
                self.back = true;
            }
        }
    }

    /// Bytes typed while the chooser was loading, replayed once it is ready.
    pub fn take_loading_input(&mut self) -> Vec<u8> {
        std::mem::take(&mut self.loading_input)
    }

    /// Enters a mode for a modal action; returns false for actions that need no mode.
    pub fn enter(&mut self, action: Action, frame: &Frame) -> bool {
        self.reset_input();
        let tab = frame.active_tab;
        let pane = frame.focused;
        self.mode = match action {
            Action::CopyMode => match pane
                .and_then(|pane| frame.pane(pane).map(|view| (pane, view)))
            {
                Some((pane, view)) => Mode::Copy(Box::new(CopySession::new(pane, view.clone()))),
                None => return false,
            },
            Action::ChooseTab => Mode::Tabs {
                choices: frame
                    .tabs
                    .iter()
                    .map(|entry| (entry.id, entry.label.clone()))
                    .collect(),
                selected: frame
                    .tabs
                    .iter()
                    .position(|entry| Some(entry.id) == tab)
                    .unwrap_or(0),
            },
            Action::RenameTab => {
                match tab.and_then(|tab| frame.tabs.iter().find(|entry| entry.id == tab)) {
                    Some(entry) => Mode::Rename {
                        tab: entry.id,
                        text: entry.label.clone(),
                    },
                    None => return false,
                }
            }
            Action::ClosePane => match pane {
                Some(pane) => Mode::ClosePane { pane },
                None => return false,
            },
            Action::CloseTab => {
                match tab.and_then(|tab| frame.tabs.iter().find(|entry| entry.id == tab)) {
                    Some(entry) => Mode::CloseTab {
                        tab: entry.id,
                        label: entry.label.clone(),
                        panes: frame.layout.len(),
                    },
                    None => return false,
                }
            }
            Action::ResizeMode => match pane {
                Some(pane) => Mode::Resize { pane },
                None => return false,
            },
            Action::NewWorkspace => Mode::NewWorkspace {
                text: String::new(),
            },
            Action::ChooseWorkspace => {
                self.loading_workspaces();
                return true;
            }
            _ => return false,
        };
        true
    }

    pub fn resolve_escape(&mut self) {
        if !self.escape_pending() {
            return;
        }
        if self.escape == [27] || self.escape.last() == Some(&27) {
            let cleared = match &mut self.mode {
                Mode::Copy(copy) => matches!(copy.key(CopyKey::Escape), CopyOutcome::Continue),
                _ => false,
            };
            if !cleared {
                self.cancel();
            }
        }
        self.escape.clear();
    }

    fn cancel(&mut self) {
        self.mode = Mode::Pane;
        self.back = true;
        self.utf8.clear();
        self.error = None;
    }

    /// Routes a mouse report while a mode is active or the pointer is over pane content.
    pub fn mouse(&mut self, mouse: MouseEvent, frame: &Frame) -> MouseDisposition {
        if !matches!(self.mode, Mode::Pane | Mode::Copy(_)) {
            return MouseDisposition::Ignore;
        }
        let x = mouse.column.saturating_sub(1);
        let y = mouse.row.saturating_sub(1);
        let Some(entry) = frame.pane_at(x, y) else {
            return if self.in_copy() {
                MouseDisposition::Ignore
            } else {
                MouseDisposition::Forward
            };
        };
        let content = entry.rect;
        let Some(view) = frame.pane(entry.pane) else {
            return MouseDisposition::Ignore;
        };
        let app_owns_mouse = view.modes.mouse_mode != MouseMode::None;
        // Shift is the documented override: fux history/selection even when the app owns the mouse.
        let local = self.in_copy()
            || mouse.shift()
            || (!app_owns_mouse && (mouse.wheel() || mouse.motion() && mouse.button() == 0));
        if !local {
            return MouseDisposition::Forward;
        }
        if !content.contains(x, y) {
            return MouseDisposition::Ignore;
        }
        let row = y.saturating_sub(content.y);
        let column = x.saturating_sub(content.x);
        if matches!(self.mode, Mode::Pane) {
            if mouse.release {
                return MouseDisposition::Ignore;
            }
            self.mode = Mode::Copy(Box::new(CopySession::new(entry.pane, view.clone())));
        }
        if let Mode::Copy(copy) = &mut self.mode {
            if copy.pane() != entry.pane {
                return MouseDisposition::Ignore;
            }
            if mouse.wheel() {
                copy.scroll(if mouse.code & 1 == 0 { 3 } else { -3 });
            } else if mouse.button() == 0 {
                copy.drag(row, column, mouse.release);
            }
        }
        MouseDisposition::Local
    }

    /// Feeds one input byte to the active mode. Returns a request to send, if any.
    pub fn feed(&mut self, byte: u8, frame: &Frame) -> Option<Request> {
        if matches!(self.mode, Mode::LoadingWorkspaces) {
            if self.loading_input.len() < 4096 {
                self.loading_input.push(byte);
            } else {
                self.cancel();
                self.loading_input.clear();
            }
            if matches!(self.mode, Mode::LoadingWorkspaces) && byte != 27 && self.escape.is_empty()
            {
                return None;
            }
        }
        if !self.escape.is_empty() || byte == 27 {
            self.escape.push(byte);
            let complete = self.escape.len() > 1
                && match self.escape.get(1) {
                    Some(b'[' | b'O') => self.escape.len() > 2 && (0x40..=0x7e).contains(&byte),
                    _ => true,
                };
            if !complete && self.escape.len() < 64 {
                return None;
            }
            let sequence = std::mem::take(&mut self.escape);
            if self.active()
                && !self.paste
                && let Some(mouse) = MouseEvent::parse(&sequence)
            {
                let _ = self.mouse(mouse, frame);
                return None;
            }
            match sequence.as_slice() {
                b"\x1b[200~" => self.paste = true,
                b"\x1b[201~" => self.paste = false,
                b"\x1bOM" if !self.paste => return self.key('\r', frame),
                b"\x1b[D" | b"\x1bOD" if !self.paste && self.in_copy() => {
                    return self.key('h', frame);
                }
                b"\x1b[C" | b"\x1bOC" if !self.paste && self.in_copy() => {
                    return self.key('l', frame);
                }
                b"\x1b[5~" if !self.paste && self.in_copy() => {
                    return self.copy_key(CopyKey::PageUp);
                }
                b"\x1b[6~" if !self.paste && self.in_copy() => {
                    return self.copy_key(CopyKey::PageDown);
                }
                b"\x1b[A" | b"\x1b[D" | b"\x1bOA" | b"\x1bOD"
                    if !self.paste && !self.text_entry() =>
                {
                    return self.key('k', frame);
                }
                b"\x1b[B" | b"\x1b[C" | b"\x1bOB" | b"\x1bOC"
                    if !self.paste && !self.text_entry() =>
                {
                    return self.key('j', frame);
                }
                _ => {}
            }
            return None;
        }
        if self.paste && !self.text_entry() {
            return None;
        }
        if byte.is_ascii() {
            self.utf8.clear();
            if self.paste && byte.is_ascii_control() {
                return None;
            }
            return self.key(char::from(byte), frame);
        }
        self.utf8.push(byte);
        match std::str::from_utf8(&self.utf8) {
            Ok(text) => {
                let character = text.chars().next();
                self.utf8.clear();
                character.and_then(|character| self.key(character, frame))
            }
            Err(error) if error.error_len().is_some() || self.utf8.len() >= 4 => {
                self.utf8.clear();
                None
            }
            Err(_) => None,
        }
    }

    fn text_entry(&self) -> bool {
        matches!(self.mode, Mode::Rename { .. } | Mode::NewWorkspace { .. })
    }

    fn copy_key(&mut self, key: CopyKey) -> Option<Request> {
        if let Mode::Copy(copy) = &mut self.mode {
            match copy.key(key) {
                CopyOutcome::Continue => {}
                CopyOutcome::Copied(text) => {
                    self.copied = Some(text);
                    self.mode = Mode::Pane;
                }
                CopyOutcome::Finished => {
                    self.mode = Mode::Pane;
                }
            }
        }
        None
    }

    fn key(&mut self, key: char, frame: &Frame) -> Option<Request> {
        match &mut self.mode {
            Mode::Copy(_) => {
                let mapped = match key {
                    'h' => CopyKey::Left,
                    'l' => CopyKey::Right,
                    'k' => CopyKey::Up,
                    'j' => CopyKey::Down,
                    'u' => return self.scroll_copy(3),
                    'd' => return self.scroll_copy(-3),
                    ' ' => CopyKey::Anchor,
                    'y' | '\r' | '\n' => CopyKey::Copy,
                    'g' => CopyKey::Live,
                    'q' => CopyKey::Quit,
                    _ => return None,
                };
                self.copy_key(mapped)
            }
            Mode::Pane | Mode::LoadingWorkspaces => None,
            Mode::Workspaces { names, selected } => {
                if self.paste || step(selected, names.len(), key) || !is_enter(key) {
                    return None;
                }
                let name = names.get(*selected).cloned()?;
                self.mode = Mode::Pane;
                if name == frame.workspace {
                    return None;
                }
                Some(Request::Workspace {
                    stream: None,
                    instance: None,
                    id: 0,
                    action: WorkspaceAction::Select { name },
                })
            }
            Mode::Tabs { choices, selected } => {
                if step(selected, choices.len(), key) || !is_enter(key) {
                    return None;
                }
                let target = choices.get(*selected)?.0;
                if !frame.tabs.iter().any(|entry| entry.id == target) {
                    self.error = Some("That tab no longer exists; Esc returns to commands.".into());
                    return None;
                }
                self.mode = Mode::Pane;
                Some(Request::Tab {
                    instance: None,
                    id: 0,
                    action: TabAction::Select {
                        target: crate::proto::control::TabTarget::Id(target),
                    },
                })
            }
            Mode::Rename { tab, text } => match key {
                '\r' | '\n' => {
                    let action = TabAction::Rename {
                        tab: *tab,
                        name: text.clone(),
                    };
                    self.mode = Mode::Pane;
                    Some(Request::Tab {
                        instance: None,
                        id: 0,
                        action,
                    })
                }
                _ => {
                    edit_text(text, key);
                    None
                }
            },
            Mode::NewWorkspace { text } => match key {
                '\r' | '\n' => {
                    let name = text.trim().to_owned();
                    if !name.is_empty() && crate::ids::validate_workspace_name(&name).is_err() {
                        self.error = Some(crate::ids::InvalidName.to_string());
                        return None;
                    }
                    self.mode = Mode::Pane;
                    Some(Request::Workspace {
                        stream: None,
                        instance: None,
                        id: 0,
                        action: WorkspaceAction::New {
                            name: (!name.is_empty()).then_some(name),
                        },
                    })
                }
                _ => {
                    edit_text(text, key);
                    None
                }
            },
            Mode::ClosePane { .. } | Mode::CloseTab { .. } => {
                if self.paste {
                    return None;
                }
                match key {
                    'y' | 'Y' => {
                        let request = match &self.mode {
                            Mode::ClosePane { pane } => Request::Kill {
                                instance: None,
                                id: 0,
                                pane: *pane,
                            },
                            Mode::CloseTab { tab, .. } => Request::Tab {
                                instance: None,
                                id: 0,
                                action: TabAction::Close { tab: *tab },
                            },
                            _ => return None,
                        };
                        self.mode = Mode::Pane;
                        Some(request)
                    }
                    'n' | 'N' => {
                        self.cancel();
                        None
                    }
                    _ => None,
                }
            }
            Mode::Resize { pane } => {
                if self.paste {
                    return None;
                }
                let delta = match key {
                    'j' | 'l' | '+' => RESIZE_STEP,
                    'k' | 'h' | '-' => -RESIZE_STEP,
                    '\r' | '\n' => {
                        self.mode = Mode::Pane;
                        return None;
                    }
                    _ => return None,
                };
                Some(Request::Resize {
                    instance: None,
                    id: 0,
                    pane: *pane,
                    delta,
                })
            }
        }
    }

    fn scroll_copy(&mut self, delta: i64) -> Option<Request> {
        if let Mode::Copy(copy) = &mut self.mode {
            copy.scroll(delta);
        }
        None
    }

    /// The panel this mode wants painted, if any.
    pub fn panel(&self) -> Option<HintPanel> {
        let (title, entries, footer, focus) = match &self.mode {
            // Pane-mode notices live in the bar, not in a popup.
            Mode::Pane => return None,
            Mode::Copy(copy) => return Some(HintPanel::bar(&copy.hint())),
            Mode::LoadingWorkspaces => (
                "Choose workspace".into(),
                vec!["Loading workspaces…".into()],
                "Esc back",
                None,
            ),
            Mode::Workspaces { names, selected } => (
                "Choose workspace".into(),
                names.clone(),
                "↑/↓ or j/k move · Enter switch · Esc back",
                Some(*selected),
            ),
            Mode::Tabs { choices, selected } => (
                "Choose tab".into(),
                choices.iter().map(|(_, name)| name.clone()).collect(),
                "↑/↓ or j/k move · Enter select · Esc back",
                Some(*selected),
            ),
            Mode::Rename { text, .. } => {
                return Some(HintPanel::text_input(
                    "Rename tab",
                    text,
                    "Enter save · Esc back · Ctrl-U clear · Backspace delete",
                ));
            }
            Mode::NewWorkspace { text } => {
                return Some(HintPanel::text_input(
                    "New workspace (empty = automatic name)",
                    text,
                    "Enter create · Esc back · Ctrl-U clear",
                ));
            }
            Mode::ClosePane { pane } => (
                format!("Close pane {pane}?"),
                vec!["Its process and unsaved work will be terminated.".into()],
                "y close · n/Esc back",
                None,
            ),
            Mode::CloseTab { tab, label, panes } => (
                format!("Close tab {label} ({tab})?"),
                vec![format!(
                    "{panes} pane{} and their processes will be terminated.",
                    if *panes == 1 { "" } else { "s" }
                )],
                "y close · n/Esc back",
                None,
            ),
            Mode::Resize { pane } => {
                return Some(HintPanel::bar(&format!(
                    "Resize {pane} · ←/↑ shrink →/↓ grow · Enter finish · Esc back · changes are kept{}",
                    self.error
                        .as_ref()
                        .map_or(String::new(), |error| format!(" · {error}")),
                )));
            }
        };
        let mut entries: Vec<String> = entries;
        if let Some(error) = &self.error {
            entries.push(error.clone());
        }
        Some(HintPanel::context(title, entries, footer, focus))
    }

    pub fn workspaces_enabled(&self) -> bool {
        self.workspaces_enabled
    }
}

fn is_enter(key: char) -> bool {
    matches!(key, '\r' | '\n')
}

/// Moves a chooser's selection with j/k; true when `key` was consumed as movement.
fn step(selected: &mut usize, len: usize, key: char) -> bool {
    match key {
        'j' => *selected = selected.saturating_add(1).min(len.saturating_sub(1)),
        'k' => *selected = selected.saturating_sub(1),
        _ => return false,
    }
    true
}

fn edit_text(text: &mut String, key: char) {
    match key {
        '\u{7f}' | '\u{8}' => {
            if let Some((index, _)) = text.grapheme_indices(true).next_back() {
                text.truncate(index);
            }
        }
        '\u{15}' => text.clear(),
        character
            if !character.is_control() && text.len() + character.len_utf8() <= MAX_TEXT_BYTES =>
        {
            text.push(character);
        }
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::view::{PaneRect, PaneView, TabEntry};

    fn frame() -> Frame {
        let mut parser = vt100::Parser::new(4, 10, 0);
        parser.process(b"hello");
        let view = PaneView::from_screen(parser.screen(), "", 0, None).unwrap_or_default();
        let mut frame = Frame {
            workspace: "default".into(),
            ..Frame::default()
        };
        frame.tabs.push(TabEntry {
            id: TabId(1),
            label: "main".into(),
        });
        frame.active_tab = Some(TabId(1));
        frame.focused = Some(PaneId(1));
        frame.layout.push(PaneRect {
            pane: PaneId(1),
            rect: crate::layout::Rect {
                x: 0,
                y: 0,
                width: 12,
                height: 6,
            },
        });
        frame.panes.insert(PaneId(1), view);
        frame
    }

    fn feed(controller: &mut Controller, bytes: &[u8], frame: &Frame) -> Vec<Request> {
        bytes
            .iter()
            .filter_map(|byte| controller.feed(*byte, frame))
            .collect()
    }

    #[test]
    fn rename_submits_fragmented_unicode_and_cancels_without_mutation() {
        let frame = frame();
        let mut controller = Controller::new(true);
        assert!(controller.enter(Action::RenameTab, &frame));
        let requests = feed(&mut controller, "\u{15}renamed界\r".as_bytes(), &frame);
        assert_eq!(
            requests,
            vec![Request::Tab {
                instance: None,
                id: 0,
                action: TabAction::Rename {
                    tab: TabId(1),
                    name: "renamed界".into()
                }
            }]
        );
        assert!(!controller.active());
        assert!(controller.enter(Action::RenameTab, &frame));
        assert!(feed(&mut controller, b"discard\x1b", &frame).is_empty());
        controller.resolve_escape();
        assert!(!controller.active() && controller.take_back());
    }

    #[test]
    fn confirmations_carry_the_original_target_and_ignore_paste() {
        let frame = frame();
        let mut controller = Controller::new(true);
        assert!(controller.enter(Action::ClosePane, &frame));
        assert!(feed(&mut controller, b"\x1b[200~y\r\x1b[201~", &frame).is_empty());
        assert!(controller.active(), "pasted confirmation ignored");
        assert_eq!(
            feed(&mut controller, b"y", &frame),
            vec![Request::Kill {
                instance: None,
                id: 0,
                pane: PaneId(1)
            }]
        );
        assert!(controller.enter(Action::CloseTab, &frame));
        assert_eq!(
            feed(&mut controller, b"Y", &frame),
            vec![Request::Tab {
                instance: None,
                id: 0,
                action: TabAction::Close { tab: TabId(1) }
            }]
        );
        // A stale target cancels with feedback when the frame no longer has it.
        assert!(controller.enter(Action::ClosePane, &frame));
        controller.reconcile(&Frame::default());
        assert!(!controller.active());
        assert!(controller.error().is_some());
    }

    #[test]
    fn resize_repeats_with_arrows_and_application_cursor_keys() {
        let frame = frame();
        let mut controller = Controller::new(true);
        assert!(controller.enter(Action::ResizeMode, &frame));
        let requests = feed(&mut controller, b"j\x1b[A\x1bOC\x1bOD\r", &frame);
        let deltas: Vec<i16> = requests
            .iter()
            .filter_map(|request| match request {
                Request::Resize { delta, .. } => Some(*delta),
                _ => None,
            })
            .collect();
        assert_eq!(deltas, vec![250, -250, 250, -250]);
        assert!(!controller.active());
    }

    #[test]
    fn workspace_chooser_replays_buffered_input_and_switches() {
        let frame = frame();
        let mut controller = Controller::new(true);
        assert!(controller.enter(Action::ChooseWorkspace, &frame));
        assert!(feed(&mut controller, b"j\r", &frame).is_empty());
        controller.workspaces_loaded(Ok(vec!["default".into(), "other".into()]), "default");
        let replay = controller.take_loading_input();
        assert_eq!(
            feed(&mut controller, &replay, &frame),
            vec![Request::Workspace {
                stream: None,
                instance: None,
                id: 0,
                action: WorkspaceAction::Select {
                    name: "other".into()
                }
            }]
        );
        let mut controller = Controller::new(true);
        controller.enter(Action::ChooseWorkspace, &frame);
        controller.workspaces_loaded(Err(anyhow::anyhow!("lookup failed")), "default");
        assert!(!controller.active() && controller.take_back());
        let mut controller = Controller::new(true);
        assert!(controller.enter(Action::NewWorkspace, &frame));
        assert_eq!(
            feed(&mut controller, b"proj\r", &frame),
            vec![Request::Workspace {
                stream: None,
                instance: None,
                id: 0,
                action: WorkspaceAction::New {
                    name: Some("proj".into())
                }
            }]
        );
    }

    #[test]
    fn copy_mode_selection_and_mouse_routing() {
        let frame = frame();
        let mut controller = Controller::new(true);
        assert!(controller.enter(Action::CopyMode, &frame));
        assert!(feed(&mut controller, b"\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D ", &frame).is_empty());
        feed(&mut controller, b"llll", &frame);
        feed(&mut controller, b"y", &frame);
        assert_eq!(controller.take_copied(), Some("hello".into()));
        assert!(!controller.active());
        // A wheel over a pane whose application does not own the mouse browses locally.
        let wheel = MouseEvent {
            code: 64,
            column: 3,
            row: 3,
            release: false,
        };
        assert!(matches!(
            controller.mouse(wheel, &frame),
            MouseDisposition::Local
        ));
        assert!(controller.in_copy());
        assert_eq!(controller.take_read().map(|(_, _, offset)| offset), Some(3));
        // A click without shift on the pane is the server's business.
        let mut plain = Controller::new(true);
        let click = MouseEvent {
            code: 0,
            column: 3,
            row: 3,
            release: false,
        };
        assert!(matches!(
            plain.mouse(click, &frame),
            MouseDisposition::Forward
        ));
        // Shift-drag selects locally even when the application owns the mouse.
        let mut owned = frame.clone();
        if let Some(view) = owned.panes.get_mut(&PaneId(1)) {
            view.modes.mouse_mode = MouseMode::AnyMotion;
        }
        let mut shift = Controller::new(true);
        let press = MouseEvent {
            code: 4,
            column: 1,
            row: 1,
            release: false,
        };
        assert!(matches!(
            shift.mouse(press, &owned),
            MouseDisposition::Local
        ));
        let drag = MouseEvent {
            code: 36,
            column: 5,
            row: 1,
            release: false,
        };
        assert!(matches!(shift.mouse(drag, &owned), MouseDisposition::Local));
        let release = MouseEvent {
            code: 4,
            column: 5,
            row: 1,
            release: true,
        };
        assert!(matches!(
            shift.mouse(release, &owned),
            MouseDisposition::Local
        ));
        feed(&mut shift, b"y", &owned);
        assert_eq!(shift.take_copied(), Some("hello".into()));
        assert!(matches!(
            Controller::new(true).mouse(wheel, &owned),
            MouseDisposition::Forward
        ));
    }

    #[test]
    fn canceled_modes_keep_owning_unfinished_pastes() {
        let frame = frame();
        let mut controller = Controller::new(true);
        controller.enter(Action::CopyMode, &frame);
        for byte in b"\x1b[200~ab" {
            assert!(controller.feed(*byte, &frame).is_none());
        }
        controller.reconcile(&Frame::default());
        assert!(!controller.active());
        assert!(
            controller.owns_input(),
            "the paste tail still belongs to the controller"
        );
        for byte in b"t\x01xy\x1b[201~" {
            assert!(controller.feed(*byte, &frame).is_none());
        }
        assert!(!controller.owns_input());
    }
}