rusty-rich 0.4.1

Rich text and beautiful formatting in the terminal — a Rust port of Python's Rich library
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
//! Panel — a bordered container. Equivalent to Rich's `panel.py`.

use crate::align::AlignMethod;
use crate::box_drawing::{get_safe_box, BoxStyle, BOX_ROUNDED};
use crate::console::{ConsoleOptions, DynRenderable, RenderResult, Renderable};
use crate::segment::Segment;
use crate::style::Style;

// ---------------------------------------------------------------------------
// Panel
// ---------------------------------------------------------------------------

/// A renderable that draws a border around its contents.
#[derive(Clone)]
pub struct Panel {
    /// The content inside the panel.
    pub renderable: DynRenderable,
    /// The box style defining the border.
    pub box_style: BoxStyle,
    /// Optional title displayed in the top border.
    pub title: Option<String>,
    /// Alignment of the title.
    pub title_align: AlignMethod,
    /// Optional subtitle displayed in the bottom border.
    pub subtitle: Option<String>,
    /// Alignment of the subtitle.
    pub subtitle_align: AlignMethod,
    /// If true, expand to fill available width.
    pub expand: bool,
    /// Style for the content area.
    pub style: Style,
    /// Style for the border.
    pub border_style: Style,
    /// Optional fixed width.
    pub width: Option<usize>,
    /// Optional fixed height.
    pub height: Option<usize>,
    /// Padding (top, right, bottom, left).
    pub padding: (usize, usize, usize, usize),
    /// If true, highlight string titles.
    pub highlight: bool,
}

impl Panel {
    /// Create a new Panel with the given content.
    pub fn new(renderable: impl Renderable + Send + Sync + 'static) -> Self {
        Self {
            renderable: DynRenderable::new(renderable),
            box_style: BOX_ROUNDED.clone(),
            title: None,
            title_align: AlignMethod::Center,
            subtitle: None,
            subtitle_align: AlignMethod::Center,
            expand: true,
            style: Style::new(),
            border_style: Style::new(),
            width: None,
            height: None,
            padding: (0, 1, 0, 1), // top, right, bottom, left
            highlight: false,
        }
    }

    /// Builder: set the box style.
    pub fn box_style(mut self, bs: BoxStyle) -> Self {
        self.box_style = bs;
        self
    }

    /// Builder: set the title.
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Builder: set the subtitle.
    pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
        self.subtitle = Some(subtitle.into());
        self
    }

    /// Builder: set the border style.
    pub fn border_style(mut self, style: Style) -> Self {
        self.border_style = style;
        self
    }

    /// Builder: set the content style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Builder: set width.
    pub fn width(mut self, width: usize) -> Self {
        self.width = Some(width);
        self
    }

    /// Builder: set height.
    pub fn height(mut self, height: usize) -> Self {
        self.height = Some(height);
        self
    }

    /// Builder: set padding.
    pub fn padding(mut self, top: usize, right: usize, bottom: usize, left: usize) -> Self {
        self.padding = (top, right, bottom, left);
        self
    }

    /// Builder: don't expand to fill width.
    pub fn fit(mut self) -> Self {
        self.expand = false;
        self
    }

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

impl std::fmt::Debug for Panel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Panel")
            .field("title", &self.title)
            .field("width", &self.width)
            .field("height", &self.height)
            .finish()
    }
}

impl Renderable for Panel {
    fn render(&self, options: &ConsoleOptions) -> RenderResult {
        let box_style = get_safe_box(&self.box_style, options.ascii_only);
        let padding = self.padding;
        let has_edge = box_style.has_visible_edges();
        // Only reserve space for borders if the box actually draws them.
        let edge_width: usize = if has_edge { 2 } else { 0 };
        let inner_max_width = options.max_width.saturating_sub(edge_width + padding.1 + padding.3);

        // Render the content
        let inner_options = options
            .update_width(inner_max_width.max(1));
        let content = self.renderable.render(&inner_options);

        // Calculate content width and height
        let content_width: usize = content
            .lines
            .iter()
            .map(|line| {
                line.iter()
                    .map(|s| s.cell_length())
                    .sum::<usize>()
            })
            .max()
            .unwrap_or(0);

        let panel_width = if self.expand {
            options.max_width
        } else {
            (content_width + edge_width + padding.1 + padding.3).min(options.max_width).max(3)
        };

        // Build the panel
        let mut lines: Vec<Vec<Segment>> = Vec::new();
        let border = &box_style;
        let border_ansi = self.border_style.to_ansi();
        let border_reset = if border_ansi.is_empty() { "" } else { "\x1b[0m" };

        // Helper: create a border segment
        let bs = |ch: char| -> Segment {
            let text = format!("{border_ansi}{ch}{border_reset}");
            Segment::new(text)
        };

        // -- Edge-less mode: render title/subtitle as plain text, skip borders --
        if !has_edge {
            // Title as plain text
            if let Some(ref title) = self.title {
                let aligned = self.title_align.align_text(title, panel_width);
                lines.push(vec![Segment::new(&aligned), Segment::line()]);
            }
            // Top padding
            for _ in 0..padding.0 {
                lines.push(vec![Segment::new(" ".repeat(panel_width)), Segment::line()]);
            }
            // Content
            for content_line in &content.lines {
                let mut line: Vec<Segment> = Vec::new();
                if padding.3 > 0 {
                    line.push(Segment::new(" ".repeat(padding.3)));
                }
                let available = panel_width.saturating_sub(padding.1 + padding.3);
                let seg_width: usize = content_line.iter().map(|s| s.cell_length()).sum();
                line.extend(content_line.iter().take(seg_width.min(available)).cloned());
                let fill = available.saturating_sub(seg_width);
                if fill > 0 {
                    line.push(Segment::new(" ".repeat(fill)));
                }
                if padding.1 > 0 {
                    line.push(Segment::new(" ".repeat(padding.1)));
                }
                line.push(Segment::line());
                lines.push(line);
            }
            // Bottom padding
            for _ in 0..padding.2 {
                lines.push(vec![Segment::new(" ".repeat(panel_width)), Segment::line()]);
            }
            // Subtitle as plain text
            if let Some(ref subtitle) = self.subtitle {
                let aligned = self.subtitle_align.align_text(subtitle, panel_width);
                lines.push(vec![Segment::new(&aligned), Segment::line()]);
            }
            return RenderResult { lines, items: Vec::new() };
        }

        // -- Bordered mode (original path) --
        // Top border (with optional title)
        let top_line = self.render_top_border(
            &box_style, panel_width, &border_ansi, &border_reset,
        );
        lines.push(top_line);

        // Pad top
        for _ in 0..padding.0 {
            let pad_line = self.render_pad_line(&box_style, panel_width, &border_ansi, &border_reset);
            lines.push(pad_line);
        }

        // Content lines
        for content_line in &content.lines {
            let mut line: Vec<Segment> = Vec::new();
            // Left border
            line.push(bs(border.mid_left));
            // Left padding
            if padding.3 > 0 {
                line.push(Segment::new(" ".repeat(padding.3)));
            }

            // Content (possibly truncated to fit)
            let available = panel_width.saturating_sub(2 + padding.1 + padding.3);
            let seg_width: usize = content_line.iter().map(|s| s.cell_length()).sum();
            line.extend(content_line.iter().take(seg_width.min(available)).cloned());

            // Fill remaining space
            let fill = available.saturating_sub(seg_width);
            if fill > 0 {
                line.push(Segment::new(" ".repeat(fill)));
            }

            // Right padding
            if padding.1 > 0 {
                line.push(Segment::new(" ".repeat(padding.1)));
            }
            // Right border
            line.push(bs(border.mid_right));
            line.push(Segment::line());
            lines.push(line);
        }

        // Pad bottom
        for _ in 0..padding.2 {
            let pad_line = self.render_pad_line(&box_style, panel_width, &border_ansi, &border_reset);
            lines.push(pad_line);
        }

        // Bottom border (with optional subtitle)
        let bottom_line = self.render_bottom_border(
            &box_style, panel_width, &border_ansi, &border_reset,
        );
        lines.push(bottom_line);

        RenderResult { lines, items: Vec::new() }
    }
}

impl Panel {
    fn render_top_border(
        &self,
        b: &BoxStyle,
        width: usize,
        border_ansi: &str,
        border_reset: &str,
    ) -> Vec<Segment> {
        let mut line = Vec::new();
        let inner = width.saturating_sub(2);

        if let Some(ref title) = self.title {
            let title_w = unicode_width::UnicodeWidthStr::width(title.as_str());
            if title_w + 2 <= inner {
                let rem = inner - title_w - 2;
                let (left_w, right_w) = match self.title_align {
                    AlignMethod::Left => (1, rem - 1),
                    AlignMethod::Right => (rem - 1, 1),
                    AlignMethod::Center => {
                        let l = rem / 2;
                        (l, rem - l)
                    }
                    AlignMethod::Full => (1, rem - 1),
                };

                // Batch repeated horizontal chars under a single ANSI wrap
                let bl = format!("{border_ansi}{}{border_reset}", b.top_left);
                let br = format!("{border_ansi}{}{border_reset}", b.top_right);
                let bt_left = format!("{border_ansi}{}{border_reset}", b.top.to_string().repeat(left_w));
                let bt_right = format!("{border_ansi}{}{border_reset}", b.top.to_string().repeat(right_w));

                line.push(Segment::new(bl));
                line.push(Segment::new(bt_left));
                line.push(Segment::new(format!(" {title} ")));
                line.push(Segment::new(bt_right));
                line.push(Segment::new(br));
                line.push(Segment::line());
                return line;
            }
        }

        // No title, or title too long
        let bl = format!("{border_ansi}{}{border_reset}", b.top_left);
        let br = format!("{border_ansi}{}{border_reset}", b.top_right);
        let bt = format!("{border_ansi}{}{border_reset}", b.top.to_string().repeat(inner));

        line.push(Segment::new(bl));
        line.push(Segment::new(bt));
        line.push(Segment::new(br));
        line.push(Segment::line());
        line
    }

    fn render_bottom_border(
        &self,
        b: &BoxStyle,
        width: usize,
        border_ansi: &str,
        border_reset: &str,
    ) -> Vec<Segment> {
        let mut line = Vec::new();
        let inner = width.saturating_sub(2);

        if let Some(ref subtitle) = self.subtitle {
            let sub_w = unicode_width::UnicodeWidthStr::width(subtitle.as_str());
            if sub_w + 2 <= inner {
                let rem = inner - sub_w - 2;
                let (left_w, right_w) = match self.subtitle_align {
                    AlignMethod::Left => (1, rem - 1),
                    AlignMethod::Right => (rem - 1, 1),
                    AlignMethod::Center => {
                        let l = rem / 2;
                        (l, rem - l)
                    }
                    AlignMethod::Full => (1, rem - 1),
                };

                let bl = format!("{border_ansi}{}{border_reset}", b.bottom_left);
                let br = format!("{border_ansi}{}{border_reset}", b.bottom_right);
                let bb_left = format!("{border_ansi}{}{border_reset}", b.bottom.to_string().repeat(left_w));
                let bb_right = format!("{border_ansi}{}{border_reset}", b.bottom.to_string().repeat(right_w));

                line.push(Segment::new(bl));
                line.push(Segment::new(bb_left));
                line.push(Segment::new(format!(" {subtitle} ")));
                line.push(Segment::new(bb_right));
                line.push(Segment::new(br));
                line.push(Segment::line());
                return line;
            }
        }

        let bl = format!("{border_ansi}{}{border_reset}", b.bottom_left);
        let br = format!("{border_ansi}{}{border_reset}", b.bottom_right);
        let bb = format!("{border_ansi}{}{border_reset}", b.bottom.to_string().repeat(inner));

        line.push(Segment::new(bl));
        line.push(Segment::new(bb));
        line.push(Segment::new(br));
        line.push(Segment::line());
        line
    }

    fn render_pad_line(
        &self,
        b: &BoxStyle,
        width: usize,
        border_ansi: &str,
        border_reset: &str,
    ) -> Vec<Segment> {
        let inner = width.saturating_sub(2);
        let left = format!("{border_ansi}{}{border_reset}", b.mid_left);
        let right = format!("{border_ansi}{}{border_reset}", b.mid_right);
        vec![
            Segment::new(left),
            Segment::new(" ".repeat(inner)),
            Segment::new(right),
            Segment::line(),
        ]
    }
}

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

    #[test]
    fn test_panel_creation() {
        let panel = Panel::new("Hello");
        assert!(panel.title.is_none());
    }

    #[test]
    fn test_panel_with_title() {
        let panel = Panel::new("Content").title("My Title");
        let opts = ConsoleOptions::default();
        let result = panel.render(&opts);
        let ansi = result.to_ansi();
        assert!(ansi.contains("My Title"));
    }
}