// 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",// )
package treeimport ()// Node defines a node in a tree.typeNodeinterface {fmt.StringerValue() stringChildren() ChildrenHidden() boolSetHidden(bool)SetValue(any)}// Leaf is a node without children.typeLeafstruct {valuestringhiddenbool}// NewLeaf returns a new Leaf.func ( any, bool) *Leaf { := Leaf{} .SetValue() .SetHidden()return &}// Children of a Leaf node are always empty.func (Leaf) () Children {returnNodeChildren(nil)}// Value returns the value of a Leaf node.func ( Leaf) () string {return .value}// SetValue sets the value of a Leaf node.func ( *Leaf) ( any) {switch item := .(type) {caseNode, fmt.Stringer: .value = .(fmt.Stringer).String()casestring: .value = casenil: .value = ""default: .value = fmt.Sprintf("%v", ) }}// Hidden returns whether a Leaf node is hidden.func ( Leaf) () bool {return .hidden}// SetHidden hides a Leaf node.func ( *Leaf) ( bool) { .hidden = }// String returns the string representation of a Leaf node.// For leaf nodes, this is the same as Value.func ( Leaf) () string {return .Value()}// Tree implements a Node.typeTreestruct {valuestringhiddenbooloffset [2]intchildrenChildrenr *rendererroncesync.Once}// Hidden returns whether a Tree node is hidden.func ( *Tree) () bool {return .hidden}// Hide sets whether to hide the Tree node. Use this when creating a new// hidden Tree.func ( *Tree) ( bool) *Tree { .hidden = return}// SetHidden hides a Tree node.func ( *Tree) ( bool) { .Hide() }// 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.func ( *Tree) (, int) *Tree {if < 0 { = 0 }if < 0 { = 0 } := .children.Length()if > { = }if > { = } .offset[0] = .offset[1] = return}// Value returns the root name of this node.// If the root implements fmt.Stringer, it will return the value returned by it.func ( *Tree) () string {return .value}// SetValue sets the value of a Tree node.func ( *Tree) ( any) { .Root()}// String returns the string representation of the Tree node.func ( *Tree) () string {return .ensureRenderer().render(, true, "")}// 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// └── Quxfunc ( *Tree) ( ...any) *Tree {for , := range {switch item := .(type) {case *Tree: , := ensureParent(.children, )if >= 0 { .children = .children.(NodeChildren).Remove() } .children = .children.(NodeChildren).Append()caseChildren:for := range .Length() { .children = .children.(NodeChildren).Append(.At()) }caseNode: .children = .children.(NodeChildren).Append()casefmt.Stringer: := Leaf{value: .String()} .children = .children.(NodeChildren).Append(&)casestring: := Leaf{value: } .children = .children.(NodeChildren).Append(&)case []any:return .(...)case []string: := make([]any, 0, len())for , := range { = append(, ) }return .(...)casenil:continuedefault:return .(fmt.Sprintf("%v", )) } }return}func ( Children, *Tree) (*Tree, int) {if .Value() != "" || .Length() == 0 {return , -1 } := .Length() - 1 := .At()switch parent := .(type) {case *Tree:for := range .Children().Length() { .Child(.children.At()) }return , case *Leaf: .value = .Value()return , }return , -1}func ( *Tree) () *renderer { .ronce.Do(func() { .r = newRenderer() })return .r}// EnumeratorStyle sets a static style for all enumerators.//// Use EnumeratorStyleFunc to conditionally set styles based on the tree node.func ( *Tree) ( lipgloss.Style) *Tree { .ensureRenderer().style.enumeratorFunc = func(Children, int) lipgloss.Style {return }return}// 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) ( StyleFunc) *Tree {if == nil { = func(Children, int) lipgloss.Style { returnlipgloss.NewStyle() } } .ensureRenderer().style.enumeratorFunc = return}// IndenterStyle sets a static style for all indenters.//// Use IndenterStyleFunc to conditionally set styles based on the tree node.func ( *Tree) ( lipgloss.Style) *Tree { .ensureRenderer().style.indenterFunc = func(Children, int) lipgloss.Style {return }return}// 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)// })func ( *Tree) ( StyleFunc) *Tree {if == nil { = func(Children, int) lipgloss.Style { returnlipgloss.NewStyle() } } .ensureRenderer().style.indenterFunc = return}// RootStyle sets a style for the root element.func ( *Tree) ( lipgloss.Style) *Tree { .ensureRenderer().style.root = return}// ItemStyle sets a static style for all items.//// Use ItemStyleFunc to conditionally set styles based on the tree node.func ( *Tree) ( lipgloss.Style) *Tree { .ensureRenderer().style.itemFunc = func(Children, int) lipgloss.Style { return }return}// 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) ( StyleFunc) *Tree {if == nil { = func(Children, int) lipgloss.Style { returnlipgloss.NewStyle() } } .ensureRenderer().style.itemFunc = return}// 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) ( Enumerator) *Tree { .ensureRenderer().enumerator = return}// 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// → → → → → Quuxfunc ( *Tree) ( Indenter) *Tree { .ensureRenderer().indenter = return}// Width sets the tree width.//// Items will be padded to account for the entire width of the tree.func ( *Tree) ( int) *Tree { .ensureRenderer().width = return}// Children returns the children of a node.func ( *Tree) () Children {var []Nodefor := .offset[0]; < .children.Length()-.offset[1]; ++ { = append(, .children.At()) }returnNodeChildren()}// Root returns a new tree with the root set.//// tree.Root(root)//// It is a shorthand for://// tree.New().Root(root)func ( any) *Tree { := New()return .Root()}// Root sets the root value of this tree.func ( *Tree) ( any) *Tree {// root is a tree or stringswitch item := .(type) {case *Tree: .value = .value = .Child(.children)caseNode, fmt.Stringer: .value = .(fmt.Stringer).String()casestring: .value = casenil: .value = ""default: .value = fmt.Sprintf("%v", ) }return}// New returns a new tree.func () *Tree {return &Tree{children: NodeChildren(nil), }}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)