package tree

Import Path
	charm.land/lipgloss/v2/tree (on go.dev)

Dependency Relation
	imports 5 packages, and imported by one package

Involved Source Files children.go enumerator.go renderer.go 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", )
Code Examples package main import ( "fmt" "charm.land/lipgloss/v2/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()) } package main import ( "fmt" "charm.land/lipgloss/v2/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())) } package main import ( "fmt" "charm.land/lipgloss/v2/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()) } package main import ( "fmt" "charm.land/lipgloss/v2/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()) } package main import ( "fmt" "charm.land/lipgloss/v2/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()) }
Package-Level Type Names (total 11, in which 10 are exported)
/* sort exporteds by: | */
Children is the interface that wraps the basic methods of a tree model. At returns the content item of the given index. Length returns the number of children in the tree. *Filter NodeChildren func NewStringData(data ...string) Children func Leaf.Children() Children func Node.Children() Children func (*Tree).Children() Children func DefaultEnumerator(children Children, index int) string func DefaultIndenter(children Children, index int) string func NewFilter(data Children) *Filter func RoundedEnumerator(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 "├──" } func (*Tree).Enumerator(enum Enumerator) *Tree
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. At returns the item at the given index. The index is relative to the filtered results. Filter uses a filter function to set a condition that all the data must satisfy to be in the Tree. Length returns the number of children in the tree. *Filter : Children func NewFilter(data Children) *Filter func (*Filter).Filter(f func(index int) bool) *Filter
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 " " } func (*Tree).Indenter(indenter Indenter) *Tree
Leaf is a node without children. Children of a Leaf node are always empty. Hidden returns whether a Leaf node is hidden. SetHidden hides a Leaf node. SetValue sets the value of a Leaf node. String returns the string representation of a Leaf node. For leaf nodes, this is the same as Value. Value returns the value of a Leaf node. *Leaf : Node Leaf : expvar.Var Leaf : fmt.Stringer func NewLeaf(value any, hidden bool) *Leaf
Node defines a node in a tree. ( Node) Children() Children ( Node) Hidden() bool ( Node) SetHidden(bool) ( Node) SetValue(any) ( Node) String() string ( Node) Value() string *Leaf *Tree Node : expvar.Var Node : fmt.Stringer func Children.At(index int) Node func (*Filter).At(index int) Node func NodeChildren.At(i int) Node func NodeChildren.Append(child Node) NodeChildren
NodeChildren is the implementation of the Children interface with tree Nodes. Append appends a child to the list of children. At returns the child at the given index. Length returns the number of children in the list. Remove removes a child from the list at the given index. NodeChildren : Children func NodeChildren.Append(child Node) NodeChildren func NodeChildren.Remove(index int) NodeChildren
Style is the styling applied to the tree.
StyleFunc allows the tree to be styled per item. func (*Tree).EnumeratorStyleFunc(fn StyleFunc) *Tree func (*Tree).IndenterStyleFunc(fn StyleFunc) *Tree func (*Tree).ItemStyleFunc(fn StyleFunc) *Tree
Tree implements a Node. Child adds a child to this Tree. If a Child Tree is passed without a root, it will be parented to its 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 Children returns the children of a node. 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) EnumeratorStyle sets a static style for all enumerators. Use EnumeratorStyleFunc to conditionally set styles based on the tree node. 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) }) Hidden returns whether a Tree node is hidden. Hide sets whether to hide the Tree node. Use this when creating a new hidden 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 IndenterStyle sets a static style for all indenters. Use IndenterStyleFunc to conditionally set styles based on the tree node. IndenterStyleFunc sets the indentation style function. Use this function for conditional styling. t := tree.New(). IndenterStyleFunc(func(_ tree.Children, i int) lipgloss.Style { if selected == i { return lipgloss.NewStyle().Foreground(hightlightColor) } return lipgloss.NewStyle().Foreground(dimColor) }) ItemStyle sets a static style for all items. Use ItemStyleFunc to conditionally set styles based on the tree node. 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) }) Offset sets the Tree children offsets. start is the number of children to skip from the beginning and end is the number of children to skip from the end, so Offset(2, 1) on a five-child tree renders children at indices 2 and 3. See charmbracelet/lipgloss#535. Root sets the root value of this tree. RootStyle sets a style for the root element. SetHidden hides a Tree node. SetValue sets the value of a Tree node. String returns the string representation of the Tree node. Value returns the root name of this node. If the root implements fmt.Stringer, it will return the value returned by it. Width sets the tree width. Items will be padded to account for the entire width of the tree. *Tree : Node *Tree : expvar.Var *Tree : fmt.Stringer func New() *Tree func Root(root any) *Tree func (*Tree).Child(children ...any) *Tree func (*Tree).Enumerator(enum Enumerator) *Tree func (*Tree).EnumeratorStyle(style lipgloss.Style) *Tree func (*Tree).EnumeratorStyleFunc(fn StyleFunc) *Tree func (*Tree).Hide(hide bool) *Tree func (*Tree).Indenter(indenter Indenter) *Tree func (*Tree).IndenterStyle(style lipgloss.Style) *Tree func (*Tree).IndenterStyleFunc(fn StyleFunc) *Tree func (*Tree).ItemStyle(style lipgloss.Style) *Tree func (*Tree).ItemStyleFunc(fn StyleFunc) *Tree func (*Tree).Offset(start, end int) *Tree func (*Tree).Root(root any) *Tree func (*Tree).RootStyle(style lipgloss.Style) *Tree func (*Tree).Width(width int) *Tree
Package-Level Functions (total 10, in which 8 are exported)
DefaultEnumerator enumerates a tree. ├── Foo ├── Bar ├── Baz └── Qux.
DefaultIndenter indents a tree for nested trees and multiline content. ├── Foo ├── Bar │ ├── Qux │ ├── Quux │ │ ├── Foo │ │ └── Bar │ └── Quuux └── Baz.
New returns a new tree.
NewFilter initializes a new Filter.
NewLeaf returns a new Leaf.
NewStringData returns a Data of strings.
Root returns a new tree with the root set. tree.Root(root) It is a shorthand for: tree.New().Root(root)
RoundedEnumerator enumerates a tree with rounded edges. ├── Foo ├── Bar ├── Baz ╰── Qux.