Skip to content

Commit

Permalink
Drop Tree (#330)
Browse files Browse the repository at this point in the history
* fix: move tree to internal

* feat: tree indenter on lists

* fix: sublist example

* fix: remove tree
  • Loading branch information
maaslalani authored Jul 11, 2024
1 parent 9564423 commit 2a67670
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 187 deletions.
2 changes: 0 additions & 2 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ go 1.19

replace github.com/charmbracelet/lipgloss => ../

replace github.com/charmbracelet/lipgloss/tree => ../tree

replace github.com/charmbracelet/lipgloss/list => ../list

require (
Expand Down
29 changes: 14 additions & 15 deletions examples/list/sublist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/list"
"github.com/charmbracelet/lipgloss/table"
"github.com/charmbracelet/lipgloss/tree"
"github.com/lucasb-eyer/go-colorful"
)

Expand Down Expand Up @@ -119,16 +118,16 @@ func main() {
Item("Lip Gloss").
Item("Lip Gloss").
Item(
tree.New().
list.New().
EnumeratorStyle(lipgloss.NewStyle().Foreground(lipgloss.Color(colors[4][0])).MarginRight(1)).
Child("\nStyle Definitions for Nice Terminal Layouts\n─────").
Child("From Charm").
Child("https://github.com/charmbracelet/lipgloss").
Child(
tree.New().
Item("\nStyle Definitions for Nice Terminal Layouts\n─────").
Item("From Charm").
Item("https://github.com/charmbracelet/lipgloss").
Item(
list.New().
EnumeratorStyle(lipgloss.NewStyle().Foreground(lipgloss.Color(colors[3][0])).MarginRight(1)).
Child("Emperors: Julio-Claudian dynasty").
Child(
Item("Emperors: Julio-Claudian dynasty").
Item(
lipgloss.NewStyle().Padding(1).Render(
list.New(
"Augustus",
Expand All @@ -139,7 +138,7 @@ func main() {
).Enumerator(list.Roman).String(),
),
).
Child(
Item(
lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FAFAFA")).
Expand All @@ -151,7 +150,7 @@ func main() {
Width(40).
Render(history),
).
Child(
Item(
table.New().
Width(30).
BorderStyle(purple.MarginRight(0)).
Expand All @@ -174,8 +173,8 @@ func main() {
Row("Orange", "2").
Row("Strawberry", "12"),
).
Child("Documents").
Child(
Item("Documents").
Item(
list.New().
Enumerator(func(_ list.Items, i int) string {
if i == 1 {
Expand All @@ -200,9 +199,9 @@ func main() {
Item("Baz Document\n" + faint.Render("10 minutes ago")).
Item("Qux Document\n" + faint.Render("1 month ago")),
).
Child("EOF"),
Item("EOF"),
).
Child("go get github.com/charmbracelet/lipgloss/list\n"),
Item("go get github.com/charmbracelet/lipgloss/list\n"),
).
Item("See ya later"),
),
Expand Down
41 changes: 0 additions & 41 deletions examples/tree/background/main.go

This file was deleted.

28 changes: 0 additions & 28 deletions examples/tree/files/main.go

This file was deleted.

41 changes: 0 additions & 41 deletions examples/tree/rounded/main.go

This file was deleted.

28 changes: 0 additions & 28 deletions examples/tree/simple/main.go

This file was deleted.

27 changes: 0 additions & 27 deletions examples/tree/styles/main.go

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tree/tree_test.go β†’ internal/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
46 changes: 46 additions & 0 deletions list/enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ import (
// Or, define your own.
type Enumerator func(items Items, index int) string

// Indenter indents the children of a tree.
//
// Indenters allow for displaying nested tree items with connecting borders
// to sibling nodes.
//
// For example, the default indenter would be:
//
// func TreeIndenter(children Children, index int) string {
// if children.Length()-1 == index {
// return "β”‚ "
// }
//
// return " "
// }
type Indenter func(items Items, index int) string

// Alphabet is the enumeration for alphabetical listing.
//
// a. Foo
Expand Down Expand Up @@ -104,3 +120,33 @@ 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 "β”‚ "
}
37 changes: 34 additions & 3 deletions 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/tree"
"github.com/charmbracelet/lipgloss/internal/tree"
)

// List represents a list of items that can be displayed. Lists can contain
Expand All @@ -49,7 +49,9 @@ type List struct{ tree *tree.Tree }
// anything you want, really.
func New(items ...any) *List {
l := &List{tree: tree.New()}
return l.Items(items...).Enumerator(Bullet)
return l.Items(items...).
Enumerator(Bullet).
Indenter(func(Items, int) string { return " " })
}

// Items represents the list items.
Expand Down Expand Up @@ -145,6 +147,36 @@ func (l *List) EnumeratorStyleFunc(f StyleFunc) *List {
return l
}

// Indenter sets the indenter implementation. This is used to change the way
// the tree is indented. The default indentor places a border connecting sibling
// elements and no border for the last child.
//
// └── Foo
// └── Bar
// └── Baz
// └── Qux
// └── Quux
//
// You can define your own indenter.
//
// func ArrowIndenter(children tree.Children, index int) string {
// return "β†’ "
// }
//
// β†’ Foo
// β†’ β†’ Bar
// β†’ β†’ β†’ Baz
// β†’ β†’ β†’ β†’ Qux
// β†’ β†’ β†’ β†’ β†’ Quux
func (l *List) Indenter(indenter Indenter) *List {
l.tree.Indenter(
func(children tree.Children, index int) string {
return indenter(children, index)
},
)
return l
}

// ItemStyle sets the item style for all items.
//
// To set the item style conditionally based on the item value or index,
Expand Down Expand Up @@ -225,6 +257,5 @@ func (l *List) Items(items ...any) *List {
// Baz. Baz
func (l *List) Enumerator(enumerator Enumerator) *List {
l.tree.Enumerator(func(c tree.Children, i int) string { return enumerator(c, i) })
l.tree.Indenter(func(tree.Children, int) string { return " " })
return l
}
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

0 comments on commit 2a67670

Please sign in to comment.