Expand description

A customizable tree rendering library and CLI for Rust
Provides low-level and high-level APIs for rendering hierarchical data structures, similar to tree, npm ls, or cargo tree. Also includes a command-line utility for quick tree visualization.
Key Features: Multiple styles • Macro DSL • Builder API • Iterator support • Tree statistics & traversal • Search & filtering • Transformation & sorting • Export to HTML/SVG/DOT • Integrations with JSON/YAML/TOML, filesystem, Git, Cargo, and more
§Table of Contents
§Installation
§As a Library
Add this to your Cargo.toml:
[dependencies]
treelog = "0.0.6"§As a CLI Tool
# Install from crates.io
cargo install treelog --features cli,arbitrary-walkdir,serde-json
# Or build from source
git clone https://github.com/muijf/treelog
cd treelog
cargo build --release --bin treelog --features cli,arbitrary-walkdir,serde-jsonNote: The CLI requires the
clifeature. Enable additional features based on the input sources you need. You can use convenience aliases likewalkdir(forarbitrary-walkdir).
§Feature Flags
Most features are optional to keep the core library lightweight. Enable only what you need.
Core Features:
builder- Builder API for constructing treesiterator- Iterator API for streaming treesmacro- Macro DSL for tree constructionformatters- Custom formatters for nodes and leavescolor- Color output support (requirescolored)
Tree Operations:
traversal- Tree traversal iterators (pre-order, post-order, level-order)transform- Tree transformation operations (map, filter, prune)path- Tree path utilities (get by path, flatten)compare- Tree comparison and diff operationssearch- Tree search operations (find nodes/leaves, get paths)sort- Tree sorting operations (sort by label, depth, custom)stats- Tree statistics and metricsmerge- Tree merging with different strategiesexport- Export to HTML, SVG, and DOT formats
Exact Serialization (Round-Trip):
serde- Meta-feature enabling all serde serializationEnables all of the following:
serde-json- JSON serialization/deserialization (Tree ↔ JSON)serde-yaml- YAML serialization/deserialization (Tree ↔ YAML)serde-toml- TOML serialization/deserialization (Tree ↔ TOML)serde-ron- RON serialization/deserialization (Tree ↔ RON)
You can also enable individual features instead of the meta-feature.
Arbitrary Conversion (One-Way):
arbitrary- Meta-feature enabling all arbitrary conversionsEnables all of the following:
arbitrary-json- Convert any JSON to Tree (requiresserde-json)arbitrary-yaml- Convert any YAML to Tree (requiresserde-yaml)arbitrary-toml- Convert any TOML to Tree (requiresserde-toml)arbitrary-xml- Convert XML/HTML to Treearbitrary-walkdir- Build trees from directory structuresarbitrary-petgraph- Convert petgraph graphs to Treearbitrary-cargo- Build trees from Cargo metadataarbitrary-git2- Build trees from Git repositoriesarbitrary-syn- Build trees from Rust ASTarbitrary-tree-sitter- Build trees from tree-sitter parse treesarbitrary-clap- Build trees from clap command structures
You can also enable individual features instead of the meta-feature.
Convenience Aliases:
walkdir- Alias forarbitrary-walkdirpetgraph- Alias forarbitrary-petgraphcargo-metadata- Alias forarbitrary-cargogit2- Alias forarbitrary-git2syn- Alias forarbitrary-syntree-sitter- Alias forarbitrary-tree-sitterclap- Alias forarbitrary-clap(also used by CLI)cli- CLI binary (includesclap)
Quick examples:
# Common feature set
treelog = { version = "0.0.6", features = ["traversal", "transform", "path", "compare", "merge", "export"] }
# Enable everything
treelog = { version = "0.0.6", features = ["all"] }Note: The
clifeature is separate and must be enabled explicitly for the binary. Use convenience aliases likewalkdir(forarbitrary-walkdir) when available.
§Quick Start
use treelog::{Tree, renderer::write_tree};
let tree = Tree::Node("root".to_string(), vec![
Tree::Leaf(vec!["item1".to_string()]),
Tree::Node("sub".to_string(), vec![
Tree::Leaf(vec!["subitem1".to_string()]),
Tree::Leaf(vec!["subitem2".to_string()]),
]),
]);
let mut output = String::new();
write_tree(&mut output, &tree).unwrap();
println!("{}", output);Output:
root
├─ item1
└─ sub
├─ subitem1
└─ subitem2For more examples and usage patterns, see the examples.
§CLI Usage
The CLI tool provides a convenient way to visualize trees from various sources without writing code. All library features are available through the command-line interface.
§Basic Usage
# Visualize a directory structure
treelog from dir . --max-depth 3
# Visualize Cargo dependencies
treelog from cargo
# Visualize Git repository structure
treelog from git .
# Render a tree from a file (JSON/YAML/TOML/RON)
treelog render tree.json
# Render a tree from stdin
cat tree.json | treelog render
# Get tree statistics
treelog from dir . --format json | treelog stats§Input Sources
# Filesystem & Git
treelog from dir <path> [--max-depth <n>] # arbitrary-walkdir
treelog from git [path] [--branches] [--commit] # arbitrary-git2
# Code & AST
treelog from rust <file> # arbitrary-syn
treelog from tree-sitter <file> [--language] # arbitrary-tree-sitter
treelog from xml <file> # arbitrary-xml
# Package Managers
treelog from cargo [--manifest <path>] [--package <name>] # arbitrary-cargo
# Data Formats
treelog from json <file> # serde-json
treelog from yaml <file> # serde-yaml
treelog from toml <file> # serde-toml
treelog from ron <file> # serde-ron§Rendering Options
# Styles
treelog from dir . --style ascii
treelog from dir . --custom-style ">-,<-,| , "
# Output
treelog from dir . --output tree.txt
treelog from dir . --format json > tree.json
treelog from dir . --format html > tree.html
treelog from dir . --format svg > tree.svg
treelog from dir . --format dot > tree.dot§Tree Operations
# Basic operations
treelog render tree.json # Render from file
treelog render - # Render from stdin
treelog stats tree.json # Get statistics
treelog search "pattern" tree.json # Search nodes/leaves
# Manipulation
treelog sort --method label tree.json
treelog sort --method depth --reverse tree.json
treelog transform map-nodes "[{}]" tree.json
treelog transform filter "src" tree.json
# Comparison & Merging
treelog compare tree1.json tree2.json
treelog merge --strategy append tree1.json tree2.json
# Export
treelog export html tree.json > output.html
treelog export svg tree.json > output.svg
treelog export dot tree.json > output.dot§Piping and Serialization
# Create tree and get statistics
treelog from dir . --format json | treelog stats
# Transform and export
treelog from dir . --format json | treelog transform filter "src" | treelog export html > output.htmlNote: When piping between commands, use
--format json(oryaml,toml,ron) to serialize the tree structure. The defaulttextformat is for human-readable output only.
§Help
treelog --help
treelog from --help
treelog from dir --help§Examples
Run any example with:
cargo run --example <name> --all-features
Core Examples:
basic- Basic tree construction and renderingbuilder- Using the builder APIiterator- Streaming large treesmacro- Using the macro DSLcustomization- Custom styles and formatterscomplex- Complex tree structuresfile_tree- File system tree example
Advanced Examples:
statistics- Tree statistics and analysistraversal- Tree traversal iteratorssearch- Tree search and query operationstransform- Tree transformation operationssorting- Tree sorting operationspath- Tree path utilitiescomparison- Tree comparison and diffmerge- Tree merging strategiesexport- Export to HTML, SVG, and DOT formats
Integration Examples:
arbitrary- Arbitrary data conversion (JSON/YAML/TOML to Tree)serde- Exact JSON and YAML serialization (Tree ↔ JSON/YAML)toml- TOML parsing and conversionfilesystem- File system tree buildingpetgraph- Graph to/from tree conversioncargo- Cargo dependency tree visualizationgit2- Git repository structure visualizationxml- XML/HTML DOM tree visualizationsyn- Rust AST visualizationron- RON serializationtree_sitter- Tree-sitter parse tree visualizationclap- Command-line argument structure visualization
§Development
Format code:
cargo fmt --allFormats all Rust code according to the official style guide.
Lint code:
cargo clippy --all-targets --all-features -- -D warningsRuns Clippy linter with all targets and features enabled, treating warnings as errors.
Run tests:
cargo test --all-featuresRuns all tests with all features enabled to ensure comprehensive coverage.
Editor setup: Recommended extensions are available in
.vscode/extensions.json. See CONTRIBUTING.md for development guidelines and pre-commit hooks.
§License
This project is licensed under the MIT License - see the LICENSE file for details.
§Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Re-exports§
pub use config::RenderConfig;pub use iterator::Line;pub use iterator::TreeIteratorExt;pub use stats::TreeStats;pub use style::StyleConfig;pub use style::TreeStyle;pub use tree::Tree;pub use renderer::render_to_string;pub use renderer::render_to_string_with_config;pub use renderer::write_tree;pub use renderer::write_tree_with_config;
Modules§
- arbitrary
- Arbitrary data structure conversion support for Tree.
- builder
- High-level builder API for constructing trees.
- compare
- Tree comparison operations.
- config
- Configuration options for tree rendering.
- export
- Tree export to various formats (HTML, SVG, DOT).
- iterator
- Iterator API for line-by-line tree access.
- merge
- Tree merging operations.
- path
- Tree path utilities for navigating and accessing tree elements by path.
- renderer
- Core rendering logic for trees.
- search
- Tree search and find operations.
- serde
- Serde serialization and deserialization support for Tree.
- sort
- Tree sorting operations.
- stats
- Tree statistics and metrics.
- style
- Tree style definitions and customization options.
- transform
- Tree transformation operations.
- traversal
- Tree traversal iterators for different traversal orders.
- tree
- Core tree data structure for representing hierarchical data.
Macros§
- tree
- Creates a tree using a DSL-like syntax.