rich-rs 1.2.2

Rich text and beautiful formatting for the terminal
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
//! Panel: a bordered box around content.
//!
//! Panel wraps any renderable in a border with optional title and subtitle.
//!
//! # Example
//!
//! ```
//! use rich_rs::{Panel, Text};
//!
//! let text = Text::plain("Hello, World!");
//! let panel = Panel::new(Box::new(text))
//!     .with_title("My Panel")
//!     .with_box(rich_rs::r#box::ROUNDED);
//! ```

use std::io::Stdout;

use crate::r#box::{Box as RichBox, ROUNDED};
use crate::console::ConsoleOptions;
use crate::measure::{Measurement, measure_renderables};
use crate::padding::PaddingDimensions;
use crate::rule::AlignMethod;
use crate::segment::{Segment, Segments};
use crate::style::Style;
use crate::text::Text;
use crate::{Console, Renderable};

// ============================================================================
// Panel
// ============================================================================

/// A bordered box around content.
///
/// Panel draws a border around any renderable, with optional title and subtitle.
///
/// # Example
///
/// ```
/// use rich_rs::{Panel, Text, Style};
/// use rich_rs::r#box::DOUBLE;
///
/// let text = Text::plain("Hello, World!");
/// let panel = Panel::new(Box::new(text))
///     .with_title("Title")
///     .with_subtitle("Subtitle")
///     .with_box(DOUBLE)
///     .with_border_style(Style::new().with_bold(true));
/// ```
pub struct Panel {
    /// The content to wrap.
    renderable: Box<dyn Renderable + Send + Sync>,
    /// Border style (box drawing characters).
    box_type: RichBox,
    /// Optional title displayed in the header.
    title: Option<Text>,
    /// Optional subtitle displayed in the footer.
    subtitle: Option<Text>,
    /// Title alignment.
    title_align: AlignMethod,
    /// Subtitle alignment.
    subtitle_align: AlignMethod,
    /// Use ASCII-safe box characters (None = use console default).
    safe_box: Option<bool>,
    /// Expand to fill available width.
    expand: bool,
    /// Style for the panel (border and content background).
    style: Style,
    /// Style for the border only.
    border_style: Style,
    /// Fixed width (None = auto).
    width: Option<usize>,
    /// Fixed height (None = auto).
    height: Option<usize>,
    /// Inner padding (top, right, bottom, left).
    padding: (usize, usize, usize, usize),
    /// Enable highlighting of content.
    highlight: bool,
}

impl std::fmt::Debug for Panel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Panel")
            .field("box_type", &self.box_type)
            .field("title", &self.title)
            .field("subtitle", &self.subtitle)
            .field("title_align", &self.title_align)
            .field("subtitle_align", &self.subtitle_align)
            .field("safe_box", &self.safe_box)
            .field("expand", &self.expand)
            .field("style", &self.style)
            .field("border_style", &self.border_style)
            .field("width", &self.width)
            .field("height", &self.height)
            .field("padding", &self.padding)
            .field("highlight", &self.highlight)
            .finish_non_exhaustive()
    }
}

impl Panel {
    /// Create a new panel with default settings (expands to fill width).
    ///
    /// # Arguments
    ///
    /// * `renderable` - The content to wrap in the panel.
    ///
    /// # Example
    ///
    /// ```
    /// use rich_rs::{Panel, Text};
    ///
    /// let text = Text::plain("Hello");
    /// let panel = Panel::new(Box::new(text));
    /// ```
    pub fn new(renderable: Box<dyn Renderable + Send + Sync>) -> Self {
        Panel {
            renderable,
            box_type: ROUNDED,
            title: None,
            subtitle: None,
            title_align: AlignMethod::Center,
            subtitle_align: AlignMethod::Center,
            safe_box: None,
            expand: true,
            style: Style::default(),
            border_style: Style::default(),
            width: None,
            height: None,
            padding: (0, 1, 0, 1), // Default: 1 space left/right padding
            highlight: false,
        }
    }

    /// Create a panel that fits its content (expand=false).
    ///
    /// # Arguments
    ///
    /// * `renderable` - The content to wrap in the panel.
    ///
    /// # Example
    ///
    /// ```
    /// use rich_rs::{Panel, Text};
    ///
    /// let text = Text::plain("Hello");
    /// let panel = Panel::fit(Box::new(text));
    /// ```
    pub fn fit(renderable: Box<dyn Renderable + Send + Sync>) -> Self {
        let mut panel = Self::new(renderable);
        panel.expand = false;
        panel
    }

    /// Set the box style for the border.
    pub fn with_box(mut self, box_type: RichBox) -> Self {
        self.box_type = box_type;
        self
    }

    /// Set the title displayed in the panel header.
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        let title_str = title.into();
        // Try to parse as markup, fall back to plain
        let title_text =
            Text::from_markup(&title_str, false).unwrap_or_else(|_| Text::plain(&title_str));
        self.title = Some(title_text);
        self
    }

    /// Set the title using a Text object directly.
    pub fn with_title_text(mut self, title: Text) -> Self {
        self.title = Some(title);
        self
    }

    /// Set the subtitle displayed in the panel footer.
    pub fn with_subtitle(mut self, subtitle: impl Into<String>) -> Self {
        let subtitle_str = subtitle.into();
        let subtitle_text =
            Text::from_markup(&subtitle_str, false).unwrap_or_else(|_| Text::plain(&subtitle_str));
        self.subtitle = Some(subtitle_text);
        self
    }

    /// Set the subtitle using a Text object directly.
    pub fn with_subtitle_text(mut self, subtitle: Text) -> Self {
        self.subtitle = Some(subtitle);
        self
    }

    /// Set the title alignment.
    pub fn with_title_align(mut self, align: AlignMethod) -> Self {
        self.title_align = align;
        self
    }

    /// Set the subtitle alignment.
    pub fn with_subtitle_align(mut self, align: AlignMethod) -> Self {
        self.subtitle_align = align;
        self
    }

    /// Set whether to use ASCII-safe box characters.
    pub fn with_safe_box(mut self, safe: bool) -> Self {
        self.safe_box = Some(safe);
        self
    }

    /// Set whether to expand to fill available width.
    pub fn with_expand(mut self, expand: bool) -> Self {
        self.expand = expand;
        self
    }

    /// Set the panel style (applied to border and content background).
    pub fn with_style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set the border style (color/attributes for border characters).
    pub fn with_border_style(mut self, style: Style) -> Self {
        self.border_style = style;
        self
    }

    /// Set a fixed width for the panel.
    pub fn with_width(mut self, width: usize) -> Self {
        self.width = Some(width);
        self
    }

    /// Set a fixed height for the panel.
    pub fn with_height(mut self, height: usize) -> Self {
        self.height = Some(height);
        self
    }

    /// Set the inner padding.
    ///
    /// Accepts CSS-style padding: 1 value (all), 2 values (vertical, horizontal),
    /// or 4 values (top, right, bottom, left).
    pub fn with_padding(mut self, padding: impl Into<PaddingDimensions>) -> Self {
        self.padding = padding.into().unpack();
        self
    }

    /// Set whether to enable highlighting of content.
    pub fn with_highlight(mut self, highlight: bool) -> Self {
        self.highlight = highlight;
        self
    }

    // ========================================================================
    // Getters
    // ========================================================================

    /// Get the box type.
    pub fn box_type(&self) -> RichBox {
        self.box_type
    }

    /// Get the title.
    pub fn title(&self) -> Option<&Text> {
        self.title.as_ref()
    }

    /// Get the subtitle.
    pub fn subtitle(&self) -> Option<&Text> {
        self.subtitle.as_ref()
    }

    /// Get whether expand is enabled.
    pub fn expand(&self) -> bool {
        self.expand
    }

    /// Get the style.
    pub fn style(&self) -> Style {
        self.style
    }

    /// Get the border style.
    pub fn border_style(&self) -> Style {
        self.border_style
    }

    /// Get the padding.
    pub fn padding(&self) -> (usize, usize, usize, usize) {
        self.padding
    }

    // ========================================================================
    // Internal helpers
    // ========================================================================

    /// Prepare title text for rendering (strip newlines, add padding).
    fn prepare_title(&self) -> Option<Text> {
        self.title.as_ref().map(|title| {
            let mut t = title.copy();
            // Replace newlines with spaces
            let plain = t.plain_text().replace('\n', " ");
            t = Text::plain(&plain);
            // Copy spans from original, adjusting for any length changes
            for span in title.spans() {
                if span.start < plain.len() && span.end > span.start {
                    t.stylize(span.start, span.end.min(plain.len()), span.style);
                }
            }
            if let Some(base) = title.base_style() {
                t.set_base_style(Some(base));
            }
            // Add 1 space padding on each side
            let mut padded = Text::plain(" ");
            padded.append_text(&t);
            padded.append(" ", None);
            padded
        })
    }

    /// Prepare subtitle text for rendering.
    fn prepare_subtitle(&self) -> Option<Text> {
        self.subtitle.as_ref().map(|subtitle| {
            let mut t = subtitle.copy();
            let plain = t.plain_text().replace('\n', " ");
            t = Text::plain(&plain);
            for span in subtitle.spans() {
                if span.start < plain.len() && span.end > span.start {
                    t.stylize(span.start, span.end.min(plain.len()), span.style);
                }
            }
            if let Some(base) = subtitle.base_style() {
                t.set_base_style(Some(base));
            }
            let mut padded = Text::plain(" ");
            padded.append_text(&t);
            padded.append(" ", None);
            padded
        })
    }

    /// Align title/subtitle text within available width.
    fn align_text(
        &self,
        text: &Text,
        width: usize,
        align: AlignMethod,
        fill_char: char,
        style: Style,
    ) -> Text {
        let mut t = text.copy();

        // Truncate if too long
        if t.cell_len() > width {
            t = t.truncate(width, crate::console::OverflowMethod::Crop, false);
        }

        let excess_space = width.saturating_sub(t.cell_len());
        if excess_space == 0 {
            return t;
        }

        // Apply border style as base before alignment
        t.stylize_before(style, 0, None);

        match align {
            AlignMethod::Left => {
                // Pad right with fill char
                let fill: String = std::iter::repeat(fill_char).take(excess_space).collect();
                let mut result = t;
                result.append(&fill, Some(style));
                result
            }
            AlignMethod::Center => {
                let left_pad = excess_space / 2;
                let right_pad = excess_space - left_pad;
                let left_fill: String = std::iter::repeat(fill_char).take(left_pad).collect();
                let right_fill: String = std::iter::repeat(fill_char).take(right_pad).collect();

                let mut result = Text::styled(&left_fill, style);
                result.append_text(&t);
                result.append(&right_fill, Some(style));
                result
            }
            AlignMethod::Right => {
                // Pad left with fill char
                let fill: String = std::iter::repeat(fill_char).take(excess_space).collect();
                let mut result = Text::styled(&fill, style);
                result.append_text(&t);
                result
            }
        }
    }
}

impl Renderable for Panel {
    fn render(&self, console: &Console<Stdout>, options: &ConsoleOptions) -> Segments {
        let mut result = Segments::new();

        // Unpack padding
        let (pad_top, pad_right, pad_bottom, pad_left) = self.padding;

        // Reference to inner renderable
        let renderable_ref: &dyn Renderable = self.renderable.as_ref();

        // Get styles
        let style = self.style;
        let border_style = style.combine(&self.border_style);

        // Determine panel width
        let max_width = match self.width {
            Some(w) => w.min(options.max_width),
            None => options.max_width,
        };

        // Determine safe_box setting
        let safe_box = self.safe_box.unwrap_or(options.legacy_windows);
        let box_chars = self.box_type.substitute(safe_box, options.ascii_only());

        // Prepare title and subtitle
        let title_text = self.prepare_title();
        let subtitle_text = self.prepare_subtitle();

        // Calculate child width (inner area between borders, including padding)
        let child_width = if self.expand {
            // When expanding, use full width minus borders
            max_width.saturating_sub(2)
        } else {
            // When fitting, measure content and add padding
            let render_opts =
                options.update_width(max_width.saturating_sub(2 + pad_left + pad_right));
            let content_width = console.measure(renderable_ref, Some(&render_opts)).maximum;
            // child_width = content + padding (fits snugly)
            content_width + pad_left + pad_right
        };

        // Adjust child width for title/subtitle if present
        // Title/subtitle fits in (width - 4) space, so child_width must be >= text_len + 2
        let mut child_width = child_width.min(max_width.saturating_sub(2));
        if let Some(ref t) = title_text {
            child_width = child_width.max(t.cell_len() + 2);
        }
        if let Some(ref s) = subtitle_text {
            child_width = child_width.max(s.cell_len() + 2);
        }
        let child_width = child_width.min(max_width.saturating_sub(2));

        // Calculate final panel width
        let width = child_width + 2;

        // Calculate child height
        let child_height = self.height.or(options.height).map(|h| h.saturating_sub(2));

        // Create child render options with padding applied
        let child_options = {
            let mut opts = options.update_width(child_width.saturating_sub(pad_left + pad_right));
            if let Some(h) = child_height {
                opts = opts.update_height(h.saturating_sub(pad_top + pad_bottom));
            }
            if self.highlight {
                opts.highlight = Some(true);
            }
            opts
        };

        // Render content lines
        let content_lines = if pad_top > 0 || pad_right > 0 || pad_bottom > 0 || pad_left > 0 {
            // Render inner content first
            let inner_lines = console.render_lines(
                renderable_ref,
                Some(&child_options),
                Some(style),
                true,
                false,
            );

            // Apply padding manually
            let mut padded_lines: Vec<Vec<Segment>> = Vec::new();
            let blank_line_width = child_width;
            let blank_segment = Segment::styled(" ".repeat(blank_line_width), style);

            // Top padding
            for _ in 0..pad_top {
                padded_lines.push(vec![blank_segment.clone()]);
            }

            // Content lines with left/right padding
            let left_pad = Segment::styled(" ".repeat(pad_left), style);
            let right_pad = Segment::styled(" ".repeat(pad_right), style);

            for line in inner_lines {
                let mut padded_line = Vec::new();
                if pad_left > 0 {
                    padded_line.push(left_pad.clone());
                }
                for seg in line {
                    padded_line.push(seg);
                }
                if pad_right > 0 {
                    padded_line.push(right_pad.clone());
                }
                // Adjust line to child_width
                padded_line =
                    Segment::adjust_line_length(&padded_line, child_width, Some(style), true);
                padded_lines.push(padded_line);
            }

            // Bottom padding
            for _ in 0..pad_bottom {
                padded_lines.push(vec![blank_segment.clone()]);
            }

            padded_lines
        } else {
            console.render_lines(
                renderable_ref,
                Some(&options.update_width(child_width)),
                Some(style),
                true,
                false,
            )
        };

        // Ensure lines match child_width
        let content_lines: Vec<Vec<Segment>> = content_lines
            .into_iter()
            .map(|line| Segment::adjust_line_length(&line, child_width, Some(style), true))
            .collect();

        // Handle height constraint
        let content_lines = if let Some(h) = child_height {
            Segment::set_shape(&content_lines, child_width, Some(h), Some(style), false)
        } else {
            content_lines
        };

        let new_line = Segment::line();

        // Render top border
        if title_text.is_none() || width <= 4 {
            // Simple top border
            let top = box_chars.get_top(&[child_width]);
            result.push(Segment::styled(top, border_style));
        } else {
            // Top border with title
            let title = title_text.as_ref().unwrap();
            let aligned_title = self.align_text(
                title,
                width.saturating_sub(4),
                self.title_align,
                box_chars.top,
                border_style,
            );

            // Left corner + one border char
            result.push(Segment::styled(
                format!("{}{}", box_chars.top_left, box_chars.top),
                border_style,
            ));

            // Render aligned title
            let title_segments =
                aligned_title.render(console, &options.update_width(width.saturating_sub(4)));
            for seg in title_segments {
                result.push(seg);
            }

            // One border char + right corner
            result.push(Segment::styled(
                format!("{}{}", box_chars.top, box_chars.top_right),
                border_style,
            ));
        }
        result.push(new_line.clone());

        // Render content lines with left/right borders
        let line_start = Segment::styled(box_chars.mid_left.to_string(), border_style);
        let line_end = Segment::styled(box_chars.mid_right.to_string(), border_style);

        for line in &content_lines {
            result.push(line_start.clone());
            for seg in line {
                result.push(seg.clone());
            }
            result.push(line_end.clone());
            result.push(new_line.clone());
        }

        // Render bottom border (subtitle_text was prepared earlier)
        if subtitle_text.is_none() || width <= 4 {
            // Simple bottom border
            let bottom = box_chars.get_bottom(&[child_width]);
            result.push(Segment::styled(bottom, border_style));
        } else {
            // Bottom border with subtitle
            let subtitle = subtitle_text.as_ref().unwrap();
            let mut styled_subtitle = subtitle.copy();
            styled_subtitle.stylize_before(border_style, 0, None);

            let aligned_subtitle = self.align_text(
                &styled_subtitle,
                width.saturating_sub(4),
                self.subtitle_align,
                box_chars.bottom,
                border_style,
            );

            // Left corner + one border char
            result.push(Segment::styled(
                format!("{}{}", box_chars.bottom_left, box_chars.bottom),
                border_style,
            ));

            // Render aligned subtitle
            let subtitle_segments =
                aligned_subtitle.render(console, &options.update_width(width.saturating_sub(4)));
            for seg in subtitle_segments {
                result.push(seg);
            }

            // One border char + right corner
            result.push(Segment::styled(
                format!("{}{}", box_chars.bottom, box_chars.bottom_right),
                border_style,
            ));
        }
        result.push(new_line);

        result
    }

    fn measure(&self, console: &Console<Stdout>, options: &ConsoleOptions) -> Measurement {
        let title = self.prepare_title();
        let subtitle = self.prepare_subtitle();
        let (_, pad_right, _, pad_left) = self.padding;
        let horizontal_padding = pad_left + pad_right;

        // Build list of renderables to measure (include both title and subtitle)
        let inner_ref: &dyn Renderable = self.renderable.as_ref();
        let title_ref: Option<&dyn Renderable> = title.as_ref().map(|t| t as &dyn Renderable);
        let subtitle_ref: Option<&dyn Renderable> = subtitle.as_ref().map(|t| t as &dyn Renderable);

        let mut renderables: Vec<&dyn Renderable> = vec![inner_ref];
        if let Some(t) = title_ref {
            renderables.push(t);
        }
        if let Some(s) = subtitle_ref {
            renderables.push(s);
        }

        if let Some(w) = self.width {
            return Measurement::exact(w).with_maximum(options.max_width);
        }

        // Measure the inner content within the space available after borders and padding.
        // Use both min and max so parent layouts (e.g., Table) can shrink panels without
        // thinking they require the full available width.
        let measure_options =
            options.update_width(options.max_width.saturating_sub(horizontal_padding + 2));
        let inner_measurement = measure_renderables(console, &measure_options, &renderables);

        let min_width = inner_measurement.minimum + horizontal_padding + 2;
        let max_width = if self.expand {
            options.max_width
        } else {
            inner_measurement.maximum + horizontal_padding + 2
        };

        Measurement::new(min_width, max_width).with_maximum(options.max_width)
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::r#box::{ASCII, DOUBLE, HEAVY, SQUARE};
    use crate::cells::cell_len;

    // ==================== Construction tests ====================

    #[test]
    fn test_panel_new() {
        let text = Text::plain("Hello");
        let panel = Panel::new(Box::new(text));
        assert!(panel.expand());
        assert_eq!(panel.box_type(), ROUNDED);
    }

    #[test]
    fn test_panel_fit() {
        let text = Text::plain("Hello");
        let panel = Panel::fit(Box::new(text));
        assert!(!panel.expand());
    }

    #[test]
    fn test_panel_with_title() {
        let text = Text::plain("Hello");
        let panel = Panel::new(Box::new(text)).with_title("Title");
        assert!(panel.title().is_some());
    }

    #[test]
    fn test_panel_with_subtitle() {
        let text = Text::plain("Hello");
        let panel = Panel::new(Box::new(text)).with_subtitle("Subtitle");
        assert!(panel.subtitle().is_some());
    }

    #[test]
    fn test_panel_with_box() {
        let text = Text::plain("Hello");
        let panel = Panel::new(Box::new(text)).with_box(DOUBLE);
        assert_eq!(panel.box_type(), DOUBLE);
    }

    #[test]
    fn test_panel_with_padding() {
        let text = Text::plain("Hello");
        let panel = Panel::new(Box::new(text)).with_padding((1, 2, 3, 4));
        assert_eq!(panel.padding(), (1, 2, 3, 4));
    }

    #[test]
    fn test_panel_with_style() {
        let text = Text::plain("Hello");
        let style = Style::new().with_bold(true);
        let panel = Panel::new(Box::new(text)).with_style(style);
        assert_eq!(panel.style().bold, Some(true));
    }

    #[test]
    fn test_panel_with_border_style() {
        let text = Text::plain("Hello");
        let style = Style::new().with_italic(true);
        let panel = Panel::new(Box::new(text)).with_border_style(style);
        assert_eq!(panel.border_style().italic, Some(true));
    }

    // ==================== Render tests ====================

    #[test]
    fn test_panel_render_basic() {
        let text = Text::plain("Hello");
        let panel = Panel::fit(Box::new(text)).with_padding(0);
        let console = Console::with_options(ConsoleOptions {
            max_width: 20,
            ..Default::default()
        });
        let options = console.options().clone();

        let segments = panel.render(&console, &options);
        let output: String = segments.iter().map(|s| s.text.to_string()).collect();

        // Should have top border, content, bottom border
        assert!(output.contains('')); // ROUNDED top-left
        assert!(output.contains('')); // ROUNDED top-right
        assert!(output.contains("Hello"));
        assert!(output.contains('')); // ROUNDED bottom-left
        assert!(output.contains('')); // ROUNDED bottom-right
    }

    #[test]
    fn test_panel_render_with_title() {
        let text = Text::plain("Hello");
        let panel = Panel::fit(Box::new(text))
            .with_title("Title")
            .with_padding(0);
        let console = Console::with_options(ConsoleOptions {
            max_width: 20,
            ..Default::default()
        });
        let options = console.options().clone();

        let segments = panel.render(&console, &options);
        let output: String = segments.iter().map(|s| s.text.to_string()).collect();

        assert!(output.contains("Title"));
    }

    #[test]
    fn test_panel_render_with_subtitle() {
        let text = Text::plain("Hello");
        let panel = Panel::fit(Box::new(text))
            .with_subtitle("Sub")
            .with_padding(0);
        let console = Console::with_options(ConsoleOptions {
            max_width: 20,
            ..Default::default()
        });
        let options = console.options().clone();

        let segments = panel.render(&console, &options);
        let output: String = segments.iter().map(|s| s.text.to_string()).collect();

        assert!(output.contains("Sub"));
    }

    #[test]
    fn test_panel_render_ascii_box() {
        let text = Text::plain("Hi");
        let panel = Panel::fit(Box::new(text)).with_box(ASCII).with_padding(0);
        let console = Console::with_options(ConsoleOptions {
            max_width: 20,
            ..Default::default()
        });
        let options = console.options().clone();

        let segments = panel.render(&console, &options);
        let output: String = segments.iter().map(|s| s.text.to_string()).collect();

        assert!(output.contains('+')); // ASCII corners
        assert!(output.contains('-')); // ASCII horizontal
    }

    #[test]
    fn test_panel_render_expand() {
        let text = Text::plain("Hi");
        let panel = Panel::new(Box::new(text)).with_padding(0);
        let console = Console::with_options(ConsoleOptions {
            max_width: 20,
            ..Default::default()
        });
        let options = console.options().clone();

        let segments = panel.render(&console, &options);
        let output: String = segments.iter().map(|s| s.text.to_string()).collect();
        let lines: Vec<&str> = output.lines().collect();

        // With expand=true, panel should fill the width
        // First line (top border) should be 20 chars
        assert_eq!(cell_len(lines[0]), 20);
    }

    #[test]
    fn test_panel_render_with_padding() {
        let text = Text::plain("X");
        let panel = Panel::fit(Box::new(text)).with_padding((1, 2, 1, 2));
        let console = Console::with_options(ConsoleOptions {
            max_width: 20,
            ..Default::default()
        });
        let options = console.options().clone();

        let segments = panel.render(&console, &options);
        let output: String = segments.iter().map(|s| s.text.to_string()).collect();
        let lines: Vec<&str> = output.lines().collect();

        // With top/bottom padding of 1, we should have more lines
        // top border + 1 blank + content + 1 blank + bottom border = 5 lines
        assert!(lines.len() >= 5);
    }

    #[test]
    fn test_panel_render_title_alignment_left() {
        let text = Text::plain("X");
        let panel = Panel::fit(Box::new(text))
            .with_title("T")
            .with_title_align(AlignMethod::Left)
            .with_padding(0)
            .with_width(10);
        let console = Console::with_options(ConsoleOptions {
            max_width: 20,
            ..Default::default()
        });
        let options = console.options().clone();

        let segments = panel.render(&console, &options);
        let output: String = segments.iter().map(|s| s.text.to_string()).collect();
        let first_line = output.lines().next().unwrap();

        // Title should be near the left (after corner + border)
        assert!(first_line.starts_with("╭─"));
    }

    #[test]
    fn test_panel_render_title_alignment_right() {
        let text = Text::plain("X");
        let panel = Panel::fit(Box::new(text))
            .with_title("T")
            .with_title_align(AlignMethod::Right)
            .with_padding(0)
            .with_width(10);
        let console = Console::with_options(ConsoleOptions {
            max_width: 20,
            ..Default::default()
        });
        let options = console.options().clone();

        let segments = panel.render(&console, &options);
        let output: String = segments.iter().map(|s| s.text.to_string()).collect();
        let first_line = output.lines().next().unwrap();

        // Title should be near the right (before corner)
        assert!(first_line.ends_with("─╮"));
    }

    // ==================== Measure tests ====================

    #[test]
    fn test_panel_measure_basic() {
        let text = Text::plain("Hello");
        let panel = Panel::fit(Box::new(text)).with_padding(0);
        let console = Console::new();
        let options = ConsoleOptions::default();

        let measurement = panel.measure(&console, &options);
        // "Hello" is 5 chars + 2 for borders = 7
        assert_eq!(measurement.minimum, 7);
        assert_eq!(measurement.maximum, 7);
    }

    #[test]
    fn test_panel_measure_with_padding() {
        let text = Text::plain("Hi");
        let panel = Panel::fit(Box::new(text)).with_padding((0, 2, 0, 2));
        let console = Console::new();
        let options = ConsoleOptions::default();

        let measurement = panel.measure(&console, &options);
        // "Hi" is 2 chars + 4 padding + 2 borders = 8
        assert_eq!(measurement.minimum, 8);
    }

    #[test]
    fn test_panel_measure_with_fixed_width() {
        let text = Text::plain("Hello");
        let panel = Panel::new(Box::new(text)).with_width(20);
        let console = Console::new();
        let options = ConsoleOptions::default();

        let measurement = panel.measure(&console, &options);
        assert_eq!(measurement.minimum, 20);
        assert_eq!(measurement.maximum, 20);
    }

    // ==================== Send + Sync tests ====================

    #[test]
    fn test_panel_is_send_sync() {
        fn assert_send<T: Send>() {}
        fn assert_sync<T: Sync>() {}
        assert_send::<Panel>();
        assert_sync::<Panel>();
    }

    // ==================== Debug tests ====================

    #[test]
    fn test_panel_debug() {
        let text = Text::plain("Hello");
        let panel = Panel::new(Box::new(text))
            .with_title("Title")
            .with_box(HEAVY);
        let debug_str = format!("{:?}", panel);
        assert!(debug_str.contains("Panel"));
        assert!(debug_str.contains("title"));
    }

    // ==================== Edge case tests ====================

    #[test]
    fn test_panel_empty_content() {
        let text = Text::plain("");
        let panel = Panel::fit(Box::new(text)).with_padding(0);
        let console = Console::with_options(ConsoleOptions {
            max_width: 20,
            ..Default::default()
        });
        let options = console.options().clone();

        let segments = panel.render(&console, &options);
        let output: String = segments.iter().map(|s| s.text.to_string()).collect();

        // Should still have borders
        assert!(output.contains(''));
        assert!(output.contains(''));
    }

    #[test]
    fn test_panel_multiline_content() {
        let text = Text::plain("Line1\nLine2");
        let panel = Panel::fit(Box::new(text)).with_padding(0);
        let console = Console::with_options(ConsoleOptions {
            max_width: 20,
            ..Default::default()
        });
        let options = console.options().clone();

        let segments = panel.render(&console, &options);
        let output: String = segments.iter().map(|s| s.text.to_string()).collect();
        let lines: Vec<&str> = output.lines().collect();

        // Top border + 2 content lines + bottom border = 4 lines
        assert!(lines.len() >= 4);
    }

    #[test]
    fn test_panel_cjk_content() {
        let text = Text::plain("你好");
        let panel = Panel::fit(Box::new(text)).with_padding(0);
        let console = Console::with_options(ConsoleOptions {
            max_width: 20,
            ..Default::default()
        });
        let options = console.options().clone();

        let segments = panel.render(&console, &options);
        let output: String = segments.iter().map(|s| s.text.to_string()).collect();

        assert!(output.contains("你好"));
    }

    #[test]
    fn test_panel_various_boxes() {
        let boxes = [ROUNDED, SQUARE, HEAVY, DOUBLE, ASCII];

        for box_type in boxes {
            let text = Text::plain("Test");
            let panel = Panel::fit(Box::new(text))
                .with_box(box_type)
                .with_padding(0);
            let console = Console::with_options(ConsoleOptions {
                max_width: 20,
                ..Default::default()
            });
            let options = console.options().clone();

            let segments = panel.render(&console, &options);
            assert!(!segments.is_empty());
        }
    }
}