diff --git a/examples/list/tree/main.go b/examples/list/tree/main.go new file mode 100644 index 00000000..d4180881 --- /dev/null +++ b/examples/list/tree/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + + "github.com/charmbracelet/lipgloss/list" +) + +func main() { + t := list.New( + "Documents", + "Downloads", + "Unfinished Projects", + ).Title("~").Enumerator(list.Tree) + + fmt.Println("A classic tree:\n" + t.String() + "\n") + + tr := list.New( + "Documents", + "Downloads", + "Unfinished Projects", + ).Title("~").Enumerator(list.TreeRounded) + + fmt.Println("A cool, rounded tree:\n" + tr.String() + "\n") + + ti := list.New( + list.New( + "Important Documents", + "Junk Drawer", + "Books", + ).Title("Documents").Enumerator(list.Tree), + "Downloads", + "Unfinished Projects", + ).Title("~").Enumerator(list.Tree).Indenter(list.TreeIndenter) + + fmt.Println("A fancy, nested tree:\n" + ti.String() + "\n") +} diff --git a/list/enumerator.go b/list/enumerator.go index ce55b1e7..38271499 100644 --- a/list/enumerator.go +++ b/list/enumerator.go @@ -3,6 +3,8 @@ package list import ( "fmt" "strings" + + "github.com/charmbracelet/lipgloss/internal/tree" ) // Enumerator enumerates a list. Given a list of items and the index of the @@ -128,10 +130,17 @@ func Dash(Items, int) string { // ├── Baz // └── Qux. func Tree(items Items, index int) string { - if items.Length()-1 == index { - return "└──" - } - return "├──" + return tree.DefaultEnumerator(items, index) +} + +// TreeRounded enumerates a tree with rounded corners. +// +// Foo +// ├── Bar +// ├── Baz +// ╰── Qux +func TreeRounded(items Items, index int) string { + return tree.RoundedEnumerator(items, index) } // DefaultIndenter indents a tree for nested trees and multiline content. @@ -145,8 +154,5 @@ func Tree(items Items, index int) string { // │ └── Quuux // └── Baz. func TreeIndenter(items Items, index int) string { - if items.Length()-1 == index { - return " " - } - return "│ " + return tree.DefaultIndenter(items, index) }