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 ¶
- func DefaultEnumerator(children Children, index int) string
- func DefaultIndenter(children Children, index int) string
- func RoundedEnumerator(children Children, index int) string
- type Children
- type Enumerator
- type Filter
- type Indenter
- type Leaf
- type Node
- type NodeChildren
- type Style
- type StyleFunc
- type Tree
- func (t *Tree) Child(children ...any) *Tree
- func (t *Tree) Children() Children
- func (t *Tree) Enumerator(enum Enumerator) *Tree
- func (t *Tree) EnumeratorStyle(style lipgloss.Style) *Tree
- func (t *Tree) EnumeratorStyleFunc(fn StyleFunc) *Tree
- func (t *Tree) Hidden() bool
- func (t *Tree) Hide(hide bool) *Tree
- func (t *Tree) Indenter(indenter Indenter) *Tree
- func (t *Tree) ItemStyle(style lipgloss.Style) *Tree
- func (t *Tree) ItemStyleFunc(fn StyleFunc) *Tree
- func (t *Tree) Offset(start, end int) *Tree
- func (t *Tree) Root(root any) *Tree
- func (t *Tree) RootStyle(style lipgloss.Style) *Tree
- func (t *Tree) SetHidden(hidden bool)
- func (t *Tree) SetValue(value any)
- func (t *Tree) String() string
- func (t *Tree) Value() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultIndenter ¶
DefaultIndenter indents a tree for nested trees and multiline content.
├── Foo ├── Bar │ ├── Qux │ ├── Quux │ │ ├── Foo │ │ └── Bar │ └── Quuux └── Baz.
func RoundedEnumerator ¶
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 ¶
NewStringData returns a Data of strings.
type Enumerator ¶
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 (*Filter) At ¶
At returns the item at the given index. The index is relative to the filtered results.
type Indenter ¶
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
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) SetHidden ¶ added in v1.1.0
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
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
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 Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree implements a Node.
func Root ¶
Root returns a new tree with the root set.
tree.Root(root)
It is a shorthand for:
tree.New().Root(root)
func (*Tree) Child ¶
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) 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 ¶
EnumeratorStyle sets a static style for all enumerators.
Use EnumeratorStyleFunc to conditionally set styles based on the tree node.
func (*Tree) EnumeratorStyleFunc ¶
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) Hide ¶
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 ¶
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 ¶
ItemStyle sets a static style for all items.
Use ItemStyleFunc to conditionally set styles based on the tree node.
func (*Tree) ItemStyleFunc ¶
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) SetHidden ¶ added in v1.1.0
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