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: move tree to root #342

Merged
merged 12 commits into from
Aug 19, 2024
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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.1.3 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // 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.1.3 h1:RBh/eleNWML5R524mjUF0yVRePTwqN9tPtV+DPgO5Lw=
github.com/charmbracelet/x/ansi v0.1.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=
github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
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/term v0.0.0-20240328150354-ab9afc214dfd h1:HqBjkSFXXfW4IgX3TMKipWoPEN08T3Pi4SA/3DLss/U=
Expand Down
30 changes: 0 additions & 30 deletions list/enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,33 +120,3 @@ func Asterisk(Items, int) string {
func Dash(Items, int) string {
return "-"
}

// Tree enumerates a tree.
//
// ├── Foo
// ├── Bar
// ├── Baz
// └── Qux.
func Tree(items Items, index int) string {
if items.Length()-1 == index {
return "└──"
}
return "├──"
}

// DefaultIndenter indents a tree for nested trees and multiline content.
//
// ├── Foo
// ├── Bar
// │ ├── Qux
// │ ├── Quux
// │ │ ├── Foo
// │ │ └── Bar
// │ └── Quuux
// └── Baz.
func TreeIndenter(items Items, index int) string {
if items.Length()-1 == index {
return " "
}
return "│ "
}
2 changes: 1 addition & 1 deletion list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ package list

import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/internal/tree"
"github.com/charmbracelet/lipgloss/tree"
)

// List represents a list of items that can be displayed. Lists can contain
Expand Down
2 changes: 1 addition & 1 deletion list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/aymanbagabas/go-udiff"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/internal/tree"
"github.com/charmbracelet/lipgloss/list"
"github.com/charmbracelet/lipgloss/tree"
)

// XXX: can't write multi-line examples if the underlying string uses
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 16 additions & 4 deletions internal/tree/tree.go → tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,25 @@ func (t *Tree) Children() Children {
// It is a shorthand for:
//
// tree.New().Root(root)
func Root(root string) *Tree {
return New().Root(root)
func Root(root any) *Tree {
t := New()
return t.Root(root)
}

// Root sets the root value of this tree.
func (t *Tree) Root(root string) *Tree {
t.value = root
func (t *Tree) Root(root any) *Tree {
// root is a tree or string
switch item := root.(type) {
case *Tree:
t.value = item.value
t = t.Child(item.children)
case Node, fmt.Stringer:
t.value = item.(fmt.Stringer).String()
case string, nil:
t.value = item.(string)
default:
t.value = fmt.Sprintf("%v", item)
}
return t
}

Expand Down
4 changes: 2 additions & 2 deletions internal/tree/tree_test.go → tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/aymanbagabas/go-udiff"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/internal/tree"
"github.com/charmbracelet/lipgloss/list"
"github.com/charmbracelet/lipgloss/table"
"github.com/charmbracelet/lipgloss/tree"
)

func TestTree(t *testing.T) {
Expand Down Expand Up @@ -607,7 +607,7 @@ func TestMultilinePrefixSubtree(t *testing.T) {
├── Foo
├── Bar
├── Baz
│ Foo Document
│ Foo Document
│ The Foo Files
│ │ Bar Document
Expand Down
Loading