Skip to content

Dynamically generating a tree #375

Description

@rchaganti

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.

  • OS [Ubuntu]
  • Shell [bash]
  • Locale [en_US.UTF-8]

To Reproduce
Steps to reproduce the behavior:
Run the following program.

Source Code

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 := root.Child(
			tree.New().Root(cat.Name),
		)

		for _, item := range cat.Items {
			cTree.Child(item.Name)
		}
	}

	fmt.Println(root)
}

Expected behavior
root, child, and leaf aligned per the relationships.

Screenshots

$ go run main.go 
root
├── cat1
├── item1
├── item2
├── cat2
├── item3
└── item4

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions