Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ require (
golang.org/x/sys v0.38.0 // indirect
golang.org/x/text v0.24.0 // indirect
)

replace charm.land/lipgloss/v2 v2.0.0-beta.3.0.20251114160003-3248589b24c9 => ../../lipgloss

replace charm.land/bubbles/v2 v2.0.0-beta.1.0.20251110211018-84a82dfeeed8 => ../../bubbles
4 changes: 0 additions & 4 deletions examples/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
charm.land/bubbles/v2 v2.0.0-beta.1.0.20251110211018-84a82dfeeed8 h1:2bzaNBvZHgpLjxlQTDjoeY0YGjQtMflWiDZMEbviiRE=
charm.land/bubbles/v2 v2.0.0-beta.1.0.20251110211018-84a82dfeeed8/go.mod h1:5AbN6cEd/47gkEf8TgiQ2O3RZ5QxMS14l9W+7F9fPC4=
charm.land/lipgloss/v2 v2.0.0-beta.3.0.20251114160003-3248589b24c9 h1:FSmPSuQzHfyzens1NukU5wP76ttNcEa8MZQIZM77RbQ=
charm.land/lipgloss/v2 v2.0.0-beta.3.0.20251114160003-3248589b24c9/go.mod h1:1qZyvvVCenJO2M1ac2mX0yyiIZJoZmDM4DG4s0udJkU=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/alecthomas/assert/v2 v2.7.0 h1:QtqSACNS3tF7oasA8CU6A6sXZSBDqnm7RfpLl9bZqbE=
Expand Down
62 changes: 62 additions & 0 deletions examples/tree-default/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"fmt"
"os"

"charm.land/bubbles/v2/tree"
tea "charm.land/bubbletea/v2"
)

type model struct {
tree tree.Model
}

func (m model) Init() tea.Cmd {
return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyPressMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
}
}
m.tree, cmd = m.tree.Update(msg)
return m, cmd
}

func (m model) View() tea.View {
return tea.NewView(m.tree.View())
}

func main() {
t := tree.New(tree.Root("~/charm").
Child(
"ayman",
tree.Root("bash").
Child(
tree.Root("tools").
Child("zsh",
"doom-emacs",
),
),
tree.Root("carlos").
Child(
tree.Root("emotes").
Child(
"chefkiss.png",
"kekw.png",
),
),
"maas",
), 70, 13)

if _, err := tea.NewProgram(model{tree: t}).Run(); err != nil {
fmt.Println("Oh no:", err)
os.Exit(1)
}
}
142 changes: 142 additions & 0 deletions examples/tree-file-system/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package main

import (
"fmt"
"os"

"charm.land/bubbles/v2/key"
"charm.land/bubbles/v2/tree"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
ltree "charm.land/lipgloss/v2/tree"
"github.com/charmbracelet/x/ansi"
)

type model struct {
tree tree.Model
choice *tree.Node
}

func (m model) Init() tea.Cmd {
return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyPressMsg:
switch msg.String() {
case "e":
m.choice = m.tree.NodeAtCurrentOffset()
return m, tea.Quit
case "q", "ctrl+c":
return m, tea.Quit
}
}
m.tree, cmd = m.tree.Update(msg)
m.updateStyles()

return m, cmd
}

func (m *model) updateStyles() {
dimmed := lipgloss.Color("239")
base := lipgloss.NewStyle()
m.tree.SetStyles(tree.Styles{
TreeStyle: base.
Padding(1).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("236")).
BorderBackground(base.GetBackground()),
RootNodeStyle: base,
NodeStyle: base,
ParentNodeStyle: base,
OpenIndicatorStyle: base,
SelectedNodeStyle: base.Bold(true).Background(lipgloss.Color("8")),
HelpStyle: base.MarginTop(1),
EnumeratorStyle: base.Foreground(dimmed),
IndenterStyle: base.Foreground(dimmed),
})
}

func (m model) View() tea.View {
return tea.NewView(m.tree.View())
}

type file struct {
name string
color string
}

func (f file) String() string {
return "⌯ " + lipgloss.NewStyle().Foreground(lipgloss.Color(f.color)).Render(f.name)
}

type dir struct {
name string
}

func (d dir) String() string {
return lipgloss.NewStyle().Foreground(lipgloss.Color("4")).Render(d.name)
}

const (
width = 50
height = 21
enumeratorWidth = 3
)

func main() {
t := tree.New(
tree.Root(dir{"charmbracelet/lipgloss"}).
Indenter(func(_ ltree.Children, _ int) string {
return "│ "
}).
Enumerator(func(_ ltree.Children, _ int) string {
return "│ "
}).
Child(
tree.Root(dir{"tree"}).
Child(file{"tree.go", "6"}).
Child(file{"renderer.go", "6"}),
).
Child(
tree.Root(dir{"table"}).
Child(
tree.Root(dir{"utils"}).
Child(file{"utils.go", "6"}),
),
).
Child(tree.Root(dir{"list"}).Child(lipgloss.NewStyle().Faint(true).Render("(empty)"))).
Child(file{"README.md", "3"}).
Child(file{"go.mod", "255"}).
Child(file{"go.sum", "255"}).
Child(file{".gitignore", "255"}),
width,
height,
)
t.SetCursorCharacter("")
t.SetOpenCharacter("📂")
t.SetClosedCharacter("📁")
kb := []key.Binding{
key.NewBinding(key.WithKeys("e"), key.WithHelp("e", "select")),
}
t.SetAdditionalShortHelpKeys(func() []key.Binding {
return kb
})
t.SetAdditionalFullHelpKeys(func() []key.Binding {
return kb
})

p := tea.NewProgram(model{tree: t})
m, err := p.Run()
if err != nil {
fmt.Println("Oh no:", err)
os.Exit(1)
}

// Assert the final tea.Model to our local model and print the choice.
if m, ok := m.(model); ok && m.choice != nil {
fmt.Printf("---\nYou chose %s!\n", ansi.Strip(m.choice.Value()))
}
}
64 changes: 64 additions & 0 deletions examples/tree-long/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"fmt"
"os"
"time"

"charm.land/bubbles/v2/tree"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
)

type model struct {
tree tree.Model
}

func (m model) Init() tea.Cmd {
return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyPressMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
}
}
m.tree, cmd = m.tree.Update(msg)
return m, cmd
}

func (m model) View() tea.View {
return tea.NewView(m.tree.View())
}

func main() {
root := tree.Root("🛂 Passport expiration date")
thisYear := time.Now().Year()
for year := thisYear; year < thisYear+10; year++ {
yRoot := tree.Root(fmt.Sprintf("%d", year)).Close()
for month := 1; month <= 12; month++ {
mRoot := tree.Root(time.Month(month).String()).Close().RootStyle(
lipgloss.NewStyle().Foreground(lipgloss.Color("1")))
for day := 1; day < daysIn(time.Month(month), year); day++ {
mRoot.Child(fmt.Sprintf("%d", day))
}
yRoot.Child(mRoot)
}
root.Child(yRoot)
}

t := tree.New(root, 80, 30)

if _, err := tea.NewProgram(model{tree: t}).Run(); err != nil {
fmt.Println("Oh no:", err)
os.Exit(1)
}
}

func daysIn(m time.Month, year int) int {
return time.Date(year, m+1, 0, 0, 0, 0, 0, time.UTC).Day()
}
Loading
Loading