Woodn’t you know, Lip Gloss has trees!
Lip Gloss ships with a tree rendering sub-package.
import "github.com/charmbracelet/lipgloss/tree"
Define a new tree.
t := tree.Root(".").
Child("A", "B", "C")
Print the tree.
fmt.Println(t)
// .
// ├── A
// ├── B
// └── C
Trees have the ability to nest.
t := tree.Root(".").
Child("macOS").
Child(
tree.New().
Root("Linux").
Child("NixOS").
Child("Arch Linux (btw)").
Child("Void Linux"),
).
Child(
tree.New().
Root("BSD").
Child("FreeBSD").
Child("OpenBSD"),
)
Print the tree.
fmt.Println(t)
Trees can be customized via their enumeration function as well as using
lipgloss.Style
s.
enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("63")).MarginRight(1)
rootStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("35"))
itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("212"))
t := tree.
Root("⁜ Makeup").
Child(
"Glossier",
"Fenty Beauty",
tree.New().Child(
"Gloss Bomb Universal Lip Luminizer",
"Hot Cheeks Velour Blushlighter",
),
"Nyx",
"Mac",
"Milk",
).
Enumerator(tree.RoundedEnumerator).
EnumeratorStyle(enumeratorStyle).
RootStyle(rootStyle).
ItemStyle(itemStyle)
Print the tree.
The predefined enumerators for trees are DefaultEnumerator
and RoundedEnumerator
.
If you need, you can also build trees incrementally:
t := tree.New()
for i := 0; i < repeat; i++ {
t.Child("Lip Gloss")
}
There’s more where that came from
Changelog
New Features
- 0618c73: feat(test): add test for
JoinHorizontal
(#346) (@aditipatelpro) - feb42a9: feat: move tree to root (#342) (@caarlos0)
Bug fixes
- 8a0e640: fix: remove unnecessary if (@aymanbagabas)
Documentation updates
- bc0de5c: docs(README): make tree example match output (@bashbunni)
- bb3e339: docs(README): match tree example alignment with list examples (@bashbunni)
- 185fde3: docs(README): update tree images (@bashbunni)
- ed7f56e: docs: fix
CompleteColor
example (#345) (@bashbunni) - cf0a7c6: docs: fix tree screenshot (@caarlos0)
Thoughts? Questions? We love hearing from you. Feel free to reach out on Twitter, The Fediverse, or on Discord.