Fix Tree Browser

This commit is contained in:
2026-02-01 17:33:34 -06:00
parent 330a2e3ddf
commit ce67592dbb
2 changed files with 48 additions and 30 deletions

View File

@@ -2,6 +2,8 @@ package widgets
import (
"errors"
"fmt"
"strings"
h "git.bullercodeworks.com/brian/expds/helpers"
t "git.bullercodeworks.com/brian/tcell-widgets"
@@ -142,12 +144,12 @@ func (w *TreeBrowser) Draw(screen tcell.Screen) {
}
func (w *TreeBrowser) SetStyle(s tcell.Style) { w.style = s }
func (w *TreeBrowser) Active() bool { return false }
func (w *TreeBrowser) SetActive(a bool) {}
func (w *TreeBrowser) Active() bool { return w.active }
func (w *TreeBrowser) SetActive(a bool) { w.active = a }
func (w *TreeBrowser) Visible() bool { return w.visible }
func (w *TreeBrowser) SetVisible(a bool) { w.visible = a }
func (w *TreeBrowser) Focusable() bool { return false }
func (w *TreeBrowser) SetFocusable(b bool) {}
func (w *TreeBrowser) Focusable() bool { return w.focusable }
func (w *TreeBrowser) SetFocusable(b bool) { w.focusable = b }
func (w *TreeBrowser) SetX(x int) { w.SetPos(t.Coord{X: x, Y: w.y}) }
func (w *TreeBrowser) SetY(y int) { w.SetPos(t.Coord{X: w.x, Y: y}) }
func (w *TreeBrowser) GetX() int { return w.x }
@@ -269,18 +271,18 @@ func (w *TreeBrowser) Title() string { return w.title }
func (w *TreeBrowser) SetTitle(ttl string) { w.title = ttl }
func (w *TreeBrowser) SetTree(l []*TreeNode) {
w.nodes = l
w.updateList()
w.UpdateList()
}
func (w *TreeBrowser) Clear() {
w.nodes = []*TreeNode{}
w.updateList()
w.UpdateList()
}
func (w *TreeBrowser) Add(n *TreeNode) {
w.nodes = append(w.nodes, n)
w.updateList()
w.UpdateList()
}
func (w *TreeBrowser) updateList() {
func (w *TreeBrowser) UpdateList() {
w.list = []string{}
w.listNodes = []*TreeNode{}
for i := range w.nodes {
@@ -309,8 +311,25 @@ func NewTreeNode(l, v string) *TreeNode {
value: v,
}
}
func (tn *TreeNode) Depth() int {
if tn.parent == nil {
return 0
}
return tn.parent.Depth() + 1
}
func (tn *TreeNode) Label() string { return tn.label }
func (tn *TreeNode) Value() string { return tn.value }
func (tn *TreeNode) GetLabelPath() []string {
var path []string
if tn.parent != nil {
path = tn.parent.GetLabelPath()
}
return append([]string{tn.Label()}, path...)
}
func (tn *TreeNode) getList() []string {
ret := []string{tn.label}
pre := strings.Repeat("-", tn.Depth())
ret := []string{fmt.Sprintf("%s%s", pre, tn.label)}
if tn.expanded {
for i := range tn.children {
ret = append(ret, tn.children[i].getList()...)
@@ -338,3 +357,5 @@ func (tn *TreeNode) AddChild(t *TreeNode, rest ...*TreeNode) {
tn.children = append(tn.children, rest[i])
}
}
func (tn *TreeNode) HasChildren() bool { return len(tn.children) > 0 }