rs-rich 0.0.9

A faithful Rust port of the Python `rich` terminal-rendering 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
//! Trees.
//!
//! Port of upstream `rich/tree.py`. A [`Tree`] renders a hierarchy with the
//! familiar `├──`/`└──` guide lines: string (markup), `Text` or renderable
//! labels, per-node `style`/`guide_style` (a bold guide style draws heavy
//! guides, `underline2` double ones, and an ASCII-only console ASCII ones),
//! collapsed (`expanded = false`) nodes and a hidden root.

use crate::console::Justify;
use crate::console::{Console, ConsoleOptions};
use crate::measure::Measurement;
use crate::protocol::Renderable;
use crate::segment::Segment;
use crate::style::{Style, StyleType};
use crate::table::Cell;

/// `Tree.ASCII_GUIDES`: the guides on a console that cannot encode Unicode.
pub const ASCII_GUIDES: [&str; 4] = ["    ", "|   ", "+-- ", "`-- "];
/// `Tree.TREE_GUIDES`: thin, heavy (bold) and double (underline2) guides.
pub const TREE_GUIDES: [[&str; 4]; 3] = [
    ["    ", "│   ", "├── ", "└── "],
    ["    ", "┃   ", "┣━━ ", "┗━━ "],
    ["    ", "║   ", "╠══ ", "╚══ "],
];

// Indexes into a guide set, as upstream's `SPACE, CONTINUE, FORK, END`.
const SPACE: usize = 0;
const CONTINUE: usize = 1;
const FORK: usize = 2;
const END: usize = 3;

/// A node in a hierarchy. Mirrors `rich.tree.Tree`.
pub struct Tree {
    label: Cell,
    style: StyleType,
    guide_style: StyleType,
    children: Vec<Tree>,
    expanded: bool,
    highlight: bool,
    hide_root: bool,
}

impl Tree {
    /// A new tree/subtree with the given label. A string label is console
    /// markup, as upstream's `Tree("[b]root")` is; pass a
    /// [`Text`](crate::text::Text) for a literal one.
    pub fn new(label: impl Into<Cell>) -> Self {
        Tree {
            label: label.into(),
            style: StyleType::Name("tree".to_string()),
            guide_style: StyleType::Name("tree.line".to_string()),
            children: Vec::new(),
            expanded: true,
            highlight: false,
            hide_root: false,
        }
    }

    /// Highlight string labels (upstream `Tree(highlight=…)`, default off).
    /// Upstream renders every label with the root's setting.
    pub fn highlight(mut self, highlight: bool) -> Self {
        self.highlight = highlight;
        self
    }

    /// The style of this node's label and, stacked, its descendants'
    /// (upstream `Tree(style=…)`, default `"tree"`).
    pub fn style(mut self, style: impl Into<StyleType>) -> Self {
        self.style = style.into();
        self
    }

    /// The style of the guide lines below this node (upstream
    /// `Tree(guide_style=…)`, default `"tree.line"`). Bold selects the heavy
    /// guides and `underline2` the double ones.
    pub fn guide_style(mut self, style: impl Into<StyleType>) -> Self {
        self.guide_style = style.into();
        self
    }

    /// Whether this node's children are shown (upstream `expanded`, default
    /// on).
    pub fn expanded(mut self, expanded: bool) -> Self {
        self.expanded = expanded;
        self
    }

    /// Hide the root node, rendering its children as the top level (upstream
    /// `hide_root`, default off; read from the root only).
    pub fn hide_root(mut self, hide_root: bool) -> Self {
        self.hide_root = hide_root;
        self
    }

    /// Set [`style`](Self::style) in place.
    pub fn set_style(&mut self, style: impl Into<StyleType>) -> &mut Self {
        self.style = style.into();
        self
    }

    /// Set [`guide_style`](Self::guide_style) in place.
    pub fn set_guide_style(&mut self, style: impl Into<StyleType>) -> &mut Self {
        self.guide_style = style.into();
        self
    }

    /// Set [`expanded`](Self::expanded) in place (upstream assigns
    /// `tree.expanded`).
    pub fn set_expanded(&mut self, expanded: bool) -> &mut Self {
        self.expanded = expanded;
        self
    }

    /// Set [`hide_root`](Self::hide_root) in place.
    pub fn set_hide_root(&mut self, hide_root: bool) -> &mut Self {
        self.hide_root = hide_root;
        self
    }

    /// Set [`highlight`](Self::highlight) in place.
    pub fn set_highlight(&mut self, highlight: bool) -> &mut Self {
        self.highlight = highlight;
        self
    }

    /// Replace the label in place.
    pub fn set_label(&mut self, label: impl Into<Cell>) -> &mut Self {
        self.label = label.into();
        self
    }

    /// The label.
    pub fn label(&self) -> &Cell {
        &self.label
    }

    /// The node's style (see [`style`](Self::style)).
    pub fn get_style(&self) -> &StyleType {
        &self.style
    }

    /// The node's guide style (see [`guide_style`](Self::guide_style)).
    pub fn get_guide_style(&self) -> &StyleType {
        &self.guide_style
    }

    /// Whether children are shown.
    pub fn is_expanded(&self) -> bool {
        self.expanded
    }

    /// Whether the root is hidden.
    pub fn is_root_hidden(&self) -> bool {
        self.hide_root
    }

    /// Whether string labels are highlighted.
    pub fn is_highlighted(&self) -> bool {
        self.highlight
    }

    /// The child nodes.
    pub fn children(&self) -> &[Tree] {
        &self.children
    }

    /// The child nodes, mutably.
    pub fn children_mut(&mut self) -> &mut Vec<Tree> {
        &mut self.children
    }

    /// Add a child with `label`, returning a mutable reference to it so further
    /// descendants can be attached. Mirrors `Tree.add` with its defaults: the
    /// child inherits this node's `style` and `guide_style` and is expanded.
    pub fn add(&mut self, label: impl Into<Cell>) -> &mut Tree {
        let mut child = Tree::new(label);
        child.style = self.style.clone();
        child.guide_style = self.guide_style.clone();
        self.add_tree(child)
    }

    /// Attach an already built subtree as the last child, returning it. Use
    /// it for upstream's `Tree.add(label, style=…, guide_style=…,
    /// expanded=…)`.
    pub fn add_tree(&mut self, child: Tree) -> &mut Tree {
        self.children.push(child);
        self.children.last_mut().expect("just pushed a child")
    }

    /// `make_guide`: the guide segment at `index` for a level in `style`.
    fn make_guide(options: &ConsoleOptions, index: usize, style: Style) -> Segment {
        let line = if options.ascii_only() {
            ASCII_GUIDES[index]
        } else {
            let guide = if style.attr(BOLD) == Some(true) {
                1
            } else if style.attr(UNDERLINE2) == Some(true) {
                2
            } else {
                0
            };
            TREE_GUIDES[if options.legacy_windows { 0 } else { guide }][index]
        };
        Segment::new(line, Some(style))
    }
}

// Attribute indexes in `Style` (bold, underline2).
const BOLD: usize = 0;
const UNDERLINE2: usize = 9;

/// `Styled(node.label, style)` for the render loop: the label rendered as
/// upstream renders a `str`/`Text`/renderable, with `style` beneath it.
struct Label<'a> {
    label: &'a Cell,
    style: &'a Style,
    highlight: bool,
}

impl Renderable for Label<'_> {
    fn rich_render(&self, console: &Console, options: &ConsoleOptions) -> Vec<Segment> {
        let segments = match self.label {
            Cell::Renderable(renderable) => renderable.rich_render(console, options),
            cell => cell
                .to_text(console, Some(self.highlight))
                .unwrap_or_default()
                .rich_render(console, options),
        };
        if self.style.is_null() {
            segments
        } else {
            Segment::apply_style(&segments, self.style)
        }
    }
}

impl Tree {
    /// Port of `Tree.__rich_console__`, as lines.
    ///
    /// Like upstream, this walks an explicit stack rather than recursing, so
    /// a very deep tree cannot overflow the thread stack. The prefix (four
    /// cells per level) is only copied for nodes that still have room to
    /// render: materialising every prefix would cost quadratic memory on a
    /// deep chain.
    fn render_lines_into(&self, console: &Console, options: &ConsoleOptions) -> Vec<Vec<Segment>> {
        let get_style = |style: &StyleType| console.get_style(style).unwrap_or_default();
        let guide_style = get_style(&self.guide_style);
        let mut levels: Vec<Segment> =
            vec![Self::make_guide(options, CONTINUE, guide_style.clone())];
        let mut stack: Vec<(&[Tree], usize)> = vec![(std::slice::from_ref(self), 0)];
        let mut guide_style_stack = vec![guide_style];
        let mut style_stack = vec![get_style(&self.style)];
        let remove_guide_styles = Style::parse("not bold not underline2").unwrap_or_default();
        let offset = if self.hide_root { 2 } else { 1 };
        let pad = options.justify != Justify::Default;
        let mut depth = 0usize;
        let mut lines = Vec::new();
        let level_style = |segment: &Segment| segment.style.clone().unwrap_or_default();

        while let Some((siblings, next)) = stack.last_mut() {
            let siblings: &[Tree] = siblings;
            let Some(node) = siblings.get(*next) else {
                stack.pop();
                levels.pop();
                if let Some(level) = levels.last_mut() {
                    let style = level_style(level);
                    *level = Self::make_guide(options, FORK, style);
                    guide_style_stack.pop();
                    style_stack.pop();
                }
                continue;
            };
            *next += 1;
            let last = *next == siblings.len();
            if last {
                let level = levels.last_mut().expect("a level per open stack entry");
                *level = Self::make_guide(options, END, level_style(level));
            }

            let current_guide = guide_style_stack.last().cloned().unwrap_or_default();
            let current_style = style_stack.last().cloned().unwrap_or_default();
            let node_guide_style = current_guide.combine(&get_style(&node.guide_style));
            let style = current_style.combine(&get_style(&node.style));
            let prefix_width = levels.len().saturating_sub(offset) * 4;

            if !(depth == 0 && self.hide_root) && prefix_width < options.max_width {
                let mut prefix: Vec<Segment> = levels[offset.min(levels.len())..].to_vec();
                let mut label_options = options.update_width(options.max_width - prefix_width);
                label_options.highlight = Some(self.highlight);
                label_options.height = None;
                let label = Label {
                    label: &node.label,
                    style: &style,
                    highlight: self.highlight,
                };
                let background = Style::from_color(None, style.bgcolor().cloned());
                for (index, label_line) in console
                    .render_lines(&label, &label_options, pad)
                    .into_iter()
                    .enumerate()
                {
                    let mut line = Vec::new();
                    if !prefix.is_empty() {
                        line.extend(prefix.iter().map(|segment| {
                            let own = segment.style.clone().unwrap_or_default();
                            let own = if background.is_null() {
                                own
                            } else {
                                background.combine(&own)
                            };
                            let style = if own.is_null() {
                                remove_guide_styles.clone()
                            } else {
                                own.combine(&remove_guide_styles)
                            };
                            Segment::new(segment.text.clone(), Some(style))
                        }));
                    }
                    line.extend(label_line);
                    lines.push(line);
                    if index == 0 && !prefix.is_empty() {
                        let level = prefix.last_mut().expect("non-empty prefix");
                        *level = Self::make_guide(
                            options,
                            if last { SPACE } else { CONTINUE },
                            level_style(level),
                        );
                    }
                }
            }

            if node.expanded && !node.children.is_empty() {
                let level = levels.last_mut().expect("a level per open stack entry");
                *level = Self::make_guide(
                    options,
                    if last { SPACE } else { CONTINUE },
                    level_style(level),
                );
                levels.push(Self::make_guide(
                    options,
                    if node.children.len() == 1 { END } else { FORK },
                    node_guide_style,
                ));
                style_stack.push(current_style.combine(&get_style(&node.style)));
                guide_style_stack.push(current_guide.combine(&get_style(&node.guide_style)));
                stack.push((&node.children, 0));
                depth += 1;
            }
        }
        lines
    }
}

impl Drop for Tree {
    /// Drop descendants from an explicit stack: the derived drop recurses
    /// once per level and overflows the stack on a very deep tree.
    fn drop(&mut self) {
        let mut pending = std::mem::take(&mut self.children);
        while let Some(mut child) = pending.pop() {
            pending.append(&mut child.children);
        }
    }
}

impl Renderable for Tree {
    fn rich_render(&self, console: &Console, options: &ConsoleOptions) -> Vec<Segment> {
        let lines = self.render_lines_into(console, options);
        let mut segments = Vec::new();
        let last = lines.len().saturating_sub(1);
        for (index, line) in lines.into_iter().enumerate() {
            // Upstream ends every row with a newline, so an empty last row
            // (`Tree("")`) is still a row; mark it with an empty segment.
            if index == last && line.is_empty() {
                segments.push(Segment::new("", None));
            }
            segments.extend(line);
            if index != last {
                segments.push(Segment::line());
            }
        }
        segments
    }

    /// Port of `Tree.__rich_measure__`: the widest label plus its indent of
    /// four cells per level, for both bounds.
    fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement {
        // Iterative, like the render: `(node, level)` pairs to visit.
        let mut width = (0, 0);
        let mut pending: Vec<(&Tree, usize)> = vec![(self, 0)];
        while let Some((tree, level)) = pending.pop() {
            let label = tree.label.measure_cell(console, options);
            let indent = level * 4;
            width.0 = width.0.max(label.minimum + indent);
            width.1 = width.1.max(label.maximum + indent);
            if tree.expanded {
                pending.extend(tree.children.iter().map(|child| (child, level + 1)));
            }
        }
        Measurement::new(width.0, width.1)
    }
}

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

    fn console() -> Console {
        Console::builder()
            .force_terminal(true)
            .color_system(Some(ColorSystem::Truecolor))
            .width(40)
            .build()
    }

    #[test]
    fn nested_tree() {
        let mut tree = Tree::new("root");
        let a = tree.add("child A");
        a.add("leaf A1");
        a.add("leaf A2");
        tree.add("child B");
        let out = console().render_export(&tree);
        let expected = concat!(
            "root\n",
            "├── child A\n",
            "│   ├── leaf A1\n",
            "│   └── leaf A2\n",
            "└── child B\n",
        );
        assert_eq!(out, expected);
    }

    #[test]
    fn deep_tree_renders_without_recursion() {
        // Upstream renders from an explicit stack, so a 20 000-level chain is
        // fine; a recursive render (or drop, or measure) overflows a normal
        // 2 MiB thread stack long before that.
        let handle = std::thread::Builder::new()
            .stack_size(2 * 1024 * 1024)
            .spawn(|| {
                let mut tree = Tree::new("0");
                let mut node = &mut tree;
                for depth in 1..20_000 {
                    node = node.add(depth.to_string());
                }
                let console = Console::builder().width(12).build();
                let out = console.render_export(&tree);
                let measured = Measurement::get(&console, &console.options(), &tree);
                (out, measured)
            })
            .expect("spawn");
        let (out, measured) = handle.join().expect("deep tree render overflowed");
        // Labels render while the guides leave room (4 cells per level at
        // width 12: depths 0-2); deeper nodes have no room and emit nothing.
        assert_eq!(out, "0\n└── 1\n    └── 2\n");
        // `Measurement.get` clamps the 80 001-cell measure to the width.
        assert_eq!(measured, Measurement::new(12, 12));
    }
}