3 releases

0.1.2 Aug 31, 2020
0.1.1 Aug 31, 2020
0.1.0 Aug 31, 2020

#621 in Text processing

Download history 602/week @ 2024-07-21 821/week @ 2024-07-28 509/week @ 2024-08-04 609/week @ 2024-08-11 342/week @ 2024-08-18 429/week @ 2024-08-25 376/week @ 2024-09-01 369/week @ 2024-09-08 445/week @ 2024-09-15 405/week @ 2024-09-22 495/week @ 2024-09-29 524/week @ 2024-10-06 874/week @ 2024-10-13 660/week @ 2024-10-20 488/week @ 2024-10-27 508/week @ 2024-11-03

2,547 downloads per month
Used in 6 crates

MIT license

32KB
422 lines

Crate text_trees

Simple textual output for tree-like structures.

MIT License Minimum Rust Version Rust GitHub stars crates.io docs.rs

This crate is another that will output a tree structure in text. Similar to the existing ascii_tree crate, however it is more flexible in its formatting options.

Example

The following creates a StringTreeNode using a combination of with_child_nodes and with_children that demonstrates the structure of the tree well.

use text_trees::StringTreeNode;

fn make_tree() -> StringTreeNode {
    StringTreeNode::with_child_nodes(
        "root".to_string(),
        vec![
            "Uncle".into(),
            StringTreeNode::with_child_nodes(
                "Parent".to_string(),
                vec![
                    StringTreeNode::with_children(
                        "Child 1".to_string(),
                        vec!["Grand Child 1".into()].into_iter(),
                    ),
                    StringTreeNode::with_child_nodes(
                        "Child 2".to_string(),
                        vec![StringTreeNode::with_child_nodes(
                            "Grand Child 2".to_string(),
                            vec![StringTreeNode::with_children(
                                "Great Grand Child 2".to_string(),
                                vec!["Great Great Grand Child 2".to_string()].into_iter(),
                            )]
                            .into_iter(),
                        )]
                        .into_iter(),
                    ),
                ]
                .into_iter(),
            ),
            StringTreeNode::with_children(
                "Aunt".to_string(),
                vec!["Child 3".to_string()].into_iter(),
            ),
        ]
        .into_iter(),
    )
}

The tree implements Display and therefore provides a to_string method. It also has a to_string_with_format method that allows for customization of the output format. Finally, it has two write methods that take implementations of std::io::Write and will serialize accordingly.

use text_trees::{FormatCharacters, TreeFormatting, TreeNode};

fn ascii_tree(tree: TreeNode<String>) {
    let result = tree.to_string_with_format(
        &TreeFormatting::dir_tree(FormatCharacters::ascii())
    );
    assert!(result.is_ok());

    // ... do something else
}

This results in a textual representation of the tree as follows.

root
+-- Uncle
+-- Parent
|   +-- Child 1
|   |   '-- Grand Child 1
|   '-- Child 2
|       '-- Grand Child 2
|           '-- Great Grand Child 2
|               '-- Great Great Grand Child 2
'-- Aunt
    '-- Child 3

Changes

Version 0.1.2

  • Documentation changes only.

Version 0.1.1

  • Bug in top-down, bottom-anchored, tree with missing spacing.
  • Updated all examples to match the tree output changes.
  • Added tls tree-ls example.

Version 0.1.0

  • Initial version, supports only directory style trees.

TODO

TBD

No runtime deps