File: README.md

package info (click to toggle)
thunderbird 1%3A140.4.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,609,432 kB
  • sloc: cpp: 7,672,442; javascript: 5,901,613; ansic: 3,898,954; python: 1,413,343; xml: 653,997; asm: 462,286; java: 180,927; sh: 113,489; makefile: 20,460; perl: 14,288; objc: 13,059; yacc: 4,583; pascal: 3,352; lex: 1,720; ruby: 1,222; exp: 762; sql: 715; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (284 lines) | stat: -rw-r--r-- 6,285 bytes parent folder | download | duplicates (15)
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
# Debug Tree

This library allows you to build a tree one element at a time and output it as a pretty string.

The tree can easily be output to a `String`, `stdout` or a file.

This is particularly convenient for generating clean output from nested and recursive functions.

* [Recursive Fibonacci Example](#recursive-fibonacci-example)
* [Overview](#overview)
* [More Examples](#more-examples)
    * [Multiple Tagged Trees](#multiple-tagged-trees)
    * [Nested Functions](#nested-functions)
    * [Panic](#panics)
    * [Without Macros](#without-macros)


## Recursive Fibonacci Example

Using the `add_branch!()` macro at the start of the `factors()` function, you can generate an entire call tree, with minimal effort.

<!--{ fibonacci.rs | code: rust }-->
```rust
use debug_tree::*;

fn factors(x: usize) {
    add_branch!("{}", x); // <~ THE MAGIC LINE
    for i in 1..x {
        if x % i == 0 {
            factors(i);
        }
    }
}

fn main() {
    // output to file at the end of this block
    defer_write!("examples/out/fibonacci.txt");
    add_branch!("A Fibonacci Tree");
    factors(6);
    add_leaf!("That's All Folks!");
}
```
<!--{ end }-->

<!--{ out/fibonacci.txt | code }-->
```
A Fibonacci Tree
├╼ 6
│ ├╼ 1
│ ├╼ 2
│ │ └╼ 1
│ └╼ 3
│   └╼ 1
└╼ That's All Folks!
```
<!--{ end }-->

## Overview

- Add a branch
    - `add_branch!("Hello, {}", "World")`
    - The branch will exit at the end of the current block

- Add a leaf
    - `add_leaf!("I am a {}", "leaf")`
    - Added to the current scoped branch

- Print a tree, or write it to file at the end of a block
    - `defer_print!()`
    - `defer_write!("filename.txt")`
    - The tree will be empty after these calls
    - To prevent clearing, use `defer_peek_print!` and `defer_peek_write!`


- Handle multiple trees using named trees
    - `add_branch_to!("A", "I'm a branch on tree 'A'")`
    - `add_leaf_to!("A", "I'm a leaf on tree 'A'")`
    - `defer_print!("A")`
    - `defer_write!("A", "filename.txt")`

- Get a named tree
    - `tree("TREE_NAME")`

- Retrieve the pretty-string from a tree
    - `tree("TREE_NAME").string()`


- Usage across threads
    - `default_tree()` is local to each thread
    - Named trees are shared between threads

## More Examples

### Multiple Tagged Trees

If you need multiple, separated trees you can use a name tag.

<!--{ multiple_trees.rs | code: rust }-->
```rust
use debug_tree::*;

fn populate(tree_name: &str, n_children: usize) {
    add_branch_to!(tree_name, "{} TREE", tree_name);
    for _ in 0..n_children {
        populate(tree_name, n_children / 2);
    }
}
fn main() {
    // Override tree config (just for "B")
    let b_tree = tree("B");
    b_tree.set_config_override(
        TreeConfig::new()
            .indent(4)
            .symbols(TreeSymbols::with_rounded().leaf("> ")),
    );
    defer_write!(b_tree, "examples/out/multiple_trees_B.txt");
    defer_write!("A", "examples/out/multiple_trees_A.txt");

    populate("A", 2);
    populate("B", 3);
}
```
<!--{ end }-->
<!--{ out/multiple_trees_A.txt | code }-->
```
A TREE
├╼ A TREE
│ └╼ A TREE
└╼ A TREE
  └╼ A TREE
```
<!--{ end }-->
<!--{ out/multiple_trees_B.txt | code }-->
```
B TREE
├──> B TREE
│   ╰──> B TREE
├──> B TREE
│   ╰──> B TREE
╰──> B TREE
    ╰──> B TREE
```
<!--{ end }-->

### Nested Functions

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

<!--{ nested.rs | code: rust }-->
```rust
use debug_tree::*;
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() {
    defer_write!("examples/out/nested.txt");
    a();
}
```
<!--{ end }-->
<!--{ out/nested.txt | code }-->
```
a
├╼ b
│ └╼ c
│   └╼ Nothing to see here
└╼ c
  └╼ Nothing to see here
```
<!--{ end }-->

### Line Breaks

Newlines in multi-line strings are automatically indented.

<!--{ multi_line.rs | code: rust }-->
```rust
use debug_tree::*;
fn main() {
    // output to file at the end of this block
    defer_write!("examples/out/multi_line.txt");
    add_branch!("1");
    add_leaf!("1.1\nAnother line...\n... and one more line");
    add_leaf!("1.2");
}
```
<!--{ end }-->

<!--{ out/multi_line.txt | code }-->
```
1
├╼ 1.1
│  Another line...
│  ... and one more line
└╼ 1.2
```
<!--{ end }-->

### Panics
Even if there is a panic, the tree is not lost!
The `defer_` functions were introduced to allow the tree
to be printed our written to file in the case of a `panic!` or early return.

<!--{ panic.rs | code: rust }-->
```rust
use debug_tree::*;

fn i_will_panic() {
    add_branch!("Here are my last words");
    add_leaf!("Stay calm, and try not to panic");
    panic!("I told you so...")
}

fn main() {
    // output to file at the end of this block
    defer_write!("examples/out/panic.txt");
    // print at the end of this block
    {
        add_branch!("By using the 'defer_' functions");
        add_branch!("Output will still be generated");
        add_branch!("Otherwise you might lose your valuable tree!");
    }
    add_branch!("Now for something crazy...");
    i_will_panic();
}
```
<!--{ end }-->

<!--{ out/panic.txt | code }-->
```
By using the 'defer_' functions
└╼ Output will still be generated
  └╼ Otherwise you might lose your valuable tree!
Now for something crazy...
└╼ Here are my last words
  └╼ Stay calm, and try not to panic
```
<!--{ end }-->


### Without Macros

If you prefer not using macros, you can construct `TreeBuilder`s manually.

<!--{ no_macros.rs | code: rust }-->
```rust
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 mut 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");
    // output to file
    tree.write("examples/out/no_macros.txt").ok(); // Write and flush.
}
```
<!--{ end }-->
<!--{ out/no_macros.txt | code }-->
```
1 Branch
└╼ 1.1 Child
2 Sibling
```
<!--{ end }-->