Skip to content
Open
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
6 changes: 5 additions & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/mattn/go-runewidth v0.0.21 // indirect
github.com/mattn/go-runewidth v0.0.20 // indirect
github.com/microcosm-cc/bluemonday v1.0.27 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
Expand All @@ -46,3 +46,7 @@ require (
golang.org/x/sys v0.42.0 // indirect
golang.org/x/text v0.24.0 // indirect
)

replace charm.land/lipgloss/v2 v2.0.0 => ../../lipgloss

replace charm.land/bubbles/v2 v2.1.0 => ../../bubbles
6 changes: 2 additions & 4 deletions examples/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
charm.land/bubbles/v2 v2.1.0 h1:YSnNh5cPYlYjPxRrzs5VEn3vwhtEn3jVGRBT3M7/I0g=
charm.land/bubbles/v2 v2.1.0/go.mod h1:l97h4hym2hvWBVfmJDtrEHHCtkIKeTEb3TTJ4ZOB3wY=
charm.land/glamour/v2 v2.0.0 h1:IDBoqLEy7Hdpb9VOXN+khLP/XSxtJy1VsHuW/yF87+U=
charm.land/glamour/v2 v2.0.0/go.mod h1:kjq9WB0s8vuUYZNYey2jp4Lgd9f4cKdzAw88FZtpj/w=
charm.land/lipgloss/v2 v2.0.2 h1:xFolbF8JdpNkM2cEPTfXEcW1p6NRzOWTSamRfYEw8cs=
Expand Down Expand Up @@ -58,8 +56,8 @@ github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQ
github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.21 h1:jJKAZiQH+2mIinzCJIaIG9Be1+0NR+5sz/lYEEjdM8w=
github.com/mattn/go-runewidth v0.0.21/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
github.com/mattn/go-runewidth v0.0.20 h1:WcT52H91ZUAwy8+HUkdM3THM6gXqXuLJi9O3rjcQQaQ=
github.com/mattn/go-runewidth v0.0.20/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk=
github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
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