rusty-rich 0.4.3

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
423
424
425
426
427
428
429
430
431
//! Box drawing — equivalent to Rich's `box.py`.
//!
//! Defines various box styles (ROUNDED, SQUARE, HEAVY, etc.) using Unicode
//! box-drawing characters, with ASCII-safe fallbacks.

// ---------------------------------------------------------------------------
// Box — defines characters for drawing a bordered box
// ---------------------------------------------------------------------------

/// A set of box-drawing characters defining the look of borders.
///
/// Layout of the 8-line string that defines a box:
///
/// ```text
/// ┌─┬┐ top
/// │ ││ head
/// ├─┼┤ head_row
/// │ ││ mid
/// ├─┼┤ row
/// ├─┼┤ foot_row
/// │ ││ foot
/// └─┴┘ bottom
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BoxStyle {
    // top row
    pub top_left: char,
    pub top: char,
    pub top_divider: char,
    pub top_right: char,
    // head row (where content is on same line as top border)
    pub head_left: char,
    pub head_horizontal: char,
    pub head_vertical: char,
    pub head_right: char,
    // head_row (separator after header)
    pub head_row_left: char,
    pub head_row_horizontal: char,
    pub head_row_cross: char,
    pub head_row_right: char,
    // mid (between rows when show_lines is off)
    pub mid_left: char,
    pub mid_horizontal: char,
    pub mid_vertical: char,
    pub mid_right: char,
    // row (between rows when show_lines is on)
    pub row_left: char,
    pub row_horizontal: char,
    pub row_cross: char,
    pub row_right: char,
    // foot_row (separator before footer)
    pub foot_row_left: char,
    pub foot_row_horizontal: char,
    pub foot_row_cross: char,
    pub foot_row_right: char,
    // foot
    pub foot_left: char,
    pub foot_horizontal: char,
    pub foot_vertical: char,
    pub foot_right: char,
    // bottom row
    pub bottom_left: char,
    pub bottom: char,
    pub bottom_divider: char,
    pub bottom_right: char,
    /// True if this box uses only ASCII characters.
    pub ascii: bool,
}

impl BoxStyle {
    /// Returns true if this box has visible outer edges (non-space corners).
    /// Edge-less styles like SIMPLE, MINIMAL, and MARKDOWN return `false`
    /// because their corner characters are all spaces — they are designed
    /// to be used in tables where internal separators provide structure.
    pub fn has_visible_edges(&self) -> bool {
        // A visible edge requires at least one non-space corner.
        self.top_left != ' '
            || self.top_right != ' '
            || self.bottom_left != ' '
            || self.bottom_right != ' '
    }

    /// Parse a box style from an 8-line string.
    pub fn from_str(box_str: &str, ascii: bool) -> Self {
        let lines: Vec<&str> = box_str.lines().collect();
        assert_eq!(lines.len(), 8, "Box definition must have exactly 8 lines");

        let line_chars: Vec<Vec<char>> = lines.iter().map(|l| l.chars().collect()).collect();

        // Each line should have 4 characters
        for (i, chars) in line_chars.iter().enumerate() {
            assert_eq!(chars.len(), 4, "Line {i} must have exactly 4 characters");
        }

        let l = &line_chars;
        Self {
            top_left: l[0][0],
            top: l[0][1],
            top_divider: l[0][2],
            top_right: l[0][3],
            head_left: l[1][0],
            head_horizontal: l[1][1],
            head_vertical: l[1][2],
            head_right: l[1][3],
            head_row_left: l[2][0],
            head_row_horizontal: l[2][1],
            head_row_cross: l[2][2],
            head_row_right: l[2][3],
            mid_left: l[3][0],
            mid_horizontal: l[3][1],
            mid_vertical: l[3][2],
            mid_right: l[3][3],
            row_left: l[4][0],
            row_horizontal: l[4][1],
            row_cross: l[4][2],
            row_right: l[4][3],
            foot_row_left: l[5][0],
            foot_row_horizontal: l[5][1],
            foot_row_cross: l[5][2],
            foot_row_right: l[5][3],
            foot_left: l[6][0],
            foot_horizontal: l[6][1],
            foot_vertical: l[6][2],
            foot_right: l[6][3],
            bottom_left: l[7][0],
            bottom: l[7][1],
            bottom_divider: l[7][2],
            bottom_right: l[7][3],
            ascii,
        }
    }

    /// Get the plain text representation of the box definition.
    pub fn to_plain_text(&self) -> String {
        format!(
            "{}{}{}{}\n{}{}{}{}\n{}{}{}{}\n{}{}{}{}\n{}{}{}{}\n{}{}{}{}\n{}{}{}{}\n{}{}{}{}",
            self.top_left,
            self.top,
            self.top_divider,
            self.top_right,
            self.head_left,
            self.head_horizontal,
            self.head_vertical,
            self.head_right,
            self.head_row_left,
            self.head_row_horizontal,
            self.head_row_cross,
            self.head_row_right,
            self.mid_left,
            self.mid_horizontal,
            self.mid_vertical,
            self.mid_right,
            self.row_left,
            self.row_horizontal,
            self.row_cross,
            self.row_right,
            self.foot_row_left,
            self.foot_row_horizontal,
            self.foot_row_cross,
            self.foot_row_right,
            self.foot_left,
            self.foot_horizontal,
            self.foot_vertical,
            self.foot_right,
            self.bottom_left,
            self.bottom,
            self.bottom_divider,
            self.bottom_right,
        )
    }
}

impl std::fmt::Display for BoxStyle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.to_plain_text())
    }
}

// ---------------------------------------------------------------------------
// Predefined box styles (matching Rich's defaults)
// ---------------------------------------------------------------------------

/// ASCII-only box.
pub const ASCII: &str = "\
+--+
| ||
|-+|
| ||
|-+|
|-+|
| ||
+--+";

/// ASCII with double edges (no distinct header).
pub const ASCII2: &str = "\
+-++
| ||
+-++
| ||
+-++
+-++
| ||
+-++";

/// Square box with double horizontal header separator.
pub const SQUARE_DOUBLE_HEAD: &str = "\
┌─┬┐
│ ││
╞═╪╡
│ ││
├─┼┤
├─┼┤
│ ││
└─┴┘";

/// Minimal box with double horizontal separator (head row only).
pub const MINIMAL_DOUBLE_HEAD: &str = "\n\n ═╪ \n\n ─┼ \n ─┼ \n\n";

/// Simple box with a single horizontal rule under the header.
pub const SIMPLE_HEAD: &str = "    \n    \n ── \n    \n    \n    \n    \n    ";

/// ASCII box style with a double header line.
pub const ASCII_DOUBLE_HEAD: &str = "\
+-++
| ||
+=++
| ||
+-++
+-++
| ||
+-++";

/// Rounded corners.
pub const ROUNDED: &str = "\
╭─┬╮
│ ││
├─┼┤
│ ││
├─┼┤
├─┼┤
│ ││
╰─┴╯";

/// Square corners.
pub const SQUARE: &str = "\
┌─┬┐
│ ││
├─┼┤
│ ││
├─┼┤
├─┼┤
│ ││
└─┴┘";

/// Heavy borders.
pub const HEAVY: &str = "\
┏━┳┓
┃ ┃┃
┣━╋┫
┃ ┃┃
┣━╋┫
┣━╋┫
┃ ┃┃
┗━┻┛";

/// Heavy edge, light inner.
pub const HEAVY_EDGE: &str = "\
┏━┯┓
┃ │┃
┠─┼┨
┃ │┃
┠─┼┨
┠─┼┨
┃ │┃
┗━┷┛";

/// Heavy header.
pub const HEAVY_HEAD: &str = "\
┏━┳┓
┃ ┃┃
┡━╇┩
│ ││
├─┼┤
├─┼┤
│ ││
└─┴┘";

/// Double borders.
pub const DOUBLE: &str = "\
╔═╦╗
║ ║║
╠═╬╣
║ ║║
╠═╬╣
╠═╬╣
║ ║║
╚═╩╝";

/// Double edge (like DOUBLE but inner is single).
pub const DOUBLE_EDGE: &str = "\
╔═╤╗
║ │║
╟─┼╢
║ │║
╟─┼╢
╟─┼╢
║ │║
╚═╧╝";

/// Simple (no borders, just vertical separators).
pub const SIMPLE: &str = "    \n    \n ── \n    \n    \n ── \n    \n    ";

/// Simple with heavy header.
pub const SIMPLE_HEAVY: &str = "    \n    \n ━━ \n    \n    \n ━━ \n    \n    ";

/// Minimal (thin rule, vertical separators, no outer edges).
pub const MINIMAL: &str = "\n\n╶─┼╴\n\n╶─┼╴\n╶─┼╴\n\n";

/// Minimal with heavy header separator (matches Python Rich MINIMAL_HEAVY_HEAD).
pub const MINIMAL_HEAVY: &str = "\n\n╺━┿╸\n\n╶─┼╴\n╶─┼╴\n\n";

// ---------------------------------------------------------------------------
// Box style constants (lazily parsed)
// ---------------------------------------------------------------------------

use std::sync::LazyLock;

/// Rounded box (default for Panel).
pub static BOX_ROUNDED: LazyLock<BoxStyle> = LazyLock::new(|| BoxStyle::from_str(ROUNDED, false));
/// Square-cornered box.
pub static BOX_SQUARE: LazyLock<BoxStyle> = LazyLock::new(|| BoxStyle::from_str(SQUARE, false));
/// Heavy (thick) borders.
pub static BOX_HEAVY: LazyLock<BoxStyle> = LazyLock::new(|| BoxStyle::from_str(HEAVY, false));
/// Heavy outer edges with light inner dividers.
pub static BOX_HEAVY_EDGE: LazyLock<BoxStyle> =
    LazyLock::new(|| BoxStyle::from_str(HEAVY_EDGE, false));
/// Heavy header row with regular body borders.
pub static BOX_HEAVY_HEAD: LazyLock<BoxStyle> =
    LazyLock::new(|| BoxStyle::from_str(HEAVY_HEAD, false));
/// Double-line borders.
pub static BOX_DOUBLE: LazyLock<BoxStyle> = LazyLock::new(|| BoxStyle::from_str(DOUBLE, false));
/// Double outer edge with single inner dividers.
pub static BOX_DOUBLE_EDGE: LazyLock<BoxStyle> =
    LazyLock::new(|| BoxStyle::from_str(DOUBLE_EDGE, false));
/// Simple borders (no vertical edges, horizontal rules only).
pub static BOX_SIMPLE: LazyLock<BoxStyle> = LazyLock::new(|| BoxStyle::from_str(SIMPLE, false));
/// Simple borders with heavy horizontal rules.
pub static BOX_SIMPLE_HEAVY: LazyLock<BoxStyle> =
    LazyLock::new(|| BoxStyle::from_str(SIMPLE_HEAVY, false));
/// Minimal box (just horizontal separators between header/body).
pub static BOX_MINIMAL: LazyLock<BoxStyle> = LazyLock::new(|| BoxStyle::from_str(MINIMAL, false));
/// Minimal box with heavy horizontal separators.
pub static BOX_MINIMAL_HEAVY: LazyLock<BoxStyle> =
    LazyLock::new(|| BoxStyle::from_str(MINIMAL_HEAVY, false));
/// ASCII-only box (uses `+`, `-`, `|` characters).
pub static BOX_ASCII: LazyLock<BoxStyle> = LazyLock::new(|| BoxStyle::from_str(ASCII, true));
/// ASCII box with doubled edges.
pub static BOX_ASCII2: LazyLock<BoxStyle> = LazyLock::new(|| BoxStyle::from_str(ASCII2, true));
/// Square box with a double horizontal header separator.
pub static BOX_SQUARE_DOUBLE_HEAD: LazyLock<BoxStyle> =
    LazyLock::new(|| BoxStyle::from_str(SQUARE_DOUBLE_HEAD, false));
/// Minimal box with a double horizontal header separator.
pub static BOX_MINIMAL_DOUBLE_HEAD: LazyLock<BoxStyle> =
    LazyLock::new(|| BoxStyle::from_str(MINIMAL_DOUBLE_HEAD, false));
/// Simple box with a single horizontal rule under the header.
pub static BOX_SIMPLE_HEAD: LazyLock<BoxStyle> =
    LazyLock::new(|| BoxStyle::from_str(SIMPLE_HEAD, false));
/// ASCII box with a double header line.
pub static BOX_ASCII_DOUBLE_HEAD: LazyLock<BoxStyle> =
    LazyLock::new(|| BoxStyle::from_str(ASCII_DOUBLE_HEAD, true));

// ---------------------------------------------------------------------------
// MARKDOWN box (no outer border)
// ---------------------------------------------------------------------------

/// Markdown-style box definition string (no outer borders).
pub const MARKDOWN: &str = "    \n| ||\n|-||\n| ||\n|-||\n|-||\n| ||\n    ";

/// Markdown-style box (no outer edges, vertical separators only).
pub static BOX_MARKDOWN: LazyLock<BoxStyle> = LazyLock::new(|| BoxStyle::from_str(MARKDOWN, false));

// ---------------------------------------------------------------------------
// Safe box (for Windows legacy terminals)
// ---------------------------------------------------------------------------

/// Return an ASCII-safe version of a box if needed.
pub fn get_safe_box(box_style: &BoxStyle, ascii_only: bool) -> BoxStyle {
    if ascii_only && !box_style.ascii {
        BOX_ASCII.clone()
    } else {
        box_style.clone()
    }
}

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

    #[test]
    fn test_rounded_box() {
        let b = &*BOX_ROUNDED;
        assert_eq!(b.top_left, '');
        assert_eq!(b.bottom_right, '');
    }

    #[test]
    fn test_box_from_str() {
        let b = BoxStyle::from_str(ROUNDED, false);
        assert_eq!(b, *BOX_ROUNDED);
    }

    #[test]
    fn test_new_box_styles_parse() {
        // Verify that the new box styles parse without panicking
        let _ = &*BOX_SQUARE_DOUBLE_HEAD;
        let _ = &*BOX_MINIMAL_DOUBLE_HEAD;
        let _ = &*BOX_SIMPLE_HEAD;
        let _ = &*BOX_ASCII_DOUBLE_HEAD;

        // Spot-check characters
        let sq = &*BOX_SQUARE_DOUBLE_HEAD;
        assert_eq!(sq.top_left, '');
        assert_eq!(sq.head_row_horizontal, '');
        assert_eq!(sq.head_row_left, '');

        let ac = &*BOX_ASCII_DOUBLE_HEAD;
        assert_eq!(ac.head_row_left, '+');
        assert_eq!(ac.head_row_horizontal, '=');
        assert_eq!(ac.row_left, '+');
    }
}