gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 89Markdown 11TOML 91930 Definiciones

⎇ be975378e34b7c7892b983971819999809e713b8 src/util/print.rs Rust · 76 líneas · 3.2 KB Historia Quién tocó qué crudo

Add write_tree method (#925) be975378e · Elchanan Haas ·

1//! Contains functions for printing a debug representation of the tree
2use crate::tree::{NodeId, PrintTree};
3use std::io;
4
5/// Prints a debug representation of the computed layout for a tree of nodes, starting with the passed root node.
6pub fn print_tree(tree: &impl PrintTree, root: NodeId) {
7    // Rust code generally makes the assumption that writes to Stdout won't fail, so paincing on error is fine here.
8    write_tree(io::stdout().lock(), tree, root).expect("Wrote tree debug representation to Stdout");
9}
10
11/// Writes a debug representation of the computed layout to the writer, starting with the passed root node.
12pub fn write_tree(mut writer: impl io::Write, tree: &impl PrintTree, root: NodeId) -> io::Result<()> {
13    writeln!(writer, "TREE")?;
14    write_node(&mut writer, tree, root, false, String::new())?;
15
16    /// Recursive function that writes each node in the tree
17    fn write_node(
18        writer: &mut impl io::Write,
19        tree: &impl PrintTree,
20        node_id: NodeId,
21        has_sibling: bool,
22        lines_string: String,
23    ) -> io::Result<()> {
24        let layout = &tree.get_final_layout(node_id);
25        let display = tree.get_debug_label(node_id);
26        let num_children = tree.child_count(node_id);
27
28        let fork_string = if has_sibling { "├── " } else { "└── " };
29        #[cfg(feature = "content_size")]
30        writeln!(
31                writer,
32                "{lines}{fork} {display} [x: {x:<4} y: {y:<4} w: {width:<4} h: {height:<4} content_w: {content_width:<4} content_h: {content_height:<4} border: l:{bl} r:{br} t:{bt} b:{bb}, padding: l:{pl} r:{pr} t:{pt} b:{pb}] ({key:?})",
33                lines = lines_string,
34                fork = fork_string,
35                display = display,
36                x = layout.location.x,
37                y = layout.location.y,
38                width = layout.size.width,
39                height = layout.size.height,
40                content_width = layout.content_size.width,
41                content_height = layout.content_size.height,
42                bl = layout.border.left,
43                br = layout.border.right,
44                bt = layout.border.top,
45                bb = layout.border.bottom,
46                pl = layout.padding.left,
47                pr = layout.padding.right,
48                pt = layout.padding.top,
49                pb = layout.padding.bottom,
50                key = node_id,
51            )?;
52        #[cfg(not(feature = "content_size"))]
53        writeln!(
54            writer,
55            "{lines}{fork} {display} [x: {x:<4} y: {y:<4} width: {width:<4} height: {height:<4}] ({key:?})",
56            lines = lines_string,
57            fork = fork_string,
58            display = display,
59            x = layout.location.x,
60            y = layout.location.y,
61            width = layout.size.width,
62            height = layout.size.height,
63            key = node_id,
64        );
65        let bar = if has_sibling { "│   " } else { "    " };
66        let new_string = lines_string + bar;
67
68        // Recurse into children
69        for (index, child) in tree.child_ids(node_id).enumerate() {
70            let has_sibling = index < num_children - 1;
71            write_node(writer, tree, child, has_sibling, new_string.clone())?;
72        }
73        Ok(())
74    }
75    Ok(())
76}