Things are working pretty well now

This commit is contained in:
2026-02-02 14:11:01 -06:00
parent f3c63fe95b
commit 486c07f2d4
9 changed files with 442 additions and 136 deletions

View File

@@ -29,6 +29,7 @@ type TreeBrowser struct {
cursor int
cursorWrap bool
nodes []*TreeNode
depthIndic string
onChange func(*TreeNode) bool
onSelect func(*TreeNode) bool
@@ -49,6 +50,7 @@ func NewTreeBrowser(id string, s tcell.Style) *TreeBrowser {
func (w *TreeBrowser) Init(id string, style tcell.Style) {
w.visible = true
w.focusable = true
w.depthIndic = "• "
w.keyMap = t.NewKeyMap(
t.NewKey(t.BuildEK(tcell.KeyUp), func(_ *tcell.EventKey) bool { return w.MoveUp() }),
t.NewKey(t.BuildEK(tcell.KeyDown), func(_ *tcell.EventKey) bool { return w.MoveDown() }),
@@ -134,33 +136,66 @@ func (w *TreeBrowser) Draw(screen tcell.Screen) {
}
x, y = x+1, y+1
h := w.h - brdSz
for i := range w.list {
th.DrawText(x, y, w.list[i], w.style.Reverse(i == w.cursor), screen)
y++
if y > x+h {
break
ln := len(w.list)
st, ed := 0, ln-1
if ln == 0 {
return
}
if ln > w.h-2 {
mid := h / 2
if w.cursor < mid {
// Start drawing at 0
ed = h
} else if w.cursor > ln-mid {
// Start at ln-h+1
st = ln - h
} else {
st = w.cursor - mid
ed = st + h
}
}
// ed cannot be higher than ln - 1
if st < 0 {
st = 0
}
if ed > ln-1 {
ed = ln - 1
}
for i := st; i <= ed; i++ {
rev := false
if i == w.cursor {
rev = true
}
txt := w.list[i]
if len(txt) > w.w-brdSz && w.w-brdSz >= 0 {
txt = txt[:(w.w - brdSz)]
}
wh.DrawText(x, y, txt, dS.Reverse(rev), screen)
y += 1
}
}
func (w *TreeBrowser) SetStyle(s tcell.Style) { w.style = s }
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 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 }
func (w *TreeBrowser) GetY() int { return w.y }
func (w *TreeBrowser) GetPos() t.Coord { return t.Coord{X: w.x, Y: w.y} }
func (w *TreeBrowser) SetPos(c t.Coord) { w.x, w.y = c.X, c.Y }
func (w *TreeBrowser) GetW() int { return w.w }
func (w *TreeBrowser) GetH() int { return w.h }
func (w *TreeBrowser) SetW(wd int) { w.SetSize(t.Coord{X: wd, Y: w.h}) }
func (w *TreeBrowser) SetH(h int) { w.SetSize(t.Coord{X: w.w, Y: h}) }
func (w *TreeBrowser) SetSize(c t.Coord) { w.w, w.h = c.X, c.Y }
func (w *TreeBrowser) SetActive(a bool) bool {
w.active = a
return w.active
}
func (w *TreeBrowser) Visible() bool { return w.visible }
func (w *TreeBrowser) SetVisible(a bool) { w.visible = a }
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 }
func (w *TreeBrowser) GetY() int { return w.y }
func (w *TreeBrowser) GetPos() t.Coord { return t.Coord{X: w.x, Y: w.y} }
func (w *TreeBrowser) SetPos(c t.Coord) { w.x, w.y = c.X, c.Y }
func (w *TreeBrowser) GetW() int { return w.w }
func (w *TreeBrowser) GetH() int { return w.h }
func (w *TreeBrowser) SetW(wd int) { w.SetSize(t.Coord{X: wd, Y: w.h}) }
func (w *TreeBrowser) SetH(h int) { w.SetSize(t.Coord{X: w.w, Y: h}) }
func (w *TreeBrowser) SetSize(c t.Coord) { w.w, w.h = c.X, c.Y }
func (w *TreeBrowser) WantW() int {
var want int
for i := range w.list {
@@ -226,7 +261,7 @@ func (w *TreeBrowser) MoveUp() bool {
return false
}
func (w *TreeBrowser) MoveDown() bool {
if w.cursor <= len(w.list) {
if w.cursor <= len(w.list)-2 {
w.cursor++
if w.onChange != nil {
n, err := w.GetActiveNode()
@@ -278,6 +313,9 @@ func (w *TreeBrowser) Clear() {
w.UpdateList()
}
func (w *TreeBrowser) Add(n *TreeNode) {
if n.depthIndic == "" {
n.depthIndic = w.depthIndic
}
w.nodes = append(w.nodes, n)
w.UpdateList()
}
@@ -301,17 +339,19 @@ func (w *TreeBrowser) UpdateList() {
* Tree Node
*/
type TreeNode struct {
label string
value string
expanded bool
parent *TreeNode
children []*TreeNode
label string
value string
expanded bool
parent *TreeNode
children []*TreeNode
depthIndic string
}
func NewTreeNode(l, v string) *TreeNode {
return &TreeNode{
label: l,
value: v,
label: l,
value: v,
depthIndic: "• ",
}
}
func (tn *TreeNode) Depth() int {
@@ -331,7 +371,7 @@ func (tn *TreeNode) GetLabelPath() []string {
return append(path, tn.Label())
}
func (tn *TreeNode) getList() []string {
pre := strings.Repeat("-", tn.Depth())
pre := strings.Repeat(tn.depthIndic, tn.Depth())
ret := []string{fmt.Sprintf("%s%s", pre, tn.label)}
if tn.expanded {
for i := range tn.children {
@@ -353,9 +393,15 @@ func (tn *TreeNode) getVisibleNodeList() []*TreeNode {
func (tn *TreeNode) ToggleExpand() { tn.expanded = !tn.expanded }
func (tn *TreeNode) AddChild(t *TreeNode, rest ...*TreeNode) {
if t.depthIndic == "" {
t.depthIndic = tn.depthIndic
}
t.parent = tn
tn.children = append(tn.children, t)
for i := range rest {
if rest[i].depthIndic == "" {
rest[i].depthIndic = tn.depthIndic
}
rest[i].parent = tn
tn.children = append(tn.children, rest[i])
}