opentui_rust 0.2.1

High-performance terminal UI rendering engine with alpha blending and diffed buffers
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
//! Buffered ANSI output writer with state tracking.

use crate::ansi::{self, ColorMode};
use crate::cell::Cell;
use crate::color::Rgba;
use crate::grapheme_pool::GraphemePool;
use crate::style::TextAttributes;
use std::io::{self, Write};

/// Buffered writer that tracks ANSI state to minimize escape sequences.
pub struct AnsiWriter<W: Write> {
    writer: W,
    buffer: Vec<u8>,

    // Color output mode
    color_mode: ColorMode,

    // Current state for delta encoding
    current_fg: Option<Rgba>,
    current_bg: Option<Rgba>,
    current_attrs: TextAttributes,
    current_link: Option<u32>,

    // Cursor position
    cursor_row: u32,
    cursor_col: u32,
}

impl<W: Write> AnsiWriter<W> {
    /// Create a new ANSI writer wrapping the given output.
    pub fn new(writer: W) -> Self {
        Self {
            writer,
            buffer: Vec::with_capacity(8192),
            color_mode: ColorMode::TrueColor,
            current_fg: None,
            current_bg: None,
            current_attrs: TextAttributes::empty(),
            current_link: None,
            cursor_row: 0,
            cursor_col: 0,
        }
    }

    /// Create a new ANSI writer with specified color mode.
    pub fn with_color_mode(writer: W, color_mode: ColorMode) -> Self {
        Self {
            writer,
            buffer: Vec::with_capacity(8192),
            color_mode,
            current_fg: None,
            current_bg: None,
            current_attrs: TextAttributes::empty(),
            current_link: None,
            cursor_row: 0,
            cursor_col: 0,
        }
    }

    /// Set the color output mode.
    pub fn set_color_mode(&mut self, mode: ColorMode) {
        self.color_mode = mode;
    }

    /// Get the current color output mode.
    #[must_use]
    pub fn color_mode(&self) -> ColorMode {
        self.color_mode
    }

    /// Reset all state tracking.
    pub fn reset_state(&mut self) {
        self.current_fg = None;
        self.current_bg = None;
        self.current_attrs = TextAttributes::empty();
        self.current_link = None;
        self.cursor_row = 0;
        self.cursor_col = 0;
    }

    /// Write raw bytes to the buffer.
    pub fn write_raw(&mut self, data: &[u8]) {
        self.buffer.extend_from_slice(data);
    }

    /// Write a raw string to the buffer.
    pub fn write_str(&mut self, s: &str) {
        self.buffer.extend_from_slice(s.as_bytes());
    }

    /// Move cursor to position, using relative moves if more efficient.
    pub fn move_cursor(&mut self, row: u32, col: u32) {
        if row == self.cursor_row && col == self.cursor_col {
            return;
        }

        let dy = row as i32 - self.cursor_row as i32;
        let dx = col as i32 - self.cursor_col as i32;

        // Calculate cost of absolute vs relative move
        // ESC[r;cH = 1+1+digits(r)+1+digits(c)+1 = 4 + digits
        let abs_cost = 4 + digits(row + 1) + digits(col + 1);
        let rel_cost = if dy != 0 {
            3 + digits(dy.unsigned_abs())
        } else {
            0
        } + if dx != 0 {
            3 + digits(dx.unsigned_abs())
        } else {
            0
        };

        if rel_cost < abs_cost && (dy != 0 || dx != 0) {
            let _ = ansi::write_cursor_move(&mut self.buffer, dx, dy);
        } else {
            let _ = ansi::write_cursor_position(&mut self.buffer, row, col);
        }

        self.cursor_row = row;
        self.cursor_col = col;
    }

    /// Set foreground color if different from current.
    pub fn set_fg(&mut self, color: Rgba) {
        if self.current_fg != Some(color) {
            let _ = ansi::write_fg_color_with_mode(&mut self.buffer, color, self.color_mode);
            self.current_fg = Some(color);
        }
    }

    /// Set background color if different from current.
    pub fn set_bg(&mut self, color: Rgba) {
        if self.current_bg != Some(color) {
            let _ = ansi::write_bg_color_with_mode(&mut self.buffer, color, self.color_mode);
            self.current_bg = Some(color);
        }
    }

    /// Set text attributes, only writing changes.
    ///
    /// Uses a stack-allocated array to avoid heap allocation on every call.
    pub fn set_attributes(&mut self, attrs: TextAttributes) {
        let attrs = attrs.flags_only();
        if self.current_attrs == attrs {
            return;
        }

        // Check what needs to be turned off
        let removed = self.current_attrs - attrs;
        if !removed.is_empty() {
            // Use stack-allocated array instead of Vec to avoid heap allocation
            // Maximum 7 reset codes possible (one per attribute type)
            let mut codes: [&str; 7] = [""; 7];
            let mut count = 0;

            if removed.contains(TextAttributes::BOLD) || removed.contains(TextAttributes::DIM) {
                codes[count] = "22";
                count += 1;
            }
            if removed.contains(TextAttributes::ITALIC) {
                codes[count] = "23";
                count += 1;
            }
            if removed.contains(TextAttributes::UNDERLINE) {
                codes[count] = "24";
                count += 1;
            }
            if removed.contains(TextAttributes::BLINK) {
                codes[count] = "25";
                count += 1;
            }
            if removed.contains(TextAttributes::INVERSE) {
                codes[count] = "27";
                count += 1;
            }
            if removed.contains(TextAttributes::HIDDEN) {
                codes[count] = "28";
                count += 1;
            }
            if removed.contains(TextAttributes::STRIKETHROUGH) {
                codes[count] = "29";
                count += 1;
            }

            if count > 0 {
                // Manually construct the SGR escape sequence
                self.buffer.extend_from_slice(b"\x1b[");
                for (i, code) in codes[..count].iter().enumerate() {
                    if i > 0 {
                        self.buffer.push(b';');
                    }
                    self.buffer.extend_from_slice(code.as_bytes());
                }
                self.buffer.push(b'm');
            }

            // Update current attributes to reflect removal
            self.current_attrs -= removed;
        }
        // Apply new attributes
        let to_add = attrs - self.current_attrs;
        if !to_add.is_empty() {
            let _ = ansi::write_attributes(&mut self.buffer, to_add);
        }

        self.current_attrs = attrs;
    }

    /// Set hyperlink if different from current.
    pub fn set_link(&mut self, link_id: Option<u32>, url: Option<&str>) {
        if self.current_link == link_id {
            return;
        }

        match (link_id, url) {
            (Some(id), Some(url)) => {
                let _ = ansi::write_hyperlink_start(&mut self.buffer, id, url);
            }
            _ => {
                self.write_str(ansi::HYPERLINK_END);
            }
        }

        self.current_link = link_id;
    }

    /// Begin an OSC 8 hyperlink region.
    ///
    /// The URL is escaped to prevent control-character injection.
    pub fn begin_hyperlink(&mut self, url: &str) {
        // Use id=0 for this convenience API; nested links should be managed via link IDs.
        let _ = ansi::write_hyperlink_start(&mut self.buffer, 0, url);
        self.current_link = Some(0);
    }

    /// End the current OSC 8 hyperlink region.
    pub fn end_hyperlink(&mut self) {
        self.write_str(ansi::HYPERLINK_END);
        self.current_link = None;
    }

    /// Write `text` as a clickable OSC 8 hyperlink.
    pub fn write_hyperlink(&mut self, url: &str, text: &str) {
        self.begin_hyperlink(url);
        self.write_str(text);
        self.end_hyperlink();
    }

    /// Set the terminal scroll region to the given rows (0-indexed, inclusive).
    pub fn set_scroll_region(&mut self, top: u32, bottom: u32) {
        if top >= bottom {
            return;
        }
        let _ = ansi::write_set_scroll_region(&mut self.buffer, top, bottom);
    }

    /// Reset the terminal scroll region to full-screen.
    pub fn reset_scroll_region(&mut self) {
        let _ = ansi::write_reset_scroll_region(&mut self.buffer);
    }

    /// Scroll the content within the scroll region up by `lines`.
    pub fn scroll_up_in_region(&mut self, lines: u32) {
        let _ = ansi::write_scroll_up(&mut self.buffer, lines);
    }

    /// Scroll the content within the scroll region down by `lines`.
    pub fn scroll_down_in_region(&mut self, lines: u32) {
        let _ = ansi::write_scroll_down(&mut self.buffer, lines);
    }

    /// Erase from the start of the current line to the cursor position (EL 1).
    pub fn erase_line_to_cursor(&mut self) {
        self.write_str(ansi::CLEAR_LINE_LEFT);
    }

    /// Erase the entire current line (EL 2).
    pub fn erase_entire_line(&mut self) {
        self.write_str(ansi::CLEAR_LINE);
    }

    /// Erase from the start of the screen to the cursor position (ED 1).
    pub fn erase_screen_to_cursor(&mut self) {
        self.write_str(ansi::CLEAR_SCREEN_ABOVE);
    }

    /// Erase the entire screen (ED 2).
    pub fn erase_entire_screen(&mut self) {
        self.write_str(ansi::CLEAR_SCREEN);
    }

    /// Erase the scrollback buffer (ED 3).
    pub fn erase_scrollback(&mut self) {
        self.write_str(ansi::ERASE_SCROLLBACK);
    }

    /// Write a cell at the current cursor position.
    pub fn write_cell(&mut self, cell: &Cell) {
        self.write_cell_with_link(cell, None);
    }

    /// Write a cell at the current cursor position with optional hyperlink URL.
    pub fn write_cell_with_link(&mut self, cell: &Cell, link_url: Option<&str>) {
        self.set_link(cell.attributes.link_id(), link_url);

        // Update style state
        self.set_attributes(cell.attributes);
        self.set_fg(cell.fg);
        self.set_bg(cell.bg);

        // Write content using the cell's string representation
        // This handles all content types correctly without fixed-size buffer limitations
        match &cell.content {
            crate::cell::CellContent::Char(c) => {
                let mut buf = [0u8; 4];
                let s = c.encode_utf8(&mut buf);
                self.buffer.extend_from_slice(s.as_bytes());
            }
            crate::cell::CellContent::Grapheme(id) => {
                for _ in 0..id.width() {
                    self.buffer.push(b' ');
                }
            }
            crate::cell::CellContent::Empty => {
                self.buffer.push(b' ');
            }
            crate::cell::CellContent::Continuation => {
                // No output for continuation cells
            }
        }

        // Track cursor movement
        self.cursor_col += cell.display_width() as u32;
    }

    /// Write a cell at the current cursor position with optional hyperlink URL,
    /// resolving grapheme IDs via the pool.
    pub fn write_cell_with_link_and_pool(
        &mut self,
        cell: &Cell,
        link_url: Option<&str>,
        pool: &GraphemePool,
    ) {
        self.set_link(cell.attributes.link_id(), link_url);

        // Update style state
        self.set_attributes(cell.attributes);
        self.set_fg(cell.fg);
        self.set_bg(cell.bg);

        // Write content using the pool to resolve graphemes
        match &cell.content {
            crate::cell::CellContent::Char(c) => {
                let mut buf = [0u8; 4];
                let s = c.encode_utf8(&mut buf);
                self.buffer.extend_from_slice(s.as_bytes());
            }
            crate::cell::CellContent::Grapheme(id) => {
                if let Some(grapheme) = pool.get(*id) {
                    self.buffer.extend_from_slice(grapheme.as_bytes());
                } else {
                    for _ in 0..id.width() {
                        self.buffer.push(b' ');
                    }
                }
            }
            crate::cell::CellContent::Empty => {
                self.buffer.push(b' ');
            }
            crate::cell::CellContent::Continuation => {
                // No output for continuation cells
            }
        }

        // Track cursor movement
        self.cursor_col += cell.display_width() as u32;
    }

    /// Write a cell at a specific position.
    pub fn write_cell_at(&mut self, row: u32, col: u32, cell: &Cell) {
        self.move_cursor(row, col);
        self.write_cell(cell);
    }

    /// Write a cell at a specific position with optional hyperlink URL.
    pub fn write_cell_at_with_link(
        &mut self,
        row: u32,
        col: u32,
        cell: &Cell,
        link_url: Option<&str>,
    ) {
        self.move_cursor(row, col);
        self.write_cell_with_link(cell, link_url);
    }

    /// Write a cell at a specific position with optional hyperlink URL,
    /// resolving grapheme IDs via the pool.
    pub fn write_cell_at_with_link_and_pool(
        &mut self,
        row: u32,
        col: u32,
        cell: &Cell,
        link_url: Option<&str>,
        pool: &GraphemePool,
    ) {
        self.move_cursor(row, col);
        self.write_cell_with_link_and_pool(cell, link_url, pool);
    }

    /// Write a cell at the current cursor position, resolving grapheme IDs from the pool.
    ///
    /// Unlike [`Self::write_cell`], this properly renders multi-codepoint graphemes
    /// by looking them up in the provided pool.
    pub fn write_cell_with_pool(&mut self, cell: &Cell, pool: &crate::grapheme_pool::GraphemePool) {
        self.write_cell_with_pool_and_link(cell, pool, None);
    }

    /// Write a cell at the current cursor position with pool lookup and optional hyperlink.
    pub fn write_cell_with_pool_and_link(
        &mut self,
        cell: &Cell,
        pool: &crate::grapheme_pool::GraphemePool,
        link_url: Option<&str>,
    ) {
        self.set_link(cell.attributes.link_id(), link_url);
        self.set_attributes(cell.attributes);
        self.set_fg(cell.fg);
        self.set_bg(cell.bg);

        match &cell.content {
            crate::cell::CellContent::Char(c) => {
                let mut buf = [0u8; 4];
                let s = c.encode_utf8(&mut buf);
                self.buffer.extend_from_slice(s.as_bytes());
            }
            crate::cell::CellContent::Grapheme(id) => {
                // Look up the grapheme in the pool
                if let Some(grapheme) = pool.get(*id) {
                    self.buffer.extend_from_slice(grapheme.as_bytes());
                } else {
                    // Fallback: write spaces matching width
                    for _ in 0..id.width() {
                        self.buffer.push(b' ');
                    }
                }
            }
            crate::cell::CellContent::Empty => {
                self.buffer.push(b' ');
            }
            crate::cell::CellContent::Continuation => {
                // No output for continuation cells
            }
        }

        self.cursor_col += cell.display_width() as u32;
    }

    /// Write a cell at a specific position, resolving grapheme IDs from the pool.
    pub fn write_cell_at_with_pool(
        &mut self,
        row: u32,
        col: u32,
        cell: &Cell,
        pool: &crate::grapheme_pool::GraphemePool,
    ) {
        self.move_cursor(row, col);
        self.write_cell_with_pool(cell, pool);
    }

    /// Write a cell at a specific position with pool lookup and optional hyperlink.
    pub fn write_cell_at_with_pool_and_link(
        &mut self,
        row: u32,
        col: u32,
        cell: &Cell,
        pool: &crate::grapheme_pool::GraphemePool,
        link_url: Option<&str>,
    ) {
        self.move_cursor(row, col);
        self.write_cell_with_pool_and_link(cell, pool, link_url);
    }

    /// Reset all ANSI attributes.
    pub fn reset(&mut self) {
        self.write_str(ansi::RESET);
        self.current_fg = None;
        self.current_bg = None;
        self.current_attrs = TextAttributes::empty();
        self.current_link = None;
    }

    /// Flush the buffer to the underlying writer.
    pub fn flush(&mut self) -> io::Result<()> {
        self.writer.write_all(&self.buffer)?;
        self.buffer.clear();
        self.writer.flush()
    }

    /// Get the underlying writer.
    pub fn into_inner(self) -> W {
        self.writer
    }

    /// Get a reference to the buffer.
    pub fn buffer(&self) -> &[u8] {
        &self.buffer
    }

    /// Clear the buffer without flushing.
    pub fn clear_buffer(&mut self) {
        self.buffer.clear();
    }
}

/// Count decimal digits in a number.
fn digits(n: u32) -> usize {
    if n == 0 { 1 } else { (n.ilog10() + 1) as usize }
}

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

    #[test]
    fn test_ansi_writer_basic() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.write_str("Hello");
        assert_eq!(writer.buffer(), b"Hello");
    }

    #[test]
    fn test_cursor_movement() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.move_cursor(5, 10);
        assert!(writer.buffer().starts_with(b"\x1b["));
    }

    #[test]
    fn test_write_hyperlink_sequence() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.write_hyperlink("https://example.com", "Click");

        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.contains("\x1b]8;id=0;https://example.com\x1b\\"));
        assert!(output.contains("Click"));
        assert!(output.contains(ansi::HYPERLINK_END));
    }

    #[test]
    fn test_begin_end_hyperlink_sequence() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.begin_hyperlink("https://example.com");
        writer.write_str("Click");
        writer.end_hyperlink();

        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.contains("\x1b]8;id=0;https://example.com\x1b\\"));
        assert!(output.contains("Click"));
        assert!(output.contains(ansi::HYPERLINK_END));
    }

    #[test]
    fn test_hyperlink_url_escapes_control_chars() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.write_hyperlink("https://example.com/\u{001B}[31m", "X");

        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.contains("https://example.com/%1B[31m"));
    }

    #[test]
    fn test_set_scroll_region_converts_to_1_indexed() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.set_scroll_region(5, 20);
        assert_eq!(writer.buffer(), b"\x1b[6;21r");
    }

    #[test]
    fn test_set_scroll_region_invalid_is_noop() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.set_scroll_region(5, 5);
        assert!(writer.buffer().is_empty());
    }

    #[test]
    fn test_reset_scroll_region() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.reset_scroll_region();
        assert_eq!(writer.buffer(), b"\x1b[r");
    }

    #[test]
    fn test_scroll_up_down_in_region() {
        let mut writer = AnsiWriter::new(Vec::new());

        writer.scroll_up_in_region(0);
        assert!(writer.buffer().is_empty());

        writer.scroll_up_in_region(2);
        writer.scroll_down_in_region(3);
        assert_eq!(writer.buffer(), b"\x1b[2S\x1b[3T");
    }

    #[test]
    fn test_erase_sequences() {
        let mut writer = AnsiWriter::new(Vec::new());

        writer.erase_line_to_cursor();
        writer.erase_entire_line();
        writer.erase_screen_to_cursor();
        writer.erase_entire_screen();
        writer.erase_scrollback();

        assert_eq!(
            writer.buffer(),
            b"\x1b[1K\x1b[2K\x1b[1J\x1b[2J\x1b[3J",
            "Should emit EL/ED erase sequences in order",
        );
    }

    #[test]
    fn test_color_caching() {
        let mut writer = AnsiWriter::new(Vec::new());

        writer.set_fg(Rgba::RED);
        let len1 = writer.buffer().len();

        writer.set_fg(Rgba::RED); // Same color
        let len2 = writer.buffer().len();

        // Should not write again
        assert_eq!(len1, len2);

        writer.set_fg(Rgba::BLUE); // Different color
        let len3 = writer.buffer().len();

        // Should write new color
        assert!(len3 > len2);
    }

    #[test]
    fn test_write_cell() {
        let mut writer = AnsiWriter::new(Vec::new());
        let cell = Cell::new('A', Style::fg(Rgba::RED));
        writer.write_cell(&cell);
        writer.flush().unwrap();

        // After flush, data is in the underlying writer (Vec), not buffer
        let inner = writer.into_inner();
        let output = String::from_utf8_lossy(inner.as_slice());
        assert!(output.contains('A'));
    }

    #[test]
    fn test_write_cell_with_pool_simple_char() {
        let mut writer = AnsiWriter::new(Vec::new());
        let pool = crate::grapheme_pool::GraphemePool::new();

        let cell = Cell::new('X', Style::NONE);
        writer.write_cell_with_pool(&cell, &pool);

        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.ends_with('X'));
    }

    #[test]
    fn test_write_cell_with_pool_grapheme() {
        let mut writer = AnsiWriter::new(Vec::new());
        let mut pool = crate::grapheme_pool::GraphemePool::new();

        // Allocate a grapheme in the pool
        let id = pool.alloc("👨‍👩‍👧");

        let cell = Cell {
            content: crate::cell::CellContent::Grapheme(id),
            fg: Rgba::WHITE,
            bg: Rgba::BLACK,
            attributes: crate::style::TextAttributes::empty(),
        };

        writer.write_cell_with_pool(&cell, &pool);

        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.contains("👨‍👩‍👧"));
    }

    #[test]
    fn test_write_cell_with_pool_invalid_id_fallback() {
        let mut writer = AnsiWriter::new(Vec::new());
        let pool = crate::grapheme_pool::GraphemePool::new();

        // Create a grapheme ID that doesn't exist in the pool
        let invalid_id = crate::cell::GraphemeId::new(999, 2);
        let cell = Cell {
            content: crate::cell::CellContent::Grapheme(invalid_id),
            fg: Rgba::WHITE,
            bg: Rgba::BLACK,
            attributes: crate::style::TextAttributes::empty(),
        };

        writer.write_cell_with_pool(&cell, &pool);

        // Should fall back to spaces matching the width
        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.ends_with("  ")); // 2 spaces for width 2
    }

    #[test]
    fn test_write_cell_continuation_no_output() {
        let mut writer = AnsiWriter::new(Vec::new());
        let pool = crate::grapheme_pool::GraphemePool::new();

        // First write a cell to establish style state
        writer.set_fg(Rgba::WHITE);
        writer.set_bg(Rgba::BLACK);
        writer.clear_buffer();

        let cell = Cell::continuation(Rgba::BLACK);
        writer.write_cell_with_pool(&cell, &pool);

        // Continuation cells produce no visible character output
        // The buffer may contain ANSI codes for style changes, but no printable content
        // The cell's display_width is 0, so cursor shouldn't advance for content
        assert_eq!(cell.display_width(), 0);
    }

    #[test]
    fn test_write_cell_at_with_pool() {
        let mut writer = AnsiWriter::new(Vec::new());
        let mut pool = crate::grapheme_pool::GraphemePool::new();

        let id = pool.alloc("🎉");
        let cell = Cell {
            content: crate::cell::CellContent::Grapheme(id),
            fg: Rgba::WHITE,
            bg: Rgba::TRANSPARENT,
            attributes: crate::style::TextAttributes::empty(),
        };

        writer.write_cell_at_with_pool(5, 10, &cell, &pool);

        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.contains("🎉"));
        // Should contain cursor positioning
        assert!(output.contains("\x1b["));
    }

    // ============================================
    // Position Tracking Tests
    // ============================================

    #[test]
    fn test_position_tracking_after_cell_write() {
        let mut writer = AnsiWriter::new(Vec::new());

        // Move to known position
        writer.move_cursor(5, 10);
        writer.clear_buffer();

        // Write a single-width character
        let cell = Cell::new('A', Style::NONE);
        writer.write_cell(&cell);

        // Cursor should advance by character width
        // Internal state should track this
        // Write same cell again - no cursor move should be needed if tracked correctly
        writer.move_cursor(5, 11); // Should be no-op since we're already there
        // Buffer should be minimal since position matches
    }

    #[test]
    fn test_position_tracking_wide_char() {
        let mut writer = AnsiWriter::new(Vec::new());
        let mut pool = crate::grapheme_pool::GraphemePool::new();

        writer.move_cursor(0, 0);
        writer.clear_buffer();

        // Write a wide emoji (width 2)
        let id = pool.alloc("😀");
        let cell = Cell {
            content: crate::cell::CellContent::Grapheme(id),
            fg: Rgba::WHITE,
            bg: Rgba::BLACK,
            attributes: TextAttributes::empty(),
        };

        writer.write_cell_with_pool(&cell, &pool);

        // Cursor should have advanced by grapheme width
        // Move to what should be current position - should be no-op
        let before_len = writer.buffer().len();
        writer.move_cursor(0, id.width() as u32);
        let after_len = writer.buffer().len();

        // If position tracking is correct, no movement sequence added
        assert_eq!(before_len, after_len, "No cursor move for current position");
    }

    // ============================================
    // Minimal Sequence Generation Tests
    // ============================================

    #[test]
    fn test_minimal_fg_sequence_generation() {
        let mut writer = AnsiWriter::new(Vec::new());

        // Set initial fg color
        writer.set_fg(Rgba::RED);
        let initial_len = writer.buffer().len();
        assert!(initial_len > 0, "Should write fg sequence");

        // Set same color again - should not write
        writer.set_fg(Rgba::RED);
        let after_same = writer.buffer().len();
        assert_eq!(
            initial_len, after_same,
            "Same fg color should not emit sequence"
        );

        // Set different color - should write
        writer.set_fg(Rgba::BLUE);
        let after_diff = writer.buffer().len();
        assert!(
            after_diff > initial_len,
            "Different fg should emit sequence"
        );
    }

    #[test]
    fn test_minimal_bg_sequence_generation() {
        let mut writer = AnsiWriter::new(Vec::new());

        writer.set_bg(Rgba::BLACK);
        let initial_len = writer.buffer().len();

        writer.set_bg(Rgba::BLACK);
        assert_eq!(
            writer.buffer().len(),
            initial_len,
            "Same bg = no new sequence"
        );

        writer.set_bg(Rgba::WHITE);
        assert!(
            writer.buffer().len() > initial_len,
            "Different bg = new sequence"
        );
    }

    #[test]
    fn test_minimal_attribute_sequence_generation() {
        let mut writer = AnsiWriter::new(Vec::new());

        writer.set_attributes(TextAttributes::BOLD);
        let initial_len = writer.buffer().len();
        assert!(initial_len > 0, "Bold should emit sequence");

        writer.set_attributes(TextAttributes::BOLD);
        assert_eq!(
            writer.buffer().len(),
            initial_len,
            "Same attrs = no sequence"
        );

        writer.set_attributes(TextAttributes::BOLD | TextAttributes::ITALIC);
        assert!(
            writer.buffer().len() > initial_len,
            "Additional attr = sequence"
        );
    }

    // ============================================
    // Movement Optimization Tests
    // ============================================

    #[test]
    fn test_movement_optimization_relative_vs_absolute() {
        let mut writer = AnsiWriter::new(Vec::new());

        // Move to initial position
        writer.move_cursor(10, 10);
        writer.clear_buffer();

        // Small relative move should use relative sequences
        writer.move_cursor(10, 11); // +1 column
        let rel_output = writer.buffer().len();

        writer.clear_buffer();
        writer.reset_state();

        // Large absolute move from origin
        writer.move_cursor(10, 11);
        let abs_output = writer.buffer().len();

        // Relative should be smaller for small moves
        // ESC[1C (4 bytes) vs ESC[11;12H (8+ bytes)
        assert!(rel_output < abs_output, "Relative move should be shorter");
    }

    #[test]
    fn test_no_movement_when_at_position() {
        let mut writer = AnsiWriter::new(Vec::new());

        writer.move_cursor(5, 5);
        writer.clear_buffer();

        // Move to same position should produce no output
        writer.move_cursor(5, 5);
        assert!(writer.buffer().is_empty(), "No move to current position");
    }

    // ============================================
    // State Reset Tests
    // ============================================

    #[test]
    fn test_reset_state() {
        let mut writer = AnsiWriter::new(Vec::new());

        // Set up some state
        writer.set_fg(Rgba::RED);
        writer.set_bg(Rgba::BLUE);
        writer.set_attributes(TextAttributes::BOLD);
        writer.move_cursor(10, 20);

        // Reset state tracking (not emitting reset sequence)
        writer.reset_state();
        writer.clear_buffer();

        // Now setting colors should emit sequences again
        writer.set_fg(Rgba::RED);
        assert!(
            !writer.buffer().is_empty(),
            "After reset, fg emits sequence"
        );
    }

    #[test]
    fn test_reset_emits_sequence() {
        let mut writer = AnsiWriter::new(Vec::new());

        writer.set_fg(Rgba::RED);
        writer.clear_buffer();

        writer.reset();

        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.contains("\x1b[0m"), "Reset emits SGR 0");
    }

    #[test]
    fn test_reset_clears_color_state() {
        let mut writer = AnsiWriter::new(Vec::new());

        writer.set_fg(Rgba::RED);
        writer.reset();
        writer.clear_buffer();

        // After reset, setting any color should emit sequence
        // (state was cleared so RED is "new" again)
        writer.set_fg(Rgba::RED);
        assert!(
            !writer.buffer().is_empty(),
            "After reset, color is re-emitted"
        );
    }

    // ============================================
    // Attribute Delta Encoding Tests
    // ============================================

    #[test]
    fn test_attribute_removal_generates_reset() {
        let mut writer = AnsiWriter::new(Vec::new());

        // Set bold
        writer.set_attributes(TextAttributes::BOLD);
        writer.clear_buffer();

        // Remove bold (set empty)
        writer.set_attributes(TextAttributes::empty());

        // Should emit reset code for bold (SGR 22)
        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.contains("22"), "Bold removal uses SGR 22");
    }

    #[test]
    fn test_attribute_partial_removal() {
        let mut writer = AnsiWriter::new(Vec::new());

        // Set bold + italic
        writer.set_attributes(TextAttributes::BOLD | TextAttributes::ITALIC);
        writer.clear_buffer();

        // Remove only italic, keep bold
        writer.set_attributes(TextAttributes::BOLD);

        // Should emit reset for italic (SGR 23) but not bold
        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.contains("23"), "Italic removal uses SGR 23");
        assert!(!output.contains("22"), "Bold should not be reset");
    }

    #[test]
    fn test_attribute_addition_only() {
        let mut writer = AnsiWriter::new(Vec::new());

        // Start with bold
        writer.set_attributes(TextAttributes::BOLD);
        writer.clear_buffer();

        // Add italic (keep bold)
        writer.set_attributes(TextAttributes::BOLD | TextAttributes::ITALIC);

        // Should only emit italic (SGR 3), not bold again
        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.contains('3'), "Should add italic");
        // Should not re-emit bold since it's already set
        assert_eq!(output.matches('1').count(), 0, "Should not re-emit bold");
    }

    // ============================================
    // Link State Tracking Tests
    // ============================================

    #[test]
    fn test_link_caching() {
        let mut writer = AnsiWriter::new(Vec::new());

        writer.set_link(Some(1), Some("https://example.com"));
        let initial_len = writer.buffer().len();
        assert!(initial_len > 0, "Link should emit OSC 8");

        // Same link again
        writer.set_link(Some(1), Some("https://example.com"));
        assert_eq!(
            writer.buffer().len(),
            initial_len,
            "Same link = no new sequence"
        );

        // Different link
        writer.set_link(Some(2), Some("https://other.com"));
        assert!(
            writer.buffer().len() > initial_len,
            "Different link = new sequence"
        );
    }

    #[test]
    fn test_link_end() {
        let mut writer = AnsiWriter::new(Vec::new());

        writer.set_link(Some(1), Some("https://example.com"));
        writer.clear_buffer();

        // End link
        writer.set_link(None, None);

        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.contains("\x1b]8;;\x1b\\"), "Link end sequence");
    }

    // ============================================
    // Color Mode Tests
    // ============================================

    #[test]
    fn test_color_mode_setting() {
        let writer = AnsiWriter::new(Vec::new());
        assert_eq!(
            writer.color_mode(),
            ColorMode::TrueColor,
            "Default is TrueColor"
        );

        let writer = AnsiWriter::with_color_mode(Vec::new(), ColorMode::Color256);
        assert_eq!(writer.color_mode(), ColorMode::Color256);
    }

    #[test]
    fn test_color_mode_affects_output() {
        // TrueColor mode
        let mut writer = AnsiWriter::with_color_mode(Vec::new(), ColorMode::TrueColor);
        writer.set_fg(Rgba::new(0.5, 0.5, 0.5, 1.0));
        let tc_output = String::from_utf8_lossy(writer.buffer()).to_string();
        assert!(tc_output.contains("38;2;"), "TrueColor uses 38;2;R;G;B");

        // 256-color mode
        let mut writer = AnsiWriter::with_color_mode(Vec::new(), ColorMode::Color256);
        writer.set_fg(Rgba::new(0.5, 0.5, 0.5, 1.0));
        let c256_output = String::from_utf8_lossy(writer.buffer()).to_string();
        assert!(c256_output.contains("38;5;"), "256-color uses 38;5;N");

        // No color mode
        let mut writer = AnsiWriter::with_color_mode(Vec::new(), ColorMode::NoColor);
        writer.set_fg(Rgba::RED);
        assert!(writer.buffer().is_empty(), "NoColor emits nothing");
    }

    // ============================================
    // Flush and Buffer Tests
    // ============================================

    #[test]
    fn test_flush_transfers_to_writer() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.write_str("Hello");
        assert_eq!(writer.buffer().len(), 5);

        writer.flush().unwrap();
        assert!(writer.buffer().is_empty(), "Buffer cleared after flush");

        let inner = writer.into_inner();
        assert_eq!(&inner[..], b"Hello", "Data transferred to writer");
    }

    #[test]
    fn test_clear_buffer_without_flush() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.write_str("Test data");
        writer.clear_buffer();

        assert!(writer.buffer().is_empty(), "Buffer cleared");

        writer.flush().unwrap();
        let inner = writer.into_inner();
        assert!(inner.is_empty(), "Nothing written since buffer was cleared");
    }

    // ============================================
    // Write Raw Tests
    // ============================================

    #[test]
    fn test_write_raw() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.write_raw(b"\x1b[2J");
        assert_eq!(writer.buffer(), b"\x1b[2J");
    }

    #[test]
    fn test_write_str() {
        let mut writer = AnsiWriter::new(Vec::new());
        writer.write_str("Hello, World!");
        assert_eq!(writer.buffer(), b"Hello, World!");
    }

    // ============================================
    // Edge Case Tests
    // ============================================

    #[test]
    fn test_digits_function() {
        assert_eq!(digits(0), 1);
        assert_eq!(digits(9), 1);
        assert_eq!(digits(10), 2);
        assert_eq!(digits(99), 2);
        assert_eq!(digits(100), 3);
        assert_eq!(digits(999), 3);
        assert_eq!(digits(1000), 4);
        assert_eq!(digits(u32::MAX), 10);
    }

    #[test]
    fn test_empty_cell_output() {
        let mut writer = AnsiWriter::new(Vec::new());
        let cell = Cell::clear(Rgba::BLACK);
        writer.write_cell(&cell);

        // Empty cell should produce a space
        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.ends_with(' '), "Empty cell renders as space");
    }

    #[test]
    fn test_cell_with_all_attributes() {
        let mut writer = AnsiWriter::new(Vec::new());

        let mut attrs = TextAttributes::empty();
        attrs |= TextAttributes::BOLD;
        attrs |= TextAttributes::ITALIC;
        attrs |= TextAttributes::UNDERLINE;
        attrs |= TextAttributes::STRIKETHROUGH;

        let cell = Cell {
            content: crate::cell::CellContent::Char('X'),
            fg: Rgba::WHITE,
            bg: Rgba::BLACK,
            attributes: attrs,
        };

        writer.write_cell(&cell);

        let output = String::from_utf8_lossy(writer.buffer());
        assert!(output.contains('X'), "Character rendered");
        // Should have attribute codes
        assert!(output.contains('1'), "Bold");
        assert!(output.contains('3'), "Italic");
        assert!(output.contains('4'), "Underline");
        assert!(output.contains('9'), "Strikethrough");
    }
}