Drop Tree (#330)

* fix: move tree to internal

* feat: tree indenter on lists

* fix: sublist example

* fix: remove tree
This commit is contained in:
Maas Lalani 2024-07-11 17:23:29 -04:00 committed by GitHub
parent 95644230eb
commit 2a6767080f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 96 additions and 187 deletions

View File

@ -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 (

View File

@ -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"
)
@ -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",
@ -139,7 +138,7 @@ func main() {
).Enumerator(list.Roman).String(),
),
).
Child(
Item(
lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FAFAFA")).
@ -151,7 +150,7 @@ func main() {
Width(40).
Render(history),
).
Child(
Item(
table.New().
Width(30).
BorderStyle(purple.MarginRight(0)).
@ -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 {
@ -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"),
),

View File

@ -1,41 +0,0 @@
package main
import (
"fmt"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/tree"
)
func main() {
enumeratorStyle := lipgloss.NewStyle().
Background(lipgloss.Color("0")).
Padding(0, 1)
headerItemStyle := lipgloss.NewStyle().
Background(lipgloss.Color("#ee6ff8")).
Foreground(lipgloss.Color("#ecfe65")).
Bold(true).
Padding(0, 1)
itemStyle := headerItemStyle.Background(lipgloss.Color("0"))
t := tree.New().
ItemStyle(itemStyle).
EnumeratorStyle(enumeratorStyle).
Root("# Table of Contents").
Child(
tree.New().
Root("## Chapter 1").
Child("Chapter 1.1").
Child("Chapter 1.2"),
).
Child(
tree.New().
Root("## Chapter 2").
Child("Chapter 2.1").
Child("Chapter 2.2"),
)
fmt.Println(t)
}

View File

@ -1,28 +0,0 @@
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/tree"
)
func main() {
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("240")).PaddingRight(1)
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("99")).Bold(true).PaddingRight(1)
t := tree.New().Root(".").EnumeratorStyle(enumeratorStyle).ItemStyle(itemStyle)
_ = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
t.Child(tree.New().Root(path))
}
return nil
})
fmt.Println(t)
}

View File

@ -1,41 +0,0 @@
package main
import (
"fmt"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/tree"
)
func main() {
itemStyle := lipgloss.NewStyle().MarginRight(1)
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("8")).MarginRight(1)
t := tree.New().
Root("Groceries").
Child(
tree.New().
Root("Fruits").
Child(
"Blood Orange",
"Papaya",
"Dragonfruit",
"Yuzu",
),
tree.New().
Root("Items").
Child(
"Cat Food",
"Nutella",
"Powdered Sugar",
),
tree.New().
Root("Veggies").
Child(
"Leek",
"Artichoke",
),
).ItemStyle(itemStyle).EnumeratorStyle(enumeratorStyle).Enumerator(tree.RoundedEnumerator)
fmt.Println(t)
}

View File

@ -1,28 +0,0 @@
package main
import (
"fmt"
"github.com/charmbracelet/lipgloss/tree"
)
func main() {
t := tree.New().
Root(".").
Child("Item 1").
Child(
tree.New().
Root("Item 2").
Child("Item 2.1").
Child("Item 2.2").
Child("Item 2.3"),
).
Child(
tree.New().
Root("Item 3").
Child("Item 3.1").
Child("Item 3.2"),
)
fmt.Println(t)
}

View File

@ -1,27 +0,0 @@
package main
import (
"fmt"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/tree"
)
func main() {
purple := lipgloss.NewStyle().Foreground(lipgloss.Color("99")).MarginRight(1)
pink := lipgloss.NewStyle().Foreground(lipgloss.Color("212")).MarginRight(1)
t := tree.New().
Child(
"Glossier",
"Claires Boutique",
tree.New().
Root("Nyx").
Child("Lip Gloss", "Foundation").
EnumeratorStyle(pink),
"Mac",
"Milk",
).
EnumeratorStyle(purple)
fmt.Println(t)
}

View File

@ -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) {

View File

@ -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
@ -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 "│ "
}

View File

@ -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
@ -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.
@ -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,
@ -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
}

View File

@ -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