Skip to content

Commit

Permalink
refactor: use tree enumerators
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Jul 18, 2024
1 parent 4e3b8d3 commit cb891ec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
37 changes: 37 additions & 0 deletions examples/list/tree/main.go
Original file line number Diff line number Diff line change
@@ -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")
}
22 changes: 14 additions & 8 deletions list/enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Check failure on line 141 in list/enumerator.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)

Check failure on line 141 in list/enumerator.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
func TreeRounded(items Items, index int) string {
return tree.RoundedEnumerator(items, index)
}

// DefaultIndenter indents a tree for nested trees and multiline content.
Expand All @@ -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)
}

0 comments on commit cb891ec

Please sign in to comment.