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
# Markdown

`MarkdownRender` renders Markdown text as styled terminal output. It uses [`pulldown-cmark`](https://github.com/raphlinus/pulldown-cmark) for parsing and supports headings, code blocks, lists, blockquotes, links, horizontal rules, and inline formatting.

```rust
use rusty_rich::{render_markdown, MarkdownRender};
use rusty_rich::Console;

let mut console = Console::new();
let md = render_markdown("# Hello\n\nThis is **rich** markdown.");
console.println(&md);
```

---

## render_markdown(text)

```rust
pub fn render_markdown(md: &str) -> MarkdownRender
```

The entry point. Takes a Markdown source string and returns a `MarkdownRender` builder with default settings:

- Width: auto (terminal width or parent container width)
- Code theme: `"default"`
- Hyperlinks: enabled

```rust
let md = render_markdown("Hello **world**");
console.println(&md);
```

---

## MarkdownRender

The builder struct returned by `render_markdown()`. Configures rendering options before display.

### width()

Override the output width. By default the renderable inherits the terminal width or parent container width.

```rust
let md = render_markdown("Some text")
    .width(60);
```

A narrower width causes text to wrap earlier; a wider width allows longer lines. This is useful when rendering Markdown inside a `Panel`, `Table`, or `Layout` where the available space is constrained.

---

## Supported Elements

### Headings (H1 -- H6)

All six heading levels are supported.

**H1 and H2** are rendered with styled prefixes and a horizontal rule beneath them:

- **H1**: `# ` prefix with `markdown.h1` style (bold + bright cyan in the default theme), followed by a double-line rule (``).
- **H2**: `## ` prefix with `markdown.h2` style (bold + cyan), followed by a single-line rule (``).

**H3 through H6** use bold text with a `#`-prefix matching the heading level.

```rust
let md = render_markdown(
    "# Heading 1\n\
     ## Heading 2\n\
     ### Heading 3\n\
     #### Heading 4\n\
     ##### Heading 5\n\
     ###### Heading 6"
);
console.println(&md);
```

Visual output (conceptual):

```
# Heading 1
══════════════════════════════════════════

## Heading 2
──────────────────────────────────────────

### Heading 3

#### Heading 4

##### Heading 5

###### Heading 6
```

The rule under H1 and H2 spans the full configured width and uses the theme style `rule.line`.

### Code Blocks

Fenced code blocks (with or without a language label) and indented code blocks are rendered in a box-drawing frame.

```rust
let md = render_markdown(
    "```rust\n\
     fn hello() {\n\
         println!(\"Hello!\");\n\
     }\n\
     ```"
);
console.println(&md);
```

Output:

```
┌─ Code: rust ───────────────────────┐
│ fn hello() {                       │
│     println!("Hello!");            │
│ }                                  │
└────────────────────────────────────┘
```

- The top border shows `┌─ Code: {language} ` with a trailing horizontal line.
- Code lines are prefixed with ``.
- The bottom border is `` followed by horizontal rules to the full width.
- If no language is specified, the label reads `Code` instead of `Code: {language}`.
- The code box uses the `markdown.code` theme style (black background + yellow foreground in the default theme).

### Inline Code

Text wrapped in backticks is styled inline using the same `markdown.code` style.

```rust
let md = render_markdown("Use the `render_markdown()` function.");
console.println(&md);
```

Inline code appears with the code style applied to the backtick-delimited text.

### Lists

Both ordered and unordered lists are supported with bullet characters and indentation.

- **Top-level items**: `` bullet character
- **Nested items (depth >= 2)**: `` bullet character with two-space indentation per level

```rust
let md = render_markdown(
    "- Item one\n\
     - Item two\n\
       - Nested item\n\
         - Deeply nested"
);
console.println(&md);
```

Output:

```
• Item one
• Item two
  ◦ Nested item
    ◦ Deeply nested
```

Ordered (numbered) lists from the Markdown source are parsed and rendered with the same bullet characters; the numeric prefixes from the source are not preserved in the terminal output.

### Blockquotes

Blockquotes render with a styled `▌` character on the left margin using the `markdown.blockquote` style.

```rust
let md = render_markdown("> This is a blockquote.\n> It can span multiple lines.");
console.println(&md);
```

Output:

```
▌ This is a blockquote.
▌ It can span multiple lines.
```

### Links

Links are rendered with the `markdown.link` style (bright blue + underline in the default theme) as `text (url)`.

```rust
let md = render_markdown("Visit [rusty-rich](https://github.com/example/rusty-rich) today!");
console.println(&md);
```

Output:

```
Visit rusty-rich (https://github.com/example/rusty-rich) today!
```

When no display text is provided (e.g., `<https://example.com>` syntax), the URL itself is shown.

### Horizontal Rules

A Markdown thematic break (`---`, `***`, `___`) renders as a horizontal line using the `Rule` component with default `─` characters.

```rust
let md = render_markdown("Above the line\n\n---\n\nBelow the line");
console.println(&md);
```

The rule spans the full available width and uses the `rule.line` style from the theme.

### Emphasis, Strong, Strikethrough

- **Emphasis** (`*text*` or `_text_`): italic styling is applied.
- **Strong** (`**text**` or `__text__`): bold styling is applied.
- **Strikethrough** (`~~text~~`): handled by the parser as a `Strikethrough` tag. Text inside strikethrough uses a style with `strikethrough(true)`.

```rust
let md = render_markdown(
    "This is *italic*, this is **bold**, and this is ~~strikethrough~~."
);
console.println(&md);
```

---

## Width Configuration

The render width can be set explicitly via `.width()` on the builder, or it falls back to `ConsoleOptions.max_width` (typically the terminal width).

```rust
// Use terminal width (default)
let md = render_markdown("Some text");

// Fixed width
let md_60 = render_markdown("Some text").width(60);

// Narrow width for side-by-side layouts
let md_narrow = render_markdown("Some text").width(30);
```

Width affects:
- The length of horizontal rules under H1/H2 headings
- The extent of the code block bottom border
- Text wrapping behavior (though the current implementation does not perform word wrapping -- lines longer than the width extend beyond the boundary)

---

## Theme Styles

The Markdown renderer uses theme style keys that users can customize via the `Theme` system.

| Style Key               | Default Style                    | Used For                         |
|-------------------------|----------------------------------|----------------------------------|
| `markdown.h1`           | bold + bright cyan               | H1 heading prefix `# `          |
| `markdown.h2`           | bold + cyan                      | H2 heading prefix `## `         |
| `markdown.code`         | yellow on black                  | Code blocks and inline code      |
| `markdown.link`         | bright blue + underline          | Link text and URL display        |
| `markdown.blockquote`   | (inherits default)               | Blockquote `` marker           |
| `markdown.item`         | (inherits default)               | List item bullets                |
| `rule.line`             | bright black                     | Horizontal rule characters       |

### Customizing styles

Override any style by setting it on the `Console`'s theme:

```rust
use rusty_rich::{Console, Style, Color};

let mut console = Console::new();
console.theme.set(
    "markdown.h1",
    Style::new().bold(true).color(Color::parse("magenta").unwrap()),
);
console.theme.set(
    "markdown.code",
    Style::new().color(Color::parse("green").unwrap()).bgcolor(Color::parse("grey").unwrap()),
);
```

---

## Example: Rendering a README

The following example loads a Markdown file and renders it to the console:

```rust
use rusty_rich::{render_markdown, Console};
use std::fs;

fn main() {
    let mut console = Console::new();

    // Read a README file from disk
    let readme = fs::read_to_string("README.md")
        .expect("Failed to read README.md");

    // Render it with a generous width
    let md = render_markdown(&readme).width(72);
    console.println(&md);
}
```

For a more complete example that demonstrates multiple Markdown features in a single render:

```rust
use rusty_rich::{render_markdown, Console};

fn main() {
    let mut console = Console::new();

    let md_content = r#"
# rusty-rich

A Rust port of Python's [Rich](https://github.com/Textualize/rich) library.

## Features

- **Styling**: bold, italic, underline, strikethrough, colors
- **Tables**: tabular data with headers and alignment
- **Markdown rendering**: headings, code, lists, blockquotes
- **Syntax highlighting**: powered by syntect

### Code Example

```rust
fn greet(name: &str) -> String {
    format!("Hello, {name}!")
}
```

### Notes

> rusty-rich aims for feature parity with Python Rich 13.x.
> It is not a drop-in replacement, but a faithful port.

---

*Built with Rust and pulldown-cmark.*
"#;

    let md = render_markdown(md_content).width(72);
    console.println(&md);
}
```

Output (conceptual, within a 72-column terminal):

```
# rusty-rich
══════════════════════════════════════════════════════════════════════════════

A Rust port of Python's Rich (https://github.com/Textualize/rich) library.

## Features
──────────────────────────────────────────────────────────────────────────────

• Styling: bold, italic, underline, strikethrough, colors
• Tables: tabular data with headers and alignment
• Markdown rendering: headings, code, lists, blockquotes
• Syntax highlighting: powered by syntect

### Code Example

┌─ Code: rust ───────────────────────────────────────────────────────────────┐
│ fn greet(name: &str) -> String {                                          │
│     format!("Hello, {name}!")                                              │
│ }                                                                          │
└────────────────────────────────────────────────────────────────────────────┘

### Notes

▌ rusty-rich aims for feature parity with Python Rich 13.x.
▌ It is not a drop-in replacement, but a faithful port.

──────────────────────────────────────────────────────────────────────────────

Built with Rust and pulldown-cmark.

```

### Integrating with other renderables

Since `MarkdownRender` implements the `Renderable` trait, it can be composed with other components:

```rust
use rusty_rich::{render_markdown, Panel, Console};

let mut console = Console::new();

let readme = render_markdown("# Project\n\nDescription here.").width(40);

let panel = Panel::new(readme)
    .title("README Preview")
    .padding(0, 2, 0, 2);

console.println(&panel);
```

The Markdown content is rendered inside a bordered panel with the title "README Preview".

---

## Import Paths

```rust
use rusty_rich::render_markdown;          // Builder function
use rusty_rich::MarkdownRender;            // The builder struct
use rusty_rich::Theme;                     // For customizing markdown styles
use rusty_rich::Style;                     // For constructing style overrides
use rusty_rich::Color;                     // For color values in custom styles
```