rusty-rich 0.3.0

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
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
# Box Drawing

The `BoxStyle` type defines the characters used to draw borders around panels, tables, and other framed renderables. Each style is an 8x4 grid of Unicode or ASCII glyphs that control corners, edges, dividers, and intersections.

---

## The BoxStyle Struct

`BoxStyle` stores 33 fields representing every character position in a bordered box layout. The positions map to an 8-line, 4-column definition string.

### 8-Line Layout

```text
┌─┬┐   line 0: top            (top_left, top, top_divider, top_right)
│ ││   line 1: head           (head_left, head_horizontal, head_vertical, head_right)
├─┼┤   line 2: head_row       (head_row_left, head_row_horizontal, head_row_cross, head_row_right)
│ ││   line 3: mid            (mid_left, mid_horizontal, mid_vertical, mid_right)
├─┼┤   line 4: row            (row_left, row_horizontal, row_cross, row_right)
├─┼┤   line 5: foot_row       (foot_row_left, foot_row_horizontal, foot_row_cross, foot_row_right)
│ ││   line 6: foot           (foot_left, foot_horizontal, foot_vertical, foot_right)
└─┴┘   line 7: bottom         (bottom_left, bottom, bottom_divider, bottom_right)
```

Each of the 8 lines has exactly 4 characters. The column positions across all 8 lines follow a consistent pattern:

| Column | Position   | Description                             |
|--------|------------|-----------------------------------------|
| 0      | Left edge  | Leftmost vertical or corner character   |
| 1      | Horizontal | Horizontal line character               |
| 2      | Divider    | Vertical divider / cross character      |
| 3      | Right edge | Rightmost vertical or corner character  |

### Fields

The 32 character fields plus the `ascii` boolean:

| Line       | Field                | Type    | Description                              |
|------------|----------------------|---------|------------------------------------------|
| top        | `top_left`           | `char`  | Top-left corner                          |
| top        | `top`                | `char`  | Top horizontal edge                      |
| top        | `top_divider`        | `char`  | Top vertical divider (column separator)  |
| top        | `top_right`          | `char`  | Top-right corner                         |
| head       | `head_left`          | `char`  | Left edge in header row                  |
| head       | `head_horizontal`    | `char`  | Horizontal fill in header row            |
| head       | `head_vertical`      | `char`  | Vertical divider in header row           |
| head       | `head_right`         | `char`  | Right edge in header row                 |
| head_row   | `head_row_left`      | `char`  | Separator under header, left junction    |
| head_row   | `head_row_horizontal`| `char`  | Separator under header, horizontal line  |
| head_row   | `head_row_cross`     | `char`  | Separator under header, cross            |
| head_row   | `head_row_right`     | `char`  | Separator under header, right junction   |
| mid        | `mid_left`           | `char`  | Left edge, mid row (no line separators)  |
| mid        | `mid_horizontal`     | `char`  | Horizontal fill, mid row                 |
| mid        | `mid_vertical`       | `char`  | Vertical divider, mid row                |
| mid        | `mid_right`          | `char`  | Right edge, mid row                      |
| row        | `row_left`           | `char`  | Row separator, left junction             |
| row        | `row_horizontal`     | `char`  | Row separator, horizontal line           |
| row        | `row_cross`          | `char`  | Row separator, cross intersection        |
| row        | `row_right`          | `char`  | Row separator, right junction            |
| foot_row   | `foot_row_left`      | `char`  | Separator before footer, left junction   |
| foot_row   | `foot_row_horizontal`| `char`  | Separator before footer, horizontal line |
| foot_row   | `foot_row_cross`     | `char`  | Separator before footer, cross           |
| foot_row   | `foot_row_right`     | `char`  | Separator before footer, right junction  |
| foot       | `foot_left`          | `char`  | Left edge, footer row                    |
| foot       | `foot_horizontal`    | `char`  | Horizontal fill, footer row              |
| foot       | `foot_vertical`      | `char`  | Vertical divider, footer row             |
| foot       | `foot_right`         | `char`  | Right edge, footer row                   |
| bottom     | `bottom_left`        | `char`  | Bottom-left corner                       |
| bottom     | `bottom`             | `char`  | Bottom horizontal edge                   |
| bottom     | `bottom_divider`     | `char`  | Bottom vertical divider                  |
| bottom     | `bottom_right`       | `char`  | Bottom-right corner                      |
|            | `ascii`              | `bool`  | `true` if this style uses only ASCII     |

---

## Predefined Styles

All predefined styles are exported as `Lazy<BoxStyle>` statics from `rusty_rich::box_drawing`. They are lazily initialized on first access.

### ROUNDED

The default box style for `Panel`. Uses rounded corner glyphs.

```
╭─┬╮
│ ││
├─┼┤
│ ││
├─┼┤
├─┼┤
│ ││
╰─┴╯
```

```rust
use rusty_rich::box_drawing::BOX_ROUNDED;
```

Example rendered panel:
```
╭──────────────────────╮
│ Rounded panel        │
╰──────────────────────╯
```

### SQUARE

Square corners with light-weight lines.

```
┌─┬┐
│ ││
├─┼┤
│ ││
├─┼┤
├─┼┤
│ ││
└─┴┘
```

```rust
use rusty_rich::box_drawing::BOX_SQUARE;
```

Example:
```
┌──────────────────────┐
│ Square panel         │
└──────────────────────┘
```

### HEAVY

Thick (heavy) lines throughout.

```
┏━┳┓
┃ ┃┃
┣━╋┫
┃ ┃┃
┣━╋┫
┣━╋┫
┃ ┃┃
┗━┻┛
```

```rust
use rusty_rich::box_drawing::BOX_HEAVY;
```

Example:
```
┏━━━━━━━━━━━━━━━━━━━━━━┓
┃ Heavy panel           ┃
┗━━━━━━━━━━━━━━━━━━━━━━┛
```

### HEAVY_EDGE

Heavy outer border with light inner dividers.

```
┏━┯┓
┃ │┃
┠─┼┨
┃ │┃
┠─┼┨
┠─┼┨
┃ │┃
┗━┷┛
```

```rust
use rusty_rich::box_drawing::BOX_HEAVY_EDGE;
```

### HEAVY_HEAD

Heavy border with a heavy header separator and light inner rows.

```
┏━┳┓
┃ ┃┃
┡━╇┩
│ ││
├─┼┤
├─┼┤
│ ││
└─┴┘
```

```rust
use rusty_rich::box_drawing::BOX_HEAVY_HEAD;
```

### DOUBLE

Double-line borders throughout.

```
╔═╦╗
║ ║║
╠═╬╣
║ ║║
╠═╬╣
╠═╬╣
║ ║║
╚═╩╝
```

```rust
use rusty_rich::box_drawing::BOX_DOUBLE;
```

Example:
```
╔══════════════════════╗
║ Double panel         ║
╚══════════════════════╝
```

### DOUBLE_EDGE

Double outer border with single-line inner dividers.

```
╔═╤╗
║ │║
╟─┼╢
║ │║
╟─┼╢
╟─┼╢
║ │║
╚═╧╝
```

```rust
use rusty_rich::box_drawing::BOX_DOUBLE_EDGE;
```

### SQUARE_DOUBLE_HEAD

Square corners with a double-line header separator.

```
┌─┬┐
│ ║│
├─╪┤
│ ││
├─┼┤
├─┼┤
│ ││
└─┴┘
```

```rust
use rusty_rich::box_drawing::BOX_SQUARE_DOUBLE_HEAD;
```

Notice the `head_vertical` (`║`) and `head_row_cross` (`╪`) positions use double-line characters while the rest of the box uses single lines.

### SIMPLE

No visible border -- only content. All characters are spaces.

```




```

```rust
use rusty_rich::box_drawing::BOX_SIMPLE;
```

### SIMPLE_HEAVY

No side borders, only a heavy horizontal rule under the header / before the footer.

```




   ━┿


```

```rust
use rusty_rich::box_drawing::BOX_SIMPLE_HEAVY;
```

### SIMPLE_HEAD

A single light horizontal rule under the header section.

```




  ━┿ 

```

```rust
use rusty_rich::box_drawing::BOX_SIMPLE_HEAD;
```

### MINIMAL

Minimal style with light horizontal rules (no side borders). Used for a clean, understated look.

```
  ╌
  ╌
  ╌



  ╌ 
```

```rust
use rusty_rich::box_drawing::BOX_MINIMAL;
```

### MINIMAL_HEAVY

Same as `MINIMAL` but with heavy horizontal rules (`╍`).

```
  ╍
  ╍
  ╍



  ╍ 
```

```rust
use rusty_rich::box_drawing::BOX_MINIMAL_HEAVY;
```

### MINIMAL_DOUBLE_HEAD

Minimal style with a double horizontal rule for the header separator.

```
  ═ 
  ═ 
  ═ 
    

    
  ═ 
    
```

```rust
use rusty_rich::box_drawing::BOX_MINIMAL_DOUBLE_HEAD;
```

### ASCII

Fully ASCII-compatible box using `+`, `-`, and `|` characters.

```
+--+
| ||
|-+|
| ||
|-+|
|-+|
| ||
+-++
```

```rust
use rusty_rich::box_drawing::BOX_ASCII;
```

Example:
```
+----------------------+
| ASCII panel          |
+----------------------+
```

This is the automatic fallback when the terminal is in ASCII-only mode.

### ASCII2

Alternative ASCII style with fewer intersections.

```
+-++
| ||
| ||
| ||
| ||
| ||
| ||
+-++
```

```rust
use rusty_rich::box_drawing::BOX_ASCII2;
```

### ASCII_DOUBLE_HEAD

ASCII style with an `=`-based double header separator.

```
+-++
| ||
+=+|
| ||
|-+|
|-+|
| ||
+-++
```

```rust
use rusty_rich::box_drawing::BOX_ASCII_DOUBLE_HEAD;
```

### MARKDOWN

Markdown pipe-table style -- no outer border, uses `|` and `-` characters like a Markdown table.

```

| ||
|-||
| ||
|-||
|-||
| ||

```

```rust
use rusty_rich::box_drawing::BOX_MARKDOWN;
```

Example table render:

```
| Name     | Value |
|----------|-------|
| Alpha    | 100   |
| Beta     | 200   |
```

### Quick Reference Table

| Constant                | Outer Border | Inner Dividers | Header Sep | ASCII-safe |
|-------------------------|-------------|----------------|------------|------------|
| `BOX_ROUNDED`           | `╭─╮╰─╯`    | single         | single     | No         |
| `BOX_SQUARE`            | `┌─┐└─┘`    | single         | single     | No         |
| `BOX_HEAVY`             | `┏━┓┗━┛`    | heavy          | heavy      | No         |
| `BOX_HEAVY_EDGE`        | heavy       | single         | single     | No         |
| `BOX_HEAVY_HEAD`        | heavy       | single         | heavy      | No         |
| `BOX_DOUBLE`            | `╔═╗╚═╝`    | double         | double     | No         |
| `BOX_DOUBLE_EDGE`       | double      | single         | single     | No         |
| `BOX_SQUARE_DOUBLE_HEAD`| square      | single         | double     | No         |
| `BOX_MINIMAL`           | none        | none           | light      | No         |
| `BOX_MINIMAL_HEAVY`     | none        | none           | heavy      | No         |
| `BOX_MINIMAL_DOUBLE_HEAD`| none       | none           | double     | No         |
| `BOX_SIMPLE`            | none        | none           | none       | Yes        |
| `BOX_SIMPLE_HEAVY`      | none        | none           | heavy      | Yes        |
| `BOX_SIMPLE_HEAD`       | none        | none           | light      | Yes        |
| `BOX_MARKDOWN`          | none        | pipe `\|`      | `-`        | Yes        |
| `BOX_ASCII`             | `+-+`       | `+`, `-`, `\|`  | `+`, `-`   | Yes        |
| `BOX_ASCII2`            | `+-+`       | minimal        | none       | Yes        |
| `BOX_ASCII_DOUBLE_HEAD` | `+-+`       | `+`, `-`, `\|`  | `=`        | Yes        |

---

## Import Paths

All box styles live under `rusty_rich::box_drawing`:

```rust
use rusty_rich::box_drawing::{
    BoxStyle,                // The struct
    BOX_ROUNDED,             // Predefined styles
    BOX_SQUARE,
    BOX_HEAVY,
    BOX_HEAVY_EDGE,
    BOX_HEAVY_HEAD,
    BOX_DOUBLE,
    BOX_DOUBLE_EDGE,
    BOX_SQUARE_DOUBLE_HEAD,
    BOX_SIMPLE,
    BOX_SIMPLE_HEAVY,
    BOX_SIMPLE_HEAD,
    BOX_MINIMAL,
    BOX_MINIMAL_HEAVY,
    BOX_MINIMAL_DOUBLE_HEAD,
    BOX_ASCII,
    BOX_ASCII2,
    BOX_ASCII_DOUBLE_HEAD,
    BOX_MARKDOWN,
    get_safe_box,            // ASCII fallback helper
};
```

---

## Using Box Styles

### With Panel

Pass a cloned `BoxStyle` to `Panel::box_style()`:

```rust
use rusty_rich::Panel;
use rusty_rich::box_drawing::BOX_DOUBLE;

let panel = Panel::new("Important notice")
    .box_style(BOX_DOUBLE.clone())
    .title("Notice");
```

### With Table

Set the box style on a `Table` to control its border glyphs:

```rust
use rusty_rich::table::Table;
use rusty_rich::box_drawing::BOX_HEAVY;

let table = Table::new()
    .box_style(BOX_HEAVY.clone());
```

---

## get_safe_box() -- ASCII Fallback

When you know the terminal supports only ASCII (for example, on Windows legacy consoles), use `get_safe_box()` to substitute an ASCII-compatible style:

```rust
use rusty_rich::box_drawing::{get_safe_box, BOX_ROUNDED, BOX_ASCII};

// If ascii_only is true, returns BOX_ASCII; otherwise returns the original
let safe = get_safe_box(&BOX_ROUNDED, ascii_only);
assert_eq!(safe.ascii, true || !ascii_only);
```

The function checks two things:

1. If `ascii_only` is `false`, it returns the input style unchanged.
2. If `ascii_only` is `true` and the input style already has `ascii == true`, it returns the input style unchanged.
3. If `ascii_only` is `true` and the input style has `ascii == false`, it returns `BOX_ASCII.clone()`.

This is used internally by `Panel` and `Table` when `ConsoleOptions.ascii_only` is set, so in most cases you do not need to call it manually.

The built-in ASCII-safe styles (those with `ascii: true`) are:

- `BOX_ASCII`
- `BOX_ASCII2`
- `BOX_ASCII_DOUBLE_HEAD`
- `BOX_SIMPLE`
- `BOX_SIMPLE_HEAVY`
- `BOX_SIMPLE_HEAD`
- `BOX_MARKDOWN`

---

## Custom Box Styles

Create a custom box style by passing an 8-line, 4-column string to `BoxStyle::from_str()`.

### from_str(string, ascii)

The string must have exactly 8 lines, each containing exactly 4 characters. The `ascii` parameter marks whether the style is compatible with ASCII-only terminals (used by `get_safe_box`).

```rust
use rusty_rich::box_drawing::BoxStyle;

let custom_def = "\
★★★★
★ ★★
★★★★
★ ★★
★★★★
★★★★
★ ★★
★★★★";

let custom_box = BoxStyle::from_str(custom_def, false);

// Use with a panel
use rusty_rich::Panel;

let panel = Panel::new("Star-bordered content")
    .box_style(custom_box);
```

The rendered panel would use star characters for all border positions:

```
★★★★★★★★★★★★★★★★★★★★
★ Star-bordered content ★
★★★★★★★★★★★★★★★★★★★★
```

### Another example -- dotted lines

```rust
use rusty_rich::box_drawing::BoxStyle;

let dotted = BoxStyle::from_str("\
┌─┬┐
│ ·│
├─┼┤
│ ·│
├─┼┤
├─┼┤
│ ·│
└─┴┘", false);

// This replaces the vertical fill in header/mid/foot rows
// with middle-dot characters while keeping standard corners.
```

### Tips for custom styles

- **Spaces are valid characters.** Styles like `SIMPLE` use spaces to create invisible borders.
- **Keep character widths consistent.** Mixing half-width and full-width characters may produce alignment artifacts.
- **Use the `ascii` flag correctly.** Set it to `true` only if every character in the definition is an ASCII printable character (code point < 128).
- **The `to_string()` method** round-trips the definition so you can inspect a style's raw definition at any time:

```rust
println!("{}", BOX_HEAVY.to_string());
// Output:
// ┏━┳┓
// ┃ ┃┃
// ┣━╋┫
// ┃ ┃┃
// ┣━╋┫
// ┣━╋┫
// ┃ ┃┃
// ┗━┻┛
```

---

## Implementation Details

- Box styles are parsed at runtime via `Lazy` initialization. The definition strings are stored as `&str` constants and parsed into `BoxStyle` structs on first access.
- The struct derives `Debug`, `Clone`, `PartialEq`, and `Eq`.
- `BoxStyle` implements `Display`, which delegates to `to_string()` to produce the 8-line definition.
- `get_safe_box()` is the only public free function in `box_drawing` (besides the style constructors).