Skip to content

Commit

Permalink
docs(tree): tidy examples
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Jan 10, 2025
1 parent 4d4942c commit f03f016
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 29 deletions.
101 changes: 74 additions & 27 deletions tree/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tree_test
import (
"fmt"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/tree"
"github.com/charmbracelet/x/ansi"
)
Expand Down Expand Up @@ -68,11 +67,69 @@ func ExampleNewLeaf() {
//
}

func ExampleTree_Replace() {
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("63")).MarginRight(1)
rootStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("35"))
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("212"))
func ExampleLeaf_Insert() {
t := tree.
Root("⁜ Makeup").
Child(
"Glossier",
"Fenty Beauty",
tree.New().Child(
"Gloss Bomb Universal Lip Luminizer",
"Hot Cheeks Velour Blushlighter",
),
"Nyx",
"Mac",
"Milk",
).
Enumerator(tree.RoundedEnumerator)
// Adds a new Tree Node to a Leaf (Mac).
t.Replace(3, t.Children().At(3).Insert(0, "Glow Play Cushion Blush"))
fmt.Println(ansi.Strip(t.String()))
// Output:
//⁜ Makeup
//├── Glossier
//├── Fenty Beauty
//│ ├── Gloss Bomb Universal Lip Luminizer
//│ ╰── Hot Cheeks Velour Blushlighter
//├── Nyx
//├── Mac
//│ ╰── Glow Play Cushion Blush
//╰── Milk
}

func ExampleLeaf_Replace() {
t := tree.
Root("⁜ Makeup").
Child(
"Glossier",
"Fenty Beauty",
tree.New().Child(
"Gloss Bomb Universal Lip Luminizer",
"Hot Cheeks Velour Blushlighter",
),
"Nyx",
"Mac",
"Milk",
).
Enumerator(tree.RoundedEnumerator)
// Add Glow Play Cushion Blush to Mac Leaf.
t.Replace(3, t.Children().At(3).Replace(0, "Glow Play Cushion Blush"))
fmt.Println(ansi.Strip(t.String()))
// Output:
//⁜ Makeup
//├── Glossier
//├── Fenty Beauty
//│ ├── Gloss Bomb Universal Lip Luminizer
//│ ╰── Hot Cheeks Velour Blushlighter
//├── Nyx
//├── Mac
//│ ╰── Glow Play Cushion Blush
//╰── Milk
}

// Tree Examples

func ExampleTree_Replace() {
t := tree.
Root("⁜ Makeup").
Child(
Expand All @@ -86,16 +143,16 @@ func ExampleTree_Replace() {
"Mac",
"Milk",
).
Enumerator(tree.RoundedEnumerator).
EnumeratorStyle(enumeratorStyle).
RootStyle(rootStyle).
ItemStyle(itemStyle)
// Add a Tree as a Child of "Glossier"
Enumerator(tree.RoundedEnumerator)
// Add a Tree as a Child of "Glossier". At this stage "Glossier" is a Leaf,
// so we re-assign the value of "Glossier" in the "Makeup" Tree to its new
// Tree value returned from Child().
t.Replace(0, t.Children().At(0).Child(
tree.Root("Apparel").Child("Pink Hoodie", "Baseball Cap"),
))

// Add a Leaf as a Child of "Glossier"
// Add a Leaf as a Child of "Glossier". At this stage "Glossier" is a Tree,
// so we don't need to use [Tree.Replace] on the parent tree.
t.Children().At(0).Child("Makeup")
fmt.Println(ansi.Strip(t.String()))
// Output:
Expand All @@ -115,11 +172,6 @@ func ExampleTree_Replace() {
}

func ExampleTree_Insert() {
// Styles are here in case we want to test that styles are properly inherited...
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("63")).MarginRight(1)
rootStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("35"))
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("212"))

t := tree.
Root("⁜ Makeup").
Child(
Expand All @@ -133,18 +185,15 @@ func ExampleTree_Insert() {
"Mac",
"Milk",
).
Enumerator(tree.RoundedEnumerator).
EnumeratorStyle(enumeratorStyle).
RootStyle(rootStyle).
ItemStyle(itemStyle)
// Adds a new Tree Node after Fenty Beauty
Enumerator(tree.RoundedEnumerator)
// Adds a new Tree Node after Fenty Beauty.
t.Insert(2, tree.Root("Lancôme").Child("Juicy Tubes Lip Gloss", "Lash Idôle", "Teint Idôle Highlighter"))

// Adds a new Tree Node in Fenty Beauty
t.Replace(1, t.Children().At(1).Insert(0, tree.NewLeaf("Blurring Skin Tint", false)))
// Adds a new Tree Node in Fenty Beauty.
t.Replace(1, t.Children().At(1).Insert(0, "Blurring Skin Tint"))

// Adds a new Tree Node to a Leaf (Mac)
t.Replace(4, t.Children().At(4).Insert(0, tree.NewLeaf("Glow Play Cushion Blush", false)))
// Adds a new Tree Node to a Leaf (Mac).
t.Replace(4, t.Children().At(4).Insert(0, "Glow Play Cushion Blush"))
fmt.Println(ansi.Strip(t.String()))
// Output:
//⁜ Makeup
Expand All @@ -163,8 +212,6 @@ func ExampleTree_Insert() {
//╰── Milk
}

// Tree Examples

func ExampleTree_Hide() {
tr := tree.New().
Child(
Expand Down
8 changes: 6 additions & 2 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,16 @@ func (s *Leaf) SetChildren(children ...any) *Tree {
return s.Child(children)
}

// Replace turns the Leaf into a Tree with the given child.
// Replace turns the Leaf into a Tree with the given child. Because of the type
// change, you'll need to reassign the result of this function to the value with
// [Tree.Replace].
func (s *Leaf) Replace(_ int, child any) *Tree {
return s.Child(child)
}

// Insert turns the Leaf into a Tree with the given child.
// Insert turns the Leaf into a Tree with the given child. Because of the type
// change, you'll need to reassign the result of this function to the value with
// [Tree.Replace].
func (s *Leaf) Insert(_ int, child any) *Tree {
return s.Child(child)
}
Expand Down

0 comments on commit f03f016

Please sign in to comment.