Skip to content

Commit

Permalink
fix: sync API with bubble
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr committed Oct 19, 2024
1 parent e51a41d commit 88406ff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
49 changes: 30 additions & 19 deletions examples/tree-file-system/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

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

func (m model) Init() tea.Cmd {
Expand All @@ -27,7 +27,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyPressMsg:
switch msg.String() {
case "e":
m.choice = m.tree.ItemAtCurrentOffset()
m.choice = m.tree.NodeAtCurrentOffset()
return m, tea.Quit
case "q", "ctrl+c":
return m, tea.Quit
Expand All @@ -39,24 +39,35 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd
}

func (m *model) updateStyles() {
m.tree.SetStyles(tree.Styles{ItemStyleFunc: func(children tree.Items, i int) lipgloss.Style {
child := children.At(i)
w := width - enumeratorWidth*child.Depth()
if strings.HasPrefix(child.Value(), m.tree.OpenCharacter) {
w -= lipgloss.Width(m.tree.OpenCharacter)
} else if strings.HasPrefix(child.Value(), m.tree.ClosedCharacter) {
w -= lipgloss.Width(m.tree.ClosedCharacter)
} else {
w -= lipgloss.Width("⌯ ")
}

if child.YOffset() == m.tree.YOffset() {
return lipgloss.NewStyle().Width(w).Background(lipgloss.Color("8"))
}
func (m *model) childWidth(child *tree.Node) int {
w := width - enumeratorWidth*child.Depth()
if strings.HasPrefix(child.Value(), m.tree.OpenCharacter) {
w -= lipgloss.Width(m.tree.OpenCharacter)
} else if strings.HasPrefix(child.Value(), m.tree.ClosedCharacter) {
w -= lipgloss.Width(m.tree.ClosedCharacter)
} else {
w -= lipgloss.Width("⌯ ")
}
return w
}

return lipgloss.NewStyle().Width(w).Background(lipgloss.Color("234"))
}})
func (m *model) updateStyles() {
m.tree.SetStyles(tree.Styles{
TreeStyle: lipgloss.NewStyle().Padding(1).Background(lipgloss.Color("234")),
NodeStyleFunc: func(children tree.Nodes, i int) lipgloss.Style {
child := children.At(i)
w := m.childWidth(child)

return lipgloss.NewStyle().Width(w).Background(lipgloss.Color("234"))
},
SelectedNodeStyleFunc: func(children tree.Nodes, i int) lipgloss.Style {
child := children.At(i)
w := m.childWidth(child)

return lipgloss.NewStyle().Bold(true).Width(w).Background(lipgloss.Color("8"))
},
HelpStyle: lipgloss.NewStyle().MarginTop(1),
})
}

func (m model) View() string {
Expand Down
4 changes: 2 additions & 2 deletions examples/tree-simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func main() {
t.SetShowHelp(false)
t.SetStyles(tree.Styles{
TreeStyle: lipgloss.NewStyle().Background(lipgloss.Color("205")),
ItemStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("0")).Background(lipgloss.Color("205")),
SelectedItemStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("54")).Background(lipgloss.Color("205")).Bold(true).Underline(true),
NodeStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("0")).Background(lipgloss.Color("205")),
SelectedNodeStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("54")).Background(lipgloss.Color("205")).Bold(true).Underline(true),
})

if _, err := tea.NewProgram(model{tree: t, s: s}).Run(); err != nil {
Expand Down

0 comments on commit 88406ff

Please sign in to comment.