text_tree_elements

Crates.iotext_tree_elements
lib.rstext_tree_elements
version0.1.0
sourcesrc
created_at2021-06-25 11:19:09.671787
updated_at2021-06-25 11:19:09.671787
descriptionRust language library for building text trees
homepage
repositoryhttps://github.com/AntonGepting/text-tree-elements-rs
max_upload_size
id414788
size17,240
Anton Gepting (AntonGepting)

documentation

README

Text Tree Elements

Build Status Crates.io Documentation

Text Tree Elements is a small Rust language library, containing few tiny functions for preparing tree like structured items for displaying them in TUI (but not building these trees). For example it can be used for directory listing output similar to tree command.

Quick Start

  1. Initialize text tree elements with default symbols:

    let tree = TextTreeElements::default();
    
  2. Get prefix and branch strings for current item of the tree in loop:

    let (prefix, branch) = tree.get_prefix_branch(level, index, size);
    let prefixes = format!("{}{}", prefixes, prefix);
    

    prefix - variable contains current item's parent branches (by default one of: ["", "", "│ ", " "] for: [root, first, middle, last] item respectively)

    branch - variable contains current item branch symbol, depending on position (by default one of: ["", "", "├─ ", "└─ "] for: [root, first, middle, last] item respectively)

    prefixes - variable contains all previous concatenated parents branches

  3. Output current tree item in loop:

    println!("{}{}{}", prefixes, branch, file_name);
    

    Output example:

    root
    ├─ home
    │  └─ user
    ├─ tmp
    └─ ...
    

Examples

In examples directory can be found recursive files list program example with some variations for demonstration of different output styles.

Default

Default init (classic branch glyphs):

// init branch and prefix arrays:
// `[root, first, middle, last]`, `[root, first, middle, last]`
// `["", "", "├─ ", "└─ "]`, `["", "", "│  ", "   "]`
let tree = TextTreeElements::default();

Output:

.
├─ TODO.md
├─ README.md
├─ src
│  ├─ lib.rs
│  ├─ text_tree_elements.rs
│  └─ text_tree_elements_tests.rs
├─ Cargo.lock
├─ LICENSE.md
├─ examples
│  └─ example_1.rs
├─ .github
│  └─ workflows
│     └─ actions.yml
├─ .gitignore
└─ Cargo.toml

Custom

Custom init (user given branch glyphs):

// init custom branch and prefix arrays:
// `[root, first, middle, last]`, `[root, first, middle, last]`
// `["", "", "|- ", "'- "]`, `["", "", "|  ", ",  "]`
let tree = TextTreeElements::new(["", "", "|  ", "   "], ["", "", "|- ", "'- "]);

Output:

.
|- TODO.md
|- README.md
|- src
|  |- lib.rs
|  |- text_tree_elements.rs
|  '- text_tree_elements_tests.rs
|- Cargo.lock
|- LICENSE.md
|- examples
|  '- example_1.rs
|- .github
|  '- workflows
|     '- actions.yml
|- .gitignore
'- Cargo.toml

License

text-tree-elements library is licensed under the MIT license. Please read the license file in the repository for more information.

See also

Commit count: 11

cargo fmt