envision 0.16.0

A ratatui framework for collaborative TUI development with headless testing support
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
//! ANSI escape sequence parser for terminal output rendering.
//!
//! Supports SGR (Select Graphic Rendition) codes including reset, bold,
//! dim, italic, underline, standard/bright foreground and background
//! colors, and 256-color palette.

use ratatui::style::{Color, Modifier, Style};

/// A segment of text with associated ANSI styling.
///
/// Produced by [`parse_ansi`] when splitting a line containing ANSI
/// escape sequences into styled fragments.
///
/// # Example
///
/// ```rust
/// # #[cfg(feature = "display-components")]
/// # {
/// use envision::component::terminal_output::AnsiSegment;
/// use ratatui::style::{Color, Modifier, Style};
///
/// let seg = AnsiSegment {
///     text: "hello".to_string(),
///     style: Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
/// };
/// assert_eq!(seg.text, "hello");
/// # }
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct AnsiSegment {
    /// The text content of this segment.
    pub text: String,
    /// The style derived from ANSI escape codes.
    pub style: Style,
}

/// Parses a string containing ANSI escape sequences into styled segments.
///
/// Supports the following SGR codes:
/// - `0` — reset all attributes
/// - `1` — bold
/// - `2` — dim/faint
/// - `3` — italic
/// - `4` — underline
/// - `22` — normal intensity (cancel bold/dim)
/// - `23` — not italic
/// - `24` — not underlined
/// - `30`–`37` — standard foreground colors
/// - `38;5;n` — 256-color foreground
/// - `39` — default foreground
/// - `40`–`47` — standard background colors
/// - `48;5;n` — 256-color background
/// - `49` — default background
/// - `90`–`97` — bright foreground colors
/// - `100`–`107` — bright background colors
///
/// Non-SGR escape sequences are silently skipped.
///
/// # Example
///
/// ```rust
/// # #[cfg(feature = "display-components")]
/// # {
/// use envision::component::terminal_output::parse_ansi;
/// use ratatui::style::{Color, Style};
///
/// let segments = parse_ansi("hello \x1b[31mworld\x1b[0m");
/// assert_eq!(segments.len(), 2);
/// assert_eq!(segments[0].text, "hello ");
/// assert_eq!(segments[1].text, "world");
/// assert_eq!(segments[1].style, Style::default().fg(Color::Red));
/// # }
/// ```
pub fn parse_ansi(input: &str) -> Vec<AnsiSegment> {
    let mut segments = Vec::new();
    let mut current_style = Style::default();
    let mut current_text = String::new();
    let mut chars = input.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '\x1b' {
            // Start of escape sequence
            if chars.peek() == Some(&'[') {
                chars.next(); // consume '['

                // Collect the parameter bytes
                let mut params = String::new();
                while let Some(&c) = chars.peek() {
                    if c.is_ascii_digit() || c == ';' {
                        params.push(c);
                        chars.next();
                    } else {
                        break;
                    }
                }

                // Consume the final byte
                let final_byte = chars.next();

                if final_byte == Some('m') {
                    // SGR sequence — flush current text
                    if !current_text.is_empty() {
                        segments.push(AnsiSegment {
                            text: std::mem::take(&mut current_text),
                            style: current_style,
                        });
                    }
                    current_style = apply_sgr(&params, current_style);
                }
                // Non-SGR sequences are silently ignored
            }
            // Bare ESC without '[' is ignored
        } else {
            current_text.push(ch);
        }
    }

    // Flush remaining text
    if !current_text.is_empty() {
        segments.push(AnsiSegment {
            text: current_text,
            style: current_style,
        });
    }

    segments
}

/// Applies SGR parameter codes to an existing style.
fn apply_sgr(params: &str, mut style: Style) -> Style {
    if params.is_empty() {
        // ESC[m is equivalent to ESC[0m (reset)
        return Style::default();
    }

    let codes: Vec<&str> = params.split(';').collect();
    let mut i = 0;

    while i < codes.len() {
        let code: u16 = match codes[i].parse() {
            Ok(n) => n,
            Err(_) => {
                i += 1;
                continue;
            }
        };

        match code {
            0 => style = Style::default(),
            1 => style = style.add_modifier(Modifier::BOLD),
            2 => style = style.add_modifier(Modifier::DIM),
            3 => style = style.add_modifier(Modifier::ITALIC),
            4 => style = style.add_modifier(Modifier::UNDERLINED),
            22 => {
                style = style
                    .remove_modifier(Modifier::BOLD)
                    .remove_modifier(Modifier::DIM);
            }
            23 => style = style.remove_modifier(Modifier::ITALIC),
            24 => style = style.remove_modifier(Modifier::UNDERLINED),

            // Standard foreground colors
            30 => style = style.fg(Color::Black),
            31 => style = style.fg(Color::Red),
            32 => style = style.fg(Color::Green),
            33 => style = style.fg(Color::Yellow),
            34 => style = style.fg(Color::Blue),
            35 => style = style.fg(Color::Magenta),
            36 => style = style.fg(Color::Cyan),
            37 => style = style.fg(Color::White),

            // 256-color foreground: 38;5;n
            38 if i + 2 < codes.len() && codes[i + 1] == "5" => {
                if let Ok(n) = codes[i + 2].parse::<u8>() {
                    style = style.fg(Color::Indexed(n));
                    i += 2;
                }
            }
            // 24-bit RGB foreground: 38;2;r;g;b
            38 if i + 4 < codes.len() && codes[i + 1] == "2" => {
                if let (Ok(r), Ok(g), Ok(b)) = (
                    codes[i + 2].parse::<u8>(),
                    codes[i + 3].parse::<u8>(),
                    codes[i + 4].parse::<u8>(),
                ) {
                    style = style.fg(Color::Rgb(r, g, b));
                    i += 4;
                }
            }
            38 => {}

            // Default foreground
            39 => style = style.fg(Color::Reset),

            // Standard background colors
            40 => style = style.bg(Color::Black),
            41 => style = style.bg(Color::Red),
            42 => style = style.bg(Color::Green),
            43 => style = style.bg(Color::Yellow),
            44 => style = style.bg(Color::Blue),
            45 => style = style.bg(Color::Magenta),
            46 => style = style.bg(Color::Cyan),
            47 => style = style.bg(Color::White),

            // 256-color background: 48;5;n
            48 if i + 2 < codes.len() && codes[i + 1] == "5" => {
                if let Ok(n) = codes[i + 2].parse::<u8>() {
                    style = style.bg(Color::Indexed(n));
                    i += 2;
                }
            }
            // 24-bit RGB background: 48;2;r;g;b
            48 if i + 4 < codes.len() && codes[i + 1] == "2" => {
                if let (Ok(r), Ok(g), Ok(b)) = (
                    codes[i + 2].parse::<u8>(),
                    codes[i + 3].parse::<u8>(),
                    codes[i + 4].parse::<u8>(),
                ) {
                    style = style.bg(Color::Rgb(r, g, b));
                    i += 4;
                }
            }
            48 => {}

            // Default background
            49 => style = style.bg(Color::Reset),

            // Bright foreground colors
            90 => style = style.fg(Color::DarkGray),
            91 => style = style.fg(Color::LightRed),
            92 => style = style.fg(Color::LightGreen),
            93 => style = style.fg(Color::LightYellow),
            94 => style = style.fg(Color::LightBlue),
            95 => style = style.fg(Color::LightMagenta),
            96 => style = style.fg(Color::LightCyan),
            97 => style = style.fg(Color::Gray),

            // Bright background colors
            100 => style = style.bg(Color::DarkGray),
            101 => style = style.bg(Color::LightRed),
            102 => style = style.bg(Color::LightGreen),
            103 => style = style.bg(Color::LightYellow),
            104 => style = style.bg(Color::LightBlue),
            105 => style = style.bg(Color::LightMagenta),
            106 => style = style.bg(Color::LightCyan),
            107 => style = style.bg(Color::Gray),

            _ => {} // Unknown code — ignore
        }

        i += 1;
    }

    style
}

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::style::{Color, Modifier, Style};

    // =========================================================================
    // Plain text (no escapes)
    // =========================================================================

    #[test]
    fn test_plain_text() {
        let segments = parse_ansi("hello world");
        assert_eq!(segments.len(), 1);
        assert_eq!(segments[0].text, "hello world");
        assert_eq!(segments[0].style, Style::default());
    }

    #[test]
    fn test_empty_string() {
        let segments = parse_ansi("");
        assert!(segments.is_empty());
    }

    // =========================================================================
    // Reset
    // =========================================================================

    #[test]
    fn test_reset_code_0() {
        let segments = parse_ansi("\x1b[31mred\x1b[0mplain");
        assert_eq!(segments.len(), 2);
        assert_eq!(segments[0].style, Style::default().fg(Color::Red));
        assert_eq!(segments[1].style, Style::default());
        assert_eq!(segments[1].text, "plain");
    }

    #[test]
    fn test_bare_reset() {
        // ESC[m is equivalent to ESC[0m
        let segments = parse_ansi("\x1b[1mbold\x1b[mnormal");
        assert_eq!(segments.len(), 2);
        assert_eq!(
            segments[0].style,
            Style::default().add_modifier(Modifier::BOLD)
        );
        assert_eq!(segments[1].style, Style::default());
    }

    // =========================================================================
    // Text modifiers
    // =========================================================================

    #[test]
    fn test_bold() {
        let segments = parse_ansi("\x1b[1mbold text");
        assert_eq!(segments.len(), 1);
        assert_eq!(
            segments[0].style,
            Style::default().add_modifier(Modifier::BOLD)
        );
    }

    #[test]
    fn test_dim() {
        let segments = parse_ansi("\x1b[2mdim text");
        assert_eq!(segments.len(), 1);
        assert_eq!(
            segments[0].style,
            Style::default().add_modifier(Modifier::DIM)
        );
    }

    #[test]
    fn test_italic() {
        let segments = parse_ansi("\x1b[3mitalic text");
        assert_eq!(segments.len(), 1);
        assert_eq!(
            segments[0].style,
            Style::default().add_modifier(Modifier::ITALIC)
        );
    }

    #[test]
    fn test_underline() {
        let segments = parse_ansi("\x1b[4munderlined");
        assert_eq!(segments.len(), 1);
        assert_eq!(
            segments[0].style,
            Style::default().add_modifier(Modifier::UNDERLINED)
        );
    }

    #[test]
    fn test_cancel_bold_dim() {
        let segments = parse_ansi("\x1b[1;2mboldim\x1b[22mnormal");
        assert_eq!(segments.len(), 2);
        assert_eq!(
            segments[0].style,
            Style::default()
                .add_modifier(Modifier::BOLD)
                .add_modifier(Modifier::DIM)
        );
        // After code 22, bold and dim are removed
        let expected = Style::default()
            .add_modifier(Modifier::BOLD)
            .add_modifier(Modifier::DIM)
            .remove_modifier(Modifier::BOLD)
            .remove_modifier(Modifier::DIM);
        assert_eq!(segments[1].style, expected);
    }

    #[test]
    fn test_cancel_italic() {
        let segments = parse_ansi("\x1b[3mitalic\x1b[23mnormal");
        assert_eq!(segments.len(), 2);
        let expected = Style::default()
            .add_modifier(Modifier::ITALIC)
            .remove_modifier(Modifier::ITALIC);
        assert_eq!(segments[1].style, expected);
    }

    #[test]
    fn test_cancel_underline() {
        let segments = parse_ansi("\x1b[4munder\x1b[24mnormal");
        assert_eq!(segments.len(), 2);
        let expected = Style::default()
            .add_modifier(Modifier::UNDERLINED)
            .remove_modifier(Modifier::UNDERLINED);
        assert_eq!(segments[1].style, expected);
    }

    // =========================================================================
    // Standard foreground colors
    // =========================================================================

    #[test]
    fn test_fg_black() {
        let segments = parse_ansi("\x1b[30mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Black));
    }

    #[test]
    fn test_fg_red() {
        let segments = parse_ansi("\x1b[31mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Red));
    }

    #[test]
    fn test_fg_green() {
        let segments = parse_ansi("\x1b[32mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Green));
    }

    #[test]
    fn test_fg_yellow() {
        let segments = parse_ansi("\x1b[33mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Yellow));
    }

    #[test]
    fn test_fg_blue() {
        let segments = parse_ansi("\x1b[34mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Blue));
    }

    #[test]
    fn test_fg_magenta() {
        let segments = parse_ansi("\x1b[35mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Magenta));
    }

    #[test]
    fn test_fg_cyan() {
        let segments = parse_ansi("\x1b[36mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Cyan));
    }

    #[test]
    fn test_fg_white() {
        let segments = parse_ansi("\x1b[37mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::White));
    }

    #[test]
    fn test_fg_default() {
        let segments = parse_ansi("\x1b[31mred\x1b[39mdefault");
        assert_eq!(segments[1].style, Style::default().fg(Color::Reset));
    }

    // =========================================================================
    // Standard background colors
    // =========================================================================

    #[test]
    fn test_bg_red() {
        let segments = parse_ansi("\x1b[41mtext");
        assert_eq!(segments[0].style, Style::default().bg(Color::Red));
    }

    #[test]
    fn test_bg_green() {
        let segments = parse_ansi("\x1b[42mtext");
        assert_eq!(segments[0].style, Style::default().bg(Color::Green));
    }

    #[test]
    fn test_bg_default() {
        let segments = parse_ansi("\x1b[41mred bg\x1b[49mdefault");
        assert_eq!(segments[1].style, Style::default().bg(Color::Reset));
    }

    // =========================================================================
    // Bright foreground colors
    // =========================================================================

    #[test]
    fn test_bright_fg_red() {
        let segments = parse_ansi("\x1b[91mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::LightRed));
    }

    #[test]
    fn test_bright_fg_green() {
        let segments = parse_ansi("\x1b[92mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::LightGreen));
    }

    #[test]
    fn test_bright_fg_yellow() {
        let segments = parse_ansi("\x1b[93mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::LightYellow));
    }

    #[test]
    fn test_bright_fg_blue() {
        let segments = parse_ansi("\x1b[94mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::LightBlue));
    }

    #[test]
    fn test_bright_fg_gray() {
        // Code 90 maps to DarkGray
        let segments = parse_ansi("\x1b[90mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::DarkGray));
    }

    // =========================================================================
    // Bright background colors
    // =========================================================================

    #[test]
    fn test_bright_bg_red() {
        let segments = parse_ansi("\x1b[101mtext");
        assert_eq!(segments[0].style, Style::default().bg(Color::LightRed));
    }

    #[test]
    fn test_bright_bg_green() {
        let segments = parse_ansi("\x1b[102mtext");
        assert_eq!(segments[0].style, Style::default().bg(Color::LightGreen));
    }

    // =========================================================================
    // 256-color palette
    // =========================================================================

    #[test]
    fn test_256_fg_color() {
        let segments = parse_ansi("\x1b[38;5;196mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Indexed(196)));
    }

    #[test]
    fn test_256_bg_color() {
        let segments = parse_ansi("\x1b[48;5;22mtext");
        assert_eq!(segments[0].style, Style::default().bg(Color::Indexed(22)));
    }

    #[test]
    fn test_256_fg_zero() {
        let segments = parse_ansi("\x1b[38;5;0mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Indexed(0)));
    }

    #[test]
    fn test_256_fg_max() {
        let segments = parse_ansi("\x1b[38;5;255mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Indexed(255)));
    }

    // =========================================================================
    // 24-bit RGB colors
    // =========================================================================

    #[test]
    fn test_rgb_fg_color() {
        let segments = parse_ansi("\x1b[38;2;255;128;0mtext");
        assert_eq!(
            segments[0].style,
            Style::default().fg(Color::Rgb(255, 128, 0))
        );
    }

    #[test]
    fn test_rgb_bg_color() {
        let segments = parse_ansi("\x1b[48;2;0;100;200mtext");
        assert_eq!(
            segments[0].style,
            Style::default().bg(Color::Rgb(0, 100, 200))
        );
    }

    #[test]
    fn test_rgb_fg_black() {
        let segments = parse_ansi("\x1b[38;2;0;0;0mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Rgb(0, 0, 0)));
    }

    #[test]
    fn test_rgb_fg_white() {
        let segments = parse_ansi("\x1b[38;2;255;255;255mtext");
        assert_eq!(
            segments[0].style,
            Style::default().fg(Color::Rgb(255, 255, 255))
        );
    }

    #[test]
    fn test_rgb_fg_and_bg() {
        let segments = parse_ansi("\x1b[38;2;255;0;0;48;2;0;0;255mtext");
        assert_eq!(
            segments[0].style,
            Style::default()
                .fg(Color::Rgb(255, 0, 0))
                .bg(Color::Rgb(0, 0, 255))
        );
    }

    #[test]
    fn test_rgb_with_bold() {
        let segments = parse_ansi("\x1b[1;38;2;128;64;32mtext");
        assert_eq!(
            segments[0].style,
            Style::default()
                .fg(Color::Rgb(128, 64, 32))
                .add_modifier(Modifier::BOLD)
        );
    }

    // =========================================================================
    // Combined codes
    // =========================================================================

    #[test]
    fn test_bold_red() {
        let segments = parse_ansi("\x1b[1;31mbold red");
        assert_eq!(
            segments[0].style,
            Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)
        );
    }

    #[test]
    fn test_bold_italic_underline() {
        let segments = parse_ansi("\x1b[1;3;4mfancy");
        assert_eq!(
            segments[0].style,
            Style::default()
                .add_modifier(Modifier::BOLD)
                .add_modifier(Modifier::ITALIC)
                .add_modifier(Modifier::UNDERLINED)
        );
    }

    #[test]
    fn test_fg_and_bg() {
        let segments = parse_ansi("\x1b[31;42mred on green");
        assert_eq!(
            segments[0].style,
            Style::default().fg(Color::Red).bg(Color::Green)
        );
    }

    // =========================================================================
    // Multiple segments
    // =========================================================================

    #[test]
    fn test_multiple_color_changes() {
        let segments = parse_ansi("\x1b[31mred\x1b[32mgreen\x1b[34mblue");
        assert_eq!(segments.len(), 3);
        assert_eq!(segments[0].text, "red");
        assert_eq!(segments[0].style, Style::default().fg(Color::Red));
        assert_eq!(segments[1].text, "green");
        assert_eq!(segments[1].style, Style::default().fg(Color::Green));
        assert_eq!(segments[2].text, "blue");
        assert_eq!(segments[2].style, Style::default().fg(Color::Blue));
    }

    #[test]
    fn test_color_then_reset_then_color() {
        let segments = parse_ansi("\x1b[31mred\x1b[0mplain\x1b[34mblue");
        assert_eq!(segments.len(), 3);
        assert_eq!(segments[1].style, Style::default());
        assert_eq!(segments[2].style, Style::default().fg(Color::Blue));
    }

    // =========================================================================
    // Style persistence across segments
    // =========================================================================

    #[test]
    fn test_style_carries_over() {
        // Bold is set, then color is changed — bold should persist
        let segments = parse_ansi("\x1b[1mbold\x1b[31mstill bold red");
        assert_eq!(segments.len(), 2);
        assert_eq!(
            segments[0].style,
            Style::default().add_modifier(Modifier::BOLD)
        );
        assert_eq!(
            segments[1].style,
            Style::default().add_modifier(Modifier::BOLD).fg(Color::Red)
        );
    }

    // =========================================================================
    // Edge cases
    // =========================================================================

    #[test]
    fn test_escape_at_end() {
        let segments = parse_ansi("text\x1b[31m");
        assert_eq!(segments.len(), 1);
        assert_eq!(segments[0].text, "text");
    }

    #[test]
    fn test_escape_at_start() {
        let segments = parse_ansi("\x1b[31mred");
        assert_eq!(segments.len(), 1);
        assert_eq!(segments[0].text, "red");
        assert_eq!(segments[0].style, Style::default().fg(Color::Red));
    }

    #[test]
    fn test_consecutive_escapes() {
        let segments = parse_ansi("\x1b[1m\x1b[31mbold red");
        assert_eq!(segments.len(), 1);
        assert_eq!(
            segments[0].style,
            Style::default().add_modifier(Modifier::BOLD).fg(Color::Red)
        );
    }

    #[test]
    fn test_non_sgr_sequence_ignored() {
        // ESC[2J is clear screen — should be ignored
        let segments = parse_ansi("before\x1b[2Jafter");
        assert_eq!(segments.len(), 1);
        assert_eq!(segments[0].text, "beforeafter");
    }

    #[test]
    fn test_bare_esc_ignored() {
        let segments = parse_ansi("hello\x1bworld");
        assert_eq!(segments.len(), 1);
        assert_eq!(segments[0].text, "helloworld");
    }

    #[test]
    fn test_unknown_sgr_code_ignored() {
        let segments = parse_ansi("\x1b[999mtext");
        assert_eq!(segments.len(), 1);
        assert_eq!(segments[0].style, Style::default());
    }

    #[test]
    fn test_incomplete_256_color_ignored() {
        // 38;5 without third param
        let segments = parse_ansi("\x1b[38;5mtext");
        assert_eq!(segments.len(), 1);
        // No color applied since the sequence is incomplete
        assert_eq!(segments[0].style, Style::default());
    }

    #[test]
    fn test_text_between_escapes() {
        let segments = parse_ansi("a\x1b[31mb\x1b[0mc");
        assert_eq!(segments.len(), 3);
        assert_eq!(segments[0].text, "a");
        assert_eq!(segments[0].style, Style::default());
        assert_eq!(segments[1].text, "b");
        assert_eq!(segments[1].style, Style::default().fg(Color::Red));
        assert_eq!(segments[2].text, "c");
        assert_eq!(segments[2].style, Style::default());
    }

    #[test]
    fn test_all_standard_bg_colors() {
        let colors = [
            (40, Color::Black),
            (41, Color::Red),
            (42, Color::Green),
            (43, Color::Yellow),
            (44, Color::Blue),
            (45, Color::Magenta),
            (46, Color::Cyan),
            (47, Color::White),
        ];
        for (code, color) in &colors {
            let input = format!("\x1b[{code}mtext");
            let segments = parse_ansi(&input);
            assert_eq!(segments[0].style, Style::default().bg(*color));
        }
    }

    #[test]
    fn test_bright_fg_magenta() {
        let segments = parse_ansi("\x1b[95mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::LightMagenta));
    }

    #[test]
    fn test_bright_fg_cyan() {
        let segments = parse_ansi("\x1b[96mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::LightCyan));
    }

    #[test]
    fn test_bright_fg_white() {
        // Code 97 maps to Gray in ratatui's model
        let segments = parse_ansi("\x1b[97mtext");
        assert_eq!(segments[0].style, Style::default().fg(Color::Gray));
    }

    #[test]
    fn test_all_bright_bg_colors() {
        let colors = [
            (100, Color::DarkGray),
            (101, Color::LightRed),
            (102, Color::LightGreen),
            (103, Color::LightYellow),
            (104, Color::LightBlue),
            (105, Color::LightMagenta),
            (106, Color::LightCyan),
            (107, Color::Gray),
        ];
        for (code, color) in &colors {
            let input = format!("\x1b[{code}mtext");
            let segments = parse_ansi(&input);
            assert_eq!(segments[0].style, Style::default().bg(*color));
        }
    }
}

#[cfg(test)]
mod property_tests {
    use super::*;
    use proptest::prelude::*;
    use ratatui::style::Style;

    /// Generates plain text that contains no ESC (\x1b) characters.
    fn plain_text(max_len: usize) -> impl Strategy<Value = String> {
        prop::collection::vec(
            prop::char::range('\u{20}', '\u{7E}').prop_filter("no ESC", |c| *c != '\x1b'),
            1..=max_len,
        )
        .prop_map(|chars| chars.into_iter().collect())
    }

    /// Generates a valid SGR escape sequence (e.g., `\x1b[1;31m`).
    fn sgr_sequence() -> impl Strategy<Value = String> {
        prop_oneof![
            // Single SGR code
            prop::sample::select(vec![
                0u16, 1, 2, 3, 4, 22, 23, 24, 30, 31, 32, 33, 34, 35, 36, 37, 39, 40, 41, 42, 43,
                44, 45, 46, 47, 49, 90, 91, 92, 93, 94, 95, 96, 97, 100, 101, 102, 103, 104, 105,
                106, 107,
            ])
            .prop_map(|code| format!("\x1b[{code}m")),
            // 256-color foreground: 38;5;n
            (0..=255u8).prop_map(|n| format!("\x1b[38;5;{n}m")),
            // 256-color background: 48;5;n
            (0..=255u8).prop_map(|n| format!("\x1b[48;5;{n}m")),
            // 24-bit RGB foreground: 38;2;r;g;b
            (0..=255u8, 0..=255u8, 0..=255u8)
                .prop_map(|(r, g, b)| format!("\x1b[38;2;{r};{g};{b}m")),
            // 24-bit RGB background: 48;2;r;g;b
            (0..=255u8, 0..=255u8, 0..=255u8)
                .prop_map(|(r, g, b)| format!("\x1b[48;2;{r};{g};{b}m")),
            // Combined: bold + color
            prop::sample::select(vec![31u16, 32, 33, 34, 35, 36, 37])
                .prop_map(|code| format!("\x1b[1;{code}m")),
            // Combined: bold + italic + underline
            Just("\x1b[1;3;4m".to_string()),
            // Reset
            Just("\x1b[0m".to_string()),
            // Bare reset (ESC[m)
            Just("\x1b[m".to_string()),
        ]
    }

    /// Builds an input string by interleaving SGR sequences and plain text.
    fn ansi_input() -> impl Strategy<Value = String> {
        prop::collection::vec(prop_oneof![plain_text(20), sgr_sequence()], 0..=10)
            .prop_map(|parts| parts.into_iter().collect::<String>())
    }

    /// Strips ANSI escape sequences, returning only visible text (test oracle).
    fn strip_ansi(input: &str) -> String {
        let mut result = String::new();
        let mut chars = input.chars().peekable();
        while let Some(ch) = chars.next() {
            if ch == '\x1b' {
                if chars.peek() == Some(&'[') {
                    chars.next(); // consume '['
                    // Consume parameter bytes and the final byte
                    while let Some(&c) = chars.peek() {
                        if c.is_ascii_digit() || c == ';' {
                            chars.next();
                        } else {
                            break;
                        }
                    }
                    // Consume final byte
                    chars.next();
                }
                // Bare ESC without '[' — just skip the ESC
            } else {
                result.push(ch);
            }
        }
        result
    }

    proptest! {
        /// Concatenating all segment text values reproduces the original
        /// input with escape sequences stripped.
        #[test]
        fn round_trip_text_preservation(input in ansi_input()) {
            let segments = parse_ansi(&input);
            let reconstructed: String = segments.iter().map(|s| s.text.as_str()).collect();
            let expected = strip_ansi(&input);
            prop_assert_eq!(reconstructed, expected);
        }

        /// The parser never panics on arbitrary input bytes.
        #[test]
        fn no_panics_on_arbitrary_input(input in "\\PC{0,200}") {
            let _ = parse_ansi(&input);
        }

        /// After an explicit reset sequence (\x1b[0m), the next segment's
        /// style must be Style::default() (assuming no further SGR codes
        /// intervene before the text).
        #[test]
        fn style_resets_after_reset_code(
            pre_text in plain_text(10),
            post_text in plain_text(10),
            sgr in sgr_sequence(),
        ) {
            // Build: <sgr><pre_text>\x1b[0m<post_text>
            let input = format!("{sgr}{pre_text}\x1b[0m{post_text}");
            let segments = parse_ansi(&input);

            // Find the segment containing post_text — it should be the last one
            let last = segments.last().expect("should have at least one segment");
            prop_assert_eq!(
                &last.text, &post_text,
                "last segment text should be the post-reset text"
            );
            prop_assert_eq!(
                last.style,
                Style::default(),
                "style after reset should be default, got {:?}",
                last.style
            );
        }

        /// Every AnsiSegment produced by the parser has non-empty text.
        /// Escape-only sequences produce no segments.
        #[test]
        fn segment_text_is_never_empty(input in ansi_input()) {
            let segments = parse_ansi(&input);
            for (i, segment) in segments.iter().enumerate() {
                prop_assert!(
                    !segment.text.is_empty(),
                    "segment {} has empty text in parse of {:?}",
                    i,
                    input
                );
            }
        }

        /// An input consisting entirely of escape sequences yields no segments.
        #[test]
        fn escape_only_input_yields_no_segments(
            escapes in prop::collection::vec(sgr_sequence(), 1..=5),
        ) {
            let input: String = escapes.into_iter().collect();
            let segments = parse_ansi(&input);
            prop_assert!(
                segments.is_empty(),
                "expected no segments for escape-only input {:?}, got {}",
                input,
                segments.len()
            );
        }

        /// The bare reset sequence ESC[m behaves identically to ESC[0m.
        #[test]
        fn bare_reset_equals_explicit_reset(
            pre_text in plain_text(10),
            post_text in plain_text(10),
            sgr in sgr_sequence(),
        ) {
            let with_bare = format!("{sgr}{pre_text}\x1b[m{post_text}");
            let with_explicit = format!("{sgr}{pre_text}\x1b[0m{post_text}");
            let segments_bare = parse_ansi(&with_bare);
            let segments_explicit = parse_ansi(&with_explicit);
            prop_assert_eq!(segments_bare, segments_explicit);
        }
    }
}