taffy/util/
print.rs

1//! Contains the print_tree function for printing a debug representation of the tree
2use crate::tree::{NodeId, PrintTree};
3
4/// Prints a debug representation of the computed layout for a tree of nodes, starting with the passed root node.
5pub fn print_tree(tree: &impl PrintTree, root: NodeId) {
6    println!("TREE");
7    print_node(tree, root, false, String::new());
8
9    /// Recursive function that prints each node in the tree
10    fn print_node(tree: &impl PrintTree, node_id: NodeId, has_sibling: bool, lines_string: String) {
11        let layout = &tree.get_final_layout(node_id);
12        let display = tree.get_debug_label(node_id);
13        let num_children = tree.child_count(node_id);
14
15        let fork_string = if has_sibling { "├── " } else { "└── " };
16        #[cfg(feature = "content_size")]
17        println!(
18                "{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:?})",
19                lines = lines_string,
20                fork = fork_string,
21                display = display,
22                x = layout.location.x,
23                y = layout.location.y,
24                width = layout.size.width,
25                height = layout.size.height,
26                content_width = layout.content_size.width,
27                content_height = layout.content_size.height,
28                bl = layout.border.left,
29                br = layout.border.right,
30                bt = layout.border.top,
31                bb = layout.border.bottom,
32                pl = layout.padding.left,
33                pr = layout.padding.right,
34                pt = layout.padding.top,
35                pb = layout.padding.bottom,
36                key = node_id,
37            );
38        #[cfg(not(feature = "content_size"))]
39        println!(
40            "{lines}{fork} {display} [x: {x:<4} y: {y:<4} width: {width:<4} height: {height:<4}] ({key:?})",
41            lines = lines_string,
42            fork = fork_string,
43            display = display,
44            x = layout.location.x,
45            y = layout.location.y,
46            width = layout.size.width,
47            height = layout.size.height,
48            key = node_id,
49        );
50        let bar = if has_sibling { "│   " } else { "    " };
51        let new_string = lines_string + bar;
52
53        // Recurse into children
54        for (index, child) in tree.child_ids(node_id).enumerate() {
55            let has_sibling = index < num_children - 1;
56            print_node(tree, child, has_sibling, new_string.clone());
57        }
58    }
59}