Skip to content

Commit feb42a9

Browse files
caarlos0bashbunnimeowgorithm
authored
feat: move tree to root (#342)
* feat: support any type for list Root func Signed-off-by: Carlos Alexandro Becker <[email protected]> * feat: move tree to root Signed-off-by: Carlos Alexandro Becker <[email protected]> * feat: more cleanup Signed-off-by: Carlos Alexandro Becker <[email protected]> * docs(examples): bring back tree examples * docs(examples): add tree toggle example * docs(examples): fix toggle tree hierarchy; as bashbunni metadata * fix(tree): stylize whitespace when vertically joining tree elements * docs: get started with trees * feat: add RootStyle with example * test: add test for RootStyle func * docs(godoc): replace list mentions in tree package * refactor(examples): use Root shorthand instead of tree.New where applicable --------- Signed-off-by: Carlos Alexandro Becker <[email protected]> Co-authored-by: bashbunni <[email protected]> Co-authored-by: Christian Rocha <[email protected]>
1 parent 0618c73 commit feb42a9

File tree

21 files changed

+438
-59
lines changed

21 files changed

+438
-59
lines changed

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,100 @@ for i := 0; i < repeat; i++ {
562562
}
563563
```
564564

565+
## Rendering Trees
566+
567+
Lip Gloss ships with a tree rendering sub-package.
568+
569+
```go
570+
import "github.com/charmbracelet/lipgloss/tree"
571+
```
572+
573+
Define a new tree.
574+
575+
```go
576+
t := tree.Root(".").
577+
Child("A", "B", "C")
578+
```
579+
580+
Print the tree.
581+
582+
```go
583+
fmt.Println(t)
584+
585+
// .
586+
// ├── A
587+
// ├── B
588+
// └── C
589+
```
590+
591+
Trees have the ability to nest.
592+
593+
```go
594+
t := tree.Root(".").
595+
Child("Item 1").
596+
Child(
597+
tree.Root("Item 2").
598+
Child("Item 2.1").
599+
Child("Item 2.2").
600+
Child("Item 2.3"),
601+
).
602+
Child(
603+
tree.Root("Item 3").
604+
Child("Item 3.1").
605+
Child("Item 3.2"),
606+
)
607+
```
608+
609+
Print the tree.
610+
611+
```go
612+
fmt.Println(t)
613+
```
614+
615+
<p align="center">
616+
<img width="400" alt="Tree Example (simple)" src="https://stuff.charm.sh/lipgloss/tree-simple.png">
617+
</p>
618+
619+
Trees can be customized via their enumeration function as well as using
620+
`lipgloss.Style`s.
621+
622+
```go
623+
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("99")).MarginRight(1)
624+
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("212")).MarginRight(1)
625+
626+
t := tree.
627+
Root("Makeup").
628+
Child(
629+
"Glossier",
630+
"Claire’s Boutique",
631+
"Nyx",
632+
"Mac",
633+
"Milk",
634+
).
635+
Enumerator(tree.RoundedEnumerator).
636+
EnumeratorStyle(enumeratorStyle).
637+
ItemStyle(itemStyle)
638+
639+
```
640+
641+
Print the tree.
642+
643+
<p align="center">
644+
<img width="600" alt="Tree Example (makeup)" src="https://stuff.charm.sh/lipgloss/tree/tree-makeup.png">
645+
</p>
646+
647+
The predefined enumerators for trees are `DefaultEnumerator` and `RoundedEnumerator`.
648+
649+
If you need, you can also build trees incrementally:
650+
651+
```go
652+
t := tree.New()
653+
654+
for i := 0; i < repeat; i++ {
655+
t.Child("Lip Gloss")
656+
}
657+
```
658+
565659
---
566660

567661
## FAQ

examples/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/charmbracelet/bubbletea v0.25.0 // indirect
2323
github.com/charmbracelet/keygen v0.5.0 // indirect
2424
github.com/charmbracelet/log v0.4.0 // indirect
25-
github.com/charmbracelet/x/ansi v0.1.3 // indirect
25+
github.com/charmbracelet/x/ansi v0.1.4 // indirect
2626
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651 // indirect
2727
github.com/charmbracelet/x/exp/term v0.0.0-20240328150354-ab9afc214dfd // indirect
2828
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect

examples/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ github.com/charmbracelet/ssh v0.0.0-20240401141849-854cddfa2917 h1:NZKjJ7d/pzk/A
1313
github.com/charmbracelet/ssh v0.0.0-20240401141849-854cddfa2917/go.mod h1:8/Ve8iGRRIGFM1kepYfRF2pEOF5Y3TEZYoJaA54228U=
1414
github.com/charmbracelet/wish v1.4.0 h1:pL1uVP/YuYgJheHEj98teZ/n6pMYnmlZq/fcHvomrfc=
1515
github.com/charmbracelet/wish v1.4.0/go.mod h1:ew4/MjJVfW/akEO9KmrQHQv1F7bQRGscRMrA+KtovTk=
16-
github.com/charmbracelet/x/ansi v0.1.3 h1:RBh/eleNWML5R524mjUF0yVRePTwqN9tPtV+DPgO5Lw=
17-
github.com/charmbracelet/x/ansi v0.1.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
16+
github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=
17+
github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
1818
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651 h1:3RXpZWGWTOeVXCTv0Dnzxdv/MhNUkBfEcbaTY0zrTQI=
1919
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651/go.mod h1:2P0UgXMEa6TsToMSuFqKFQR+fZTO9CNGUNokkPatT/0=
2020
github.com/charmbracelet/x/exp/term v0.0.0-20240328150354-ab9afc214dfd h1:HqBjkSFXXfW4IgX3TMKipWoPEN08T3Pi4SA/3DLss/U=

examples/tree/background/main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/charmbracelet/lipgloss"
7+
"github.com/charmbracelet/lipgloss/tree"
8+
)
9+
10+
func main() {
11+
enumeratorStyle := lipgloss.NewStyle().
12+
Background(lipgloss.Color("0")).
13+
Padding(0, 1)
14+
15+
headerItemStyle := lipgloss.NewStyle().
16+
Background(lipgloss.Color("#ee6ff8")).
17+
Foreground(lipgloss.Color("#ecfe65")).
18+
Bold(true).
19+
Padding(0, 1)
20+
21+
itemStyle := headerItemStyle.Background(lipgloss.Color("0"))
22+
23+
t := tree.Root("# Table of Contents").
24+
RootStyle(itemStyle).
25+
ItemStyle(itemStyle).
26+
EnumeratorStyle(enumeratorStyle).
27+
Child(
28+
tree.Root("## Chapter 1").
29+
Child("Chapter 1.1").
30+
Child("Chapter 1.2"),
31+
).
32+
Child(
33+
tree.Root("## Chapter 2").
34+
Child("Chapter 2.1").
35+
Child("Chapter 2.2"),
36+
)
37+
38+
fmt.Println(t)
39+
}

examples/tree/files/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/charmbracelet/lipgloss"
9+
"github.com/charmbracelet/lipgloss/tree"
10+
)
11+
12+
func main() {
13+
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("240")).PaddingRight(1)
14+
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("99")).Bold(true).PaddingRight(1)
15+
16+
t := tree.Root(".").EnumeratorStyle(enumeratorStyle).ItemStyle(itemStyle)
17+
_ = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
18+
if err != nil {
19+
return err
20+
}
21+
if info.IsDir() {
22+
t.Child(tree.Root(path))
23+
}
24+
return nil
25+
})
26+
27+
fmt.Println(t)
28+
}

examples/tree/makeup/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/charmbracelet/lipgloss"
7+
"github.com/charmbracelet/lipgloss/tree"
8+
)
9+
10+
func main() {
11+
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("99")).MarginRight(1)
12+
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("212")).MarginRight(1)
13+
14+
t := tree.
15+
Root("Makeup").
16+
Child(
17+
"Glossier",
18+
"Claire’s Boutique",
19+
"Nyx",
20+
"Mac",
21+
"Milk",
22+
).
23+
Enumerator(tree.RoundedEnumerator).
24+
EnumeratorStyle(enumeratorStyle).
25+
ItemStyle(itemStyle).
26+
RootStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("#04B575")))
27+
28+
fmt.Println(t)
29+
}

examples/tree/rounded/main.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/charmbracelet/lipgloss"
7+
"github.com/charmbracelet/lipgloss/tree"
8+
)
9+
10+
func main() {
11+
itemStyle := lipgloss.NewStyle().MarginRight(1)
12+
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("8")).MarginRight(1)
13+
14+
t := tree.Root("Groceries").
15+
Child(
16+
tree.Root("Fruits").
17+
Child(
18+
"Blood Orange",
19+
"Papaya",
20+
"Dragonfruit",
21+
"Yuzu",
22+
),
23+
tree.Root("Items").
24+
Child(
25+
"Cat Food",
26+
"Nutella",
27+
"Powdered Sugar",
28+
),
29+
tree.Root("Veggies").
30+
Child(
31+
"Leek",
32+
"Artichoke",
33+
),
34+
).ItemStyle(itemStyle).EnumeratorStyle(enumeratorStyle).Enumerator(tree.RoundedEnumerator)
35+
36+
fmt.Println(t)
37+
}

examples/tree/simple/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/charmbracelet/lipgloss/tree"
7+
)
8+
9+
func main() {
10+
t := tree.Root(".").
11+
Child("Item 1").
12+
Child(
13+
tree.New().
14+
Root("Item 2").
15+
Child("Item 2.1").
16+
Child("Item 2.2").
17+
Child("Item 2.3"),
18+
).
19+
Child(
20+
tree.New().
21+
Root("Item 3").
22+
Child("Item 3.1").
23+
Child("Item 3.2"),
24+
)
25+
26+
fmt.Println(t)
27+
}

examples/tree/styles/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/charmbracelet/lipgloss"
7+
"github.com/charmbracelet/lipgloss/tree"
8+
)
9+
10+
func main() {
11+
purple := lipgloss.NewStyle().Foreground(lipgloss.Color("99")).MarginRight(1)
12+
pink := lipgloss.NewStyle().Foreground(lipgloss.Color("212")).MarginRight(1)
13+
14+
t := tree.New().
15+
Child(
16+
"Glossier",
17+
"Claire’s Boutique",
18+
tree.Root("Nyx").
19+
Child("Lip Gloss", "Foundation").
20+
EnumeratorStyle(pink),
21+
"Mac",
22+
"Milk",
23+
).
24+
EnumeratorStyle(purple)
25+
fmt.Println(t)
26+
}

0 commit comments

Comments
 (0)