TreeLog
A highly customizable, optimized, and modular tree rendering library for Rust.
This library provides both low-level and high-level APIs for rendering hierarchical data structures as trees, similar to the output of tools like tree, npm ls, or cargo tree.
Features
- Multiple Styles: Unicode, ASCII, Box drawing, and custom character sets
- Flexible API: Both low-level and high-level builder APIs
- Iterator Support: Stream lines one at a time without materializing the entire tree
- Customizable: Per-node formatting, custom styles, and configuration options
- Optimized: Pre-computed prefixes, efficient string allocation
- Well Documented: Comprehensive documentation with examples
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
Basic Usage
use ;
let tree = Node;
let mut output = Stringnew;
write_tree.unwrap;
println!;
Output:
root
└─ item
API Overview
High-Level Builder API
The builder API provides a fluent interface for constructing trees:
use TreeBuilder;
let mut builder = new;
builder.node.leaf.node.leaf.leaf.end.leaf;
let tree = builder.build;
println!;
Output:
root
├─ item1
├─ sub
│ ├─ subitem1
│ └─ subitem2
└─ item2
Low-Level API
For more control, construct trees directly:
use Tree;
let l1 = Leaf;
let l2 = Leaf;
let n1 = Node;
let n2 = Node;
let n3 = Node;
let n4 = Node;
println!;
Iterator API
Get lines one at a time for streaming or custom processing:
use ;
let tree = Node;
for line in lines
Or collect all lines at once:
use ;
let tree = Node;
let lines: = tree.to_lines;
for line in lines
Customization
Different Styles
use ;
let tree = Node;
// Unicode style (default)
let output1 = tree.render_to_string_with_config;
// ASCII style
let output2 = tree.render_to_string_with_config;
// Custom style
use StyleConfig;
let custom_style = custom;
let output3 = tree.render_to_string_with_config;
Custom Formatters
use ;
let tree = Node;
let config = default
.with_node_formatter
.with_leaf_formatter;
let output = tree.render_to_string_with_config;
Custom Style Configuration
use ;
let style = custom;
let config = default.with_style;
Advanced Examples
Complex Tree with Multiple Lines
use Tree;
let l1 = Leaf;
let l2 = Leaf;
let n1 = Node;
let n2 = Node;
let n3 = Node;
let n4 = Node;
println!;
Output:
node 4
├─ node 1
│ ├─ line1
│ │ line2
│ │ line3
│ │ line4
│ └─ only one line
├─ node 2
│ ├─ only one line
│ ├─ line1
│ │ line2
│ │ line3
│ │ line4
│ └─ only one line
└─ node 3
├─ node 1
│ ├─ line1
│ │ line2
│ │ line3
│ │ line4
│ └─ only one line
├─ line1
│ line2
│ line3
│ line4
└─ only one line
Streaming Large Trees
For very large trees, use the iterator API to avoid materializing the entire string:
use ;
use ;
let tree = Node;
let mut stdout = stdout;
for line in lines
Performance
The library is optimized for performance:
- Pre-computed prefixes: Tree structure prefixes are computed efficiently
- Capacity estimation: String buffers are pre-allocated with estimated capacity
- Zero-copy where possible: Iterator API avoids unnecessary allocations
- Efficient traversal: Stack-based iteration minimizes overhead
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.