#tree #formatting #pretty-print #derive #charset

display_tree

Simple, automatic, and customizable tree pretty-printing

4 stable releases

1.1.2 Dec 20, 2022
1.1.0 Dec 19, 2022
1.0.3 Dec 19, 2022

#212 in Value formatting

Download history 3/week @ 2024-07-19 61/week @ 2024-07-26 30/week @ 2024-08-02 65/week @ 2024-08-09 40/week @ 2024-08-16 36/week @ 2024-08-23 35/week @ 2024-08-30 14/week @ 2024-09-06 7/week @ 2024-09-13 21/week @ 2024-09-20 25/week @ 2024-09-27 10/week @ 2024-10-04 36/week @ 2024-10-11 18/week @ 2024-10-18 14/week @ 2024-10-25 29/week @ 2024-11-01

98 downloads per month
Used in atspi

MIT/Apache

41KB
510 lines

display_tree

Simple, automatic, and customizable tree pretty-printing in Rust.

Example

This crate provides tools to easily pretty-print data as a tree, including a trait that represents the ability to be printed as a tree, and a derive macro to automatically implement it for your types

See the crate-level documentation to get started.

Examples

use display_tree::{AsTree, CharSet, DisplayTree, StyleBuilder};

// A tree representing a numerical expression.
#[derive(DisplayTree)]
enum Expr {
    Int(i32),
    BinOp {
        #[node_label]
        op: char,
        #[tree]
        left: Box<Self>,
        #[tree]
        right: Box<Self>,
    },
    UnaryOp {
        #[node_label]
        op: char,
        #[tree]
        arg: Box<Self>,
    },
}

let expr: Expr = Expr::BinOp {
    op: '+',
    left: Box::new(Expr::UnaryOp {
        op: '-',
        arg: Box::new(Expr::Int(2)),
    }),
    right: Box::new(Expr::Int(7)),
};

assert_eq!(
    format!(
        "{}",
        AsTree::new(&expr)
            .indentation(1)
            .char_set(CharSet::DOUBLE_LINE)
    ),
    concat!(
        "+\n",
        "╠═ -\n",
        "║  ╚═ Int\n",
        "║     ╚═ 2\n",
        "╚═ Int\n",
        "   ╚═ 7",
    ),
);

Dependencies

~1.5MB
~38K SLoC