Skip to content

Commit

Permalink
chore: tidy tree godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Aug 8, 2024
1 parent c1c679a commit f0de4ff
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
10 changes: 9 additions & 1 deletion tree/children.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func (n NodeChildren) At(i int) Node {
return nil
}

// NewStringData returns a Data of strings.
// NewStringData returns the given strings as Children (not attached to a tree).
// This can used for filtering with NewFilter.
func NewStringData(data ...string) Children {
result := make([]Node, 0, len(data))
for _, d := range data {
Expand Down Expand Up @@ -81,6 +82,13 @@ func (m *Filter) At(index int) Node {
}

// Filter uses a filter function to set a condition that all the data must satisfy to be in the Tree.
//
// Example:
//
// data := tree.NewFilter(tree.NewStringData("Foo","Bar","Baz")).
// Filter(func(index int) bool {
// return index != 1
// })
func (m *Filter) Filter(f func(index int) bool) *Filter {
m.filter = f
return m
Expand Down
42 changes: 20 additions & 22 deletions tree/enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ package tree
// Enumerator enumerates a tree. Typically, this is used to draw the branches
// for the tree nodes and is different for the last child.
//
// For example, the default enumerator would be:
// Example:
//
// func TreeEnumerator(children Children, index int) string {
// func DefaultEnumerator(children Children, index int) string {
// if children.Length()-1 == index {
// return "└──"
// }
//
// return "β”œβ”€β”€"
// }
type Enumerator func(children Children, index int) string

// DefaultEnumerator enumerates a tree.
//
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”œβ”€β”€ Baz
// └── Qux.
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”œβ”€β”€ Baz
// └── Qux.
func DefaultEnumerator(children Children, index int) string {
if children.Length()-1 == index {
return "└──"
Expand All @@ -29,10 +28,10 @@ func DefaultEnumerator(children Children, index int) string {

// RoundedEnumerator enumerates a tree with rounded edges.
//
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”œβ”€β”€ Baz
// ╰── Qux.
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”œβ”€β”€ Baz
// ╰── Qux.
func RoundedEnumerator(children Children, index int) string {
if children.Length()-1 == index {
return "╰──"
Expand All @@ -45,27 +44,26 @@ func RoundedEnumerator(children Children, index int) string {
// Indenters allow for displaying nested tree items with connecting borders
// to sibling nodes.
//
// For example, the default indenter would be:
// Example:
//
// func TreeIndenter(children Children, index int) string {
// func DefaultIndenter(children Children, index int) string {
// if children.Length()-1 == index {
// return "β”‚ "
// }
//
// return " "
// }
type Indenter func(children Children, index int) string

// DefaultIndenter indents a tree for nested trees and multiline content.
//
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”‚ β”œβ”€β”€ Qux
// β”‚ β”œβ”€β”€ Quux
// β”‚ β”‚ β”œβ”€β”€ Foo
// β”‚ β”‚ └── Bar
// β”‚ └── Quuux
// └── Baz.
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”‚ β”œβ”€β”€ Qux
// β”‚ β”œβ”€β”€ Quux
// β”‚ β”‚ β”œβ”€β”€ Foo
// β”‚ β”‚ └── Bar
// β”‚ └── Quuux
// └── Baz.
func DefaultIndenter(children Children, index int) string {
if children.Length()-1 == index {
return " "
Expand Down
9 changes: 3 additions & 6 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ func (t *Tree) EnumeratorStyle(style lipgloss.Style) *Tree {
// EnumeratorStyleFunc sets the enumeration style function. Use this function
// for conditional styling.
//
// t := tree.New().
// EnumeratorStyleFunc(func(_ tree.Children, i int) lipgloss.Style {
// t := tree.New().EnumeratorStyleFunc(func(_ tree.Children, i int) lipgloss.Style {
// if selected == i {
// return lipgloss.NewStyle().Foreground(hightlightColor)
// }
Expand Down Expand Up @@ -240,8 +239,7 @@ func (t *Tree) ItemStyle(style lipgloss.Style) *Tree {
// ItemStyleFunc sets the item style function. Use this for conditional styling.
// For example:
//
// t := tree.New().
// ItemStyleFunc(func(_ tree.Data, i int) lipgloss.Style {
// t := tree.New().ItemStyleFunc(func(_ tree.Data, i int) lipgloss.Style {
// if selected == i {
// return lipgloss.NewStyle().Foreground(hightlightColor)
// }
Expand All @@ -259,8 +257,7 @@ func (t *Tree) ItemStyleFunc(fn StyleFunc) *Tree {
// 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)
// tree.New().Enumerator(RoundedEnumerator)
func (t *Tree) Enumerator(enum Enumerator) *Tree {
t.ensureRenderer().enumerator = enum
return t
Expand Down

0 comments on commit f0de4ff

Please sign in to comment.