Skip to content

Commit

Permalink
docs(examples): add testable examples for Hide and SetHidden
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Dec 12, 2024
1 parent 9fbb417 commit 88340e0
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 59 deletions.
119 changes: 119 additions & 0 deletions tree/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package tree_test

import (
"fmt"

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

// Leaf Examples

func ExampleLeaf_SetHidden() {
tr := tree.New().
Child(
"Foo",
tree.Root("Bar").
Child(
"Qux",
tree.Root("Quux").
Child("Hello!"),
"Quuux",
),
"Baz",
)

tr.Children().At(1).Children().At(2).SetHidden(true)
fmt.Println(tr.String())
// Output:
//
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”‚ β”œβ”€β”€ Qux
// β”‚ └── Quux
// β”‚ └── Hello!
// └── Baz
//
}

func ExampleNewLeaf() {
tr := tree.New().
Child(
"Foo",
tree.Root("Bar").
Child(
"Qux",
tree.Root("Quux").
Child(
tree.NewLeaf("This should be hidden", true),
tree.NewLeaf(
tree.Root("I am groot").Child("leaves"), false),
),
"Quuux",
),
"Baz",
)

fmt.Println(tr.String())
// Output:
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”‚ β”œβ”€β”€ Qux
// β”‚ β”œβ”€β”€ Quux
// β”‚ β”‚ └── I am groot
// β”‚ β”‚ └── leaves
// β”‚ └── Quuux
// └── Baz
//
}

// Tree Examples

func ExampleTree_Hide() {
tr := tree.New().
Child(
"Foo",
tree.Root("Bar").
Child(
"Qux",
tree.Root("Quux").
Child("Foo", "Bar").
Hide(true),
"Quuux",
),
"Baz",
)

fmt.Println(tr.String())
// Output:
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”‚ β”œβ”€β”€ Qux
// β”‚ └── Quuux
// └── Baz
}

func ExampleTree_SetHidden() {
tr := tree.New().
Child(
"Foo",
tree.Root("Bar").
Child(
"Qux",
tree.Root("Quux").
Child("Foo", "Bar"),
"Quuux",
),
"Baz",
)

// Hide a tree after its creation. We'll hide Quux.
tr.Children().At(1).Children().At(1).SetHidden(true)
// Output:
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”‚ β”œβ”€β”€ Qux
// β”‚ └── Quuux
// └── Baz
//
fmt.Println(tr.String())
}
59 changes: 0 additions & 59 deletions tree/tree_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tree_test

import (
"fmt"
"strings"
"testing"
"unicode"
Expand Down Expand Up @@ -757,61 +756,3 @@ func trimSpace(s string) string {
}
return strings.Join(result, "\n")
}

func ExampleLeaf_SetHidden() {
tr := tree.New().
Child(
"Foo",
tree.Root("Bar").
Child(
"Qux",
tree.Root("Quux").
Child("Hello!"),
"Quuux",
),
"Baz",
)

tr.Children().At(1).Children().At(2).SetHidden(true)
fmt.Println(tr.String())
// Output:
//
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”‚ β”œβ”€β”€ Qux
// β”‚ └── Quux
// β”‚ └── Hello!
// └── Baz
//
}

func ExampleNewLeaf() {
tr := tree.New().
Child(
"Foo",
tree.Root("Bar").
Child(
"Qux",
tree.Root("Quux").
Child(
tree.NewLeaf("This should be hidden", true),
tree.NewLeaf(
tree.Root("I am groot").Child("leaves"), false),
),
"Quuux",
),
"Baz",
)

fmt.Println(tr.String())
// Output:
// β”œβ”€β”€ Foo
// β”œβ”€β”€ Bar
// β”‚ β”œβ”€β”€ Qux
// β”‚ β”œβ”€β”€ Quux
// β”‚ β”‚ └── I am groot
// β”‚ β”‚ └── leaves
// β”‚ └── Quuux
// └── Baz
//
}

0 comments on commit 88340e0

Please sign in to comment.