Closed rchaganti closed 3 weeks ago
Hey, the issue here is where you assign the output of root.Child
to cTree
which makes cTree
a pointer to the root tree, not the child.
Here's a working version of what you're looking for:
package main
import (
"fmt"
"github.com/charmbracelet/lipgloss/tree"
)
type Node struct {
Name string
Category []Category
}
type Category struct {
Name string
Items []Item
}
type Item struct {
Name string
}
var categories = []Category{
{
Name: "cat1",
Items: []Item{
{Name: "item1"},
{Name: "item2"},
},
},
{
Name: "cat2",
Items: []Item{
{Name: "item3"},
{Name: "item4"},
},
},
}
func main() {
data := Node{
Name: "root",
Category: categories,
}
// Create a new tree
root := tree.Root(data.Name)
for _, cat := range data.Category {
cTree := tree.New().Root(cat.Name)
for _, item := range cat.Items {
cTree.Child(item.Name)
}
root.Child(cTree)
}
fmt.Println(root)
}
Output
root
βββ cat1
β βββ item1
β βββ item2
βββ cat2
βββ item3
βββ item4
Awesome! Thank you.
Describe the bug When dynamically generating a tree, the child nodes are not aligned properly
Setup Please complete the following information along with version numbers, if applicable.
To Reproduce Steps to reproduce the behavior: Run the following program.
Source Code
Expected behavior root, child, and leaf aligned per the relationships.
Screenshots
Additional context Add any other context about the problem here.