fux 0.11.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
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
//! Pane terminal emulation and bounded history: a long-lived `vt100::Parser` fed by PTY output
//! plus the terminal queries answered on the application's behalf.
//!
//! Adapted from koh (MIT); the upstream notice is retained in LICENSES/koh.txt.

use crate::proto::control::CaptureLine;
use crate::view::{Cursor, Line, PaneModes, PaneUpdate, push_vt100};
use vt100::Screen;

pub const MIN_DIM: u16 = 2;
/// Peer-controlled dimensions are clamped here so vt100 never allocates an unbounded grid.
pub const MAX_DIM: u16 = 512;
pub const MAX_CLIPBOARD_BASE64: usize = 16 * 1024;
const MAX_TITLE_CHARS: usize = 256;
/// Bound on one OSC/DCS/APC control string before it is dropped.
const MAX_CONTROL_STRING_BYTES: usize = 64 * 1024;

pub fn clamp_dims(rows: u16, cols: u16) -> (u16, u16) {
    (rows.clamp(MIN_DIM, MAX_DIM), cols.clamp(MIN_DIM, MAX_DIM))
}

/// A ConEmu / Windows Terminal progress report (`OSC 9;4;<state>;<percent>`), exposed through the
/// control listing for external observers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Progress {
    pub state: u8,
    pub percent: u8,
}

fn parse_progress(params: &[&[u8]]) -> Option<Progress> {
    if params.len() < 3
        || params.first().copied() != Some(b"9".as_slice())
        || params.get(1).copied() != Some(b"4".as_slice())
    {
        return None;
    }
    let number = |index: usize| -> Option<u8> {
        std::str::from_utf8(params.get(index).copied()?)
            .ok()?
            .parse::<u8>()
            .ok()
    };
    let state = number(2)?;
    if state > 4 {
        return None;
    }
    let percent = if state == 0 { 0 } else { number(3)? };
    (percent <= 100).then_some(Progress { state, percent })
}

fn title_from(bytes: &[u8]) -> String {
    crate::view::printable(&String::from_utf8_lossy(bytes), MAX_TITLE_CHARS)
}

#[derive(Default)]
struct Callbacks {
    title: String,
    clipboard: String,
    bell_count: u64,
    /// Query answers the application expects back on its input.
    host_replies: Vec<u8>,
    progress: Option<Progress>,
}

impl vt100::Callbacks for Callbacks {
    fn set_window_title(&mut self, _: &mut Screen, title: &[u8]) {
        self.title = title_from(title);
    }
    fn set_window_icon_name(&mut self, _: &mut Screen, _: &[u8]) {}
    fn audible_bell(&mut self, _: &mut Screen) {
        self.bell_count = self.bell_count.saturating_add(1);
    }
    fn copy_to_clipboard(&mut self, _: &mut Screen, _selection: &[u8], data: &[u8]) {
        if data.len() <= MAX_CLIPBOARD_BASE64 {
            self.clipboard = String::from_utf8_lossy(data).into_owned();
        }
    }
    fn unhandled_osc(&mut self, _: &mut Screen, params: &[&[u8]]) {
        match parse_progress(params) {
            Some(progress) if progress.state == 0 => self.progress = None,
            Some(progress) => self.progress = Some(progress),
            None => {}
        }
    }
    fn unhandled_csi(
        &mut self,
        screen: &mut Screen,
        intermediate: Option<u8>,
        second: Option<u8>,
        params: &[&[u16]],
        action: char,
    ) {
        let first = params
            .first()
            .and_then(|values| values.first())
            .copied()
            .unwrap_or(0);
        match (intermediate, second, action) {
            (None, _, 'n') => match first {
                6 => {
                    let (row, column) = screen.cursor_position();
                    self.host_replies.extend_from_slice(
                        format!("\x1b[{};{}R", u32::from(row) + 1, u32::from(column) + 1)
                            .as_bytes(),
                    );
                }
                5 => self.host_replies.extend_from_slice(b"\x1b[0n"),
                _ => {}
            },
            (Some(b'?'), _, 'n') if first == 6 => {
                let (row, column) = screen.cursor_position();
                self.host_replies.extend_from_slice(
                    format!("\x1b[?{};{}R", u32::from(row) + 1, u32::from(column) + 1).as_bytes(),
                );
            }
            (None, _, 'c') => self.host_replies.extend_from_slice(b"\x1b[?62;1;6c"),
            (Some(b'>'), _, 'c') => self.host_replies.extend_from_slice(b"\x1b[>1;10;0c"),
            (Some(b'?'), Some(b'$'), 'p') => {
                let status: u16 = match first {
                    2004 => {
                        if screen.bracketed_paste() {
                            1
                        } else {
                            2
                        }
                    }
                    _ => 0,
                };
                self.host_replies
                    .extend_from_slice(format!("\x1b[?{first};{status}$y").as_bytes());
            }
            _ => {}
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum ControlStringKind {
    Osc,
    Other,
}

/// Bounds control strings independently of pane history and keeps UTF-8 continuation bytes from
/// being mistaken for C1 string introducers or terminators.
#[derive(Default)]
struct ControlStringFilter {
    utf8_remaining: u8,
    pending_escape: bool,
    string: Option<ControlStringKind>,
    string_escape: bool,
    dropping: bool,
    buffered: Vec<u8>,
}

/// Upper bound on continuation bytes still needed by the current UTF-8 prefix.
fn utf8_remaining(remaining: u8, byte: u8) -> u8 {
    if remaining > 0 && (0x80..=0xbf).contains(&byte) {
        remaining - 1
    } else {
        match byte {
            0xc2..=0xdf => 1,
            0xe0..=0xef => 2,
            0xf0..=0xf4 => 3,
            _ => 0,
        }
    }
}

/// `utf8_remaining` folded over `bytes` from a zero state, without scanning them all: a
/// non-continuation byte resets the state regardless of history, and any run of four or more
/// continuation bytes ends at zero, so only the final four bytes can matter.
fn utf8_remaining_after(bytes: &[u8]) -> u8 {
    let tail = bytes.get(bytes.len().saturating_sub(4)..).unwrap_or(bytes);
    tail.iter()
        .fold(0, |remaining, &byte| utf8_remaining(remaining, byte))
}

impl ControlStringFilter {
    /// Filters `input` into `output` (cleared first); the buffer is the pane's scratch space so a
    /// chunk costs no allocation once it has grown to the chunk size.
    fn process_into(&mut self, input: &[u8], output: &mut Vec<u8>) {
        output.clear();
        for &byte in input {
            let continuation = self.utf8_remaining > 0 && (0x80..=0xbf).contains(&byte);
            self.utf8_remaining = utf8_remaining(self.utf8_remaining, byte);
            if let Some(kind) = self.string {
                let terminated = (byte == 0x9c && !continuation)
                    || (kind == ControlStringKind::Osc && byte == 0x07)
                    || (self.string_escape && byte == b'\\');
                if !self.dropping {
                    if self.buffered.len() < MAX_CONTROL_STRING_BYTES {
                        self.buffered.push(byte);
                    } else {
                        self.buffered.clear();
                        self.dropping = true;
                    }
                }
                self.string_escape = byte == 0x1b;
                if terminated || matches!(byte, 0x18 | 0x1a) {
                    if !self.dropping {
                        output.extend_from_slice(&self.buffered);
                    }
                    self.buffered.clear();
                    self.string = None;
                    self.string_escape = false;
                    self.dropping = false;
                }
                continue;
            }
            if self.pending_escape {
                self.pending_escape = false;
                if let Some(kind) = control_string_introducer(byte) {
                    self.string = Some(kind);
                    self.buffered.extend_from_slice(&[0x1b, byte]);
                    continue;
                }
                output.push(0x1b);
            }
            if byte == 0x1b {
                self.pending_escape = true;
            } else if let Some(kind) = c1_control_string_introducer(byte).filter(|_| !continuation)
            {
                self.string = Some(kind);
                self.buffered.push(byte);
            } else {
                output.push(byte);
            }
        }
    }
}

fn control_string_introducer(byte: u8) -> Option<ControlStringKind> {
    match byte {
        b']' => Some(ControlStringKind::Osc),
        b'P' | b'X' | b'_' | b'^' => Some(ControlStringKind::Other),
        _ => None,
    }
}

fn c1_control_string_introducer(byte: u8) -> Option<ControlStringKind> {
    match byte {
        0x9d => Some(ControlStringKind::Osc),
        0x90 | 0x98 | 0x9e | 0x9f => Some(ControlStringKind::Other),
        _ => None,
    }
}

/// A copy of what an observer last saw of the pane: the visible screen with the sequence each
/// row last changed at, plus cursor, modes, title and exit status. The sequence advances once per
/// refresh that changed anything, so a frame carries only the rows a viewer has not seen and
/// `capture` and `list` report the sequence.
#[derive(Default)]
pub struct Grid {
    rows: u16,
    columns: u16,
    /// Retained vt100 cells (`None` only for a position the screen did not report).
    cells: Vec<Option<vt100::Cell>>,
    wrapped: Vec<bool>,
    changed: Vec<u64>,
    seq: u64,
    cursor: Cursor,
    modes: PaneModes,
    title: String,
    exit: Option<u32>,
}

impl Grid {
    #[must_use]
    pub fn size(&self) -> (u16, u16) {
        (self.rows, self.columns)
    }

    /// The output sequence: advances when a refresh found anything an observer can see changed.
    #[must_use]
    pub fn seq(&self) -> u64 {
        self.seq
    }

    #[must_use]
    pub fn cursor(&self) -> Cursor {
        self.cursor
    }

    /// Brings the grid up to date with `screen`, `title` and `exit`; when anything changed the
    /// sequence advances, the rows that differ are stamped with it, and `true` is returned.
    fn refresh(&mut self, screen: &vt100::Screen, title: &str, exit: Option<u32>) -> bool {
        let next = self.seq.saturating_add(1);
        let (rows, columns) = screen.size();
        let width = usize::from(columns);
        let mut changed = false;
        if (rows, columns) != (self.rows, self.columns) {
            self.rows = rows;
            self.columns = columns;
            self.cells = vec![None; usize::from(rows) * width];
            self.wrapped = vec![false; usize::from(rows)];
            self.changed = vec![next; usize::from(rows)];
            for row in 0..rows {
                self.copy_row(screen, row, width);
            }
            changed = true;
        } else {
            for row in 0..rows {
                let wrapped = screen.row_wrapped(row);
                let start = usize::from(row) * width;
                let differs = self.wrapped.get(usize::from(row)) != Some(&wrapped)
                    || (0..columns).any(|column| {
                        self.cells
                            .get(start + usize::from(column))
                            .is_none_or(|current| current.as_ref() != screen.cell(row, column))
                    });
                if differs {
                    self.copy_row(screen, row, width);
                    if let Some(stamp) = self.changed.get_mut(usize::from(row)) {
                        *stamp = next;
                    }
                    changed = true;
                }
            }
        }
        let (row, column) = screen.cursor_position();
        let cursor = Cursor {
            row,
            column,
            hidden: screen.hide_cursor(),
        };
        let modes = PaneModes::from_vt100(screen);
        if cursor != self.cursor || modes != self.modes || title != self.title || exit != self.exit
        {
            self.cursor = cursor;
            self.modes = modes;
            if title != self.title {
                self.title.clear();
                self.title.push_str(title);
            }
            self.exit = exit;
            changed = true;
        }
        if changed {
            self.seq = next;
        }
        changed
    }

    /// Every visible row as wire cells, top to bottom, keeping whole rows while the JSON encoding
    /// of the kept rows stays within `max_bytes`; returns the rows and whether any were dropped.
    #[must_use]
    pub fn capture_lines(&self, max_bytes: usize) -> (Vec<CaptureLine>, bool) {
        let width = usize::from(self.columns);
        let mut lines = Vec::with_capacity(usize::from(self.rows));
        let mut bytes = 0_usize;
        for row in 0..self.rows {
            let index = usize::from(row);
            let mut cells = Vec::new();
            for cell in self.cells.iter().skip(index * width).take(width) {
                push_vt100(&mut cells, 0, cell.as_ref());
            }
            let line = CaptureLine {
                row,
                wrapped: self.wrapped.get(index).copied().unwrap_or(false),
                cells,
            };
            // Each row is one array element; the separating comma counts toward the bound.
            bytes = bytes
                .saturating_add(line.encoded_len())
                .saturating_add(usize::from(row > 0));
            if bytes > max_bytes {
                return (lines, true);
            }
            lines.push(line);
        }
        (lines, false)
    }

    fn copy_row(&mut self, screen: &vt100::Screen, row: u16, width: usize) {
        let start = usize::from(row) * width;
        for column in 0..self.columns {
            if let Some(slot) = self.cells.get_mut(start + usize::from(column)) {
                *slot = screen.cell(row, column).cloned();
            }
        }
        if let Some(flag) = self.wrapped.get_mut(usize::from(row)) {
            *flag = screen.row_wrapped(row);
        }
    }

    /// The rows changed after `since` (every row when `since` is `None`) as a pane update carrying
    /// the retained cursor, modes, title and exit status.
    #[must_use]
    pub fn update(&self, since: Option<u64>) -> PaneUpdate {
        let mut update = PaneUpdate {
            rows: self.rows,
            columns: self.columns,
            full: since.is_none(),
            cursor: self.cursor,
            modes: self.modes,
            title: self.title.clone(),
            exit: self.exit,
            ..PaneUpdate::default()
        };
        let width = usize::from(self.columns);
        for row in 0..self.rows {
            let index = usize::from(row);
            if since
                .is_some_and(|since| self.changed.get(index).is_none_or(|stamp| *stamp <= since))
            {
                continue;
            }
            let start = update.cells.len();
            for cell in self.cells.iter().skip(index * width).take(width) {
                push_vt100(&mut update.cells, start, cell.as_ref());
            }
            update.lines.push(Line {
                row,
                wrapped: self.wrapped.get(index).copied().unwrap_or(false),
                len: u16::try_from(update.cells.len() - start).unwrap_or(u16::MAX),
            });
        }
        update
    }
}

/// A bounded history viewport, not an append-only output transcript. Its first row is
/// `scrollback_offset` rows above the live screen and its height is `rows`.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CaptureSnapshot {
    pub text: String,
    pub revision: u64,
    pub rows: u16,
    pub columns: u16,
    pub scrollback_offset: u32,
    pub title: String,
    pub progress: Option<(u8, u8)>,
    pub unchanged: bool,
    /// Whether the returned text hit the byte limit. For unchanged responses consult the cache.
    pub truncated: bool,
}

/// The authoritative emulator and bounded history for one pane.
pub struct ServerTerminal {
    parser: vt100::Parser<Callbacks>,
    filter: ControlStringFilter,
    parser_utf8_remaining: u8,
    revision: u64,
    history_limit: usize,
    grid: Grid,
    /// Filtered output of the chunk being fed; reused across chunks.
    scratch: Vec<u8>,
}

impl ServerTerminal {
    #[must_use]
    pub fn new(rows: u16, cols: u16, history_limit: usize) -> Self {
        let (rows, cols) = clamp_dims(rows, cols);
        Self {
            parser: vt100::Parser::new_with_callbacks(
                rows,
                cols,
                history_limit,
                Callbacks::default(),
            ),
            filter: ControlStringFilter::default(),
            parser_utf8_remaining: 0,
            revision: 0,
            history_limit,
            grid: Grid::default(),
            scratch: Vec::new(),
        }
    }

    /// Brings the retained grid up to date with the live screen, `title` and `exit`; returns
    /// whether the output sequence advanced.
    pub fn refresh_grid(&mut self, title: &str, exit: Option<u32>) -> bool {
        self.grid.refresh(self.parser.screen(), title, exit)
    }

    #[must_use]
    pub fn grid(&self) -> &Grid {
        &self.grid
    }

    /// Feeds application output. A `vt100` panic on hostile output is contained: the chunk is
    /// dropped and later output repaints.
    pub fn process(&mut self, bytes: &[u8]) {
        if !bytes.is_empty() {
            self.revision = self.revision.wrapping_add(1);
        }
        self.filter.process_into(bytes, &mut self.scratch);
        let contained = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            // Complete split UTF-8 characters separately: vte lookahead can otherwise
            // consume bytes following the completed character. Keep the reusable buffer.
            let mut rest = self.scratch.as_slice();
            while self.parser_utf8_remaining > 0 && !rest.is_empty() {
                let (next, suffix) = rest.split_at(1);
                self.parser.process(next);
                if let Some(&byte) = next.first() {
                    self.parser_utf8_remaining = utf8_remaining(self.parser_utf8_remaining, byte);
                }
                rest = suffix;
            }
            if !rest.is_empty() {
                self.parser.process(rest);
                self.parser_utf8_remaining = utf8_remaining_after(rest);
            }
        }));
        if contained.is_err() {
            tracing::error!("terminal emulator rejected application output; chunk dropped");
        }
    }

    /// Bytes the application must receive in reply to its queries (DSR, DA, DECRQM).
    pub fn take_host_replies(&mut self) -> Vec<u8> {
        std::mem::take(&mut self.parser.callbacks_mut().host_replies)
    }

    pub fn resize(&mut self, rows: u16, cols: u16) {
        let (rows, cols) = clamp_dims(rows, cols);
        if self.size() != (rows, cols) {
            self.revision = self.revision.wrapping_add(1);
            self.parser.screen_mut().set_size(rows, cols);
        }
    }

    /// A change token scoped to this terminal lifetime; not a timestamp or byte count.
    #[must_use]
    pub fn revision(&self) -> u64 {
        self.revision
    }

    #[must_use]
    pub fn size(&self) -> (u16, u16) {
        self.parser.screen().size()
    }

    #[must_use]
    pub fn screen(&self) -> &Screen {
        self.parser.screen()
    }

    #[must_use]
    pub fn title(&self) -> &str {
        &self.parser.callbacks().title
    }

    #[must_use]
    pub fn clipboard(&self) -> &str {
        &self.parser.callbacks().clipboard
    }

    #[must_use]
    pub fn bell_count(&self) -> u64 {
        self.parser.callbacks().bell_count
    }

    #[must_use]
    pub fn progress(&self) -> Option<Progress> {
        self.parser.callbacks().progress
    }

    #[must_use]
    pub fn history_limit(&self) -> usize {
        self.history_limit
    }

    /// Temporarily selects a bounded history viewport and reads it, restoring the live viewport
    /// before returning. The offset is clamped to the retained history.
    pub fn with_history_screen<R>(&mut self, offset: usize, read: impl FnOnce(&Screen) -> R) -> R {
        let previous = self.parser.screen().scrollback();
        self.parser
            .screen_mut()
            .set_scrollback(offset.min(self.history_limit));
        let result =
            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| read(self.parser.screen())));
        self.parser.screen_mut().set_scrollback(previous);
        match result {
            Ok(result) => result,
            Err(payload) => std::panic::resume_unwind(payload),
        }
    }

    /// Plain or attribute-preserving text of a viewport shifted up by at most `scrollback`
    /// retained history rows, truncated to `max_bytes` on a character boundary.
    pub fn capture(&mut self, scrollback: usize, attrs: bool, max_bytes: usize) -> String {
        self.capture_snapshot(scrollback, attrs, max_bytes, None)
            .text
    }

    /// Text and metadata from one terminal revision. Conditional callers must reuse capture
    /// options and invalidate cached revisions when the pane/server identity changes.
    pub fn capture_snapshot(
        &mut self,
        scrollback: usize,
        attrs: bool,
        max_bytes: usize,
        if_revision: Option<u64>,
    ) -> CaptureSnapshot {
        let (rows, columns) = self.size();
        let progress = self.progress().map(|p| (p.state, p.percent));
        let mut snapshot = CaptureSnapshot {
            text: String::new(),
            revision: self.revision,
            rows,
            columns,
            scrollback_offset: 0,
            title: self.title().to_owned(),
            progress,
            unchanged: if_revision == Some(self.revision),
            truncated: false,
        };
        let columns = usize::from(columns).max(1);
        let bytes_per_row = if attrs {
            columns.saturating_mul(128)
        } else {
            columns.saturating_mul(4).saturating_add(1)
        };
        let bounded_rows = max_bytes.saturating_div(bytes_per_row).saturating_add(1);
        let offset = scrollback.min(self.history_limit).min(bounded_rows);
        self.with_history_screen(offset, |screen| {
            snapshot.scrollback_offset = u32::try_from(screen.scrollback()).unwrap_or(u32::MAX);
            if !snapshot.unchanged {
                snapshot.text = if attrs {
                    String::from_utf8_lossy(&screen.contents_formatted()).into_owned()
                } else {
                    screen.contents()
                };
                snapshot.truncated = snapshot.text.len() > max_bytes;
                if snapshot.truncated {
                    snapshot
                        .text
                        .truncate(snapshot.text.floor_char_boundary(max_bytes));
                }
            }
        });
        snapshot
    }
}

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

    #[test]
    fn utf8_remaining_after_equals_full_fold() {
        let full = |bytes: &[u8]| bytes.iter().fold(0, |r, &b| utf8_remaining(r, b));
        let classes = [
            0x41u8, 0x80, 0xbf, 0xc2, 0xdf, 0xe0, 0xef, 0xf0, 0xf4, 0xf5, 0xff,
        ];
        let mut sequence = Vec::new();
        assert_eq!(utf8_remaining_after(&sequence), full(&sequence));
        // Every sequence of up to six class representatives, including ones whose
        // history a naive last-byte check would get wrong.
        for length in 1..=6 {
            let total = classes.len().pow(length);
            for index in 0..total {
                sequence.clear();
                let mut rest = index;
                for _ in 0..length {
                    sequence.push(classes.get(rest % classes.len()).copied().unwrap_or(0));
                    rest /= classes.len();
                }
                assert_eq!(
                    utf8_remaining_after(&sequence),
                    full(&sequence),
                    "{sequence:x?}"
                );
            }
        }
    }
    use crate::view::PaneView;
    use proptest::prelude::*;

    #[test]
    fn capture_revision_is_distinct_from_grid_sequence() {
        let mut terminal = ServerTerminal::new(4, 8, 20);
        terminal.process(b"hello");
        terminal.refresh_grid("", None);
        let grid_sequence = terminal.grid().seq();
        let first = terminal.capture_snapshot(0, false, 4096, None);

        // Progress belongs to coherent capture metadata, but does not change grid cells.
        terminal.process(b"\x1b]9;4;1;50\x07");
        assert!(!terminal.refresh_grid("", None));
        assert_eq!(terminal.grid().seq(), grid_sequence);
        let changed = terminal.capture_snapshot(0, false, 4096, Some(first.revision));
        assert!(!changed.unchanged);
        assert_eq!(changed.progress, Some((1, 50)));
        assert_eq!(changed.text, first.text);

        // Grid refresh and temporary history selection must not themselves dirty captures.
        terminal.with_history_screen(3, |_| ());
        assert_eq!(terminal.revision(), changed.revision);
        assert_eq!(terminal.screen().scrollback(), 0);
        assert!(
            terminal
                .capture_snapshot(0, false, 4096, Some(changed.revision))
                .unchanged
        );
    }

    #[test]
    fn history_changes_invalidate_capture_even_when_live_grid_is_identical() {
        let mut terminal = ServerTerminal::new(2, 8, 20);
        terminal.process(b"old\r\nscreen");
        terminal.process(b"\x1b[2J\x1b[H");
        terminal.refresh_grid("", None);
        let before = terminal.capture_snapshot(1, false, 4096, None);
        let seq = terminal.grid().seq();
        terminal.process(b"new\r\nline\r\n\x1b[2J\x1b[H");
        assert!(!terminal.refresh_grid("", None));
        assert_eq!(terminal.grid().seq(), seq);
        let after = terminal.capture_snapshot(1, false, 4096, Some(before.revision));
        assert!(!after.unchanged);
        assert_ne!(after.text, before.text);
        assert_eq!(terminal.screen().scrollback(), 0);
    }

    #[test]
    fn conditional_capture_tracks_output_resize_and_metadata_without_moving_history() {
        let mut terminal = ServerTerminal::new(4, 8, 20);
        terminal.process(b"one\r\ntwo\r\nthree\r\nfour\r\nfive\x1b]2;title\x07");
        let first = terminal.capture_snapshot(1, false, 4096, None);
        assert_eq!((first.rows, first.columns), (4, 8));
        assert_eq!(first.scrollback_offset, 1);
        assert_eq!(first.title, "title");
        assert!(!first.unchanged);
        assert_eq!(terminal.screen().scrollback(), 0);
        let cached = terminal.capture_snapshot(1, false, 4096, Some(first.revision));
        assert!(cached.unchanged);
        assert!(cached.text.is_empty());
        assert_eq!(cached.scrollback_offset, first.scrollback_offset);
        terminal.resize(4, 8);
        assert_eq!(terminal.revision(), first.revision);
        terminal.resize(6, 10);
        let resized = terminal.capture_snapshot(1, false, 4096, Some(first.revision));
        assert!(!resized.unchanged);
        assert_eq!((resized.rows, resized.columns), (6, 10));
        terminal.process(b"\x1b]9;4;1;50\x07");
        let progress = terminal.capture_snapshot(0, true, 4096, Some(resized.revision));
        assert!(!progress.unchanged);
        assert_eq!(progress.progress, Some((1, 50)));
        assert!(terminal.capture_snapshot(0, true, 1, None).truncated);
    }

    /// The rows of a view, as text plus wrap flags, for comparing deltas with the screen.
    fn rows_of(view: &PaneView) -> Vec<(String, bool)> {
        (0..view.rows)
            .map(|row| {
                let text: String = (0..view.columns)
                    .filter_map(|column| view.cell(row, column))
                    .map(|cell| format!("{}:{:?}:{:?}|", cell.text, cell.kind, cell.style))
                    .collect();
                (
                    text,
                    view.wrapped_rows
                        .get(usize::from(row))
                        .copied()
                        .unwrap_or(false),
                )
            })
            .collect()
    }

    fn edit(terminal: &mut ServerTerminal, op: u8, byte: u8) {
        match op % 6 {
            0 => terminal.process(&[b'a' + byte % 26]),
            1 => terminal.process(b"\r\n"),
            2 => terminal.process("\u{65e5}".as_bytes()),
            3 => terminal.process(format!("\x1b[{};{}H", byte % 5 + 1, byte % 11 + 1).as_bytes()),
            4 => terminal.process(b"\x1b[1;31mX\x1b[m"),
            _ => terminal.process(b"\x1b[2J"),
        }
    }

    proptest! {
        #[test]
        fn deltas_and_merged_deltas_reproduce_the_screen(
            ops in proptest::collection::vec((0_u8..6, any::<u8>(), any::<bool>()), 1..60)
        ) {
            let mut terminal = ServerTerminal::new(4, 10, 0);
            // `held` is what a viewer applied; `pending` is the update queued for it (merged).
            let mut held: Option<PaneView> = None;
            let mut held_seq: Option<u64> = None;
            let mut pending: Option<PaneUpdate> = None;
            for (op, byte, deliver) in ops {
                edit(&mut terminal, op, byte);
                terminal.refresh_grid("", None);
                let update = terminal.grid().update(held_seq);
                held_seq = Some(terminal.grid().seq());
                match &mut pending {
                    Some(queued) => queued.merge(update),
                    None => pending = Some(update),
                }
                if deliver {
                    let Some(update) = pending.take() else { continue };
                    let mut view = held.take().unwrap_or_default();
                    prop_assert!(view.apply(&update).is_ok(), "{update:?}");
                    let direct = PaneView::from_screen(terminal.screen(), "", 0, None)
                        .unwrap_or_default();
                    prop_assert_eq!(rows_of(&view), rows_of(&direct));
                    held = Some(view);
                }
            }
        }
    }

    #[test]
    fn the_sequence_advances_only_for_observable_changes() {
        let mut terminal = ServerTerminal::new(4, 10, 0);
        assert!(
            terminal.refresh_grid("", None),
            "first refresh sizes the grid"
        );
        let first = terminal.grid().seq();
        assert!(!terminal.refresh_grid("", None), "nothing changed");
        assert_eq!(terminal.grid().seq(), first);
        terminal.process(b"\x07");
        assert!(!terminal.refresh_grid("", None), "a bell is not visible");
        terminal.process(b"hi");
        assert!(terminal.refresh_grid("", None));
        assert_eq!(terminal.grid().seq(), first + 1);
        assert_eq!(
            changed_rows(terminal.grid(), Some(first)),
            vec![(0, "hi".to_owned())]
        );
        assert!(changed_rows(terminal.grid(), Some(first + 1)).is_empty());
        // Cursor moves, titles and the exit status are observable too.
        terminal.process(b"\x1b[3;1H");
        assert!(terminal.refresh_grid("", None));
        assert_eq!(terminal.grid().cursor().row, 2);
        assert!(changed_rows(terminal.grid(), Some(first + 1)).is_empty());
        assert!(terminal.refresh_grid("title", None));
        assert!(terminal.refresh_grid("title", Some(3)));
        assert!(!terminal.refresh_grid("title", Some(3)));
        assert_eq!(terminal.grid().seq(), first + 4);
        // A resize stamps every row.
        terminal.resize(5, 10);
        assert!(terminal.refresh_grid("title", Some(3)));
        assert_eq!(changed_rows(terminal.grid(), Some(first + 4)).len(), 5);
        // Wide characters take one entry in the row text and blanks are trimmed.
        terminal.process("\x1b[H\u{65e5}x  ".as_bytes());
        terminal.refresh_grid("title", Some(3));
        assert_eq!(
            changed_rows(terminal.grid(), None)
                .first()
                .map(|(_, text)| text.as_str()),
            Some("\u{65e5}x")
        );
    }

    /// The rows a viewer update carries after `since`, as `(row, text)` with trailing blanks
    /// trimmed, so tests can state which rows changed without decoding wire cells by hand.
    fn changed_rows(grid: &Grid, since: Option<u64>) -> Vec<(u16, String)> {
        let update = grid.update(since);
        let mut rows = Vec::new();
        let mut start = 0_usize;
        for line in &update.lines {
            let mut text = String::new();
            let len = usize::from(line.len);
            for cell in update.cells.iter().skip(start).take(len) {
                match (&cell.text, cell.kind) {
                    (_, Some(CellKind::WideContinuation)) => {}
                    (Some(t), _) => text.push_str(t),
                    (None, _) => {
                        text.extend(std::iter::repeat_n(' ', usize::from(cell.run.max(1))));
                    }
                }
            }
            let trimmed = text.trim_end_matches(' ').len();
            text.truncate(trimmed);
            rows.push((line.row, text));
            start += len;
        }
        rows
    }

    proptest! {
        /// Folding viewer updates over any split of an output stream reproduces the rows a full
        /// update returns.
        #[test]
        fn row_updates_since_fold_to_the_full_update(
            ops in proptest::collection::vec((0_u8..6, any::<u8>(), any::<bool>()), 1..60)
        ) {
            let mut terminal = ServerTerminal::new(4, 10, 0);
            let mut folded: std::collections::BTreeMap<u16, String> =
                std::collections::BTreeMap::new();
            let mut seen: Option<u64> = None;
            for (op, byte, read) in ops {
                edit(&mut terminal, op, byte);
                if !read {
                    continue;
                }
                terminal.refresh_grid("", None);
                for (row, text) in changed_rows(terminal.grid(), seen) {
                    folded.insert(row, text);
                }
                seen = Some(terminal.grid().seq());
            }
            terminal.refresh_grid("", None);
            for (row, text) in changed_rows(terminal.grid(), seen) {
                folded.insert(row, text);
            }
            let full = changed_rows(terminal.grid(), None);
            let folded: Vec<(u16, String)> = folded.into_iter().collect();
            prop_assert_eq!(folded, full);
        }
    }

    #[test]
    fn answers_cursor_position_and_device_attributes() {
        let mut terminal = ServerTerminal::new(24, 80, 0);
        terminal.process(b"\x1b[5;3H\x1b[6n");
        assert_eq!(terminal.take_host_replies(), b"\x1b[5;3R");
        assert!(terminal.take_host_replies().is_empty());
        terminal.process(b"\x1b[c\x1b[>c\x1b[?2004$p");
        assert_eq!(
            terminal.take_host_replies(),
            b"\x1b[?62;1;6c\x1b[>1;10;0c\x1b[?2004;2$y"
        );
    }

    #[test]
    fn title_bell_clipboard_and_progress_are_captured_and_bounded() {
        let mut terminal = ServerTerminal::new(24, 80, 0);
        terminal.process(b"\x1b]2;my-title\x07\x07\x1b]52;c;aGVsbG8=\x07\x1b]9;4;1;50\x1b\\");
        assert_eq!(terminal.title(), "my-title");
        assert_eq!(terminal.bell_count(), 1);
        assert_eq!(terminal.clipboard(), "aGVsbG8=");
        assert_eq!(
            terminal.progress(),
            Some(Progress {
                state: 1,
                percent: 50
            })
        );
        terminal.process(b"\x1b]9;4;0\x07");
        assert_eq!(terminal.progress(), None);
        let huge = "x".repeat(MAX_TITLE_CHARS + 500);
        terminal.process(format!("\x1b]2;{huge}\x07").as_bytes());
        assert_eq!(terminal.title().chars().count(), MAX_TITLE_CHARS);
        let big = "A".repeat(MAX_CLIPBOARD_BASE64 + 1);
        terminal.process(format!("\x1b]52;c;{big}\x07").as_bytes());
        assert_eq!(terminal.clipboard(), "aGVsbG8=");
    }

    #[test]
    fn resize_clamps_and_wide_output_never_panics() {
        let mut terminal = ServerTerminal::new(24, 80, 0);
        terminal.resize(65000, 65000);
        assert_eq!(terminal.size(), (MAX_DIM, MAX_DIM));
        terminal.resize(0, 0);
        assert_eq!(terminal.size(), (MIN_DIM, MIN_DIM));
        terminal.process("AAAA日本🦀\r\nBBBB\r\n".repeat(8).as_bytes());
        terminal.resize(40, 120);
        assert_eq!(terminal.size(), (40, 120));
    }

    #[test]
    fn unterminated_control_strings_are_bounded_and_reset() {
        let mut terminal = ServerTerminal::new(24, 80, 0);
        terminal.process(b"before\x1b]");
        for _ in 0..32 {
            terminal.process(&vec![b'x'; MAX_CONTROL_STRING_BYTES / 4]);
            assert!(terminal.filter.buffered.len() <= MAX_CONTROL_STRING_BYTES);
        }
        assert!(terminal.filter.dropping);
        terminal.process(b"\x1b\\after");
        assert!(!terminal.filter.dropping);
        assert!(terminal.screen().contents().contains("beforeafter"));
    }

    #[test]
    fn utf8_continuations_do_not_open_or_terminate_control_strings() {
        let text = "beforeАИМНОП😀 prefix Ü 🐝 end";
        for split in 0..=text.len() {
            let mut terminal = ServerTerminal::new(24, 80, 0);
            let (first, second) = text.as_bytes().split_at(split);
            terminal.process(first);
            terminal.process(second);
            assert_eq!(terminal.screen().contents(), text, "split {split}");
        }
        let mut terminal = ServerTerminal::new(24, 80, 0);
        for byte in "\x1b]2;М title\x1b\\visible".as_bytes() {
            terminal.process(&[*byte]);
        }
        assert_eq!(terminal.title(), "М title");
        assert_eq!(terminal.screen().contents(), "visible");
        terminal.process(b"\x1b]2;");
        terminal.process(&vec![b'x'; MAX_CONTROL_STRING_BYTES]);
        for byte in "Мstill hidden".as_bytes() {
            terminal.process(&[*byte]);
        }
        assert!(terminal.filter.dropping);
        terminal.process(b"\x1b\\after");
        assert_eq!(terminal.title(), "М title");
        assert_eq!(terminal.screen().contents(), "visibleafter");
    }

    #[test]
    fn split_osc_and_csi_sequences_keep_their_meaning() {
        let mut terminal = ServerTerminal::new(24, 80, 0);
        for chunk in [
            &b"\x1b"[..],
            &b"]2;split"[..],
            &b" title\x1b"[..],
            &b"\\\x1b"[..],
            &b"[5;3Hok"[..],
        ] {
            terminal.process(chunk);
        }
        assert_eq!(terminal.title(), "split title");
        assert!(terminal.screen().contents().contains("ok"));
        for introducer in [&b"\x1bX"[..], &b"\x98"[..]] {
            let mut terminal = ServerTerminal::new(24, 80, 0);
            terminal.process(introducer);
            for _ in 0..5 {
                terminal.process(&vec![b'x'; MAX_CONTROL_STRING_BYTES / 4]);
            }
            assert!(terminal.filter.dropping);
            terminal.process(b"\x1b\\ok");
            assert!(terminal.screen().contents().contains("ok"));
        }
    }

    #[test]
    fn history_reads_restore_the_live_viewport_and_capture_is_bounded() {
        let mut terminal = ServerTerminal::new(4, 10, 100);
        for line in 0..20 {
            terminal.process(format!("line{line}\r\n").as_bytes());
        }
        let live = terminal.screen().contents();
        let older = terminal.with_history_screen(10, |screen| screen.contents());
        assert!(older.contains("line8"), "{older}");
        assert_eq!(terminal.screen().contents(), live);
        assert_eq!(terminal.screen().scrollback(), 0);
        let capture = terminal.capture(50, false, 40);
        assert!(capture.len() <= 40);
        let full = terminal.capture(50, false, 100_000);
        assert!(full.contains("line0"));
        assert!(terminal.with_history_screen(usize::MAX, |screen| screen.scrollback()) <= 100);
    }
}