Make tree debug printing more readable (#270)
* Make tree debug printing more readable
* Remove unused parameter
b35fae9dc527a5369fc7347a7acfb509dd5eef57 · por George Atkinson · (hace 3 años) · padre ffa0c5d94 · ver el árbol en este commit
diff --git a/src/debug.rs b/src/debug.rs
index e2935858d..729921628 100644
--- a/src/debug.rs
+++ b/src/debug.rs
@@ -7,26 +7,32 @@ use crate::tree::LayoutTree;
/// Prints a debug representation of the computed layout for a tree of nodes, starting with the passed root node.
pub fn print_tree(tree: &impl LayoutTree, root: Node) {
- println!();
- print_node(tree, root, 0);
+ println!("TREE");
+ print_node(tree, root, false, String::new());
}
-fn print_node(tree: &impl LayoutTree, node: Node, level: usize) {
+fn print_node(tree: &impl LayoutTree, node: Node, has_sibling: bool, lines_string: String) {
let layout = tree.layout(node);
+
+ let num_children = tree.child_count(node);
+
+ let fork_string = if has_sibling { "├── " } else { "└── " };
println!(
- "{space:leftpad$}{key:?} (x:{x}, y: {y}, width: {width}, height: {height})",
- space = " ",
- leftpad = level * 4,
- key = node.data(),
+ "{lines}{fork}[x: {x}, y: {y}, width: {width}, height: {height}]",
+ lines = lines_string,
+ fork = fork_string,
x = layout.location.x,
y = layout.location.y,
width = layout.size.width,
height = layout.size.height,
);
+ let bar = if has_sibling { "│ " } else { " " };
+ let new_string = lines_string + bar;
// Recurse into children
- for child in tree.children(node) {
- print_node(tree, *child, level + 1)
+ for (index, child) in tree.children(node).enumerate() {
+ let has_sibling = index < num_children - 1;
+ print_node(tree, *child, has_sibling, new_string.clone());
}
}