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.
-
Initialize text tree elements with default symbols:
let tree = TextTreeElements::default();
-
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 -
Output current tree item in loop:
println!("{}{}{}", prefixes, branch, file_name);
Output example:
root ├─ home │ └─ user ├─ tmp └─ ...
In examples directory can be found recursive files list program example with some variations for demonstration of different output styles.
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 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
text-tree-elements
library is licensed under the MIT license. Please read the
license file in the repository for more information.