From f0de4ffa079b1c648345efee64b66fa4025b3fa4 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Wed, 7 Aug 2024 17:36:15 -0700 Subject: [PATCH] chore: tidy tree godoc --- tree/children.go | 10 +++++++++- tree/enumerator.go | 42 ++++++++++++++++++++---------------------- tree/tree.go | 9 +++------ 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/tree/children.go b/tree/children.go index 6727092a..438d0815 100644 --- a/tree/children.go +++ b/tree/children.go @@ -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 { @@ -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 diff --git a/tree/enumerator.go b/tree/enumerator.go index 5e5bc59d..8bc29dc0 100644 --- a/tree/enumerator.go +++ b/tree/enumerator.go @@ -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 "└──" @@ -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 "╰──" @@ -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 " " diff --git a/tree/tree.go b/tree/tree.go index 94f5dd08..77c9649b 100644 --- a/tree/tree.go +++ b/tree/tree.go @@ -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) // } @@ -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) // } @@ -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