gilt 2.1.0

Fast, beautiful terminal formatting for Rust — styles, tables, trees, syntax highlighting, progress bars, markdown.
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
//! Scope module -- renders a mapping of key-value pairs in a panel.
//!
//!
//! In Python, `render_scope` introspects a `dict` and displays variable names
//! and their repr values inside a bordered panel. In Rust we accept
//! pre-formatted `&str` pairs instead.

use std::fmt;

use crate::console::{Console, ConsoleOptions, Renderable};
use crate::panel::Panel;
use crate::segment::Segment;
use crate::style::Style;
use crate::table::{ColumnOptions, Table};
use crate::text::{JustifyMethod, Text};
use crate::utils::highlighter::Highlighter;
use crate::utils::padding::PaddingDimensions;

// ---------------------------------------------------------------------------
// Scope
// ---------------------------------------------------------------------------

/// A renderable that displays key-value pairs inside a bordered panel.
///
/// This is the Rust equivalent of Python's `render_scope`.
#[derive(Debug, Clone)]
pub struct Scope {
    /// The key-value pairs to display.
    items: Vec<(String, String)>,
    /// Optional title for the panel.
    title: Option<String>,
    /// Whether to sort keys (default: `true`).
    sort_keys: bool,
}

impl Scope {
    /// Create a new `Scope` with the given key-value pairs.
    pub fn new(items: Vec<(String, String)>) -> Self {
        Scope {
            items,
            title: None,
            sort_keys: true,
        }
    }

    /// Create a `Scope` from borrowed string slices.
    pub fn from_pairs(pairs: &[(&str, &str)]) -> Self {
        Scope {
            items: pairs
                .iter()
                .map(|(k, v)| (k.to_string(), v.to_string()))
                .collect(),
            title: None,
            sort_keys: true,
        }
    }

    /// Set the panel title.
    #[must_use]
    pub fn title(mut self, title: &str) -> Self {
        self.title = Some(title.to_string());
        self
    }

    /// Set whether keys should be sorted.
    #[must_use]
    pub fn sort_keys(mut self, sort: bool) -> Self {
        self.sort_keys = sort;
        self
    }

    /// Add a key-value pair.
    pub fn add(&mut self, key: &str, value: &str) {
        self.items.push((key.to_string(), value.to_string()));
    }

    /// Return a sorted or unsorted iterator over the items, following Python
    /// rich's sort order: dunder keys first (alphabetically), then regular
    /// keys (alphabetically, case-insensitive).
    fn ordered_items(&self) -> Vec<(&str, &str)> {
        let mut pairs: Vec<(&str, &str)> = self
            .items
            .iter()
            .map(|(k, v)| (k.as_str(), v.as_str()))
            .collect();

        if self.sort_keys {
            // Pre-compute lowercased keys (one alloc per pair) instead of
            // re-lowercasing on every comparison (O(N log N) allocs).
            let lower: Vec<String> = pairs.iter().map(|(k, _)| k.to_lowercase()).collect();
            let mut indices: Vec<usize> = (0..pairs.len()).collect();
            indices.sort_by(|&i, &j| {
                let a_regular = !pairs[i].0.starts_with("__");
                let b_regular = !pairs[j].0.starts_with("__");
                a_regular
                    .cmp(&b_regular)
                    .then_with(|| lower[i].cmp(&lower[j]))
            });
            pairs = indices.into_iter().map(|i| pairs[i]).collect();
        }

        pairs
    }

    /// Build the inner table and wrap it in a panel, returning segments.
    fn render_panel(&self, console: &Console, options: &ConsoleOptions) -> Vec<Segment> {
        // Build a grid table with padding (0, 1)
        let mut grid = Table::grid(&[]);
        grid.padding = (0, 1, 0, 1);

        // Key column: right-justified
        grid.add_column(
            "",
            "",
            ColumnOptions {
                justify: Some(JustifyMethod::Right),
                ..Default::default()
            },
        );
        // Equals column: left-justified, no extra padding so "=" sits next
        // to the key. The column-style is the equals theme style.
        grid.add_column(
            "",
            "scope.equals",
            ColumnOptions {
                justify: Some(JustifyMethod::Left),
                ..Default::default()
            },
        );
        // Value column: default (left-justified)
        grid.add_column("", "", ColumnOptions::default());

        // Resolve the per-cell theme styles once. `get_style` falls back
        // to `Style::null()` on any failure (missing theme entry, parse
        // error) so the render never breaks on a custom theme.
        let key_style = console
            .get_style("scope.key")
            .unwrap_or_else(|_| Style::null());
        let key_special_style = console
            .get_style("scope.key.special")
            .unwrap_or_else(|_| Style::null());
        let equals_style = console
            .get_style("scope.equals")
            .unwrap_or_else(|_| Style::null());
        let highlighter = crate::utils::highlighter::ReprHighlighter::new();

        let items = self.ordered_items();

        for (key, value) in &items {
            // Per-cell styling (Phase 7 / Plan 7.27): mirror rich's
            // `render_scope` by splitting the row into three styled cells
            // — key, "=", value. The key uses `scope.key` for normal
            // identifiers and `scope.key.special` for dunder / special
            // names (keys starting with `__` — rich parity: rich uses
            // `key.startswith("__")` for both sorting and styling); the
            // equals gets `scope.equals`; the value is run through
            // `ReprHighlighter` so numbers / strings / paths / etc. are
            // colored regardless of the console's `highlight` setting.
            let is_special = key.starts_with("__");
            let cell_key_style = if is_special {
                key_special_style.clone()
            } else {
                key_style.clone()
            };
            let key_text = Text::new(key, cell_key_style);
            let equals_text = Text::new(" =", equals_style.clone());
            let mut value_text = Text::new(value, Style::null());
            highlighter.highlight(&mut value_text);
            grid.add_row_text(&[key_text, equals_text, value_text]);
        }

        // Get the border style
        let border_style = console
            .get_style("scope.border")
            .unwrap_or_else(|_| Style::null());

        // Render the grid table and rebuild a styled Text from the resulting
        // segments — preserves per-segment styles (was previously lost via
        // `seg.text.push_str` into a plain String).
        let table_segments = grid.gilt_console(console, options);
        let mut content = Text::new("", Style::null());
        for seg in &table_segments {
            // Skip the trailing terminator-only segments; the Text appender
            // handles internal newlines correctly.
            content.append_str(&seg.text, seg.style().cloned());
        }
        // Trim the final newline for cleaner Panel rendering.
        if content.plain().ends_with('\n') {
            let new_len = content.plain().len() - 1;
            let new_text = content.plain()[..new_len].to_string();
            content.set_plain(&new_text);
        }

        // Build the panel
        let mut panel = Panel::fit(content)
            .with_border_style(border_style)
            .with_padding(PaddingDimensions::Pair(0, 1));

        if let Some(ref title) = self.title {
            panel = panel.with_title(Text::new(title, Style::null()));
        }

        panel.gilt_console(console, options)
    }
}

// ---------------------------------------------------------------------------
// Renderable
// ---------------------------------------------------------------------------

impl Renderable for Scope {
    fn gilt_console(&self, console: &Console, options: &ConsoleOptions) -> Vec<Segment> {
        self.render_panel(console, options)
    }
}

// ---------------------------------------------------------------------------
// Display
// ---------------------------------------------------------------------------

impl fmt::Display for Scope {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let w = f.width().unwrap_or(80);
        let mut console = Console::builder()
            .width(w)
            .force_terminal(true)
            .no_color(true)
            .build();
        console.begin_capture();
        console.print(self);
        let output = console.end_capture();
        write!(f, "{}", output.trim_end_matches('\n'))
    }
}

// ---------------------------------------------------------------------------
// render_scope (free function)
// ---------------------------------------------------------------------------

/// Render key-value pairs as a panel with a table grid inside.
///
/// This is the direct Rust equivalent of Python's `render_scope()`.
///
/// # Arguments
///
/// * `scope` - Slice of `(key, value)` string pairs to display.
/// * `title` - Optional title for the panel border.
/// * `sort_keys` - If `true`, sort keys with dunder keys first.
///
/// # Returns
///
/// A `Vec<Segment>` ready for console output.
pub fn render_scope(scope: &[(&str, &str)], title: Option<&str>, sort_keys: bool) -> Vec<Segment> {
    let console = Console::builder()
        .width(80)
        .force_terminal(true)
        .no_color(true)
        .markup(false)
        .build();
    let options = console.options();

    let mut builder = Scope::from_pairs(scope);
    builder.sort_keys = sort_keys;
    if let Some(t) = title {
        builder.title = Some(t.to_string());
    }

    builder.gilt_console(&console, &options)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_console(width: usize) -> Console {
        Console::builder()
            .width(width)
            .force_terminal(true)
            .no_color(true)
            .markup(false)
            .build()
    }

    fn segments_to_text(segments: &[Segment]) -> String {
        segments.iter().map(|s| s.text.as_str()).collect()
    }

    fn render_scope_output(scope: &Scope, width: usize) -> String {
        let console = make_console(width);
        let opts = console.options();
        let segments = scope.gilt_console(&console, &opts);
        segments_to_text(&segments)
    }

    // -- Empty scope --------------------------------------------------------

    #[test]
    fn test_empty_scope() {
        let scope = Scope::new(vec![]);
        let output = render_scope_output(&scope, 40);
        // Should still produce a panel (empty content)
        assert!(output.contains('\u{256d}') || output.contains('\u{250c}') || output.contains('+'));
    }

    // -- Single key-value pair ----------------------------------------------

    #[test]
    fn test_single_pair() {
        let scope = Scope::from_pairs(&[("name", "Alice")]);
        let output = render_scope_output(&scope, 40);
        assert!(output.contains("name"), "output was: {:?}", output);
        assert!(output.contains("Alice"), "output was: {:?}", output);
        assert!(output.contains("="), "output was: {:?}", output);
    }

    // -- Multiple pairs -----------------------------------------------------

    #[test]
    fn test_multiple_pairs() {
        let scope = Scope::from_pairs(&[("x", "1"), ("y", "2"), ("z", "3")]);
        let output = render_scope_output(&scope, 40);
        assert!(output.contains("x"));
        assert!(output.contains("y"));
        assert!(output.contains("z"));
        assert!(output.contains("1"));
        assert!(output.contains("2"));
        assert!(output.contains("3"));
    }

    // -- Sort keys (alphabetical) -------------------------------------------

    #[test]
    fn test_sort_keys_alphabetical() {
        let scope = Scope::from_pairs(&[("zebra", "z"), ("apple", "a"), ("mango", "m")]);
        let output = render_scope_output(&scope, 40);
        // All keys should appear
        assert!(output.contains("zebra"));
        assert!(output.contains("apple"));
        assert!(output.contains("mango"));

        // Check ordering: apple should come before mango, mango before zebra
        let apple_pos = output.find("apple").unwrap();
        let mango_pos = output.find("mango").unwrap();
        let zebra_pos = output.find("zebra").unwrap();
        assert!(apple_pos < mango_pos);
        assert!(mango_pos < zebra_pos);
    }

    // -- Special keys (__dunder__) sorted first -----------------------------

    #[test]
    fn test_dunder_keys_sorted_first() {
        let scope = Scope::from_pairs(&[
            ("regular", "val"),
            ("__special__", "dunder"),
            ("another", "val2"),
        ]);
        let output = render_scope_output(&scope, 50);
        // __special__ should appear before regular keys
        let special_pos = output.find("__special__").unwrap();
        let regular_pos = output.find("regular").unwrap();
        let another_pos = output.find("another").unwrap();
        assert!(
            special_pos < regular_pos,
            "dunder key should come first: special={}, regular={}",
            special_pos,
            regular_pos
        );
        assert!(
            special_pos < another_pos,
            "dunder key should come first: special={}, another={}",
            special_pos,
            another_pos
        );
    }

    #[test]
    fn test_multiple_dunder_keys_sorted() {
        let scope = Scope::from_pairs(&[
            ("beta", "b"),
            ("__z__", "z"),
            ("alpha", "a"),
            ("__a__", "a"),
        ]);
        let output = render_scope_output(&scope, 50);
        // Dunder keys should come first, then regular keys
        let z_dunder_pos = output.find("__z__").unwrap();
        let a_dunder_pos = output.find("__a__").unwrap();
        let alpha_pos = output.find("alpha").unwrap();
        let beta_pos = output.find("beta").unwrap();

        // __a__ before __z__ (alphabetical among dunders)
        assert!(a_dunder_pos < z_dunder_pos);
        // Both dunders before regular keys
        assert!(z_dunder_pos < alpha_pos);
        assert!(z_dunder_pos < beta_pos);
        // alpha before beta
        assert!(alpha_pos < beta_pos);
    }

    // -- Title displayed ----------------------------------------------------

    #[test]
    fn test_title_displayed() {
        let scope = Scope::from_pairs(&[("key", "value")]).title("My Scope");
        let output = render_scope_output(&scope, 40);
        assert!(output.contains("My Scope"), "output was: {:?}", output);
    }

    // -- No title -----------------------------------------------------------

    #[test]
    fn test_no_title() {
        let scope = Scope::from_pairs(&[("key", "value")]);
        assert!(scope.title.is_none());
        let output = render_scope_output(&scope, 40);
        // Should still render without error
        assert!(output.contains("key"));
        assert!(output.contains("value"));
    }

    // -- Key styling --------------------------------------------------------

    #[test]
    fn test_key_contains_equals() {
        let scope = Scope::from_pairs(&[("myvar", "42")]);
        let output = render_scope_output(&scope, 40);
        // The output should contain "myvar =" (key followed by equals)
        assert!(output.contains("myvar ="), "output was: {:?}", output);
    }

    #[test]
    fn test_special_key_format() {
        let scope = Scope::from_pairs(&[("__init__", "method")]);
        let output = render_scope_output(&scope, 40);
        assert!(output.contains("__init__"), "output was: {:?}", output);
        assert!(output.contains("="), "output was: {:?}", output);
    }

    // -- Values rendered in output ------------------------------------------

    #[test]
    fn test_value_rendered() {
        let scope = Scope::from_pairs(&[("count", "42"), ("name", "hello")]);
        let output = render_scope_output(&scope, 40);
        assert!(output.contains("42"));
        assert!(output.contains("hello"));
    }

    // -- Renderable trait integration ---------------------------------------

    #[test]
    fn test_renderable_trait() {
        let scope = Scope::from_pairs(&[("a", "1")]);
        let console = make_console(40);
        let opts = console.options();
        let segments = scope.gilt_console(&console, &opts);
        assert!(!segments.is_empty());
        let text = segments_to_text(&segments);
        assert!(text.contains("a"));
        assert!(text.contains("1"));
    }

    #[test]
    fn test_renderable_produces_panel() {
        let scope = Scope::from_pairs(&[("x", "10")]);
        let console = make_console(30);
        let opts = console.options();
        let segments = scope.gilt_console(&console, &opts);
        let text = segments_to_text(&segments);
        // Panel should have border characters (rounded box)
        assert!(
            text.contains('\u{256d}') || text.contains('\u{250c}'),
            "expected panel border in: {:?}",
            text
        );
    }

    // -- Builder methods ----------------------------------------------------

    #[test]
    fn test_builder_title() {
        let scope = Scope::from_pairs(&[("k", "v")]).title("Title");
        assert_eq!(scope.title, Some("Title".to_string()));
    }

    #[test]
    fn test_builder_sort_keys() {
        let scope = Scope::from_pairs(&[("k", "v")]).sort_keys(false);
        assert!(!scope.sort_keys);
    }

    #[test]
    fn test_builder_chain() {
        let scope = Scope::from_pairs(&[("k", "v")]).title("T").sort_keys(false);
        assert_eq!(scope.title, Some("T".to_string()));
        assert!(!scope.sort_keys);
    }

    // -- Add method ---------------------------------------------------------

    #[test]
    fn test_add_items() {
        let mut scope = Scope::new(vec![]);
        scope.add("first", "1");
        scope.add("second", "2");
        assert_eq!(scope.items.len(), 2);
        assert_eq!(scope.items[0], ("first".to_string(), "1".to_string()));
        assert_eq!(scope.items[1], ("second".to_string(), "2".to_string()));
    }

    // -- Sort keys disabled -------------------------------------------------

    #[test]
    fn test_no_sort_preserves_order() {
        let scope =
            Scope::from_pairs(&[("zebra", "z"), ("apple", "a"), ("mango", "m")]).sort_keys(false);
        let output = render_scope_output(&scope, 40);
        // Original order should be preserved
        let zebra_pos = output.find("zebra").unwrap();
        let apple_pos = output.find("apple").unwrap();
        let mango_pos = output.find("mango").unwrap();
        assert!(zebra_pos < apple_pos);
        assert!(apple_pos < mango_pos);
    }

    // -- render_scope free function -----------------------------------------

    #[test]
    fn test_render_scope_function() {
        let segments = render_scope(&[("key", "value")], Some("Title"), true);
        let text = segments_to_text(&segments);
        assert!(text.contains("key"));
        assert!(text.contains("value"));
        assert!(text.contains("Title"));
    }

    #[test]
    fn test_render_scope_no_title() {
        let segments = render_scope(&[("a", "b")], None, true);
        let text = segments_to_text(&segments);
        assert!(text.contains("a"));
        assert!(text.contains("b"));
    }

    #[test]
    fn test_render_scope_empty() {
        let segments = render_scope(&[], None, true);
        let text = segments_to_text(&segments);
        // Should produce a panel even with no items
        assert!(!text.is_empty());
    }

    // -- Ordered items internal method --------------------------------------

    #[test]
    fn test_ordered_items_sort() {
        let scope = Scope::from_pairs(&[("B", "2"), ("a", "1"), ("C", "3")]);
        let ordered = scope.ordered_items();
        assert_eq!(ordered[0].0, "a");
        assert_eq!(ordered[1].0, "B");
        assert_eq!(ordered[2].0, "C");
    }

    #[test]
    fn test_ordered_items_no_sort() {
        let scope = Scope::from_pairs(&[("B", "2"), ("a", "1"), ("C", "3")]).sort_keys(false);
        let ordered = scope.ordered_items();
        assert_eq!(ordered[0].0, "B");
        assert_eq!(ordered[1].0, "a");
        assert_eq!(ordered[2].0, "C");
    }

    #[test]
    fn test_ordered_items_dunders_first() {
        let scope = Scope::from_pairs(&[("normal", "n"), ("__dunder__", "d"), ("alpha", "a")]);
        let ordered = scope.ordered_items();
        assert_eq!(ordered[0].0, "__dunder__");
        assert_eq!(ordered[1].0, "alpha");
        assert_eq!(ordered[2].0, "normal");
    }

    // -- Case insensitive sort ----------------------------------------------

    #[test]
    fn test_case_insensitive_sort() {
        let scope = Scope::from_pairs(&[("Zulu", "z"), ("alpha", "a"), ("Beta", "b")]);
        let ordered = scope.ordered_items();
        assert_eq!(ordered[0].0, "alpha");
        assert_eq!(ordered[1].0, "Beta");
        assert_eq!(ordered[2].0, "Zulu");
    }

    // -- Panel border style -------------------------------------------------

    #[test]
    fn test_panel_has_border() {
        let scope = Scope::from_pairs(&[("x", "1")]);
        let console = make_console(30);
        let opts = console.options();
        let segments = scope.gilt_console(&console, &opts);
        let text = segments_to_text(&segments);
        let lines: Vec<&str> = text.split('\n').filter(|l| !l.is_empty()).collect();
        // Should have at least 3 lines: top border, content, bottom border
        assert!(
            lines.len() >= 3,
            "expected at least 3 lines, got {}: {:?}",
            lines.len(),
            lines
        );
    }

    // -- Scope with long values ---------------------------------------------

    #[test]
    fn test_long_values() {
        let scope = Scope::from_pairs(&[
            ("short", "ok"),
            ("long_key", "this is a rather long value string"),
        ]);
        let output = render_scope_output(&scope, 60);
        assert!(output.contains("short"));
        assert!(output.contains("long_key"));
        assert!(output.contains("this is a rather long value string"));
    }

    // -- Scope with numeric-looking values ----------------------------------

    #[test]
    fn test_numeric_values() {
        let scope = Scope::from_pairs(&[("pi", "3.14159"), ("count", "42"), ("hex", "0xFF")]);
        let output = render_scope_output(&scope, 40);
        assert!(output.contains("3.14159"));
        assert!(output.contains("42"));
        assert!(output.contains("0xFF"));
    }

    // -- from_pairs constructor ---------------------------------------------

    #[test]
    fn test_from_pairs() {
        let scope = Scope::from_pairs(&[("a", "1"), ("b", "2")]);
        assert_eq!(scope.items.len(), 2);
        assert_eq!(scope.items[0].0, "a");
        assert_eq!(scope.items[0].1, "1");
        assert!(scope.sort_keys);
        assert!(scope.title.is_none());
    }

    // -- Clone --------------------------------------------------------------

    #[test]
    fn test_scope_clone() {
        let scope = Scope::from_pairs(&[("k", "v")]).title("T").sort_keys(false);
        let cloned = scope.clone();
        assert_eq!(cloned.items, scope.items);
        assert_eq!(cloned.title, scope.title);
        assert_eq!(cloned.sort_keys, scope.sort_keys);
    }

    #[test]
    fn render_preserves_styling_from_inner_table() {
        // Regression for B2: render_panel was flattening the inner table's
        // styled segments into a plain String, dropping all SGR markup. Verify
        // ANSI escapes from styled cells reach the captured output.
        use crate::console::Console;
        let scope = Scope::from_pairs(&[("user", "alice"), ("role", "admin")]).title("Login");
        let mut console = Console::builder()
            .width(60)
            .force_terminal(true)
            .color_system("truecolor")
            .build();
        console.begin_capture();
        console.print(&scope);
        let out = console.end_capture();
        // Some SGR sequence must be present — the cell-key style ("scope.key")
        // and panel border style produce ANSI escapes.
        assert!(
            out.contains('\x1b'),
            "expected ANSI escapes in scope render, got: {out:?}"
        );
        assert!(out.contains("user"));
        assert!(out.contains("alice"));
    }
    // -- Per-cell styling (Phase 7 / Plan 7.27) ----------------------------

    #[test]
    fn test_key_cell_carries_scope_key_style() {
        // Per-cell key styling (Phase 7 / Plan 7.27): the segment carrying
        // the key text "count" must carry the resolved "scope.key" theme
        // style. The segment carrying "42" must carry the "repr.number"
        // theme style (from ReprHighlighter). The "=" segment must carry
        // the resolved "scope.equals" theme style.
        let scope = Scope::from_pairs(&[("count", "42")]);
        let console = Console::builder()
            .width(60)
            .force_terminal(true)
            .color_system("truecolor")
            .highlight(false)
            .build();
        let opts = console.options();
        let key_style = console
            .get_style("scope.key")
            .expect("scope.key must be defined in default theme");
        let equals_style = console
            .get_style("scope.equals")
            .expect("scope.equals must be defined in default theme");
        let number_style = console
            .get_style("repr.number")
            .expect("repr.number must be defined in default theme");
        let segments = scope.gilt_console(&console, &opts);
        let segments: Vec<&Segment> = segments.iter().filter(|s| !s.text.is_empty()).collect();
        let key_segs: Vec<&&Segment> = segments
            .iter()
            .filter(|s| s.text.contains("count"))
            .collect();
        let eq_segs: Vec<&&Segment> = segments.iter().filter(|s| s.text.contains("=")).collect();
        let val_segs: Vec<&&Segment> = segments.iter().filter(|s| s.text.contains("42")).collect();
        assert!(
            !key_segs.is_empty(),
            "expected at least one segment containing key text 'count'"
        );
        assert!(
            !eq_segs.is_empty(),
            "expected at least one segment containing '='"
        );
        assert!(
            !val_segs.is_empty(),
            "expected at least one segment containing value '42'"
        );
        for seg in &key_segs {
            let st = seg.style().expect("key cell segment must carry a style");
            assert_eq!(
                st, &key_style,
                "key cell must carry scope.key style; got {st:?}"
            );
        }
        for seg in &eq_segs {
            let st = seg.style().expect("equals cell segment must carry a style");
            assert_eq!(
                st, &equals_style,
                "equals cell must carry scope.equals style; got {st:?}"
            );
        }
        for seg in &val_segs {
            let st = seg.style().expect("value cell segment must carry a style");
            assert_eq!(
                st, &number_style,
                "value cell must carry repr.number style (ReprHighlighter); got {st:?}"
            );
        }
    }

    #[test]
    fn test_dunder_key_uses_special_style() {
        // A dunder key like "__init__" must use the "scope.key.special"
        // theme style for its key cell, not the regular "scope.key".
        let scope = Scope::from_pairs(&[("__init__", "method")]);
        let console = Console::builder()
            .width(60)
            .force_terminal(true)
            .color_system("truecolor")
            .highlight(false)
            .build();
        let opts = console.options();
        let special_style = console
            .get_style("scope.key.special")
            .expect("scope.key.special must be defined in default theme");
        let key_style = console.get_style("scope.key").unwrap();
        assert_ne!(
            special_style, key_style,
            "scope.key.special must differ from scope.key in the theme"
        );
        let segments = scope.gilt_console(&console, &opts);
        let segments: Vec<&Segment> = segments.iter().filter(|s| !s.text.is_empty()).collect();
        let key_segs: Vec<&&Segment> = segments
            .iter()
            .filter(|s| s.text.contains("__init__"))
            .collect();
        assert!(
            !key_segs.is_empty(),
            "expected at least one segment containing '__init__'"
        );
        for seg in &key_segs {
            let st = seg.style().expect("dunder key segment must be styled");
            assert_eq!(
                st, &special_style,
                "dunder key cell must carry scope.key.special; got {st:?}"
            );
        }
    }

    #[test]
    fn test_value_repr_highlighted() {
        // A numeric value "3.14" must be ReprHighlighter-annotated — the
        // segment carrying the digits must carry the resolved
        // "repr.number" theme style.
        let scope = Scope::from_pairs(&[("count", "3.14")]);
        let console = Console::builder()
            .width(60)
            .force_terminal(true)
            .color_system("truecolor")
            .highlight(false)
            .build();
        let opts = console.options();
        let number_style = console
            .get_style("repr.number")
            .expect("repr.number must be defined in default theme");
        let segments = scope.gilt_console(&console, &opts);
        let segments: Vec<&Segment> = segments.iter().filter(|s| !s.text.is_empty()).collect();
        let val_segs: Vec<&&Segment> = segments
            .iter()
            .filter(|s| s.text.contains("3.14"))
            .collect();
        assert!(
            !val_segs.is_empty(),
            "expected at least one segment containing value '3.14'"
        );
        for seg in &val_segs {
            let st = seg
                .style()
                .expect("value segment must carry a style (ReprHighlighter)");
            assert_eq!(
                st, &number_style,
                "value cell must carry repr.number style; got {st:?}"
            );
        }
    }

    /// Deep-review: rich uses `key.startswith("__")` (double underscore) to
    /// decide `scope.key.special` — not `starts_with('_') && ends_with('_')`.
    /// A key like `__private` (dunder prefix, no trailing underscore) must
    /// use `scope.key.special`.  Previously gilt required both leading AND
    /// trailing underscore, so `__private` wrongly got the regular style.
    #[test]
    fn test_dunder_prefix_only_uses_special_style() {
        let scope = Scope::from_pairs(&[("__private", "val")]);
        let console = Console::builder()
            .width(60)
            .force_terminal(true)
            .color_system("truecolor")
            .highlight(false)
            .build();
        let opts = console.options();
        let special_style = console
            .get_style("scope.key.special")
            .expect("scope.key.special must be defined in default theme");
        let key_style = console.get_style("scope.key").unwrap();
        assert_ne!(
            special_style, key_style,
            "scope.key.special must differ from scope.key in the theme"
        );
        let segments = scope.gilt_console(&console, &opts);
        let key_segs: Vec<&Segment> = segments
            .iter()
            .filter(|s| s.text.contains("__private"))
            .collect();
        assert!(
            !key_segs.is_empty(),
            "expected at least one segment containing '__private'"
        );
        for seg in &key_segs {
            let st = seg
                .style()
                .expect("dunder-prefix key segment must be styled");
            assert_eq!(
                st, &special_style,
                "dunder-prefix key '__private' must carry scope.key.special (rich uses startswith('__')); got {st:?}"
            );
        }
    }

    /// Deep-review: single-underscore `_private` must use `scope.key` (not
    /// special) — rich only treats double-underscore keys as special.
    #[test]
    fn test_single_underscore_uses_regular_style() {
        let scope = Scope::from_pairs(&[("_private", "val")]);
        let console = Console::builder()
            .width(60)
            .force_terminal(true)
            .color_system("truecolor")
            .highlight(false)
            .build();
        let opts = console.options();
        let key_style = console.get_style("scope.key").unwrap();
        let special_style = console.get_style("scope.key.special").unwrap();
        let segments = scope.gilt_console(&console, &opts);
        let key_segs: Vec<&Segment> = segments
            .iter()
            .filter(|s| s.text.contains("_private"))
            .collect();
        assert!(
            !key_segs.is_empty(),
            "expected at least one segment containing '_private'"
        );
        for seg in &key_segs {
            let st = seg.style().expect("key segment must be styled");
            assert_eq!(
                st, &key_style,
                "single-underscore key '_private' must carry scope.key (not special); got {st:?}"
            );
            assert_ne!(
                st, &special_style,
                "single-underscore key must NOT carry scope.key.special"
            );
        }
    }
}