tree

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2025 License: MIT Imports: 4 Imported by: 29

Documentation

Overview

Package tree allows you to build trees, as simple or complicated as you need.

Define a tree with a root node, and children, set rendering properties (such as style, enumerators, etc...), and print it.

t := tree.New().
	Child(
		".git",
		tree.Root("examples/").
			Child(
				tree.Root("list/").
					Child("main.go").
				tree.Root("table/").
					Child("main.go").
			).
		tree.Root("list/").
			Child("list.go", "list_test.go").
		tree.New().
			Root("table/").
			Child("table.go", "table_test.go").
		"align.go",
		"align_test.go",
		"join.go",
		"join_test.go",
	)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultEnumerator

func DefaultEnumerator(children Children, index int) string

DefaultEnumerator enumerates a tree.

├── Foo ├── Bar ├── Baz └── Qux.

func DefaultIndenter

func DefaultIndenter(children Children, index int) string

DefaultIndenter indents a tree for nested trees and multiline content.

├── Foo ├── Bar │ ├── Qux │ ├── Quux │ │ ├── Foo │ │ └── Bar │ └── Quuux └── Baz.

func RoundedEnumerator

func RoundedEnumerator(children Children, index int) string

RoundedEnumerator enumerates a tree with rounded edges.

├── Foo ├── Bar ├── Baz ╰── Qux.

Types

type Children

type Children interface {
	// At returns the content item of the given index.
	At(index int) Node

	// Length returns the number of children in the tree.
	Length() int
}

Children is the interface that wraps the basic methods of a tree model.

func NewStringData

func NewStringData(data ...string) Children

NewStringData returns a Data of strings.

type Enumerator

type Enumerator func(children Children, index int) string

Enumerator enumerates a tree. Typically, this is used to draw the branches for the tree nodes and is different for the last child.

For example, the default enumerator would be:

func TreeEnumerator(children Children, index int) string {
	if children.Length()-1 == index {
		return "└──"
	}

	return "├──"
}

type Filter

type Filter struct {
	// contains filtered or unexported fields
}

Filter applies a filter on some data. You could use this to create a new tree whose values all satisfy the condition provided in the Filter() function.

func NewFilter

func NewFilter(data Children) *Filter

NewFilter initializes a new Filter.

func (*Filter) At

func (m *Filter) At(index int) Node

At returns the item at the given index. The index is relative to the filtered results.

func (*Filter) Filter

func (m *Filter) Filter(f func(index int) bool) *Filter

Filter uses a filter function to set a condition that all the data must satisfy to be in the Tree.

func (*Filter) Length

func (m *Filter) Length() int

Length returns the number of children in the tree.

type Indenter

type Indenter func(children Children, index int) string

Indenter indents the children of a tree.

Indenters allow for displaying nested tree items with connecting borders to sibling nodes.

For example, the default indenter would be:

func TreeIndenter(children Children, index int) string {
	if children.Length()-1 == index {
		return "│  "
	}

	return "   "
}

type Leaf

type Leaf struct {
	// contains filtered or unexported fields
}

Leaf is a node without children.

func NewLeaf added in v1.1.0

func NewLeaf(value any, hidden bool) *Leaf

NewLeaf returns a new Leaf.

Example
package main

import (
	"fmt"

	"github.com/charmbracelet/lipgloss/tree"
)

func main() {
	tr := tree.New().
		Child(
			"Foo",
			tree.Root("Bar").
				Child(
					"Qux",
					tree.Root("Quux").
						Child(
							tree.NewLeaf("This should be hidden", true),
							tree.NewLeaf(
								tree.Root("I am groot").Child("leaves"), false),
						),
					"Quuux",
				),
			"Baz",
		)

	fmt.Println(tr.String())
}
Output:

├── Foo
├── Bar
│   ├── Qux
│   ├── Quux
│   │   └── I am groot
│   │       └── leaves
│   └── Quuux
└── Baz

func (Leaf) Children

func (Leaf) Children() Children

Children of a Leaf node are always empty.

func (Leaf) Hidden

func (s Leaf) Hidden() bool

Hidden returns whether a Leaf node is hidden.

func (*Leaf) SetHidden added in v1.1.0

func (s *Leaf) SetHidden(hidden bool)

SetHidden hides a Leaf node.

Example
package main

import (
	"fmt"

	"github.com/charmbracelet/lipgloss/tree"
)

func main() {
	tr := tree.New().
		Child(
			"Foo",
			tree.Root("Bar").
				Child(
					"Qux",
					tree.Root("Quux").
						Child("Hello!"),
					"Quuux",
				),
			"Baz",
		)

	tr.Children().At(1).Children().At(2).SetHidden(true)
	fmt.Println(tr.String())
}
Output:


├── Foo
├── Bar
│   ├── Qux
│   └── Quux
│       └── Hello!
└── Baz

func (*Leaf) SetValue added in v1.1.0

func (s *Leaf) SetValue(value any)

SetValue sets the value of a Leaf node.

Example
package main

import (
	"fmt"

	"github.com/charmbracelet/lipgloss/tree"
	"github.com/charmbracelet/x/ansi"
)

func main() {
	t := tree.
		Root("⁜ Makeup").
		Child(
			"Glossier",
			"Fenty Beauty",
			tree.New().Child(
				"Gloss Bomb Universal Lip Luminizer",
				"Hot Cheeks Velour Blushlighter",
			),
			"Nyx",
			"Mac",
			"Milk",
		).
		Enumerator(tree.RoundedEnumerator)
	glossier := t.Children().At(0)
	glossier.SetValue("Il Makiage")
	fmt.Println(ansi.Strip(t.String()))
}
Output:

⁜ Makeup
├── Il Makiage
├── Fenty Beauty
│   ├── Gloss Bomb Universal Lip Luminizer
│   ╰── Hot Cheeks Velour Blushlighter
├── Nyx
├── Mac
╰── Milk

func (Leaf) String

func (s Leaf) String() string

String returns the string representation of a Leaf node.

func (Leaf) Value

func (s Leaf) Value() string

Value returns the value of a Leaf node.

type Node

type Node interface {
	fmt.Stringer
	Value() string
	Children() Children
	Hidden() bool
	SetHidden(bool)
	SetValue(any)
}

Node defines a node in a tree.

type NodeChildren

type NodeChildren []Node

NodeChildren is the implementation of the Children interface with tree Nodes.

func (NodeChildren) Append

func (n NodeChildren) Append(child Node) NodeChildren

Append appends a child to the list of children.

func (NodeChildren) At

func (n NodeChildren) At(i int) Node

At returns the child at the given index.

func (NodeChildren) Length

func (n NodeChildren) Length() int

Length returns the number of children in the list.

func (NodeChildren) Remove

func (n NodeChildren) Remove(index int) NodeChildren

Remove removes a child from the list at the given index.

type Style

type Style struct {
	// contains filtered or unexported fields
}

Style is the styling applied to the tree.

type StyleFunc

type StyleFunc func(children Children, i int) lipgloss.Style

StyleFunc allows the tree to be styled per item.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

Tree implements a Node.

func New

func New() *Tree

New returns a new tree.

func Root

func Root(root any) *Tree

Root returns a new tree with the root set.

tree.Root(root)

It is a shorthand for:

tree.New().Root(root)

func (*Tree) Child

func (t *Tree) Child(children ...any) *Tree

Child adds a child to this Tree.

If a Child Tree is passed without a root, it will be parented to it's sibling child (auto-nesting).

tree.Root("Foo").Child("Bar", tree.New().Child("Baz"), "Qux")
tree.Root("Foo").Child(tree.Root("Bar").Child("Baz"), "Qux")

├── Foo
├── Bar
│   └── Baz
└── Qux

func (*Tree) Children

func (t *Tree) Children() Children

Children returns the children of a node.

func (*Tree) Enumerator

func (t *Tree) Enumerator(enum Enumerator) *Tree

Enumerator sets the enumerator implementation. This can be used to change the way the branches indicators look. Lipgloss includes predefined enumerators for a classic or rounded tree. For example, you can have a rounded tree:

tree.New().
	Enumerator(RoundedEnumerator)

func (*Tree) EnumeratorStyle

func (t *Tree) EnumeratorStyle(style lipgloss.Style) *Tree

EnumeratorStyle sets a static style for all enumerators.

Use EnumeratorStyleFunc to conditionally set styles based on the tree node.

func (*Tree) EnumeratorStyleFunc

func (t *Tree) EnumeratorStyleFunc(fn StyleFunc) *Tree

EnumeratorStyleFunc sets the enumeration style function. Use this function for conditional styling.

t := tree.New().
	EnumeratorStyleFunc(func(_ tree.Children, i int) lipgloss.Style {
	    if selected == i {
	        return lipgloss.NewStyle().Foreground(hightlightColor)
	    }
	    return lipgloss.NewStyle().Foreground(dimColor)
	})

func (*Tree) Hidden

func (t *Tree) Hidden() bool

Hidden returns whether a Tree node is hidden.

func (*Tree) Hide

func (t *Tree) Hide(hide bool) *Tree

Hide sets whether to hide the Tree node. Use this when creating a new hidden Tree.

Example
package main

import (
	"fmt"

	"github.com/charmbracelet/lipgloss/tree"
)

func main() {
	tr := tree.New().
		Child(
			"Foo",
			tree.Root("Bar").
				Child(
					"Qux",
					tree.Root("Quux").
						Child("Foo", "Bar").
						Hide(true),
					"Quuux",
				),
			"Baz",
		)

	fmt.Println(tr.String())
}
Output:

├── Foo
├── Bar
│   ├── Qux
│   └── Quuux
└── Baz

func (*Tree) Indenter

func (t *Tree) Indenter(indenter Indenter) *Tree

Indenter sets the indenter implementation. This is used to change the way the tree is indented. The default indentor places a border connecting sibling elements and no border for the last child.

└── Foo
    └── Bar
        └── Baz
            └── Qux
                └── Quux

You can define your own indenter.

func ArrowIndenter(children tree.Children, index int) string {
	return "→ "
}

→ Foo
→ → Bar
→ → → Baz
→ → → → Qux
→ → → → → Quux

func (*Tree) ItemStyle

func (t *Tree) ItemStyle(style lipgloss.Style) *Tree

ItemStyle sets a static style for all items.

Use ItemStyleFunc to conditionally set styles based on the tree node.

func (*Tree) ItemStyleFunc

func (t *Tree) ItemStyleFunc(fn StyleFunc) *Tree

ItemStyleFunc sets the item style function. Use this for conditional styling. For example:

t := tree.New().
	ItemStyleFunc(func(_ tree.Data, i int) lipgloss.Style {
		if selected == i {
			return lipgloss.NewStyle().Foreground(hightlightColor)
		}
		return lipgloss.NewStyle().Foreground(dimColor)
	})

func (*Tree) Offset

func (t *Tree) Offset(start, end int) *Tree

Offset sets the Tree children offsets.

func (*Tree) Root

func (t *Tree) Root(root any) *Tree

Root sets the root value of this tree.

func (*Tree) RootStyle added in v0.13.0

func (t *Tree) RootStyle(style lipgloss.Style) *Tree

RootStyle sets a style for the root element.

func (*Tree) SetHidden added in v1.1.0

func (t *Tree) SetHidden(hidden bool)

SetHidden hides a Tree node.

Example
package main

import (
	"fmt"

	"github.com/charmbracelet/lipgloss/tree"
)

func main() {
	tr := tree.New().
		Child(
			"Foo",
			tree.Root("Bar").
				Child(
					"Qux",
					tree.Root("Quux").
						Child("Foo", "Bar"),
					"Quuux",
				),
			"Baz",
		)

	// Hide a tree after its creation. We'll hide Quux.
	tr.Children().At(1).Children().At(1).SetHidden(true)

	fmt.Println(tr.String())
}
Output:

├── Foo
├── Bar
│   ├── Qux
│   └── Quuux
└── Baz

func (*Tree) SetValue added in v1.1.0

func (t *Tree) SetValue(value any)

SetValue sets the value of a Tree node.

func (*Tree) String

func (t *Tree) String() string

String returns the string representation of the Tree node.

func (*Tree) Value

func (t *Tree) Value() string

Value returns the root name of this node.