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) String() string
- func (t *Tree) Value() string
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.
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) 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) })