rich_rust 0.2.1

A Rust port of Python's Rich library for beautiful terminal output
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
//! Logged assertion helpers for rich_rust tests.
//!
//! These functions wrap standard assertions with tracing logs,
//! providing detailed context when assertions fail.
//!
//! Note: Not all helpers are currently used, but they're available for future tests.

#![allow(dead_code)]

use std::fmt::Debug;

/// Assert equality with detailed logging.
///
/// Logs both values at debug level before comparison,
/// making it easier to diagnose failures in CI logs.
///
/// # Example
///
/// ```rust,ignore
/// assert_eq_logged("color count", colors.len(), 16);
/// ```
#[track_caller]
pub fn assert_eq_logged<T: PartialEq + Debug>(context: &str, actual: T, expected: T) {
    tracing::debug!(
        context = context,
        expected = ?expected,
        actual = ?actual,
        "asserting equality"
    );

    if actual != expected {
        tracing::error!(
            context = context,
            expected = ?expected,
            actual = ?actual,
            "assertion failed: values not equal"
        );
    }

    assert_eq!(
        actual, expected,
        "{context}: expected {expected:?}, got {actual:?}"
    );

    tracing::trace!(context = context, "assertion passed");
}

/// Assert that a value is true with logging.
///
/// # Example
///
/// ```rust,ignore
/// assert_true_logged("color is valid", color.is_valid());
/// ```
#[track_caller]
pub fn assert_true_logged(context: &str, value: bool) {
    tracing::debug!(context = context, value = value, "asserting true");

    if !value {
        tracing::error!(
            context = context,
            value = value,
            "assertion failed: expected true"
        );
    }

    assert!(value, "{context}: expected true, got false");

    tracing::trace!(context = context, "assertion passed");
}

/// Assert that a value is false with logging.
///
/// # Example
///
/// ```rust,ignore
/// assert_false_logged("should not be terminal", is_terminal);
/// ```
#[track_caller]
pub fn assert_false_logged(context: &str, value: bool) {
    tracing::debug!(context = context, value = value, "asserting false");

    if value {
        tracing::error!(
            context = context,
            value = value,
            "assertion failed: expected false"
        );
    }

    assert!(!value, "{context}: expected false, got true");

    tracing::trace!(context = context, "assertion passed");
}

/// Assert that a Result is Ok with logging.
///
/// Returns the Ok value for further assertions.
///
/// # Example
///
/// ```rust,ignore
/// let color = assert_ok_logged("parse color", Color::parse("#ff0000"));
/// ```
#[track_caller]
pub fn assert_ok_logged<T: Debug, E: Debug>(context: &str, result: Result<T, E>) -> T {
    tracing::debug!(context = context, result = ?result, "asserting Ok");

    match result {
        Ok(value) => {
            tracing::trace!(context = context, value = ?value, "assertion passed: got Ok");
            value
        }
        Err(ref e) => {
            tracing::error!(context = context, error = ?e, "assertion failed: expected Ok, got Err");
            panic!("{context}: expected Ok, got Err({e:?})");
        }
    }
}

/// Assert that a Result is Err with logging.
///
/// Returns the Err value for further assertions.
///
/// # Example
///
/// ```rust,ignore
/// let error = assert_err_logged("invalid color", Color::parse("not-a-color"));
/// ```
#[track_caller]
pub fn assert_err_logged<T: Debug, E: Debug>(context: &str, result: Result<T, E>) -> E {
    tracing::debug!(context = context, result = ?result, "asserting Err");

    match result {
        Err(e) => {
            tracing::trace!(context = context, error = ?e, "assertion passed: got Err");
            e
        }
        Ok(ref value) => {
            tracing::error!(
                context = context,
                value = ?value,
                "assertion failed: expected Err, got Ok"
            );
            panic!("{context}: expected Err, got Ok({value:?})");
        }
    }
}

/// Assert that an Option is Some with logging.
///
/// Returns the inner value for further assertions.
///
/// # Example
///
/// ```rust,ignore
/// let triplet = assert_some_logged("color triplet", color.triplet());
/// ```
#[track_caller]
pub fn assert_some_logged<T: Debug>(context: &str, option: Option<T>) -> T {
    tracing::debug!(context = context, option = ?option, "asserting Some");

    match option {
        Some(value) => {
            tracing::trace!(context = context, value = ?value, "assertion passed: got Some");
            value
        }
        None => {
            tracing::error!(
                context = context,
                "assertion failed: expected Some, got None"
            );
            panic!("{context}: expected Some, got None");
        }
    }
}

/// Assert that an Option is None with logging.
///
/// # Example
///
/// ```rust,ignore
/// assert_none_logged("default has no triplet", default_color.triplet());
/// ```
#[track_caller]
pub fn assert_none_logged<T: Debug>(context: &str, option: Option<T>) {
    tracing::debug!(context = context, option = ?option, "asserting None");

    if let Some(ref value) = option {
        tracing::error!(
            context = context,
            value = ?value,
            "assertion failed: expected None, got Some"
        );
        panic!("{context}: expected None, got Some({value:?})");
    }

    tracing::trace!(context = context, "assertion passed");
}

/// Assert that a string contains a substring with logging.
///
/// # Example
///
/// ```rust,ignore
/// assert_contains_logged("ansi output", &output, "\x1b[31m");
/// ```
#[track_caller]
pub fn assert_contains_logged(context: &str, haystack: &str, needle: &str) {
    tracing::debug!(
        context = context,
        haystack_len = haystack.len(),
        needle = needle,
        "asserting contains"
    );

    if !haystack.contains(needle) {
        tracing::error!(
            context = context,
            haystack = haystack,
            needle = needle,
            "assertion failed: string does not contain substring"
        );
        panic!(
            "{context}: expected string to contain {needle:?}, but it doesn't.\nString: {haystack:?}"
        );
    }

    tracing::trace!(context = context, "assertion passed");
}

/// Assert that a string does not contain a substring with logging.
///
/// # Example
///
/// ```rust,ignore
/// assert_not_contains_logged("plain output", &output, "\x1b[");
/// ```
#[track_caller]
pub fn assert_not_contains_logged(context: &str, haystack: &str, needle: &str) {
    tracing::debug!(
        context = context,
        haystack_len = haystack.len(),
        needle = needle,
        "asserting not contains"
    );

    if haystack.contains(needle) {
        tracing::error!(
            context = context,
            haystack = haystack,
            needle = needle,
            "assertion failed: string contains unwanted substring"
        );
        panic!(
            "{context}: expected string to not contain {needle:?}, but it does.\nString: {haystack:?}"
        );
    }

    tracing::trace!(context = context, "assertion passed");
}

/// Assert approximate equality for floating point values with logging.
///
/// Uses a relative epsilon for comparison.
///
/// # Example
///
/// ```rust,ignore
/// assert_approx_eq_logged("normalized red", normalized.0, 1.0, 0.001);
/// ```
#[track_caller]
pub fn assert_approx_eq_logged(context: &str, actual: f64, expected: f64, epsilon: f64) {
    tracing::debug!(
        context = context,
        expected = expected,
        actual = actual,
        epsilon = epsilon,
        "asserting approximate equality"
    );

    let diff = (actual - expected).abs();
    if diff > epsilon {
        tracing::error!(
            context = context,
            expected = expected,
            actual = actual,
            diff = diff,
            epsilon = epsilon,
            "assertion failed: values not approximately equal"
        );
        panic!("{context}: expected {expected} (within {epsilon}), got {actual} (diff: {diff})");
    }

    tracing::trace!(context = context, "assertion passed");
}

/// Assert that a slice has a specific length with logging.
///
/// # Example
///
/// ```rust,ignore
/// assert_len_logged("segments", &segments, 5);
/// ```
#[track_caller]
pub fn assert_len_logged<T>(context: &str, slice: &[T], expected_len: usize) {
    let actual_len = slice.len();
    tracing::debug!(
        context = context,
        expected_len = expected_len,
        actual_len = actual_len,
        "asserting length"
    );

    if actual_len != expected_len {
        tracing::error!(
            context = context,
            expected_len = expected_len,
            actual_len = actual_len,
            "assertion failed: unexpected length"
        );
        panic!("{context}: expected length {expected_len}, got {actual_len}");
    }

    tracing::trace!(context = context, "assertion passed");
}

// =============================================================================
// Style Verification Helpers (bd-2sa2)
// These helpers verify that ANSI styles are correctly applied in output.
// =============================================================================

/// Common ANSI SGR (Select Graphic Rendition) codes.
pub mod ansi {
    /// Bold (SGR 1)
    pub const BOLD: &str = "\x1b[1m";
    /// Dim (SGR 2)
    pub const DIM: &str = "\x1b[2m";
    /// Italic (SGR 3)
    pub const ITALIC: &str = "\x1b[3m";
    /// Underline (SGR 4)
    pub const UNDERLINE: &str = "\x1b[4m";
    /// Blink (SGR 5)
    pub const BLINK: &str = "\x1b[5m";
    /// Reverse (SGR 7)
    pub const REVERSE: &str = "\x1b[7m";
    /// Strikethrough (SGR 9)
    pub const STRIKETHROUGH: &str = "\x1b[9m";
    /// Reset all (SGR 0)
    pub const RESET: &str = "\x1b[0m";

    // Standard foreground colors (SGR 30-37)
    /// Black foreground (SGR 30)
    pub const FG_BLACK: &str = "\x1b[30m";
    /// Red foreground (SGR 31)
    pub const FG_RED: &str = "\x1b[31m";
    /// Green foreground (SGR 32)
    pub const FG_GREEN: &str = "\x1b[32m";
    /// Yellow foreground (SGR 33)
    pub const FG_YELLOW: &str = "\x1b[33m";
    /// Blue foreground (SGR 34)
    pub const FG_BLUE: &str = "\x1b[34m";
    /// Magenta foreground (SGR 35)
    pub const FG_MAGENTA: &str = "\x1b[35m";
    /// Cyan foreground (SGR 36)
    pub const FG_CYAN: &str = "\x1b[36m";
    /// White foreground (SGR 37)
    pub const FG_WHITE: &str = "\x1b[37m";

    // Standard background colors (SGR 40-47)
    /// Black background (SGR 40)
    pub const BG_BLACK: &str = "\x1b[40m";
    /// Red background (SGR 41)
    pub const BG_RED: &str = "\x1b[41m";
    /// Green background (SGR 42)
    pub const BG_GREEN: &str = "\x1b[42m";
    /// Yellow background (SGR 43)
    pub const BG_YELLOW: &str = "\x1b[43m";
    /// Blue background (SGR 44)
    pub const BG_BLUE: &str = "\x1b[44m";
    /// Magenta background (SGR 45)
    pub const BG_MAGENTA: &str = "\x1b[45m";
    /// Cyan background (SGR 46)
    pub const BG_CYAN: &str = "\x1b[46m";
    /// White background (SGR 47)
    pub const BG_WHITE: &str = "\x1b[47m";
}

/// Check if output contains any ANSI escape sequences.
///
/// Returns true if the string contains `\x1b[` (CSI sequence intro).
///
/// # Example
///
/// ```rust,ignore
/// let output = render_markup("[bold]text[/]");
/// assert!(has_ansi_codes(&output), "Expected ANSI codes in styled output");
/// ```
#[must_use]
pub fn has_ansi_codes(output: &str) -> bool {
    output.contains("\x1b[")
}

/// Assert that output contains ANSI escape sequences.
///
/// Use this to verify that styling was applied to the output.
///
/// # Example
///
/// ```rust,ignore
/// let output = render_markup("[bold]text[/]");
/// assert_has_ansi_codes("styled output", &output);
/// ```
#[track_caller]
pub fn assert_has_ansi_codes(context: &str, output: &str) {
    tracing::debug!(
        context = context,
        output_len = output.len(),
        "asserting has ANSI codes"
    );

    if !has_ansi_codes(output) {
        tracing::error!(
            context = context,
            output = output,
            "assertion failed: no ANSI codes found"
        );
        panic!("{context}: expected ANSI escape codes but found none.\nOutput: {output:?}");
    }

    tracing::trace!(context = context, "assertion passed: has ANSI codes");
}

/// Assert that output does NOT contain ANSI escape sequences.
///
/// Use this to verify that plain text rendering works correctly.
///
/// # Example
///
/// ```rust,ignore
/// let output = console.export_text("plain text");
/// assert_no_ansi_codes("plain export", &output);
/// ```
#[track_caller]
pub fn assert_no_ansi_codes(context: &str, output: &str) {
    tracing::debug!(
        context = context,
        output_len = output.len(),
        "asserting no ANSI codes"
    );

    if has_ansi_codes(output) {
        tracing::error!(
            context = context,
            output = output,
            "assertion failed: unexpected ANSI codes found"
        );
        panic!("{context}: expected no ANSI codes but found some.\nOutput: {output:?}");
    }

    tracing::trace!(context = context, "assertion passed: no ANSI codes");
}

/// Check if output contains a specific ANSI code.
///
/// # Example
///
/// ```rust,ignore
/// assert!(has_style(&output, ansi::BOLD), "Expected bold");
/// assert!(has_style(&output, ansi::FG_RED), "Expected red");
/// ```
#[must_use]
pub fn has_style(output: &str, ansi_code: &str) -> bool {
    output.contains(ansi_code)
}

/// Assert that output contains bold styling.
///
/// # Example
///
/// ```rust,ignore
/// let output = render_markup("[bold]text[/]");
/// assert_has_bold("bold output", &output);
/// ```
#[track_caller]
pub fn assert_has_bold(context: &str, output: &str) {
    tracing::debug!(context = context, "asserting has bold style");

    // Bold can be \x1b[1m or combined like \x1b[1;31m
    let has_bold = output.contains("\x1b[1m") || output.contains("\x1b[1;");

    if !has_bold {
        tracing::error!(
            context = context,
            output = output,
            "assertion failed: no bold ANSI code found"
        );
        panic!("{context}: expected bold style (SGR 1) but not found.\nOutput: {output:?}");
    }

    tracing::trace!(context = context, "assertion passed: has bold");
}

/// Assert that output contains italic styling.
///
/// # Example
///
/// ```rust,ignore
/// let output = render_markup("[italic]text[/]");
/// assert_has_italic("italic output", &output);
/// ```
#[track_caller]
pub fn assert_has_italic(context: &str, output: &str) {
    tracing::debug!(context = context, "asserting has italic style");

    // Italic is SGR 3
    let has_italic = output.contains("\x1b[3m") || output.contains(";3m") || output.contains(";3;");

    if !has_italic {
        tracing::error!(
            context = context,
            output = output,
            "assertion failed: no italic ANSI code found"
        );
        panic!("{context}: expected italic style (SGR 3) but not found.\nOutput: {output:?}");
    }

    tracing::trace!(context = context, "assertion passed: has italic");
}

/// Assert that output contains underline styling.
///
/// # Example
///
/// ```rust,ignore
/// let output = render_markup("[underline]text[/]");
/// assert_has_underline("underline output", &output);
/// ```
#[track_caller]
pub fn assert_has_underline(context: &str, output: &str) {
    tracing::debug!(context = context, "asserting has underline style");

    // Underline is SGR 4
    let has_underline =
        output.contains("\x1b[4m") || output.contains(";4m") || output.contains(";4;");

    if !has_underline {
        tracing::error!(
            context = context,
            output = output,
            "assertion failed: no underline ANSI code found"
        );
        panic!("{context}: expected underline style (SGR 4) but not found.\nOutput: {output:?}");
    }

    tracing::trace!(context = context, "assertion passed: has underline");
}

/// Assert that output contains a specific foreground color.
///
/// Checks for standard 16-color, 256-color, and true color sequences.
///
/// # Example
///
/// ```rust,ignore
/// assert_has_color("red text", &output, "red");
/// assert_has_color("green text", &output, "green");
/// ```
#[track_caller]
pub fn assert_has_color(context: &str, output: &str, color_name: &str) {
    tracing::debug!(context = context, color = color_name, "asserting has color");

    let has_color = match color_name.to_lowercase().as_str() {
        "black" => {
            output.contains("\x1b[30m")
                || output.contains("\x1b[38;5;0m")
                || output.contains("\x1b[38;2;0;0;0m")
        }
        "red" => {
            output.contains("\x1b[31m")
                || output.contains("\x1b[38;5;")
                || output.contains("\x1b[38;2;")
        }
        "green" => {
            output.contains("\x1b[32m")
                || output.contains("\x1b[38;5;")
                || output.contains("\x1b[38;2;")
        }
        "yellow" => {
            output.contains("\x1b[33m")
                || output.contains("\x1b[38;5;")
                || output.contains("\x1b[38;2;")
        }
        "blue" => {
            output.contains("\x1b[34m")
                || output.contains("\x1b[38;5;")
                || output.contains("\x1b[38;2;")
        }
        "magenta" => {
            output.contains("\x1b[35m")
                || output.contains("\x1b[38;5;")
                || output.contains("\x1b[38;2;")
        }
        "cyan" => {
            output.contains("\x1b[36m")
                || output.contains("\x1b[38;5;")
                || output.contains("\x1b[38;2;")
        }
        "white" => {
            output.contains("\x1b[37m")
                || output.contains("\x1b[38;5;15m")
                || output.contains("\x1b[38;2;")
        }
        _ => {
            // For unknown colors, just check if ANY color code is present
            output.contains("\x1b[3") || output.contains("\x1b[38;")
        }
    };

    if !has_color {
        tracing::error!(
            context = context,
            color = color_name,
            output = output,
            "assertion failed: color not found"
        );
        panic!("{context}: expected {color_name} color but not found.\nOutput: {output:?}");
    }

    tracing::trace!(
        context = context,
        color = color_name,
        "assertion passed: has color"
    );
}

/// Assert that output contains the reset sequence.
///
/// Properly styled output should reset styles after styled regions.
///
/// # Example
///
/// ```rust,ignore
/// assert_has_reset("styled output", &output);
/// ```
#[track_caller]
pub fn assert_has_reset(context: &str, output: &str) {
    tracing::debug!(context = context, "asserting has reset");

    if !output.contains(ansi::RESET) {
        tracing::error!(
            context = context,
            output = output,
            "assertion failed: no reset code found"
        );
        panic!("{context}: expected reset code (SGR 0) but not found.\nOutput: {output:?}");
    }

    tracing::trace!(context = context, "assertion passed: has reset");
}

/// Strip ANSI escape sequences from a string.
///
/// Returns plain text with all ANSI codes removed.
///
/// # Example
///
/// ```rust,ignore
/// let plain = strip_ansi("\x1b[1mBold\x1b[0m");
/// assert_eq!(plain, "Bold");
/// ```
#[must_use]
pub fn strip_ansi(s: &str) -> String {
    let re = regex::Regex::new(r"\x1b\[[0-9;]*m|\x1b\]8;;[^\x1b]*\x1b\\").unwrap();
    re.replace_all(s, "").to_string()
}

/// Assert that output contains NO raw markup tags.
///
/// Markup like `[bold]` should be parsed, not appear literally.
///
/// # Example
///
/// ```rust,ignore
/// let output = render_markup("[bold]text[/]");
/// assert_no_raw_markup("parsed output", &output);
/// ```
#[track_caller]
pub fn assert_no_raw_markup(context: &str, output: &str) {
    tracing::debug!(context = context, "asserting no raw markup tags");

    let markup_patterns = [
        "[bold]",
        "[/bold]",
        "[italic]",
        "[/italic]",
        "[underline]",
        "[/underline]",
        "[red]",
        "[/red]",
        "[green]",
        "[/green]",
        "[blue]",
        "[/blue]",
        "[yellow]",
        "[/yellow]",
        "[/]",
    ];

    for pattern in markup_patterns {
        if output.contains(pattern) {
            tracing::error!(
                context = context,
                pattern = pattern,
                output = output,
                "assertion failed: raw markup tag found"
            );
            panic!("{context}: found raw markup tag '{pattern}' in output.\nOutput: {output:?}");
        }
    }

    tracing::trace!(context = context, "assertion passed: no raw markup");
}

/// Count occurrences of a specific ANSI code in output.
///
/// Useful for verifying the correct number of style transitions.
///
/// # Example
///
/// ```rust,ignore
/// let bold_count = count_ansi_code(&output, ansi::BOLD);
/// assert_eq!(bold_count, 2, "Expected 2 bold regions");
/// ```
#[must_use]
pub fn count_ansi_code(output: &str, ansi_code: &str) -> usize {
    output.matches(ansi_code).count()
}

/// Extract styled regions from output.
///
/// Returns a vector of (text, has_style) tuples showing styled vs unstyled regions.
/// Useful for debugging and detailed style verification.
///
/// # Example
///
/// ```rust,ignore
/// let regions = extract_styled_regions(&output);
/// for (text, styled) in regions {
///     println!("{}: styled={}", text, styled);
/// }
/// ```
#[must_use]
pub fn extract_styled_regions(output: &str) -> Vec<(String, bool)> {
    let re = regex::Regex::new(r"(\x1b\[[0-9;]*m)").unwrap();
    let mut regions = Vec::new();
    let mut last_end = 0;
    let mut in_styled_region = false;

    for cap in re.captures_iter(output) {
        let mat = cap.get(0).unwrap();

        // Text before this escape sequence
        if mat.start() > last_end {
            let text = &output[last_end..mat.start()];
            if !text.is_empty() {
                regions.push((text.to_string(), in_styled_region));
            }
        }

        // Check if this is a reset or a style
        let code = cap.get(0).map_or("", |m| m.as_str());
        in_styled_region = code != ansi::RESET;

        last_end = mat.end();
    }

    // Remaining text after last escape
    if last_end < output.len() {
        let text = &output[last_end..];
        if !text.is_empty() {
            regions.push((text.to_string(), in_styled_region));
        }
    }

    regions
}

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

    #[test]
    fn test_assert_eq_logged_pass() {
        init_test_logging();
        assert_eq_logged("simple equality", 42, 42);
    }

    #[test]
    #[should_panic(expected = "expected 42")]
    fn test_assert_eq_logged_fail() {
        init_test_logging();
        assert_eq_logged("will fail", 0, 42);
    }

    #[test]
    fn test_assert_ok_logged_pass() {
        init_test_logging();
        let result: Result<i32, &str> = Ok(42);
        let value = assert_ok_logged("ok result", result);
        assert_eq!(value, 42);
    }

    #[test]
    fn test_assert_contains_logged_pass() {
        init_test_logging();
        assert_contains_logged("substring", "hello world", "world");
    }

    #[test]
    fn test_assert_len_logged_pass() {
        init_test_logging();
        let vec = vec![1, 2, 3, 4, 5];
        assert_len_logged("vec length", &vec, 5);
    }
}