Involved Source Fileschildren.goenumerator.gorenderer.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",
)
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.dataChildrenfilterfunc(index int) bool 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.hiddenboolvaluestring 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
Leaf : context.stringer
Leaf : os/signal.stringer
Leaf : runtime.stringer
func NewLeaf(value any, hidden bool) *Leaf
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
Tree implements a Node.childrenChildrenhiddenbooloffset[2]intr*rendererroncesync.Oncevaluestring 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) ensureRenderer() *renderer
*Tree : Node
*Tree : expvar.Var
*Tree : fmt.Stringer
*Tree : context.stringer
*Tree : os/signal.stringer
*Tree : runtime.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
func ensureParent(nodes Children, item *Tree) (*Tree, int)
func ensureParent(nodes Children, item *Tree) (*Tree, int)
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.