retach 0.10.0

Persistent terminal sessions with native scrollback passthrough
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
use std::collections::VecDeque;

use super::cell::{Cell, Row};
use super::style::StyleTable;

/// DEC character set designator.
#[derive(Copy, Clone, Debug, Default, PartialEq, Hash)]
pub enum Charset {
    #[default]
    Ascii,
    LineDrawing,
}

/// Which character set slot (G0/G1) is active.
#[derive(Copy, Clone, Debug, Default, PartialEq, Hash)]
pub enum ActiveCharset {
    #[default]
    G0,
    G1,
}

/// DECSCUSR cursor shape.
#[derive(Copy, Clone, Debug, Default, PartialEq, Hash)]
pub enum CursorShape {
    #[default]
    Default,
    BlinkBlock,
    SteadyBlock,
    BlinkUnderline,
    SteadyUnderline,
    BlinkBar,
    SteadyBar,
}

impl CursorShape {
    /// Convert from raw DECSCUSR parameter.
    pub fn from_param(n: u8) -> Self {
        match n {
            1 => Self::BlinkBlock,
            2 => Self::SteadyBlock,
            3 => Self::BlinkUnderline,
            4 => Self::SteadyUnderline,
            5 => Self::BlinkBar,
            6 => Self::SteadyBar,
            _ => Self::Default,
        }
    }

    /// Raw DECSCUSR parameter value.
    pub fn to_param(self) -> u8 {
        match self {
            Self::Default => 0,
            Self::BlinkBlock => 1,
            Self::SteadyBlock => 2,
            Self::BlinkUnderline => 3,
            Self::SteadyUnderline => 4,
            Self::BlinkBar => 5,
            Self::SteadyBar => 6,
        }
    }
}

/// Terminal mode flags and character set state, separated from cell storage.
#[derive(Clone, Debug, PartialEq)]
pub struct TerminalModes {
    pub cursor_key_mode: bool, // ?1 DECCKM
    pub bracketed_paste: bool, // ?2004
    pub origin_mode: bool,     // ?6 DECOM
    pub autowrap_mode: bool,   // ?7 DECAWM (default true)
    pub insert_mode: bool,     // 4 IRM — insert/replace mode (standard, not DEC private)
    pub focus_reporting: bool, // ?1004
    pub mouse_modes: MouseModes,
    pub mouse_encoding: MouseEncoding,
    pub keypad_app_mode: bool, // ESC = / ESC >
    pub cursor_shape: CursorShape,
    // DEC character sets
    pub g0_charset: Charset,
    pub g1_charset: Charset,
    pub active_charset: ActiveCharset,
}

impl Default for TerminalModes {
    fn default() -> Self {
        Self {
            cursor_key_mode: false,
            bracketed_paste: false,
            origin_mode: false,
            autowrap_mode: true,
            insert_mode: false,
            focus_reporting: false,
            mouse_modes: MouseModes::default(),
            mouse_encoding: MouseEncoding::X10,
            keypad_app_mode: false,
            cursor_shape: CursorShape::Default,
            g0_charset: Charset::Ascii,
            g1_charset: Charset::Ascii,
            active_charset: ActiveCharset::G0,
        }
    }
}

/// Mouse tracking mode.
#[cfg(test)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Hash)]
pub enum MouseMode {
    #[default]
    Off,
    Click,  // ?1000
    Button, // ?1002
    Any,    // ?1003
}

/// Per-mode mouse tracking flags, matching xterm behavior.
/// Each mode can be independently enabled/disabled. The effective mode
/// is the highest-priority enabled mode.
#[derive(Clone, Debug, Default, PartialEq, Hash)]
pub struct MouseModes {
    pub click: bool,  // ?1000
    pub button: bool, // ?1002
    pub any: bool,    // ?1003
}

impl MouseModes {
    /// Set a mouse mode flag from a DEC private mode parameter.
    pub fn set(&mut self, param: u16, enable: bool) {
        match param {
            1000 => self.click = enable,
            1002 => self.button = enable,
            1003 => self.any = enable,
            _ => {}
        }
    }

    /// Return the effective mouse mode (highest priority enabled).
    #[cfg(test)]
    pub fn effective(&self) -> MouseMode {
        if self.any {
            MouseMode::Any
        } else if self.button {
            MouseMode::Button
        } else if self.click {
            MouseMode::Click
        } else {
            MouseMode::Off
        }
    }
}

/// Mouse coordinate encoding.
#[derive(Copy, Clone, Debug, Default, PartialEq, Hash)]
pub enum MouseEncoding {
    #[default]
    X10,
    Utf8, // ?1005
    Sgr,  // ?1006
}

impl MouseEncoding {
    /// Convert from a DEC private mode parameter.
    pub fn from_param(p: u16) -> Option<Self> {
        match p {
            1005 => Some(Self::Utf8),
            1006 => Some(Self::Sgr),
            _ => None,
        }
    }
}

/// Two-dimensional cell storage with cursor position, scroll region, and terminal modes.
///
/// Uses a unified buffer: `cells` holds `[scrollback | visible]` rows.
/// `scrollback_len` marks the boundary; `pending_start` tracks unsent scrollback.
pub struct Grid {
    cols: u16,
    rows: u16,
    /// Unified buffer: `cells[0..scrollback_len]` = scrollback,
    /// `cells[scrollback_len..]` = visible rows.
    cells: VecDeque<Row>,
    cursor_x: u16,
    cursor_y: u16,
    /// Deferred wrap: cursor is at the right margin, next printable char triggers wrap
    wrap_pending: bool,
    /// Scroll region top (inclusive, 0-based)
    scroll_top: u16,
    /// Scroll region bottom (inclusive, 0-based)
    scroll_bottom: u16,
    /// Cursor visibility (DECTCEM ?25h/?25l)
    cursor_visible: bool,
    /// Terminal modes and character set state
    modes: TerminalModes,
    /// Tab stop positions (true = tab stop set at this column)
    tab_stops: Vec<bool>,
    /// Number of scrollback rows at the front of `cells`
    scrollback_len: usize,
    /// Maximum number of scrollback lines to retain
    scrollback_limit: usize,
    /// Index where unsent scrollback begins (for live client updates)
    pending_start: usize,
    /// Interned style table shared by all cells in this grid
    style_table: StyleTable,
}

/// Saved visible rows and scrollback limit for alt screen save/restore.
/// Scrollback rows stay in the active grid during alt screen.
pub struct SavedGrid {
    visible_cells: VecDeque<Row>,
    scrollback_limit: usize,
}

impl SavedGrid {
    /// Create a new SavedGrid.
    pub(super) fn new(visible_cells: VecDeque<Row>, scrollback_limit: usize) -> Self {
        Self {
            visible_cells,
            scrollback_limit,
        }
    }

    /// Iterate over saved visible rows (for style compaction).
    pub(super) fn visible_rows(&self) -> impl Iterator<Item = &Row> {
        self.visible_cells.iter()
    }

    /// Scrollback limit that was saved.
    pub(super) fn scrollback_limit(&self) -> usize {
        self.scrollback_limit
    }

    /// Consume the SavedGrid, returning (visible_cells, scrollback_limit).
    pub(super) fn into_parts(self) -> (VecDeque<Row>, usize) {
        (self.visible_cells, self.scrollback_limit)
    }
}

/// Create default tab stops every 8 columns for the given width.
pub(super) fn default_tab_stops(cols: u16) -> Vec<bool> {
    // Default tab stops every 8 columns per ECMA-48 / VT100 standard.
    (0..cols).map(|c| c > 0 && c % 8 == 0).collect()
}

impl Grid {
    /// Create a grid with the given dimensions, sanitized to at least 1x1.
    pub fn new(cols: u16, rows: u16, scrollback_limit: usize) -> Self {
        let TerminalSize { cols, rows } = sanitize_dimensions(cols, rows);
        Self {
            cols,
            rows,
            cells: (0..rows as usize)
                .map(|_| Row::new(cols as usize))
                .collect(),
            cursor_x: 0,
            cursor_y: 0,
            wrap_pending: false,
            scroll_top: 0,
            scroll_bottom: rows - 1,
            cursor_visible: true,
            modes: TerminalModes::default(),
            tab_stops: default_tab_stops(cols),
            scrollback_len: 0,
            scrollback_limit,
            pending_start: 0,
            style_table: StyleTable::new(),
        }
    }

    // =================================================================
    // Public read-only accessors
    // =================================================================

    /// Number of columns.
    #[inline]
    pub fn cols(&self) -> u16 {
        self.cols
    }

    /// Number of visible rows.
    #[inline]
    pub fn rows(&self) -> u16 {
        self.rows
    }

    /// Current cursor position (x, y), both 0-based.
    #[inline]
    pub fn cursor_pos(&self) -> (u16, u16) {
        (self.cursor_x, self.cursor_y)
    }

    /// Whether the cursor is currently visible (DECTCEM).
    #[inline]
    pub fn cursor_visible(&self) -> bool {
        self.cursor_visible
    }

    /// Current scroll region (top, bottom), both 0-based inclusive.
    #[inline]
    pub fn scroll_region(&self) -> (u16, u16) {
        (self.scroll_top, self.scroll_bottom)
    }

    /// Terminal modes (read-only).
    #[inline]
    pub fn modes(&self) -> &TerminalModes {
        &self.modes
    }

    /// Number of scrollback rows.
    #[inline]
    pub fn scrollback_len(&self) -> usize {
        self.scrollback_len
    }

    /// Maximum scrollback lines to retain.
    #[inline]
    pub fn scrollback_limit(&self) -> usize {
        self.scrollback_limit
    }

    /// Index where unsent scrollback begins.
    #[inline]
    pub fn pending_start(&self) -> usize {
        self.pending_start
    }

    /// Style table (read-only).
    #[inline]
    pub fn style_table(&self) -> &StyleTable {
        &self.style_table
    }

    /// Whether a deferred wrap is pending.
    #[inline]
    pub fn wrap_pending(&self) -> bool {
        self.wrap_pending
    }

    /// Whether a tab stop is set at the given column.
    #[cfg(test)]
    #[inline]
    pub fn tab_stop_at(&self, col: usize) -> bool {
        self.tab_stops.get(col).copied().unwrap_or(false)
    }

    /// Number of tab stop slots (equals column count).
    #[cfg(test)]
    #[inline]
    pub fn tab_stops_len(&self) -> usize {
        self.tab_stops.len()
    }

    // =================================================================
    // Mutation accessors (for performer)
    // =================================================================

    /// Current cursor X position.
    #[inline]
    pub(super) fn cursor_x(&self) -> u16 {
        self.cursor_x
    }

    /// Current cursor Y position.
    #[inline]
    pub(super) fn cursor_y(&self) -> u16 {
        self.cursor_y
    }

    /// Set cursor X (caller ensures valid value).
    #[inline]
    pub(super) fn set_cursor_x_unclamped(&mut self, x: u16) {
        self.cursor_x = x;
    }

    /// Set cursor Y (caller ensures valid value).
    #[inline]
    pub(super) fn set_cursor_y_unclamped(&mut self, y: u16) {
        self.cursor_y = y;
    }

    /// Set wrap_pending flag.
    #[inline]
    pub(super) fn set_wrap_pending(&mut self, val: bool) {
        self.wrap_pending = val;
    }

    /// Set cursor visibility.
    #[inline]
    pub(super) fn set_cursor_visible(&mut self, visible: bool) {
        self.cursor_visible = visible;
    }

    /// Set scroll region. Caller must ensure top <= bottom < rows.
    #[inline]
    pub(super) fn set_scroll_region(&mut self, top: u16, bottom: u16) {
        self.scroll_top = top;
        self.scroll_bottom = bottom;
    }

    /// Scroll region top (0-based inclusive).
    #[inline]
    pub(super) fn scroll_top(&self) -> u16 {
        self.scroll_top
    }

    /// Scroll region bottom (0-based inclusive).
    #[inline]
    pub(super) fn scroll_bottom(&self) -> u16 {
        self.scroll_bottom
    }

    /// Terminal modes (mutable).
    #[inline]
    pub(super) fn modes_mut(&mut self) -> &mut TerminalModes {
        &mut self.modes
    }

    /// Set terminal modes directly (for restore).
    #[inline]
    pub(super) fn set_modes(&mut self, modes: TerminalModes) {
        self.modes = modes;
    }

    /// Style table (mutable).
    #[inline]
    pub(super) fn style_table_mut(&mut self) -> &mut StyleTable {
        &mut self.style_table
    }

    /// Set a tab stop at the given column.
    pub(super) fn set_tab_stop(&mut self, col: u16) {
        let c = col as usize;
        if c < self.tab_stops.len() {
            self.tab_stops[c] = true;
        }
    }

    /// Clear a tab stop at the given column.
    pub(super) fn clear_tab_stop(&mut self, col: u16) {
        let c = col as usize;
        if c < self.tab_stops.len() {
            self.tab_stops[c] = false;
        }
    }

    /// Clear all tab stops.
    pub(super) fn clear_all_tab_stops(&mut self) {
        for stop in self.tab_stops.iter_mut() {
            *stop = false;
        }
    }

    /// Reset tab stops to default (every 8 columns).
    pub(super) fn reset_tab_stops(&mut self) {
        self.tab_stops = default_tab_stops(self.cols);
    }

    /// Set maximum scrollback limit.
    #[inline]
    pub(super) fn set_scrollback_limit(&mut self, limit: usize) {
        self.scrollback_limit = limit;
    }

    /// Set pending_start index.
    #[inline]
    pub(super) fn set_pending_start(&mut self, val: usize) {
        self.pending_start = val;
    }

    /// Iterate over scrollback rows.
    pub fn scrollback_rows(&self) -> impl Iterator<Item = &Row> {
        self.cells.iter().take(self.scrollback_len)
    }

    /// Drain visible rows for alt screen save. Returns saved rows.
    pub(super) fn drain_visible(&mut self) -> VecDeque<Row> {
        self.cells.drain(self.scrollback_len..).collect()
    }

    /// Replace visible rows (for alt screen restore).
    pub(super) fn replace_visible(&mut self, rows: VecDeque<Row>) {
        self.cells.truncate(self.scrollback_len);
        for row in rows {
            self.cells.push_back(row);
        }
    }

    /// Add blank visible rows for alt screen.
    pub(super) fn fill_visible_blank(&mut self) {
        for _ in 0..self.rows as usize {
            self.cells.push_back(Row::new(self.cols as usize));
        }
        self.check_invariants();
    }

    /// Adjust visible row count to match `self.rows` (trim or pad).
    pub(super) fn adjust_visible_to_fit(&mut self) {
        let rows_usize = self.rows as usize;
        while self.visible_row_count() > rows_usize {
            self.cells.pop_back();
        }
        while self.visible_row_count() < rows_usize {
            self.cells.push_back(Row::new(self.cols as usize));
        }
        self.check_invariants();
    }

    /// Decrement scrollback_len by `count` (for restoring scrollback on vertical grow).
    /// Note: does NOT call check_invariants() because the caller (Screen::resize)
    /// adjusts visible row count via grid.resize() immediately after.
    pub(super) fn restore_scrollback(&mut self, count: usize) {
        debug_assert!(count <= self.scrollback_len);
        self.scrollback_len = self.scrollback_len.saturating_sub(count);
        self.pending_start = self.pending_start.min(self.scrollback_len);
    }

    /// Access a scrollback row by index (0-based, oldest first).
    pub(super) fn scrollback_row(&self, idx: usize) -> &Row {
        assert!(
            idx < self.scrollback_len,
            "scrollback_row index {idx} out of bounds (scrollback_len {})",
            self.scrollback_len
        );
        &self.cells[idx]
    }

    // =================================================================
    // Existing methods
    // =================================================================

    /// Access a visible row by index.
    pub fn visible_row(&self, y: usize) -> &Row {
        debug_assert!(
            y < self.rows as usize,
            "visible_row: y={} out of bounds (rows={})",
            y,
            self.rows
        );
        &self.cells[self.scrollback_len + y]
    }

    /// Mutably access a visible row by index.
    pub fn visible_row_mut(&mut self, y: usize) -> &mut Row {
        debug_assert!(
            y < self.rows as usize,
            "visible_row_mut: y={} out of bounds (rows={})",
            y,
            self.rows
        );
        let offset = self.scrollback_len;
        &mut self.cells[offset + y]
    }

    /// Iterate over visible rows.
    pub fn visible_rows(&self) -> impl Iterator<Item = &Row> {
        self.cells
            .iter()
            .skip(self.scrollback_len)
            .take(self.rows as usize)
    }

    /// Number of visible rows.
    pub fn visible_row_count(&self) -> usize {
        self.cells.len().saturating_sub(self.scrollback_len)
    }

    /// Remove a visible row by index, returning it.
    pub fn remove_visible_row(&mut self, y: usize) -> Row {
        let idx = self.scrollback_len + y;
        debug_assert!(
            idx < self.cells.len(),
            "remove_visible_row: index {} out of bounds (cells len {})",
            idx,
            self.cells.len()
        );
        self.cells
            .remove(idx)
            .unwrap_or_else(|| Row::new(self.cols as usize))
    }

    /// Insert a row at a visible row index.
    pub fn insert_visible_row(&mut self, y: usize, row: Row) {
        self.cells.insert(self.scrollback_len + y, row);
    }

    /// Find the next tab stop column at or after `col`, clamped to right margin.
    pub fn next_tab_stop(&self, col: u16) -> u16 {
        for c in (col as usize + 1)..self.tab_stops.len() {
            if self.tab_stops[c] {
                return c as u16;
            }
        }
        self.cols - 1
    }

    /// Scroll the region up by one line, capturing scrollback on the main screen.
    ///
    /// When scrollback is enabled and the full screen scrolls, the top visible row
    /// becomes a scrollback row by moving the boundary — zero clones.
    pub fn scroll_up(&mut self, in_alt_screen: bool, fill: Cell) {
        let top = self.scroll_top as usize;
        let bottom = self.scroll_bottom as usize;
        let visible_len = self.cells.len() - self.scrollback_len;

        if !in_alt_screen && top == 0 && self.scrollback_limit > 0 {
            // Top visible row becomes scrollback — just move the boundary
            self.scrollback_len += 1;
            if self.scrollback_len > self.scrollback_limit {
                self.cells.pop_front();
                self.scrollback_len -= 1;
                if self.pending_start > 0 {
                    self.pending_start -= 1;
                }
            }
            // Insert blank row at the scroll region bottom, not necessarily
            // the end of cells (partial scroll region must not shift rows below).
            if bottom >= visible_len - 1 {
                self.cells
                    .push_back(Row::from_cells(vec![fill; self.cols as usize]));
            } else {
                self.cells.insert(
                    self.scrollback_len + bottom,
                    Row::from_cells(vec![fill; self.cols as usize]),
                );
            }
        } else {
            self.scroll_region_up(fill);
        }
        self.check_invariants();
    }

    /// Scroll the region up by one line WITHOUT capturing the scrolled-out row
    /// into scrollback (used by SU / CSI Ps S, matching xterm semantics).
    pub fn scroll_up_no_capture(&mut self, fill: Cell) {
        self.scroll_region_up(fill);
        self.check_invariants();
    }

    fn scroll_region_up(&mut self, fill: Cell) {
        let top = self.scroll_top as usize;
        let bottom = self.scroll_bottom as usize;
        let visible_len = self.cells.len() - self.scrollback_len;

        if top <= bottom && bottom < visible_len {
            if top == 0 && bottom == visible_len - 1 {
                // Full screen, no scrollback: O(1)
                self.cells.remove(self.scrollback_len);
                self.cells
                    .push_back(Row::from_cells(vec![fill; self.cols as usize]));
            } else {
                // Partial scroll region
                self.cells.remove(self.scrollback_len + top);
                self.cells.insert(
                    self.scrollback_len + bottom,
                    Row::from_cells(vec![fill; self.cols as usize]),
                );
            }
        }
    }

    /// Scroll the region down by one line, inserting a blank row at the top.
    pub fn scroll_down(&mut self, fill: Cell) {
        let top = self.scroll_top as usize;
        let bottom = self.scroll_bottom as usize;
        let visible_len = self.cells.len() - self.scrollback_len;

        if top <= bottom && bottom < visible_len {
            self.cells.remove(self.scrollback_len + bottom);
            self.cells.insert(
                self.scrollback_len + top,
                Row::from_cells(vec![fill; self.cols as usize]),
            );
        }
        self.check_invariants();
    }

    /// Clear all scrollback rows and reset pending counters.
    pub fn clear_scrollback(&mut self) {
        self.cells.drain(..self.scrollback_len);
        self.scrollback_len = 0;
        self.pending_start = 0;
        self.check_invariants();
    }

    /// Reset scroll region to full screen.
    pub fn reset_scroll_region(&mut self) {
        self.scroll_top = 0;
        self.scroll_bottom = self.rows - 1;
    }

    /// Create a new blank row filled with `fill`, matching grid width.
    pub fn new_blank_row(&self, fill: Cell) -> Row {
        Row::from_cells(vec![fill; self.cols as usize])
    }

    /// Number of scrollback rows that haven't been sent to the client yet.
    pub fn pending_scrollback_count(&self) -> usize {
        self.scrollback_len.saturating_sub(self.pending_start)
    }

    /// Resize the grid, clamping cursor position and resetting scroll region and tab stops.
    /// Only resizes visible rows; scrollback rows keep their original column width.
    pub fn resize(&mut self, cols: u16, rows: u16) {
        let TerminalSize { cols, rows } = sanitize_dimensions(cols, rows);
        self.cols = cols;
        self.rows = rows;
        let rows_usize = rows as usize;
        let visible_len = self.cells.len() - self.scrollback_len;
        if visible_len > rows_usize {
            let excess = visible_len - rows_usize;
            for _ in 0..excess {
                self.cells.pop_back();
            }
        } else if visible_len < rows_usize {
            let deficit = rows_usize - visible_len;
            for _ in 0..deficit {
                self.cells.push_back(Row::new(cols as usize));
            }
        }
        let cols_usize = cols as usize;
        for row in self.cells.iter_mut().skip(self.scrollback_len) {
            row.fix_wide_char_orphan_at_boundary(cols_usize);
            row.resize(cols_usize, Cell::default());
        }
        if self.cursor_x >= cols {
            self.cursor_x = cols - 1;
        }
        if self.cursor_y >= rows {
            self.cursor_y = rows - 1;
        }
        self.wrap_pending = false;
        self.scroll_top = 0;
        self.scroll_bottom = rows - 1;
        self.tab_stops = default_tab_stops(cols);
        self.check_invariants();
    }

    /// Write a cell safely, with automatic wide-char fixup and continuation placement.
    ///
    /// - If overwriting part of a wide char, blanks the other half
    /// - If `cell.width == 2`, places continuation cell at `x+1`
    /// - Clears combining marks at affected positions
    /// - No-op if (x, y) is out of bounds
    pub fn set_cell(&mut self, x: usize, y: usize, cell: Cell) {
        if y >= self.rows as usize || x >= self.cols as usize {
            return;
        }
        self.fixup_wide_char_at(x, y);
        let row = self.visible_row_mut(y);
        row[x] = cell;
        row.clear_combining(x as u16);
        if cell.width == 2 {
            let next = x + 1;
            if next < self.cols as usize {
                self.fixup_wide_char_at(next, y);
                let row = self.visible_row_mut(y);
                row[next] = Cell::new('\0', cell.style_id, 0);
                row.clear_combining(next as u16);
            }
        }
    }

    /// Erase cells in range [from, to) on row y, with wide-char fixup at boundaries.
    pub fn erase_cells(&mut self, y: usize, from: usize, to: usize, blank: Cell) {
        if y >= self.rows as usize {
            return;
        }
        let cols = self.cols as usize;
        let from = from.min(cols);
        let to = to.min(cols);
        if from >= to {
            return;
        }
        self.fixup_wide_char_at(from, y);
        if to < cols {
            self.fixup_wide_char_at(to, y);
        }
        let row = self.visible_row_mut(y);
        for i in from..to {
            row[i] = blank;
        }
        row.clear_combining_range(from as u16, to as u16);
    }

    /// Erase all cells in rows [from_y, to_y) with `blank`.
    pub fn erase_rows(&mut self, from_y: usize, to_y: usize, blank: Cell) {
        let max_y = self.rows as usize;
        let from_y = from_y.min(max_y);
        let to_y = to_y.min(max_y);
        for y in from_y..to_y {
            let row = self.visible_row_mut(y);
            for cell in row.iter_mut() {
                *cell = blank;
            }
            row.clear_all_combining();
        }
    }

    /// Fix up a wide char at position (x, y).
    /// If (x, y) is part of a wide char pair, blanks both halves.
    pub(crate) fn fixup_wide_char_at(&mut self, x: usize, y: usize) {
        if y >= self.rows as usize || x >= self.cols as usize {
            return;
        }
        let cell_width = self.visible_row(y)[x].width;
        if cell_width == 2 {
            let next = x + 1;
            if next < self.cols as usize {
                self.visible_row_mut(y)[next] = Cell::default();
            }
            self.visible_row_mut(y)[x] = Cell::default();
        } else if cell_width == 0 && x > 0 {
            self.visible_row_mut(y)[x - 1] = Cell::default();
            self.visible_row_mut(y)[x] = Cell::default();
        }
    }

    /// Reclaim unused style table slots, scanning this grid and an optional saved grid.
    pub(super) fn compact_styles(&mut self, saved_grid: Option<&SavedGrid>) {
        super::compact_styles(self, saved_grid);
    }

    /// Debug-only invariant check. Call after mutations in debug builds.
    #[cfg(debug_assertions)]
    pub(crate) fn check_invariants(&self) {
        debug_assert!(
            self.pending_start <= self.scrollback_len,
            "pending_start ({}) > scrollback_len ({})",
            self.pending_start,
            self.scrollback_len
        );
        let expected_total = self.scrollback_len + self.rows as usize;
        debug_assert!(
            self.cells.len() == expected_total,
            "cells.len() ({}) != scrollback_len ({}) + rows ({})",
            self.cells.len(),
            self.scrollback_len,
            self.rows
        );
    }

    #[cfg(not(debug_assertions))]
    #[inline(always)]
    pub(crate) fn check_invariants(&self) {}
}

/// Terminal dimensions.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TerminalSize {
    pub cols: u16,
    pub rows: u16,
}

/// Maximum supported terminal dimension (columns or rows).
///
/// Caps the worst-case eager allocation in `Grid::new`/`resize` at
/// 4096*4096 cells (~134 MB) instead of `u16::MAX`^2 (~34 GB) — untrusted
/// resize requests must not be able to exhaust memory.
pub const MAX_DIMENSION: u16 = 4096;

/// Clamp dimensions to `1..=MAX_DIMENSION` on both axes.
///
/// The lower bound prevents arithmetic underflow on `cols-1`/`rows-1`
/// (fix I3); the upper bound caps eager grid allocation (memory-DoS fix).
pub fn sanitize_dimensions(cols: u16, rows: u16) -> TerminalSize {
    TerminalSize {
        cols: cols.clamp(1, MAX_DIMENSION),
        rows: rows.clamp(1, MAX_DIMENSION),
    }
}

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

    #[test]
    fn sanitize_zero_dimensions() {
        assert_eq!(sanitize_dimensions(0, 0), TerminalSize { cols: 1, rows: 1 });
        assert_eq!(
            sanitize_dimensions(80, 0),
            TerminalSize { cols: 80, rows: 1 }
        );
        assert_eq!(
            sanitize_dimensions(0, 24),
            TerminalSize { cols: 1, rows: 24 }
        );
    }

    #[test]
    fn sanitize_caps_at_max_dimension() {
        assert_eq!(
            sanitize_dimensions(u16::MAX, u16::MAX),
            TerminalSize {
                cols: MAX_DIMENSION,
                rows: MAX_DIMENSION
            }
        );
        // At the cap exactly: untouched.
        assert_eq!(
            sanitize_dimensions(MAX_DIMENSION, MAX_DIMENSION),
            TerminalSize {
                cols: MAX_DIMENSION,
                rows: MAX_DIMENSION
            }
        );
        // One past the cap on a single axis.
        assert_eq!(
            sanitize_dimensions(MAX_DIMENSION + 1, 24),
            TerminalSize {
                cols: MAX_DIMENSION,
                rows: 24
            }
        );
    }

    #[test]
    fn resize_to_huge_dimensions_clamps_instead_of_allocating() {
        // Before the cap this allocated ~34 GB and took 15+ seconds.
        let mut grid = Grid::new(80, 24, 0);
        grid.resize(u16::MAX, u16::MAX);
        assert_eq!(grid.cols(), MAX_DIMENSION);
        assert_eq!(grid.rows(), MAX_DIMENSION);
    }

    #[test]
    fn grid_new_creates_correct_size() {
        let grid = Grid::new(80, 24, 0);
        assert_eq!(grid.visible_row_count(), 24);
        assert_eq!(grid.visible_row(0).len(), 80);
    }

    #[test]
    fn grid_new_zero_dimensions() {
        let grid = Grid::new(0, 0, 0);
        assert_eq!(grid.cols(), 1);
        assert_eq!(grid.rows(), 1);
        assert_eq!(grid.visible_row_count(), 1);
        assert_eq!(grid.visible_row(0).len(), 1);
    }

    #[test]
    fn grid_resize() {
        let mut grid = Grid::new(80, 24, 0);
        grid.set_cursor_x_unclamped(79);
        grid.set_cursor_y_unclamped(23);
        grid.resize(40, 12);
        assert_eq!(grid.visible_row_count(), 12);
        assert_eq!(grid.visible_row(0).len(), 40);
        assert_eq!(grid.cursor_x(), 39);
        assert_eq!(grid.cursor_y(), 11);
    }

    #[test]
    fn grid_resize_zero() {
        let mut grid = Grid::new(80, 24, 0);
        grid.resize(0, 0);
        assert_eq!(grid.cols(), 1);
        assert_eq!(grid.rows(), 1);
    }

    #[test]
    fn grid_scroll_up() {
        let mut grid = Grid::new(10, 3, 100);
        grid.visible_row_mut(0)[0].c = 'A';
        grid.scroll_up(false, Cell::default());
        assert_eq!(grid.scrollback_len(), 1);
        // Pending count = scrollback_len - pending_start
        assert_eq!(grid.scrollback_len() - grid.pending_start(), 1);
        assert_eq!(grid.visible_row_count(), 3);
        // Scrollback row should contain 'A'
        assert_eq!(grid.scrollback_row(0)[0].c, 'A');
        // Row 0 should now be what was row 1 (blank)
        assert_eq!(grid.visible_row(0)[0].c, ' ');
    }

    #[test]
    fn grid_scroll_up_alt_screen_no_scrollback() {
        let mut grid = Grid::new(10, 3, 100);
        grid.visible_row_mut(0)[0].c = 'A';
        grid.scroll_up(true, Cell::default());
        assert_eq!(grid.scrollback_len(), 0);
    }

    #[test]
    fn grid_scroll_up_respects_limit() {
        let mut grid = Grid::new(10, 3, 3);
        for _ in 0..5 {
            grid.scroll_up(false, Cell::default());
        }
        assert_eq!(grid.scrollback_len(), 3);
    }

    #[test]
    fn pending_scrollback_respects_limit() {
        let mut grid = Grid::new(10, 3, 5);
        for _ in 0..20 {
            grid.scroll_up(false, Cell::default());
        }
        let pending_count = grid.scrollback_len() - grid.pending_start();
        assert_eq!(
            pending_count, 5,
            "pending scrollback should be exactly at limit, got {}",
            pending_count
        );
    }

    /// scroll_up with partial scroll region (top=0, bottom < last row) and scrollback enabled
    /// should NOT corrupt rows below the scroll region.
    #[test]
    fn scroll_up_partial_region_with_scrollback_preserves_rows_below() {
        // 5 visible rows, scrollback enabled, scroll region = rows 0..2
        let mut grid = Grid::new(5, 5, 100);
        // Label rows: A=0, B=1, C=2, D=3, E=4
        for (r, ch) in ['A', 'B', 'C', 'D', 'E'].iter().enumerate() {
            grid.visible_row_mut(r)[0].c = *ch;
        }
        grid.set_scroll_region(0, 2); // partial: rows 0-2 scroll, rows 3-4 fixed

        grid.scroll_up(false, Cell::default());

        // Row 'A' should go to scrollback
        assert_eq!(grid.scrollback_len(), 1);
        assert_eq!(grid.scrollback_row(0)[0].c, 'A'); // scrollback row

        // Visible: [B, C, blank, D, E]
        assert_eq!(grid.visible_row(0)[0].c, 'B', "row 0 should be B");
        assert_eq!(grid.visible_row(1)[0].c, 'C', "row 1 should be C");
        assert_eq!(grid.visible_row(2)[0].c, ' ', "row 2 should be blank (new)");
        assert_eq!(
            grid.visible_row(3)[0].c,
            'D',
            "row 3 should be D (untouched)"
        );
        assert_eq!(
            grid.visible_row(4)[0].c,
            'E',
            "row 4 should be E (untouched)"
        );

        // Total visible rows should still be 5
        assert_eq!(grid.visible_row_count(), 5);
    }

    /// scroll_up with full scroll region + scrollback should still work (regression guard)
    #[test]
    fn scroll_up_full_region_with_scrollback_still_works() {
        let mut grid = Grid::new(5, 5, 100);
        for (r, ch) in ['A', 'B', 'C', 'D', 'E'].iter().enumerate() {
            grid.visible_row_mut(r)[0].c = *ch;
        }
        // Full screen scroll region (default)
        assert_eq!(grid.scroll_top(), 0);
        assert_eq!(grid.scroll_bottom(), 4);

        grid.scroll_up(false, Cell::default());

        assert_eq!(grid.scrollback_len(), 1);
        assert_eq!(grid.scrollback_row(0)[0].c, 'A');
        assert_eq!(grid.visible_row(0)[0].c, 'B');
        assert_eq!(grid.visible_row(1)[0].c, 'C');
        assert_eq!(grid.visible_row(2)[0].c, 'D');
        assert_eq!(grid.visible_row(3)[0].c, 'E');
        assert_eq!(grid.visible_row(4)[0].c, ' ');
        assert_eq!(grid.visible_row_count(), 5);
    }

    #[test]
    fn terminal_modes_default() {
        let modes = TerminalModes::default();
        assert!(modes.autowrap_mode);
        assert!(!modes.cursor_key_mode);
        assert!(!modes.bracketed_paste);
        assert_eq!(modes.mouse_modes, MouseModes::default());
        assert_eq!(modes.cursor_shape, CursorShape::Default);
    }

    // ---------------------------------------------------------------
    // Helper: paint a checkerboard pattern on the grid using two chars
    // ---------------------------------------------------------------

    /// Fill the grid with a checkerboard pattern: 'A' for even (row+col), 'B' for odd.
    fn paint_checkerboard(grid: &mut Grid) {
        for r in 0..grid.rows() as usize {
            for c in 0..grid.cols() as usize {
                grid.visible_row_mut(r)[c].c = if (r + c) % 2 == 0 { 'A' } else { 'B' };
            }
        }
    }

    /// Assert the checkerboard pattern holds for all cells within (rows x cols).
    fn assert_checkerboard(grid: &Grid, rows: usize, cols: usize) {
        for r in 0..rows {
            for c in 0..cols {
                let expected = if (r + c) % 2 == 0 { 'A' } else { 'B' };
                assert_eq!(
                    grid.visible_row(r)[c].c,
                    expected,
                    "checkerboard mismatch at ({}, {}): expected '{}', got '{}'",
                    r,
                    c,
                    expected,
                    grid.visible_row(r)[c].c
                );
            }
        }
    }

    // ---------------------------------------------------------------
    // Horizontal resize — columns only
    // ---------------------------------------------------------------

    #[test]
    fn resize_horizontal_expand_preserves_content() {
        let mut grid = Grid::new(5, 4, 0);
        paint_checkerboard(&mut grid);
        grid.resize(10, 4); // widen: 5 -> 10 cols, same rows
        assert_eq!(grid.cols(), 10);
        assert_eq!(grid.visible_row(0).len(), 10);
        // Original 5x4 region untouched
        assert_checkerboard(&grid, 4, 5);
        // New columns should be blank
        for r in 0..4 {
            for c in 5..10 {
                assert_eq!(
                    grid.visible_row(r)[c].c,
                    ' ',
                    "new cell at ({}, {}) should be blank",
                    r,
                    c
                );
            }
        }
    }

    #[test]
    fn resize_horizontal_shrink_preserves_visible_content() {
        let mut grid = Grid::new(10, 4, 0);
        paint_checkerboard(&mut grid);
        grid.resize(5, 4); // narrow: 10 -> 5 cols
        assert_eq!(grid.cols(), 5);
        assert_eq!(grid.visible_row(0).len(), 5);
        // First 5 columns of pattern intact
        assert_checkerboard(&grid, 4, 5);
    }

    #[test]
    fn resize_horizontal_shrink_then_expand_loses_truncated() {
        let mut grid = Grid::new(10, 3, 0);
        paint_checkerboard(&mut grid);
        grid.resize(5, 3); // shrink — cols 5..9 lost
        grid.resize(10, 3); // expand back
                            // First 5 cols: pattern intact
        assert_checkerboard(&grid, 3, 5);
        // Cols 5..9: blank (data was truncated, not recoverable)
        for r in 0..3 {
            for c in 5..10 {
                assert_eq!(
                    grid.visible_row(r)[c].c,
                    ' ',
                    "truncated cell at ({}, {}) should be blank after re-expand",
                    r,
                    c
                );
            }
        }
    }

    // ---------------------------------------------------------------
    // Vertical resize — rows only
    // ---------------------------------------------------------------

    #[test]
    fn resize_vertical_expand_preserves_content() {
        let mut grid = Grid::new(6, 3, 0);
        paint_checkerboard(&mut grid);
        grid.resize(6, 8); // taller: 3 -> 8 rows
        assert_eq!(grid.rows(), 8);
        assert_eq!(grid.visible_row_count(), 8);
        // Original 3 rows intact
        assert_checkerboard(&grid, 3, 6);
        // New rows blank
        for r in 3..8 {
            for c in 0..6 {
                assert_eq!(
                    grid.visible_row(r)[c].c,
                    ' ',
                    "new cell at ({}, {}) should be blank",
                    r,
                    c
                );
            }
        }
    }

    #[test]
    fn resize_vertical_shrink_preserves_visible_content() {
        let mut grid = Grid::new(6, 8, 0);
        paint_checkerboard(&mut grid);
        grid.resize(6, 3); // shorter: 8 -> 3 rows
        assert_eq!(grid.rows(), 3);
        assert_eq!(grid.visible_row_count(), 3);
        // First 3 rows of pattern intact
        assert_checkerboard(&grid, 3, 6);
    }

    #[test]
    fn resize_vertical_shrink_then_expand_loses_truncated() {
        let mut grid = Grid::new(6, 8, 0);
        paint_checkerboard(&mut grid);
        grid.resize(6, 3); // rows 3..7 lost
        grid.resize(6, 8); // expand back
        assert_checkerboard(&grid, 3, 6);
        for r in 3..8 {
            for c in 0..6 {
                assert_eq!(
                    grid.visible_row(r)[c].c,
                    ' ',
                    "truncated cell at ({}, {}) should be blank after re-expand",
                    r,
                    c
                );
            }
        }
    }

    // ---------------------------------------------------------------
    // Combined resize — both dimensions at once
    // ---------------------------------------------------------------

    #[test]
    fn resize_both_expand() {
        let mut grid = Grid::new(4, 3, 0);
        paint_checkerboard(&mut grid);
        grid.resize(8, 6); // double both
        assert_checkerboard(&grid, 3, 4);
        // New cols in old rows blank
        for r in 0..3 {
            for c in 4..8 {
                assert_eq!(
                    grid.visible_row(r)[c].c,
                    ' ',
                    "new col cell at ({}, {}) should be blank",
                    r,
                    c
                );
            }
        }
        // New rows entirely blank
        for r in 3..6 {
            for c in 0..8 {
                assert_eq!(
                    grid.visible_row(r)[c].c,
                    ' ',
                    "new row cell at ({}, {}) should be blank",
                    r,
                    c
                );
            }
        }
    }

    #[test]
    fn resize_both_shrink() {
        let mut grid = Grid::new(10, 8, 0);
        paint_checkerboard(&mut grid);
        grid.resize(5, 4); // halve both
        assert_eq!(grid.visible_row_count(), 4);
        assert_eq!(grid.visible_row(0).len(), 5);
        assert_checkerboard(&grid, 4, 5);
    }

    #[test]
    fn resize_expand_cols_shrink_rows() {
        let mut grid = Grid::new(4, 8, 0);
        paint_checkerboard(&mut grid);
        grid.resize(10, 3); // wider but shorter
        assert_eq!(grid.visible_row_count(), 3);
        assert_eq!(grid.visible_row(0).len(), 10);
        // First 3 rows x 4 cols intact
        assert_checkerboard(&grid, 3, 4);
        // New cols in surviving rows blank
        for r in 0..3 {
            for c in 4..10 {
                assert_eq!(
                    grid.visible_row(r)[c].c,
                    ' ',
                    "new cell at ({}, {}) should be blank",
                    r,
                    c
                );
            }
        }
    }

    #[test]
    fn resize_shrink_cols_expand_rows() {
        let mut grid = Grid::new(10, 3, 0);
        paint_checkerboard(&mut grid);
        grid.resize(4, 8); // narrower but taller
        assert_eq!(grid.visible_row_count(), 8);
        assert_eq!(grid.visible_row(0).len(), 4);
        // First 3 rows x 4 cols intact
        assert_checkerboard(&grid, 3, 4);
        // New rows blank
        for r in 3..8 {
            for c in 0..4 {
                assert_eq!(
                    grid.visible_row(r)[c].c,
                    ' ',
                    "new row cell at ({}, {}) should be blank",
                    r,
                    c
                );
            }
        }
    }

    // ---------------------------------------------------------------
    // Multiple sequential resizes — stress pattern preservation
    // ---------------------------------------------------------------

    #[test]
    fn resize_multiple_sequential_preserves_overlap() {
        let mut grid = Grid::new(10, 10, 0);
        paint_checkerboard(&mut grid);
        // Shrink → expand → shrink differently
        grid.resize(5, 5);
        assert_checkerboard(&grid, 5, 5);
        grid.resize(8, 12);
        assert_checkerboard(&grid, 5, 5);
        grid.resize(3, 3);
        assert_checkerboard(&grid, 3, 3);
        grid.resize(20, 20);
        assert_checkerboard(&grid, 3, 3);
    }

    // ---------------------------------------------------------------
    // Resize with cursor in content area
    // ---------------------------------------------------------------

    #[test]
    fn resize_horizontal_shrink_clamps_cursor() {
        let mut grid = Grid::new(10, 5, 0);
        grid.set_cursor_x_unclamped(8);
        grid.set_cursor_y_unclamped(2);
        grid.resize(5, 5);
        assert_eq!(grid.cursor_x(), 4, "cursor_x should clamp to cols-1");
        assert_eq!(grid.cursor_y(), 2, "cursor_y should not change");
    }

    #[test]
    fn resize_vertical_shrink_clamps_cursor() {
        let mut grid = Grid::new(10, 10, 0);
        grid.set_cursor_x_unclamped(3);
        grid.set_cursor_y_unclamped(8);
        grid.resize(10, 5);
        assert_eq!(grid.cursor_x(), 3, "cursor_x should not change");
        assert_eq!(grid.cursor_y(), 4, "cursor_y should clamp to rows-1");
    }

    #[test]
    fn resize_both_shrink_clamps_cursor() {
        let mut grid = Grid::new(20, 20, 0);
        grid.set_cursor_x_unclamped(15);
        grid.set_cursor_y_unclamped(18);
        grid.resize(5, 5);
        assert_eq!(grid.cursor_x(), 4);
        assert_eq!(grid.cursor_y(), 4);
    }

    #[test]
    fn resize_expand_preserves_cursor() {
        let mut grid = Grid::new(10, 10, 0);
        grid.set_cursor_x_unclamped(5);
        grid.set_cursor_y_unclamped(7);
        grid.resize(20, 20);
        assert_eq!(grid.cursor_x(), 5, "cursor_x should not change on expand");
        assert_eq!(grid.cursor_y(), 7, "cursor_y should not change on expand");
    }

    // ---------------------------------------------------------------
    // Resize to same dimensions — no-op semantics
    // ---------------------------------------------------------------

    #[test]
    fn resize_same_dimensions_preserves_everything() {
        let mut grid = Grid::new(8, 6, 0);
        paint_checkerboard(&mut grid);
        grid.set_cursor_x_unclamped(3);
        grid.set_cursor_y_unclamped(2);
        grid.resize(8, 6); // same
        assert_checkerboard(&grid, 6, 8);
        assert_eq!(grid.cursor_x(), 3);
        assert_eq!(grid.cursor_y(), 2);
    }

    // ---------------------------------------------------------------
    // Resize scroll region / tab stops reset
    // ---------------------------------------------------------------

    #[test]
    fn resize_resets_scroll_region() {
        let mut grid = Grid::new(80, 24, 0);
        grid.set_scroll_region(5, 18);
        grid.resize(80, 30);
        assert_eq!(grid.scroll_top(), 0);
        assert_eq!(grid.scroll_bottom(), 29, "scroll_bottom should be rows-1");
    }

    #[test]
    fn resize_resets_tab_stops() {
        let mut grid = Grid::new(80, 24, 0);
        // Manually set a custom tab stop
        grid.set_tab_stop(3);
        grid.resize(40, 24);
        assert_eq!(grid.tab_stops_len(), 40);
        // Tab stops should be default (every 8 cols)
        assert!(!grid.tab_stop_at(0));
        assert!(grid.tab_stop_at(8));
        assert!(grid.tab_stop_at(16));
        assert!(
            !grid.tab_stop_at(3),
            "custom tab stop should be gone after resize"
        );
    }
}

#[cfg(test)]
mod tests_grid_safe_api {
    use super::*;
    use crate::screen::style::StyleId;

    #[test]
    fn set_cell_basic() {
        let mut grid = Grid::new(10, 5, 0);
        let cell = Cell::new('A', StyleId::default(), 1);
        grid.set_cell(3, 0, cell);
        assert_eq!(grid.visible_row(0)[3].c, 'A');
    }

    #[test]
    fn set_cell_wide_char_overwrites_previous() {
        let mut grid = Grid::new(10, 5, 0);
        let wide = Cell::new('\u{6F22}', StyleId::default(), 2);
        grid.set_cell(3, 0, wide);
        assert_eq!(grid.visible_row(0)[3].width, 2);
        assert_eq!(grid.visible_row(0)[4].width, 0);

        let narrow = Cell::new('B', StyleId::default(), 1);
        grid.set_cell(4, 0, narrow);
        assert_eq!(grid.visible_row(0)[3].c, ' ');
        assert_eq!(grid.visible_row(0)[4].c, 'B');
    }

    #[test]
    fn set_cell_wide_char_places_continuation() {
        let mut grid = Grid::new(10, 5, 0);
        let wide = Cell::new('\u{6F22}', StyleId::default(), 2);
        grid.set_cell(2, 0, wide);
        assert_eq!(grid.visible_row(0)[2].c, '\u{6F22}');
        assert_eq!(grid.visible_row(0)[2].width, 2);
        assert_eq!(grid.visible_row(0)[3].c, '\0');
        assert_eq!(grid.visible_row(0)[3].width, 0);
    }

    #[test]
    fn set_cell_out_of_bounds_noop() {
        let mut grid = Grid::new(10, 5, 0);
        let cell = Cell::new('X', StyleId::default(), 1);
        grid.set_cell(10, 0, cell);
        grid.set_cell(0, 5, cell);
    }

    #[test]
    fn erase_cells_basic() {
        let mut grid = Grid::new(10, 5, 0);
        let cell_a = Cell::new('A', StyleId::default(), 1);
        for i in 0..10 {
            grid.visible_row_mut(0)[i] = cell_a;
        }
        grid.erase_cells(0, 3, 7, Cell::default());
        assert_eq!(grid.visible_row(0)[2].c, 'A');
        assert_eq!(grid.visible_row(0)[3].c, ' ');
        assert_eq!(grid.visible_row(0)[6].c, ' ');
        assert_eq!(grid.visible_row(0)[7].c, 'A');
    }

    #[test]
    fn erase_cells_fixes_wide_char_at_boundary() {
        let mut grid = Grid::new(10, 5, 0);
        grid.visible_row_mut(0)[4] = Cell::new('\u{6F22}', StyleId::default(), 2);
        grid.visible_row_mut(0)[5] = Cell::new('\0', StyleId::default(), 0);
        grid.erase_cells(0, 3, 5, Cell::default());
        assert_eq!(grid.visible_row(0)[5].c, ' ');
    }

    #[test]
    fn erase_rows_basic() {
        let mut grid = Grid::new(10, 5, 0);
        let cell_a = Cell::new('A', StyleId::default(), 1);
        for i in 0..10 {
            grid.visible_row_mut(1)[i] = cell_a;
        }
        grid.erase_rows(0, 3, Cell::default());
        assert_eq!(grid.visible_row(0)[0].c, ' ');
        assert_eq!(grid.visible_row(1)[0].c, ' ');
        assert_eq!(grid.visible_row(2)[0].c, ' ');
    }

    #[test]
    fn check_invariants_passes_on_fresh_grid() {
        let grid = Grid::new(80, 24, 100);
        grid.check_invariants();
    }
}