### Run tree example Source: https://github.com/rust-cli/termtree/blob/main/README.md Executes the provided example program to visualize a directory structure. ```bash $ cargo run --example tree target Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs Running `target/debug/examples/tree target` target └── debug ├── .cargo-lock ├── .fingerprint | └── termtree-21a5bdbd42e0b6da | ├── dep-example-tree | ├── dep-lib-termtree | ├── example-tree | ├── example-tree.json | ├── lib-termtree | └── lib-termtree.json ├── build ├── deps | └── libtermtree.rlib ├── examples | ├── tree | └── tree.dSYM | └── Contents | ├── Info.plist | └── Resources | └── DWARF | └── tree ├── libtermtree.rlib └── native ``` -------------------------------- ### Build Directory Tree Recursively Source: https://context7.com/rust-cli/termtree/llms.txt A practical implementation showing how to traverse a filesystem path and construct a Tree structure recursively. ```rust use termtree::Tree; use std::path::Path; use std::fs; fn build_dir_tree>(path: P) -> std::io::Result> { let path = path.as_ref(); let name = path.file_name() .map(|n| n.to_string_lossy().into_owned()) .unwrap_or_else(|| path.display().to_string()); let mut tree = Tree::new(name); if path.is_dir() { for entry in fs::read_dir(path)? { let entry = entry?; let child = build_dir_tree(entry.path())?; tree.push(child); } } Ok(tree) } // Usage: // let tree = build_dir_tree("src").unwrap(); // println!("{}", tree); // Output: // src // ├── lib.rs // └── tests.rs ``` -------------------------------- ### Customize Tree Rendering with `Tree::with_glyphs` Source: https://context7.com/rust-cli/termtree/llms.txt Customize tree rendering characters using `GlyphPalette` with the `with_glyphs` method. This allows for different visual styles, including ASCII-only output or custom symbols for nodes. ```rust use termtree::{Tree, GlyphPalette}; // Create custom ASCII-only glyphs let ascii_glyphs = GlyphPalette { middle_item: "+", last_item: "`", item_indent: "-- ", middle_skip: "|", last_skip: " ", skip_indent: " ", }; let tree = Tree::new("root") .with_glyphs(ascii_glyphs) .with_leaves(["child1", "child2"]); println!("{}", tree); // Output: // root // +-- child1 // `-- child2 // Custom status indicators (e.g., for test results) let pass_glyph = GlyphPalette { middle_item: "├", last_item: "└", item_indent: "─ ✅ ", middle_skip: "│", last_skip: " ", skip_indent: " ", }; let fail_glyph = GlyphPalette { middle_item: "├", last_item: "└", item_indent: "─ ❌ ", middle_skip: "│", last_skip: " ", skip_indent: " ", }; let test_tree = Tree::new("tests").with_leaves([ Tree::new("test_add").with_glyphs(pass_glyph), Tree::new("test_sub").with_glyphs(fail_glyph), ]); println!("{}", test_tree); // Output: // tests // ├─ ✅ test_add // └─ ❌ test_sub ``` -------------------------------- ### Create a New Tree Node with `Tree::new` Source: https://context7.com/rust-cli/termtree/llms.txt Use `Tree::new` to create a new tree node with a specified root value. The root can be any type implementing `Display`. This is the primary method for initiating tree construction. ```rust use termtree::Tree; // Create a simple tree with a string root let tree = Tree::new("root"); println!("{}", tree); // Output: // root // Create a tree with numeric root let numeric_tree = Tree::new(42); println!("{}", numeric_tree); // Output: // 42 ``` -------------------------------- ### Tree::new Source: https://context7.com/rust-cli/termtree/llms.txt Creates a new tree node with the specified root value. ```APIDOC ## Tree::new ### Description Creates a new tree node with the specified root value. The root can be any type that implements Display. ### Parameters #### Request Body - **root** (T: Display) - Required - The value to be used as the root of the tree. ``` -------------------------------- ### Enable Multi-line Node Support with `Tree::with_multiline` Source: https://context7.com/rust-cli/termtree/llms.txt Call `with_multiline(true)` to enable proper indentation for nodes containing newline characters. This ensures all lines of the node's content are correctly aligned within the tree structure. ```rust use termtree::Tree; // Multi-line content in tree nodes let tree = Tree::new("Files").with_leaves([ Tree::new("README.md\n(documentation)").with_multiline(true), Tree::new("main.rs\n(entry point)").with_multiline(true), ]); println!("{}", tree); // Output: // Files // ├── README.md // │ (documentation) // └── main.rs // (entry point) ``` -------------------------------- ### Configure GlyphPalette for Tree Rendering Source: https://context7.com/rust-cli/termtree/llms.txt Use GlyphPalette to define the visual characters for tree branches and indentation. The struct supports both default Unicode box-drawing and custom ASCII configurations. ```rust use termtree::GlyphPalette; // Default glyph palette (Unicode box-drawing) let default = GlyphPalette::new(); // middle_item: "├" - prefix for non-last items // last_item: "└" - prefix for last item // item_indent: "── " - spacing after item prefix // middle_skip: "│" - vertical line for non-last branches // last_skip: " " - empty space for last branches // skip_indent: " " - spacing after skip prefix // Create minimal ASCII palette let minimal = GlyphPalette { middle_item: "|", last_item: "`", item_indent: "- ", middle_skip: "|", last_skip: " ", skip_indent: " ", }; ``` -------------------------------- ### Tree::with_multiline Source: https://context7.com/rust-cli/termtree/llms.txt Enables proper indentation for nodes containing newline characters. ```APIDOC ## Tree::with_multiline ### Description Enables proper indentation for nodes containing newline characters, ensuring all lines of the root content are properly indented. ### Parameters #### Request Body - **enabled** (bool) - Required - Whether to enable multi-line support. ``` -------------------------------- ### Add a Single Child Node with `Tree::push` Source: https://context7.com/rust-cli/termtree/llms.txt Use the `push` method to add a single child node to the tree mutably. This is useful for iterative tree building or when the structure is determined at runtime. ```rust use termtree::Tree; // Build tree iteratively with push let mut tree = Tree::new("project"); tree.push("src"); tree.push("tests"); tree.push("docs"); println!("{}", tree); // Output: // project // ├── src // ├── tests // └── docs // Push nested trees let mut root = Tree::new("app"); let mut src = Tree::new("src"); src.push("main.rs"); src.push("lib.rs"); root.push(src); root.push("Cargo.toml"); println!("{}", root); // Output: // app // ├── src // │ ├── main.rs // │ └── lib.rs // └── Cargo.toml ``` -------------------------------- ### Tree::with_leaves Source: https://context7.com/rust-cli/termtree/llms.txt Adds multiple child nodes to a tree using a builder pattern. ```APIDOC ## Tree::with_leaves ### Description Adds multiple child nodes to a tree in a builder pattern style. Accepts any iterator of items that can be converted into Tree. ### Parameters #### Request Body - **leaves** (Iterator) - Required - An iterator of items to be added as children. ``` -------------------------------- ### Convert Types to Tree using From Trait Source: https://context7.com/rust-cli/termtree/llms.txt Types implementing Display can be converted into a Tree using the From trait. This also enables direct manipulation of the root and leaves fields. ```rust use termtree::Tree; // Automatic conversion from strings let tree: Tree<&str> = "root".into(); // Works with with_leaves accepting any Into> let tree = Tree::new("config") .with_leaves(["option1", "option2"]); // &str auto-converts to Tree // Direct access to root and leaves fields let mut tree = Tree::new("parent"); tree.root = "renamed_parent"; // Direct field access tree.leaves.push(Tree::new("child")); // Direct vector manipulation println!("{}", tree); // Output: // renamed_parent // └── child ``` -------------------------------- ### Tree::with_glyphs Source: https://context7.com/rust-cli/termtree/llms.txt Customizes the tree rendering characters using GlyphPalette. ```APIDOC ## Tree::with_glyphs ### Description Allows customization of the tree rendering characters using GlyphPalette. ### Parameters #### Request Body - **palette** (GlyphPalette) - Required - The set of characters to use for rendering the tree structure. ``` -------------------------------- ### Tree::push Source: https://context7.com/rust-cli/termtree/llms.txt Adds a single child node to the tree mutably. ```APIDOC ## Tree::push ### Description Adds a single child node to the tree mutably. Useful when building trees iteratively. ### Parameters #### Request Body - **child** (T) - Required - The child node or value to add to the tree. ``` -------------------------------- ### Add Multiple Child Nodes with `Tree::with_leaves` Source: https://context7.com/rust-cli/termtree/llms.txt The `with_leaves` method adds multiple child nodes to a tree using a builder pattern. It accepts any iterator of items convertible to `Tree`, facilitating flexible tree construction. ```rust use termtree::Tree; // Create a tree with multiple leaves using strings (auto-converted to Tree) let tree = Tree::new("parent") .with_leaves(["child1", "child2", "child3"]); println!("{}", tree); // Output: // parent // ├── child1 // ├── child2 // └── child3 // Create nested tree structure let nested = Tree::new("root") .with_leaves([ Tree::new("branch1").with_leaves(["leaf1", "leaf2"]), Tree::new("branch2").with_leaves(["leaf3"]), ]); println!("{}", nested); // Output: // root // ├── branch1 // │ ├── leaf1 // │ └── leaf2 // └── branch2 // └── leaf3 ``` -------------------------------- ### Extend Tree with Iterators Source: https://context7.com/rust-cli/termtree/llms.txt The Tree type implements the Extend trait, allowing for efficient addition of multiple child nodes from iterators containing raw values or other Tree nodes. ```rust use termtree::Tree; // Extend with raw values let mut tree = Tree::new("numbers"); tree.extend([1, 2, 3, 4, 5]); println!("{}", tree); // Output: // numbers // ├── 1 // ├── 2 // ├── 3 // ├── 4 // └── 5 // Extend with Tree nodes let mut root = Tree::new("modules"); let modules = vec!["auth", "api", "core"] .into_iter() .map(|name| Tree::new(name).with_leaves(["mod.rs", "tests.rs"])); root.extend(modules); println!("{}", root); // Output: // modules // ├── auth // │ ├── mod.rs // │ └── tests.rs // ├── api // │ ├── mod.rs // │ └── tests.rs // └── core // ├── mod.rs // └── tests.rs ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.