Crate ptree[][src]

ptree

Pretty-print tree-like structures

Basic usage

// Build a tree using a TreeBuilder
let tree = TreeBuilder::new("tree".to_string())
    .add_empty_child("empty branch".to_string())
    .build();

// Print out the tree using default formatting
print_tree(&tree)?;

Implementing the TreeItem trait

Rather than construct a new tree, one can implement the TreeItem trait for a custom data structure.

#[derive(Clone)]
struct MyCustomTree {}

impl TreeItem for MyCustomTree {
    type Child = Self;
    fn write_self<W: io::Write>(&self, f: &mut W, config: &PrintConfig) -> io::Result<()> {
        write!(f, "{}", config.paint_leaf("My custom tree"))
    }
    fn children(&self) -> Cow<[Self::Child]> {
        Cow::from(vec![])
    }
}

// Build my custom tree structure
let tree = MyCustomTree {};

// Print out the tree using default formatting
print_tree(&tree)?;

Output formatting

// Build a tree using a TreeBuilder
let tree = TreeBuilder::new("tree".to_string())
    .add_empty_child("empty branch".to_string())
    .build();

// Set up the print configuration
let config = {
    let mut config = PrintConfig::for_stdout();
    config.branch_style = Style::new().fg(Color::Red).on(Color::Yellow).dimmed();
    config.leaf_style = Style::new().bold();
    config.chars = UTF_CHARS_BOLD;
    config.indent_size = 4;
    config
};

// Print out the tree using custom formatting
print_tree_with(&tree, &config)?;

Re-exports

pub use print_tree::print_tree;
pub use print_tree::print_tree_with;
pub use print_tree::write_tree;
pub use print_tree::write_tree_with;
pub use builder::TreeBuilder;
pub use item::TreeItem;
pub use config::IndentChars;
pub use config::PrintConfig;

Modules

builder
config
graph
item
path
print_tree
value