Skip to content

Commit

Permalink
feat(tree): accept root name in New() constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed May 22, 2024
1 parent 9f80573 commit 3e553c6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (n *List) Enumerator(enum Enumerator) *List {
// New returns a new list with a bullet enumerator.
func New(items ...any) *List {
l := &List{
inner: tree.New(),
inner: tree.New(""),
}
return l.Items(items...).Enumerator(Bullet)
}
20 changes: 9 additions & 11 deletions tree/tree.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// Package tree provides an API to create printable tree-like structures that
// can be included in any command line application. It goes something like:
//
// t := tree.New().
// Root(".").
// t := tree.New(".").
// Item("Item 1").
// Item(
// tree.New().Root("Item 2").
// tree.New("Item 2").
// Item("Item 2.1").
// Item("Item 2.2").
// Item("Item 2.3"),
// ).
// Item(
// tree.New().
// Root("Item 3").
// tree.New("Item 3").
// Item("Item 3.1").
// Item("Item 3.2"),
// )
Expand Down Expand Up @@ -112,8 +110,8 @@ func (n *Tree) String() string {
//
// Both of these should result in the same thing:
//
// New().Root("foo").Items("bar", New().Item("zaz"), "qux")
// New().Root("foo").Items(New().Root("bar").Item("zaz"), "qux")
// New("foo").Items("bar", New().Item("zaz"), "qux")
// New("foo").Items(New("bar").Item("zaz"), "qux")
//
// The resulting tree would be:
//
Expand Down Expand Up @@ -165,8 +163,7 @@ func (n *Tree) Item(item any) *Tree {

// Items add multiple items to the tree.
//
// t := tree.New().
// Root("Nyx").
// t := tree.New("Nyx").
// Items("Qux", "Quux").
func (n *Tree) Items(items ...any) *Tree {
for _, item := range items {
Expand Down Expand Up @@ -295,9 +292,10 @@ func (n *Tree) Root(root string) *Tree {
return n
}

// New returns a new tree.
func New() *Tree {
// New returns a new tree, with the argument being the name at the root.
func New(root string) *Tree {
return &Tree{
name: root,
children: nodeData(nil),
}
}

0 comments on commit 3e553c6

Please sign in to comment.