Skip to content

Latest commit

 

History

History
191 lines (137 loc) · 4.13 KB

File metadata and controls

191 lines (137 loc) · 4.13 KB

Tree

Tree displays hierarchical data as a structured, readable tree with connector lines. It's ideal for file structures, dependency graphs, nested configurations, or any parent-child relationships.

Basic Usage

Simple Tree

Tree tree = Clique.tree("project/")
    .add("src/")
    .add("README.md")
    .add("build.gradle");

tree.print();

Nested Tree

Tree tree = Clique.tree("project/");

Tree src = tree.add("src/");
src.add("Main.java");
src.add("Utils.java");

Tree test = tree.add("test/");
test.add("MainTest.java");

tree.add("README.md");

tree.print();

Output:

project/
├─ src/
│  ├─ Main.java
│  └─ Utils.java
├─ test/
│  └─ MainTest.java
└─ README.md

Adding Nodes

Add a Child

Tree child = tree.add("child label");

add() returns the newly created child node, so you can nest further calls off it.

Tree child = Clique.tree("Child node");
Tree parent = Clique.tree("Parent Node");
Tree tree = parent.add(child); //Returns the child node

add()returns the pre-created created child node, so you can nest further calls off it.

Nesting Arbitrarily Deep

Tree a = tree.add("level 1");
Tree b = a.add("level 2");
b.add("level 3");

Using Markup in Labels

Markup tags work in any label, root or child:

Tree tree = Clique.tree("[*magenta, bold]my_project/");

Tree src = tree.add("[cyan]src/");
src.add("[green]Main.java        [dim]✓ ok");
src.add("[yellow]Utils.java       [dim]⚠ needs review");
src.add("[red]Broken.java      [dim]✗ error");

tree.add("[dim].gitignore");

tree.print();

Tree Configuration

Use TreeConfiguration to customize the guide connector style.

Basic Configuration

TreeConfiguration config = TreeConfiguration.builder()
    .guideStyle("cyan, bold")
    .build();

Tree tree = Clique.tree("project/", config);

Configuration Options

Guide Style

Controls the color and style of the connector characters (├─, └─, ). Accepts any valid markup style string:

TreeConfiguration config = TreeConfiguration.builder()
    .guideStyle("*blue, bold")
    .build();

Custom Parser

Provide a custom configured parser for markup processing in labels:

ParserConfiguration parserConfig = ParserConfiguration
        .builder()
        .delimiter(' ')
        .build();

TreeConfiguration config = TreeConfiguration.builder()
        .parser(Clique.parser(parserConfig))
        .build();

Full Configuration Example

TreeConfiguration config = TreeConfiguration.builder()
        .guideStyle("*cyan, bold") //Do not add the markup tag borders i.e [*cyan, bold]
        .build();

Tree tree = Clique.tree("[*magenta, bold]clique-lib/", config);

Tree src = tree.add("[*cyan, bold]src/");
Tree core = src.add("[cyan]core/");
core.add("[green]Parser.java         [dim]✓ 312 lines");
core.add("[green]StyleResolver.java  [dim]✓ 198 lines");
core.add("[yellow]Renderer.java       [dim]⚠ needs review");

Tree tests = tree.add("[*cyan, bold]tests/");
tests.add("[green, bold]ParserTest.java     [dim]✓ 14/14 pass");
tests.add("[red, bold]RendererTest.java   [dim]✗  9/14 pass");
tests.add("[dim, strike]TreeTest.java       skipped");

tree.add("[white]README.md");
tree.add("[dim].gitignore");

tree.print();

Getting the Tree as a String

Use get() to retrieve the rendered tree as a string without printing:

String result = tree.get();
System.out.println(result);

Flushing

Recursively clears all children from the root and null out their parent references:

tree.flush();

Parent

Tree src = tree.add("src/");
Tree main = src.add("Main.java");

main.parent(); // returns src
tree.parent();  // returns null — root has no parent

Factory Methods

// Default configuration
Clique.tree("label");

// Custom configuration
Clique.tree("label", config);

See Also