Skip to content

Commit

Permalink
docs(tree): add NodeChildren Replace example
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Jan 10, 2025
1 parent f615974 commit d60ca5c
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions tree/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/tree"
"github.com/charmbracelet/x/ansi"
)

// Leaf Examples
Expand Down Expand Up @@ -67,7 +68,7 @@ func ExampleNewLeaf() {
//
}

func ExampleSetValue() {
func ExampleNodeChildren_Replace() {
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("63")).MarginRight(1)
rootStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("35"))
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("212"))
Expand All @@ -89,11 +90,36 @@ func ExampleSetValue() {
EnumeratorStyle(enumeratorStyle).
RootStyle(rootStyle).
ItemStyle(itemStyle)
glossier := t.Children().At(0)
glossier.SetValue(tree.Root(glossier.Value()).Child(tree.Root("Apparel").Child("Pink Hoodie", "Baseball Cap")))
fmt.Println(t.String())
// Add a Tree as a Child of "Glossier"
// This rewrites all of the tree data to do one replace. Not the most
// efficient. Maybe we can improve on this.
//
// That is how we're handling any Child manipulation in the Child() func as
// well. Because the children are an interface it's a bit trickier. We need
// to do an assignment, can't just manipulate the children directly.
t.SetChildren(t.Children().(tree.NodeChildren).
Replace(0, t.Children().At(0).Child(
tree.Root("Apparel").Child("Pink Hoodie", "Baseball Cap"),
)))

// Add a Leaf as a Child of "Glossier"
t.Children().At(0).Child("Makeup")
fmt.Println(ansi.Strip(t.String()))

// Output:
// hello
// ⁜ Makeup
// β”œβ”€β”€ Glossier
// β”‚ β”œβ”€β”€ Apparel
// β”‚ β”‚ β”œβ”€β”€ Pink Hoodie
// β”‚ β”‚ ╰── Baseball Cap
// β”‚ ╰── Makeup
// β”œβ”€β”€ Fenty Beauty
// β”‚ β”œβ”€β”€ Gloss Bomb Universal Lip Luminizer
// β”‚ ╰── Hot Cheeks Velour Blushlighter
// β”œβ”€β”€ Nyx
// β”œβ”€β”€ Mac
// ╰── Milk
//
}

// Tree Examples
Expand Down

0 comments on commit d60ca5c

Please sign in to comment.