Skip to content

Commit

Permalink
wip: debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Dec 10, 2024
1 parent fe12bcb commit cd33269
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/charmbracelet/bubbletea v0.25.0 // indirect
github.com/charmbracelet/keygen v0.5.0 // indirect
github.com/charmbracelet/log v0.4.0 // indirect
github.com/charmbracelet/x/ansi v0.4.5 // indirect
github.com/charmbracelet/x/ansi v0.5.2 // indirect
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651 // indirect
github.com/charmbracelet/x/exp/term v0.0.0-20240328150354-ab9afc214dfd // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
Expand Down
4 changes: 2 additions & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/charmbracelet/ssh v0.0.0-20240401141849-854cddfa2917 h1:NZKjJ7d/pzk/A
github.com/charmbracelet/ssh v0.0.0-20240401141849-854cddfa2917/go.mod h1:8/Ve8iGRRIGFM1kepYfRF2pEOF5Y3TEZYoJaA54228U=
github.com/charmbracelet/wish v1.4.0 h1:pL1uVP/YuYgJheHEj98teZ/n6pMYnmlZq/fcHvomrfc=
github.com/charmbracelet/wish v1.4.0/go.mod h1:ew4/MjJVfW/akEO9KmrQHQv1F7bQRGscRMrA+KtovTk=
github.com/charmbracelet/x/ansi v0.4.5 h1:LqK4vwBNaXw2AyGIICa5/29Sbdq58GbGdFngSexTdRM=
github.com/charmbracelet/x/ansi v0.4.5/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/ansi v0.5.2 h1:dEa1x2qdOZXD/6439s+wF7xjV+kZLu/iN00GuXXrU9E=
github.com/charmbracelet/x/ansi v0.5.2/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q=
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651 h1:3RXpZWGWTOeVXCTv0Dnzxdv/MhNUkBfEcbaTY0zrTQI=
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651/go.mod h1:2P0UgXMEa6TsToMSuFqKFQR+fZTO9CNGUNokkPatT/0=
github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a h1:G99klV19u0QnhiizODirwVksQB91TJKV/UaTnACcG30=
Expand Down
36 changes: 36 additions & 0 deletions examples/tree/hidden/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"

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

func main() {
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(
"Glossier",
"Fenty Beauty",
tree.New().Child(
"Gloss Bomb Universal Lip Luminizer",
"Hot Cheeks Velour Blushlighter",
),
"Nyx",
"Mac",
"Milk",
).
Enumerator(tree.RoundedEnumerator).
EnumeratorStyle(enumeratorStyle).
RootStyle(rootStyle).
ItemStyle(itemStyle)
t.Children().At(1).Hide(true)
t.Children().At(2).Hide(true)

fmt.Println(t)
}
1 change: 0 additions & 1 deletion examples/tree/makeup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ func main() {
EnumeratorStyle(enumeratorStyle).
RootStyle(rootStyle).
ItemStyle(itemStyle)

fmt.Println(t)
}
19 changes: 11 additions & 8 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
Hide(bool) *Node
}

// Leaf is a node without children.
Expand All @@ -47,22 +48,27 @@ type Leaf struct {
}

// Children of a Leaf node are always empty.
func (Leaf) Children() Children {
func (*Leaf) Children() Children {
return NodeChildren(nil)
}

// Value of a leaf node returns its value.
func (s Leaf) Value() string {
func (s *Leaf) Value() string {
return s.value
}

// Hidden returns whether a Leaf node is hidden.
func (s Leaf) Hidden() bool {
func (s *Leaf) Hidden() bool {
return s.hidden
}

func (s *Leaf) Hide(hide bool) *Leaf {
s.hidden = hide
return s
}

// String returns the string representation of a Leaf node.
func (s Leaf) String() string {
func (s *Leaf) String() string {
return s.Value()
}

Expand Down Expand Up @@ -147,7 +153,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)

Check failure on line 156 in tree/tree.go

View workflow job for this annotation

GitHub Actions / coverage (^1, ubuntu-latest)

cannot use &s (value of type *Leaf) as Node value in argument to t.children.(NodeChildren).Append: *Leaf does not implement Node (wrong type for method Hide)
case string:
s := Leaf{value: item}
t.children = t.children.(NodeChildren).Append(&s)

Check failure on line 159 in tree/tree.go

View workflow job for this annotation

GitHub Actions / coverage (^1, ubuntu-latest)

cannot use &s (value of type *Leaf) as Node value in argument to t.children.(NodeChildren).Append: *Leaf does not implement Node (wrong type for method Hide)
Expand Down Expand Up @@ -180,9 +186,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:

Check failure on line 189 in tree/tree.go

View workflow job for this annotation

GitHub Actions / coverage (^1, ubuntu-latest)

impossible type switch case: *Leaf
item.value = parent.Value()
return item, j
Expand Down
30 changes: 30 additions & 0 deletions tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ func TestTreeHidden(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("Foo", "Bar").
Hide(true),
"Quuux",
),
"Baz",
)

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

want := `
β”œβ”€β”€ Foo
β”œβ”€β”€ Bar
β”‚ β”œβ”€β”€ Quux
β”‚ β”‚ β”œβ”€β”€ Foo
β”‚ β”‚ ╰── Bar
β”‚ └── Quuux
└── Baz
`
assertEqual(t, want, tr.String())
}

func TestTreeAllHidden(t *testing.T) {
tree := tree.New().
Child(
Expand Down

0 comments on commit cd33269

Please sign in to comment.