Rendering Trees
Render hierarchical trees in Lip Gloss for structured terminal output.
Render Hierarchical Trees
Lip Gloss’s tree rendering sub-package lets you create hierarchical, styled trees for your terminal apps, ideal for displaying nested data like file structures or menus.
Customize trees with colors and formatting for a polished look.
Tree Rendering Options
Lip Gloss provides tools to create and style trees:
Define a simple tree:
import "github.com/charmbracelet/lipgloss/tree"
t := tree.Root(".").Child("A", "B", "C")
fmt.Println(t)
// output will be:
// .
// ├── A
// ├── B
// └── CCreate nested trees for complex hierarchies:
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"),
)
fmt.Println(t)Customize trees with styles and enumerators:
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)Build trees incrementally:
t := tree.New()
for i := 0; i < repeat; i++ {
t.Child("Lip Gloss")
}How It Works
Create and render trees in your terminal apps:
Import the Tree Package
Use github.com/charmbracelet/lipgloss/tree to access tree rendering tools.
Define Tree Structure
Create a tree with tree.Root() and add children or nested trees:
t := tree.Root(".").Child("A", "B", "C")Style and Render
Apply styles or enumerators, then render with fmt.Println(t).
Test tree rendering in your target terminal to ensure enumerators and styles display correctly.
How is this guide?