#debug #debugging #tree #string #pretty #time

debug_tree

Build a tree ocone element at a time and output it as a pretty string

5 releases

✓ Uses Rust 2018 edition

0.2.5 Jul 1, 2019
0.2.4 May 21, 2019
0.2.3 May 15, 2019
0.2.2 May 12, 2019
0.2.1 May 12, 2019
Download history 14/week @ 2019-05-06 29/week @ 2019-05-13 33/week @ 2019-05-20 12/week @ 2019-05-27 5/week @ 2019-06-03 16/week @ 2019-06-10 12/week @ 2019-06-17 58/week @ 2019-06-24 22/week @ 2019-07-01 27/week @ 2019-07-08 2/week @ 2019-07-15 11/week @ 2019-07-22 16/week @ 2019-07-29

59 downloads per month

MIT license

34KB
680 lines

Debug Tree

This library allows you to build a tree one element at a time and output it as a pretty string. This is particularly convenient for generating clean output from nested and recursive functions. A design goal was to allow this library to be used as a drop-in replacement of println!(...).

Recursive Example

By adding the add_branch!(...) macro at the start of a recursive function, you can see the entire call tree, instantly.

use debug_tree::{default_tree, add_branch, add_leaf};
fn factors(x: usize) {
    add_branch!("{}", x);
    for i in 1..x {
        if x % i == 0 {
            factors(i);
        }
    }
}
fn main() {
    factors(6);
    default_tree().flush_print();
}
6
├╼ 1
├╼ 2
│ └╼ 1
└╼ 3
 └╼ 1

Nested Example

Branches also make nested function calls a lot easier to follow.

use debug_tree::{default_tree, add_branch, add_leaf};
fn a() {
    add_branch!("a");
    b();
    c();
}
fn b() {
    add_branch!("b");
    c();
}
fn c() {
    add_branch!("c");
    add_leaf!("Nothing to see here");
}

fn main() {
    a();
    default_tree().flush_print();
}
a
├╼ b
│ └╼ c
│   └╼ Nothing to see here
└╼ c
  └╼ Nothing to see here

Line Breaks

Newlines in multi-line strings are automatically indented.

use debug_tree::{default_tree, add_branch, add_leaf};
fn main() {
    add_branch!("1");
    add_leaf!("1.1\nNext line");
    add_leaf!("1.2");
    default_tree().flush_print();
}
1
├╼ 1.1
│  Next line
└╼ 1.2

Non-Macro Version

In the case that multiple trees are needed, the trees can be created manually without the helper macros.

use debug_tree::TreeBuilder;
fn main() {
    // Make a new tree.
    let tree = TreeBuilder::new();
    
    // Add a scoped branch. The next item added will belong to the branch.
    let branch = tree.add_branch("1 Branch"); 
    
    // Add a leaf to the current branch
    tree.add_leaf("1.1 Child");
    
    // Leave scope early
    branch.release();
    tree.add_leaf("2 Sibling"); 
    
    tree.flush_print(); // Print and clear.
    // default_tree().peek_print(); // Would print but not clear.
}

1 Branch
└╼ 1.1 Child
2 Sibling

No runtime deps