Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tree): hide children #454

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
25 changes: 15 additions & 10 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Node interface {
Value() string
Children() Children
Hidden() bool
SetHidden(bool)
}

// Leaf is a node without children.
Expand All @@ -51,7 +52,7 @@ func (Leaf) Children() Children {
return NodeChildren(nil)
}

// Value of a leaf node returns its value.
// Value of a Leaf node returns its value.
func (s Leaf) Value() string {
return s.value
}
Expand All @@ -61,6 +62,9 @@ func (s Leaf) Hidden() bool {
return s.hidden
}

// SetHidden hides the Leaf.
func (s *Leaf) SetHidden(hidden bool) { s.hidden = hidden }

// String returns the string representation of a Leaf node.
func (s Leaf) String() string {
return s.Value()
Expand All @@ -77,18 +81,22 @@ type Tree struct { //nolint:revive
ronce sync.Once
}

// Hidden returns whether this node is hidden.
// Hidden returns whether a Tree node is hidden.
func (t *Tree) Hidden() bool {
return t.hidden
}

// Hide sets whether to hide the tree node.
// Hide sets whether to hide the Tree node. Use this when creating a new
// hidden Tree.
func (t *Tree) Hide(hide bool) *Tree {
t.hidden = hide
return t
}

// Offset sets the tree children offsets.
// SetHidden hides the Tree.
func (t *Tree) SetHidden(hidden bool) { t.Hide(hidden) }

// Offset sets the Tree children offsets.
func (t *Tree) Offset(start, end int) *Tree {
if start > end {
_start := start
Expand All @@ -113,12 +121,12 @@ func (t *Tree) Value() string {
return t.value
}

// String returns the string representation of the tree node.
// String returns the string representation of the Tree node.
func (t *Tree) String() string {
return t.ensureRenderer().render(t, true, "")
}

// Child adds a child to this tree.
// Child adds a child to this Tree.
//
// If a Child Tree is passed without a root, it will be parented to it's sibling
// child (auto-nesting).
Expand Down Expand Up @@ -147,7 +155,7 @@ func (t *Tree) Child(children ...any) *Tree {
t.children = t.children.(NodeChildren).Append(item)
case fmt.Stringer:
s := Leaf{value: item.String()}
t.children = t.children.(NodeChildren).Append(s)
t.children = t.children.(NodeChildren).Append(&s)
case string:
s := Leaf{value: item}
t.children = t.children.(NodeChildren).Append(&s)
Expand Down Expand Up @@ -180,9 +188,6 @@ func ensureParent(nodes Children, item *Tree) (*Tree, int) {
parent.Child(item.children.At(i))
}
return parent, j
case Leaf:
item.value = parent.Value()
return item, j
case *Leaf:
item.value = parent.Value()
return item, j
Expand Down
27 changes: 27 additions & 0 deletions tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,33 @@ func TestTypes(t *testing.T) {
assertEqual(t, want, tree.String())
}

func TestLeafHidden(t *testing.T) {
tr := tree.New().
Child(
"Foo",
tree.Root("Bar").
Child(
"Qux",
tree.Root("Quux").
Child("This should be hidden").
Hide(true),
"Quuux",
),
"Baz",
)

// Hide Qux.
tr.Children().At(1).Children().At(0).SetHidden(true)

want := `
├── Foo
├── Bar
│ └── Quuux
└── Baz
`
assertEqual(t, want, tr.String())
}

// assertEqual verifies the strings are equal, assuming its terminal output.
func assertEqual(tb testing.TB, want, got string) {
tb.Helper()
Expand Down
Loading