term39 1.5.1

A modern, retro-styled terminal multiplexer with a classic MS-DOS aesthetic
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
use std::collections::VecDeque;
use std::fmt;
use unicode_width::UnicodeWidthChar;

/// Terminal color representation supporting 256-color palette and 24-bit truecolor
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Color {
    /// Default/reset color - uses the terminal's native default
    #[default]
    Default,
    /// Named ANSI colors (0-15)
    Named(NamedColor),
    /// 256-color palette (0-255)
    Indexed(u8),
    /// 24-bit RGB color
    Rgb(u8, u8, u8),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NamedColor {
    Black = 0,
    Red = 1,
    Green = 2,
    Yellow = 3,
    Blue = 4,
    Magenta = 5,
    Cyan = 6,
    White = 7,
    BrightBlack = 8,
    BrightRed = 9,
    BrightGreen = 10,
    BrightYellow = 11,
    BrightBlue = 12,
    BrightMagenta = 13,
    BrightCyan = 14,
    BrightWhite = 15,
}

/// Character cell attributes (bold, italic, underline, etc.)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CellAttributes {
    pub bold: bool,
    pub dim: bool,
    pub italic: bool,
    pub underline: bool,
    pub blink: bool,
    pub reverse: bool,
    pub hidden: bool,
    pub strikethrough: bool,
}

/// A single terminal cell containing a character and its display attributes
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TerminalCell {
    pub c: char,
    pub fg: Color,
    pub bg: Color,
    pub attrs: CellAttributes,
}

impl Default for TerminalCell {
    fn default() -> Self {
        Self {
            c: ' ',
            fg: Color::Default,
            bg: Color::Default,
            attrs: CellAttributes::default(),
        }
    }
}

impl TerminalCell {
    #[allow(dead_code)]
    pub fn reset(&mut self) {
        self.c = ' ';
        self.fg = Color::Default;
        self.bg = Color::Default;
        self.attrs = CellAttributes::default();
    }
}

/// Cursor position and visibility state
#[derive(Debug, Clone, Copy)]
pub struct Cursor {
    pub x: usize,
    pub y: usize,
    pub visible: bool,
    pub shape: CursorShape,
}

/// Saved cursor state (for DECSC/DECRC)
#[derive(Debug, Clone, Copy)]
pub struct SavedCursorState {
    pub cursor: Cursor,
    pub attrs: CellAttributes,
    pub fg: Color,
    pub bg: Color,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CursorShape {
    Block,
    Underline,
    Bar,
}

/// VT100 Character Set designation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CharacterSet {
    /// Standard ASCII character set
    #[default]
    Ascii,
    /// DEC Special Graphics (line drawing characters)
    /// Maps ASCII 0x5f-0x7e to box-drawing characters
    DecSpecialGraphics,
}

impl Default for Cursor {
    fn default() -> Self {
        Self {
            x: 0,
            y: 0,
            visible: true,
            shape: CursorShape::Block,
        }
    }
}

/// Terminal grid with scrollback buffer
pub struct TerminalGrid {
    /// Screen rows (visible portion)
    rows: Vec<Vec<TerminalCell>>,
    /// Scrollback buffer (lines that have scrolled off the top)
    /// Uses VecDeque for O(1) removal from front when exceeding max_scrollback
    scrollback: VecDeque<Vec<TerminalCell>>,
    /// Maximum scrollback lines
    max_scrollback: usize,
    /// Terminal dimensions
    cols: usize,
    rows_count: usize,
    /// Cursor state
    pub cursor: Cursor,
    /// Current cell attributes (for new characters)
    pub current_attrs: CellAttributes,
    pub current_fg: Color,
    pub current_bg: Color,
    /// Scroll region (for CSI scrolling)
    scroll_region_top: usize,
    scroll_region_bottom: usize,
    /// Saved cursor state (for DECSC/DECRC - includes colors and attributes)
    saved_cursor: Option<SavedCursorState>,
    /// Alternate screen buffer
    alt_screen: Option<Vec<Vec<TerminalCell>>>,
    /// Tab stops (every 8 columns by default)
    tab_stops: Vec<bool>,
    /// DEC Private Modes
    /// Application cursor keys mode (DECCKM ?1)
    pub application_cursor_keys: bool,
    /// Bracketed paste mode (?2004)
    pub bracketed_paste_mode: bool,
    /// Focus event reporting (?1004)
    pub focus_event_mode: bool,
    /// Synchronized output mode (?2026)
    pub synchronized_output: bool,
    /// Snapshot of rows when synchronized output began (for rendering during sync mode)
    sync_snapshot: Option<Vec<Vec<TerminalCell>>>,
    /// Snapshot of cursor when synchronized output began
    sync_cursor_snapshot: Option<Cursor>,
    /// Mouse tracking modes
    pub mouse_normal_tracking: bool, // ?1000 - Normal mouse tracking (clicks)
    pub mouse_button_tracking: bool, // ?1002 - Button event tracking
    pub mouse_any_event_tracking: bool, // ?1003 - Any event tracking (all motion)
    pub mouse_utf8_mode: bool,       // ?1005 - UTF-8 mouse encoding
    pub mouse_sgr_mode: bool,        // ?1006 - SGR extended mouse mode
    pub mouse_urxvt_mode: bool,      // ?1015 - URXVT mouse mode
    /// Line Feed/New Line Mode (LNM - mode 20)
    /// When set, LF also performs CR (linefeed acts as newline)
    pub lnm_mode: bool,
    /// Auto-wrap mode (DECAWM - ?7)
    /// When set, characters wrap to next line at end of line
    pub auto_wrap_mode: bool,
    /// Pending wrap flag (for deferred wrap behavior like xterm)
    /// When true, next character will trigger wrap before being printed
    wrap_pending: bool,
    /// Insert/Replace mode (IRM - mode 4)
    /// When set, characters are inserted; when reset, characters replace
    pub insert_mode: bool,
    /// Origin mode (DECOM - ?6)
    /// When set, cursor positioning is relative to scroll region
    pub origin_mode: bool,
    /// Response queue for DSR and other queries that need to send data back
    response_queue: Vec<String>,
    /// G0 character set (selected by ESC ( X)
    pub charset_g0: CharacterSet,
    /// G1 character set (selected by ESC ) X)
    pub charset_g1: CharacterSet,
    /// Active character set: true = G0, false = G1 (toggled by SI/SO)
    pub charset_use_g0: bool,
    /// Generation counter - incremented when grid content changes
    /// Used for render cache invalidation
    generation: u64,
}

impl TerminalGrid {
    pub fn new(cols: usize, rows: usize, max_scrollback: usize) -> Self {
        let mut tab_stops = vec![false; cols];
        for i in (0..cols).step_by(8) {
            tab_stops[i] = true;
        }

        Self {
            rows: vec![vec![TerminalCell::default(); cols]; rows],
            scrollback: VecDeque::new(),
            max_scrollback,
            cols,
            rows_count: rows,
            cursor: Cursor::default(),
            current_attrs: CellAttributes::default(),
            current_fg: Color::Default,
            current_bg: Color::Default,
            scroll_region_top: 0,
            scroll_region_bottom: rows.saturating_sub(1),
            saved_cursor: None,
            alt_screen: None,
            tab_stops,
            application_cursor_keys: false,
            bracketed_paste_mode: false,
            focus_event_mode: false,
            synchronized_output: false,
            sync_snapshot: None,
            sync_cursor_snapshot: None,
            mouse_normal_tracking: false,
            mouse_button_tracking: false,
            mouse_any_event_tracking: false,
            mouse_utf8_mode: false,
            mouse_sgr_mode: false,
            mouse_urxvt_mode: false,
            lnm_mode: false,
            auto_wrap_mode: true, // Default: enabled (xterm behavior)
            wrap_pending: false,  // No pending wrap initially
            insert_mode: false,   // Default: replace mode
            origin_mode: false,   // Default: absolute positioning
            response_queue: Vec::new(),
            charset_g0: CharacterSet::Ascii,
            charset_g1: CharacterSet::Ascii,
            charset_use_g0: true, // Default: use G0
            generation: 0,
        }
    }

    pub fn cols(&self) -> usize {
        self.cols
    }

    pub fn rows(&self) -> usize {
        self.rows_count
    }

    pub fn scroll_region_top(&self) -> usize {
        self.scroll_region_top
    }

    pub fn scroll_region_bottom(&self) -> usize {
        self.scroll_region_bottom
    }

    #[allow(dead_code)]
    pub fn scrollback_len(&self) -> usize {
        self.scrollback.len()
    }

    /// Get the current generation counter
    /// This is incremented whenever grid content changes
    #[allow(dead_code)]
    pub fn generation(&self) -> u64 {
        self.generation
    }

    /// Get the currently active character set
    pub fn active_charset(&self) -> CharacterSet {
        if self.charset_use_g0 {
            self.charset_g0
        } else {
            self.charset_g1
        }
    }

    /// Map a character through the active character set
    /// DEC Special Graphics maps ASCII characters to box-drawing characters
    pub fn map_char(&self, c: char) -> char {
        match self.active_charset() {
            CharacterSet::Ascii => c,
            CharacterSet::DecSpecialGraphics => {
                // DEC Special Graphics character mapping
                // Maps ASCII 0x5f-0x7e to box-drawing characters
                match c {
                    '_' => ' ', // 0x5f -> blank
                    '`' => '', // 0x60 -> diamond
                    'a' => '', // 0x61 -> checkerboard
                    'b' => '', // 0x62 -> HT symbol
                    'c' => '', // 0x63 -> FF symbol
                    'd' => '', // 0x64 -> CR symbol
                    'e' => '', // 0x65 -> LF symbol
                    'f' => '°', // 0x66 -> degree symbol
                    'g' => '±', // 0x67 -> plus/minus
                    'h' => '', // 0x68 -> NL symbol
                    'i' => '', // 0x69 -> VT symbol
                    'j' => '', // 0x6a -> lower right corner
                    'k' => '', // 0x6b -> upper right corner
                    'l' => '', // 0x6c -> upper left corner
                    'm' => '', // 0x6d -> lower left corner
                    'n' => '', // 0x6e -> crossing lines
                    'o' => '', // 0x6f -> horizontal line - scan 1
                    'p' => '', // 0x70 -> horizontal line - scan 3
                    'q' => '', // 0x71 -> horizontal line - scan 5
                    'r' => '', // 0x72 -> horizontal line - scan 7
                    's' => '', // 0x73 -> horizontal line - scan 9
                    't' => '', // 0x74 -> left tee
                    'u' => '', // 0x75 -> right tee
                    'v' => '', // 0x76 -> bottom tee
                    'w' => '', // 0x77 -> top tee
                    'x' => '', // 0x78 -> vertical line
                    'y' => '', // 0x79 -> less than or equal
                    'z' => '', // 0x7a -> greater than or equal
                    '{' => 'π', // 0x7b -> pi
                    '|' => '', // 0x7c -> not equal
                    '}' => '£', // 0x7d -> UK pound
                    '~' => '·', // 0x7e -> bullet/centered dot
                    _ => c,     // Other characters pass through unchanged
                }
            }
        }
    }

    /// Set G0 character set (ESC ( X)
    pub fn set_charset_g0(&mut self, charset: CharacterSet) {
        self.charset_g0 = charset;
    }

    /// Set G1 character set (ESC ) X)
    pub fn set_charset_g1(&mut self, charset: CharacterSet) {
        self.charset_g1 = charset;
    }

    /// Shift In (SI / Ctrl+O / 0x0F) - Select G0 character set
    pub fn shift_in(&mut self) {
        self.charset_use_g0 = true;
    }

    /// Shift Out (SO / Ctrl+N / 0x0E) - Select G1 character set
    pub fn shift_out(&mut self) {
        self.charset_use_g0 = false;
    }

    /// Queue a response to be sent back to the PTY (for DSR and other queries)
    pub fn queue_response(&mut self, response: String) {
        self.response_queue.push(response);
    }

    /// Take all queued responses (drains the queue)
    pub fn take_responses(&mut self) -> Vec<String> {
        std::mem::take(&mut self.response_queue)
    }

    /// Queue cursor position report (DSR response to CSI 6 n)
    /// Format: CSI row ; col R (1-based coordinates)
    /// When origin mode (DECOM) is set, reports position relative to scroll region
    pub fn queue_cursor_position_report(&mut self) {
        let (row, col) = if self.origin_mode {
            // In origin mode, report position relative to scroll region
            let row = (self.cursor.y.saturating_sub(self.scroll_region_top)) + 1;
            let col = self.cursor.x + 1;
            (row, col)
        } else {
            // Normal mode, report absolute position
            let row = self.cursor.y + 1;
            let col = self.cursor.x + 1;
            (row, col)
        };
        let response = format!("\x1b[{};{}R", row, col);
        self.queue_response(response);
    }

    /// Resize the terminal grid
    pub fn resize(&mut self, new_cols: usize, new_rows: usize) {
        // Resize existing rows
        for row in &mut self.rows {
            row.resize(new_cols, TerminalCell::default());
        }

        // Add or remove rows
        match new_rows.cmp(&self.rows_count) {
            std::cmp::Ordering::Greater => {
                self.rows
                    .resize(new_rows, vec![TerminalCell::default(); new_cols]);
            }
            std::cmp::Ordering::Less => {
                self.rows.truncate(new_rows);
            }
            std::cmp::Ordering::Equal => {}
        }

        // Also resize the saved alt_screen buffer if in alternate screen mode
        if let Some(alt_screen) = &mut self.alt_screen {
            for row in alt_screen.iter_mut() {
                row.resize(new_cols, TerminalCell::default());
            }
            match new_rows.cmp(&alt_screen.len()) {
                std::cmp::Ordering::Greater => {
                    alt_screen.resize(new_rows, vec![TerminalCell::default(); new_cols]);
                }
                std::cmp::Ordering::Less => {
                    alt_screen.truncate(new_rows);
                }
                std::cmp::Ordering::Equal => {}
            }
        }

        // Update tab stops
        self.tab_stops.resize(new_cols, false);
        for i in (0..new_cols).step_by(8) {
            self.tab_stops[i] = true;
        }

        self.cols = new_cols;
        self.rows_count = new_rows;
        self.scroll_region_bottom = new_rows.saturating_sub(1);

        // Clamp cursor to new bounds
        self.cursor.x = self.cursor.x.min(new_cols.saturating_sub(1));
        self.cursor.y = self.cursor.y.min(new_rows.saturating_sub(1));
    }

    /// Get a cell at the given position (returns None if out of bounds)
    /// This returns the live cell, use get_render_cell for rendering during synchronized output
    pub fn get_cell(&self, x: usize, y: usize) -> Option<&TerminalCell> {
        self.rows.get(y)?.get(x)
    }

    /// Get a cell for rendering - respects synchronized output snapshot
    /// During synchronized output mode, returns the snapshot cell to prevent visual tearing
    pub fn get_render_cell(&self, x: usize, y: usize) -> Option<&TerminalCell> {
        if let Some(snapshot) = &self.sync_snapshot {
            snapshot.get(y)?.get(x)
        } else {
            self.rows.get(y)?.get(x)
        }
    }

    /// Get cursor for rendering - respects synchronized output snapshot
    pub fn get_render_cursor(&self) -> &Cursor {
        if let Some(cursor) = &self.sync_cursor_snapshot {
            cursor
        } else {
            &self.cursor
        }
    }

    /// Get a mutable cell at the given position
    pub fn get_cell_mut(&mut self, x: usize, y: usize) -> Option<&mut TerminalCell> {
        self.rows.get_mut(y)?.get_mut(x)
    }

    /// Get a line from scrollback (0 = oldest)
    #[allow(dead_code)]
    pub fn get_scrollback_line(&self, idx: usize) -> Option<&Vec<TerminalCell>> {
        self.scrollback.get(idx)
    }

    /// Begin synchronized output mode - takes a snapshot of current state for rendering
    pub fn begin_synchronized_output(&mut self) {
        if !self.synchronized_output {
            self.synchronized_output = true;
            // Take snapshot of current state for rendering during sync mode
            self.sync_snapshot = Some(self.rows.clone());
            self.sync_cursor_snapshot = Some(self.cursor);
        }
    }

    /// End synchronized output mode - clears snapshot to allow live rendering
    pub fn end_synchronized_output(&mut self) {
        self.synchronized_output = false;
        self.sync_snapshot = None;
        self.sync_cursor_snapshot = None;
        // Increment generation when sync mode ends to trigger re-render with live data
        self.generation = self.generation.wrapping_add(1);
    }

    /// Write a character at the current cursor position
    pub fn put_char(&mut self, c: char) {
        if self.cursor.y >= self.rows_count {
            return;
        }

        match c {
            '\n' => self.linefeed(),
            '\r' => self.carriage_return(),
            '\t' => self.tab(),
            '\x08' => self.backspace(),
            c if c.is_control() => {
                // Ignore other control characters
            }
            c => {
                // Map character through active character set (for line drawing, etc.)
                let c = self.map_char(c);

                // Get character width (0 for combining marks, 1 for normal, 2 for wide/fullwidth)
                // Fast path: ASCII characters are always width 1 (avoiding Unicode table lookup)
                let char_width = if c.is_ascii() {
                    1
                } else {
                    c.width().unwrap_or(0)
                };

                // Skip zero-width characters (combining marks, etc.) - they don't advance cursor
                // but we should still render them (TODO: proper combining char support)
                if char_width == 0 {
                    return;
                }

                // Handle pending wrap (deferred wrap like xterm)
                // When a character was written to the last column, wrap is deferred until
                // the next printable character, allowing exact-width lines without extra wrap
                if self.wrap_pending && self.auto_wrap_mode {
                    self.wrap_pending = false;
                    if self.cursor.y == self.rows_count - 1 {
                        // At last row, scroll up
                        self.scroll_up(1);
                        self.cursor.x = 0;
                    } else {
                        self.cursor.x = 0;
                        self.cursor.y += 1;
                    }
                }

                // Copy values before mutable borrow
                let fg = self.current_fg;
                let bg = self.current_bg;
                let attrs = self.current_attrs;

                // Handle wide characters (width = 2)
                if char_width == 2 {
                    // Check if we have room for a wide character
                    if self.cursor.x + 1 >= self.cols {
                        // Not enough room on this line for wide char
                        // Either wrap to next line or stay at end
                        if self.auto_wrap_mode {
                            if self.cursor.y == self.rows_count - 1 {
                                // At last row, scroll and wrap
                                self.scroll_up(1);
                                self.cursor.x = 0;
                            } else {
                                // Wrap to next line
                                self.cursor.x = 0;
                                self.cursor.y += 1;
                            }
                        } else {
                            // No wrap mode, skip the character
                            return;
                        }
                    }

                    // Write the wide character to first cell
                    if let Some(cell) = self.get_cell_mut(self.cursor.x, self.cursor.y) {
                        cell.c = c;
                        cell.fg = fg;
                        cell.bg = bg;
                        cell.attrs = attrs;
                    }

                    // Write a placeholder space to second cell (for wide char continuation)
                    if let Some(cell) = self.get_cell_mut(self.cursor.x + 1, self.cursor.y) {
                        cell.c = ' ';
                        cell.fg = fg;
                        cell.bg = bg;
                        cell.attrs = attrs;
                    }

                    self.cursor.x += 2;

                    // Check if we're now at or past end of line
                    if self.cursor.x >= self.cols {
                        if self.auto_wrap_mode {
                            // Set pending wrap instead of wrapping immediately
                            self.cursor.x = self.cols - 1;
                            self.wrap_pending = true;
                        } else {
                            self.cursor.x = self.cols - 1;
                        }
                    }
                } else {
                    // Normal width character
                    if self.cursor.x < self.cols {
                        if let Some(cell) = self.get_cell_mut(self.cursor.x, self.cursor.y) {
                            cell.c = c;
                            cell.fg = fg;
                            cell.bg = bg;
                            cell.attrs = attrs;
                        }
                        self.cursor.x += 1;

                        // Check if we've reached the end of line
                        if self.cursor.x >= self.cols {
                            if self.auto_wrap_mode {
                                // Set pending wrap instead of wrapping immediately (deferred wrap)
                                self.cursor.x = self.cols - 1;
                                self.wrap_pending = true;
                            } else {
                                self.cursor.x = self.cols - 1;
                            }
                        }
                    }
                }

                // Increment generation when a printable character is written
                // Uses wrapping to avoid overflow panic
                self.generation = self.generation.wrapping_add(1);
            }
        }
    }

    /// Move cursor to the next line, scrolling if necessary
    /// If LNM (Line Feed/New Line Mode) is set, also performs carriage return
    fn linefeed(&mut self) {
        // Clear pending wrap - explicit cursor movement cancels deferred wrap
        self.wrap_pending = false;

        // If LNM is set, linefeed also performs carriage return
        if self.lnm_mode {
            self.cursor.x = 0;
        }

        if self.cursor.y == self.scroll_region_bottom {
            // At bottom of scroll region - always scroll
            // Synchronized output only affects when changes are rendered to screen,
            // not the underlying terminal state
            self.scroll_up(1);
        } else if self.cursor.y < self.rows_count - 1 {
            self.cursor.y += 1;
        }
    }

    /// Move cursor to start of line
    fn carriage_return(&mut self) {
        // Clear pending wrap - explicit cursor movement cancels deferred wrap
        self.wrap_pending = false;
        self.cursor.x = 0;
    }

    /// Reverse linefeed - move cursor up one line, scrolling down if at top of scroll region
    pub fn reverse_linefeed(&mut self) {
        if self.cursor.y == self.scroll_region_top {
            // At top of scroll region, scroll down
            self.scroll_down(1);
        } else if self.cursor.y > 0 {
            self.cursor.y -= 1;
        }
    }

    /// Next line - carriage return + linefeed
    pub fn next_line(&mut self) {
        self.carriage_return();
        self.linefeed();
    }

    /// Reset terminal to initial state
    pub fn reset(&mut self) {
        // Clear screen
        self.clear_screen();

        // Reset cursor
        self.cursor = Cursor::default();

        // Reset attributes
        self.current_attrs = CellAttributes::default();
        self.current_fg = Color::Default;
        self.current_bg = Color::Default;

        // Reset scroll region
        self.scroll_region_top = 0;
        self.scroll_region_bottom = self.rows_count.saturating_sub(1);

        // Clear saved cursor
        self.saved_cursor = None;

        // Clear alt screen
        self.alt_screen = None;

        // Reset scrollback
        self.scrollback.clear();

        // Reset DEC private modes
        self.application_cursor_keys = false;
        self.bracketed_paste_mode = false;
        self.focus_event_mode = false;
        self.synchronized_output = false;
        self.sync_snapshot = None;
        self.sync_cursor_snapshot = None;
        self.mouse_normal_tracking = false;
        self.mouse_button_tracking = false;
        self.mouse_any_event_tracking = false;
        self.mouse_utf8_mode = false;
        self.mouse_sgr_mode = false;
        self.mouse_urxvt_mode = false;
        self.lnm_mode = false;
        self.auto_wrap_mode = true;
        self.wrap_pending = false;
        self.insert_mode = false;
        self.origin_mode = false;

        // Reset character sets
        self.charset_g0 = CharacterSet::Ascii;
        self.charset_g1 = CharacterSet::Ascii;
        self.charset_use_g0 = true;

        // Clear response queue
        self.response_queue.clear();
    }

    /// Move cursor to next tab stop
    fn tab(&mut self) {
        // Clear pending wrap - tab movement cancels deferred wrap
        self.wrap_pending = false;
        for x in (self.cursor.x + 1)..self.cols {
            if self.tab_stops[x] {
                self.cursor.x = x;
                return;
            }
        }
        self.cursor.x = self.cols.saturating_sub(1);
    }

    /// Move cursor back one position
    fn backspace(&mut self) {
        // Clear pending wrap - backspace cancels deferred wrap
        self.wrap_pending = false;
        if self.cursor.x > 0 {
            self.cursor.x -= 1;
        }
    }

    /// Scroll the scroll region up by n lines
    pub fn scroll_up(&mut self, n: usize) {
        for _ in 0..n {
            // Remove top line of scroll region
            if self.scroll_region_top < self.rows_count {
                let line = self.rows.remove(self.scroll_region_top);

                // Only add to scrollback if NOT in alternate screen
                if self.alt_screen.is_none() {
                    self.scrollback.push_back(line);

                    // Limit scrollback size (O(1) with VecDeque)
                    if self.scrollback.len() > self.max_scrollback {
                        self.scrollback.pop_front();
                    }
                }

                // Insert blank line at bottom of scroll region
                let insert_pos = self.scroll_region_bottom.min(self.rows_count - 1);
                self.rows
                    .insert(insert_pos, vec![TerminalCell::default(); self.cols]);
            }
        }
        self.generation = self.generation.wrapping_add(1);
    }

    /// Scroll the scroll region down by n lines
    pub fn scroll_down(&mut self, n: usize) {
        for _ in 0..n {
            // Remove line at bottom of scroll region
            if self.scroll_region_bottom < self.rows_count {
                self.rows.remove(self.scroll_region_bottom);

                // Insert blank line at top of scroll region
                self.rows.insert(
                    self.scroll_region_top,
                    vec![TerminalCell::default(); self.cols],
                );
            }
        }
        self.generation = self.generation.wrapping_add(1);
    }

    /// Clear the screen
    pub fn clear_screen(&mut self) {
        let bg = self.current_bg;
        for row in &mut self.rows {
            for cell in row {
                cell.c = ' ';
                cell.fg = Color::Named(NamedColor::White);
                cell.bg = bg;
                cell.attrs = CellAttributes::default();
            }
        }
        self.generation = self.generation.wrapping_add(1);
    }

    /// Clear the current line
    pub fn clear_line(&mut self) {
        let bg = self.current_bg;
        if let Some(row) = self.rows.get_mut(self.cursor.y) {
            for cell in row {
                cell.c = ' ';
                cell.fg = Color::Named(NamedColor::White);
                cell.bg = bg;
                cell.attrs = CellAttributes::default();
            }
            self.generation = self.generation.wrapping_add(1);
        }
    }

    /// Erase from cursor to end of line
    pub fn erase_to_eol(&mut self) {
        let bg = self.current_bg;
        if let Some(row) = self.rows.get_mut(self.cursor.y) {
            for x in self.cursor.x..self.cols {
                if let Some(cell) = row.get_mut(x) {
                    cell.c = ' ';
                    cell.fg = Color::Named(NamedColor::White);
                    cell.bg = bg;
                    cell.attrs = CellAttributes::default();
                }
            }
            self.generation = self.generation.wrapping_add(1);
        }
    }

    /// Erase from beginning of line to cursor (inclusive)
    pub fn erase_to_bol(&mut self) {
        let bg = self.current_bg;
        if let Some(row) = self.rows.get_mut(self.cursor.y) {
            for x in 0..=self.cursor.x {
                if let Some(cell) = row.get_mut(x) {
                    cell.c = ' ';
                    cell.fg = Color::Named(NamedColor::White);
                    cell.bg = bg;
                    cell.attrs = CellAttributes::default();
                }
            }
            self.generation = self.generation.wrapping_add(1);
        }
    }

    /// Erase from cursor to end of screen
    pub fn erase_to_eos(&mut self) {
        // Clear rest of current line
        self.erase_to_eol();

        // Clear all lines below
        let bg = self.current_bg;
        for y in (self.cursor.y + 1)..self.rows_count {
            if let Some(row) = self.rows.get_mut(y) {
                for cell in row {
                    cell.c = ' ';
                    cell.fg = Color::Named(NamedColor::White);
                    cell.bg = bg;
                    cell.attrs = CellAttributes::default();
                }
            }
        }
        self.generation = self.generation.wrapping_add(1);
    }

    /// Erase from beginning of screen to cursor (inclusive)
    pub fn erase_from_bos(&mut self) {
        let bg = self.current_bg;

        // Clear all lines above cursor
        for y in 0..self.cursor.y {
            if let Some(row) = self.rows.get_mut(y) {
                for cell in row {
                    cell.c = ' ';
                    cell.fg = Color::Named(NamedColor::White);
                    cell.bg = bg;
                    cell.attrs = CellAttributes::default();
                }
            }
        }

        // Clear from beginning of current line to cursor (inclusive)
        self.erase_to_bol();
        self.generation = self.generation.wrapping_add(1);
    }

    /// Delete n characters at cursor, shifting remaining characters left (DCH)
    pub fn delete_chars(&mut self, n: usize) {
        if let Some(row) = self.rows.get_mut(self.cursor.y) {
            let start = self.cursor.x;
            let end = self.cols;
            let n = n.min(end.saturating_sub(start));

            // Shift characters left
            for x in start..(end - n) {
                if let Some(src_cell) = row.get(x + n).cloned() {
                    if let Some(cell) = row.get_mut(x) {
                        *cell = src_cell;
                    }
                }
            }

            // Fill vacated positions with blanks
            let bg = self.current_bg;
            for x in (end - n)..end {
                if let Some(cell) = row.get_mut(x) {
                    cell.c = ' ';
                    cell.fg = Color::Named(NamedColor::White);
                    cell.bg = bg;
                    cell.attrs = CellAttributes::default();
                }
            }
            self.generation = self.generation.wrapping_add(1);
        }
    }

    /// Insert n blank characters at cursor, shifting existing characters right (ICH)
    pub fn insert_chars(&mut self, n: usize) {
        if let Some(row) = self.rows.get_mut(self.cursor.y) {
            let start = self.cursor.x;
            let end = self.cols;
            let n = n.min(end.saturating_sub(start));

            // Shift characters right (from end to start to avoid overwriting)
            for x in (start + n..end).rev() {
                if let Some(src_cell) = row.get(x - n).cloned() {
                    if let Some(cell) = row.get_mut(x) {
                        *cell = src_cell;
                    }
                }
            }

            // Fill inserted positions with blanks
            let bg = self.current_bg;
            for x in start..(start + n).min(end) {
                if let Some(cell) = row.get_mut(x) {
                    cell.c = ' ';
                    cell.fg = Color::Named(NamedColor::White);
                    cell.bg = bg;
                    cell.attrs = CellAttributes::default();
                }
            }
            self.generation = self.generation.wrapping_add(1);
        }
    }

    /// Erase n characters at cursor without moving cursor (ECH)
    pub fn erase_chars(&mut self, n: usize) {
        let bg = self.current_bg;
        if let Some(row) = self.rows.get_mut(self.cursor.y) {
            for x in self.cursor.x..(self.cursor.x + n).min(self.cols) {
                if let Some(cell) = row.get_mut(x) {
                    cell.c = ' ';
                    cell.fg = Color::Named(NamedColor::White);
                    cell.bg = bg;
                    cell.attrs = CellAttributes::default();
                }
            }
            self.generation = self.generation.wrapping_add(1);
        }
    }

    /// Move cursor to absolute position (0-indexed)
    pub fn goto(&mut self, x: usize, y: usize) {
        // Clear pending wrap - explicit cursor movement cancels deferred wrap
        self.wrap_pending = false;
        self.cursor.x = x.min(self.cols.saturating_sub(1));
        self.cursor.y = y.min(self.rows_count.saturating_sub(1));
    }

    /// Move cursor with origin mode awareness (for CSI H and similar)
    /// When origin mode (DECOM) is set, positions are relative to scroll region
    /// and cursor is constrained to scroll region
    pub fn goto_origin_aware(&mut self, x: usize, y: usize) {
        // Clear pending wrap - explicit cursor movement cancels deferred wrap
        self.wrap_pending = false;
        if self.origin_mode {
            // In origin mode, positions are relative to scroll region
            let actual_y = (self.scroll_region_top + y).min(self.scroll_region_bottom);
            self.cursor.x = x.min(self.cols.saturating_sub(1));
            self.cursor.y = actual_y;
        } else {
            // Normal mode, absolute positioning
            self.goto(x, y);
        }
    }

    /// Move cursor relatively
    pub fn move_cursor(&mut self, dx: isize, dy: isize) {
        // Clear pending wrap - explicit cursor movement cancels deferred wrap
        self.wrap_pending = false;
        let new_x = (self.cursor.x as isize + dx).max(0) as usize;
        let new_y = (self.cursor.y as isize + dy).max(0) as usize;
        // Note: goto() also clears wrap_pending but that's fine
        self.goto(new_x, new_y);
    }

    /// Set origin mode (DECOM - CSI ?6h)
    /// Per VT100 spec: When origin mode is set, cursor moves to home (top of scroll region)
    pub fn set_origin_mode(&mut self, enabled: bool) {
        self.origin_mode = enabled;
        // Per VT100 spec: cursor moves to home when origin mode changes
        if enabled {
            // In origin mode, home is top-left of scroll region
            self.cursor.x = 0;
            self.cursor.y = self.scroll_region_top;
        } else {
            // In normal mode, home is top-left of screen
            self.cursor.x = 0;
            self.cursor.y = 0;
        }
    }

    /// Save cursor position only (CSI s - SCP)
    pub fn save_cursor_position(&mut self) {
        self.saved_cursor = Some(SavedCursorState {
            cursor: self.cursor,
            attrs: self.current_attrs,
            fg: self.current_fg,
            bg: self.current_bg,
        });
    }

    /// Restore cursor position only (CSI u - RCP)
    pub fn restore_cursor_position(&mut self) {
        if let Some(saved) = self.saved_cursor {
            self.cursor = saved.cursor;
            // Don't restore colors/attrs for CSI u
        }
    }

    /// Save cursor position and attributes (DECSC - ESC 7)
    pub fn save_cursor(&mut self) {
        self.saved_cursor = Some(SavedCursorState {
            cursor: self.cursor,
            attrs: self.current_attrs,
            fg: self.current_fg,
            bg: self.current_bg,
        });
    }

    /// Restore cursor position and attributes (DECRC - ESC 8)
    pub fn restore_cursor(&mut self) {
        if let Some(saved) = self.saved_cursor {
            self.cursor = saved.cursor;
            self.current_attrs = saved.attrs;
            self.current_fg = saved.fg;
            self.current_bg = saved.bg;
        }
    }

    /// Switch to alternate screen buffer
    pub fn use_alt_screen(&mut self) {
        if self.alt_screen.is_none() {
            // Save cursor position (part of DECSC/DECRC behavior with alt screen)
            self.save_cursor();
            // Save current screen
            self.alt_screen = Some(self.rows.clone());
            // Clear current screen
            self.clear_screen();
            // Reset cursor to home position
            self.cursor = Cursor::default();
            self.generation = self.generation.wrapping_add(1);
        }
    }

    /// Switch back to main screen buffer
    pub fn use_main_screen(&mut self) {
        if let Some(mut main_screen) = self.alt_screen.take() {
            // The terminal may have been resized while in alternate screen.
            // Resize the saved main screen to match current dimensions.
            let current_cols = self.cols;
            let current_rows = self.rows_count;

            // Resize columns in existing rows
            for row in &mut main_screen {
                row.resize(current_cols, TerminalCell::default());
            }

            // Add or remove rows to match current size
            match current_rows.cmp(&main_screen.len()) {
                std::cmp::Ordering::Greater => {
                    main_screen.resize(current_rows, vec![TerminalCell::default(); current_cols]);
                }
                std::cmp::Ordering::Less => {
                    main_screen.truncate(current_rows);
                }
                std::cmp::Ordering::Equal => {}
            }

            self.rows = main_screen;
            // Restore cursor position (part of DECSC/DECRC behavior with alt screen)
            self.restore_cursor();

            // Clamp cursor to current bounds after restore
            self.cursor.x = self.cursor.x.min(current_cols.saturating_sub(1));
            self.cursor.y = self.cursor.y.min(current_rows.saturating_sub(1));

            // Reset scroll region to full screen (alt screen may have set custom regions)
            self.scroll_region_top = 0;
            self.scroll_region_bottom = current_rows.saturating_sub(1);

            self.generation = self.generation.wrapping_add(1);
        }
    }

    /// Set scroll region (DECSTBM)
    /// Per VT100/VT510 spec: After setting margins, cursor moves to home position
    /// In origin mode: home is top of scroll region; otherwise: top-left of screen
    pub fn set_scroll_region(&mut self, top: usize, bottom: usize) {
        // Validate: top must be less than bottom, minimum 2 lines
        let top = top.min(self.rows_count.saturating_sub(2));
        let bottom = bottom.min(self.rows_count.saturating_sub(1));

        if top < bottom {
            self.scroll_region_top = top;
            self.scroll_region_bottom = bottom;
        } else {
            // Invalid region, reset to full screen
            self.scroll_region_top = 0;
            self.scroll_region_bottom = self.rows_count.saturating_sub(1);
        }

        // Per VT510 spec: DECSTBM moves cursor to home position
        // In origin mode, home is column 1, line 1 of scroll region
        // In normal mode, home is column 1, line 1 of screen (0,0)
        if self.origin_mode {
            self.cursor.x = 0;
            self.cursor.y = self.scroll_region_top;
        } else {
            self.cursor.x = 0;
            self.cursor.y = 0;
        }
    }

    /// Reset scroll region to full screen
    #[allow(dead_code)]
    pub fn reset_scroll_region(&mut self) {
        self.scroll_region_top = 0;
        self.scroll_region_bottom = self.rows_count.saturating_sub(1);
    }

    /// Restore terminal content from saved lines (for session restoration)
    /// This replaces the scrollback and visible screen with the saved content
    pub fn restore_content(&mut self, lines: Vec<Vec<TerminalCell>>) {
        if lines.is_empty() {
            return;
        }

        // Clear current content
        self.scrollback.clear();
        self.rows.clear();

        // Split lines into scrollback and visible screen
        let total_lines = lines.len();
        if total_lines <= self.rows_count {
            // All lines fit in visible screen, no scrollback
            self.rows = lines;
            // Pad with empty lines if needed
            while self.rows.len() < self.rows_count {
                self.rows.push(vec![TerminalCell::default(); self.cols]);
            }
        } else {
            // Some lines go into scrollback
            let scrollback_count = total_lines - self.rows_count;
            self.scrollback = lines[..scrollback_count].to_vec().into();
            self.rows = lines[scrollback_count..].to_vec();
        }

        // Ensure rows match the current column count
        for row in &mut self.rows {
            row.resize(self.cols, TerminalCell::default());
        }
        for row in &mut self.scrollback {
            row.resize(self.cols, TerminalCell::default());
        }

        self.generation = self.generation.wrapping_add(1);
    }

    /// Set cursor position (for session restoration)
    pub fn set_cursor(&mut self, x: usize, y: usize, visible: bool) {
        self.cursor.x = x.min(self.cols.saturating_sub(1));
        self.cursor.y = y.min(self.rows_count.saturating_sub(1));
        self.cursor.visible = visible;
    }
}

impl fmt::Debug for TerminalGrid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TerminalGrid")
            .field("cols", &self.cols)
            .field("rows", &self.rows_count)
            .field("scrollback_lines", &self.scrollback.len())
            .field("cursor", &self.cursor)
            .finish()
    }
}