Merge branch 'main' of ssh://git.bullercodeworks.com:2200/brian/expds

This commit is contained in:
2026-02-12 06:13:56 -06:00
10 changed files with 954 additions and 33 deletions

View File

@@ -114,7 +114,7 @@ func (b *StatusBlock) DrawPath(screen tcell.Screen) {
x := b.x
wh.DrawText(x, b.y, b.end, b.style, screen)
x++
pts := fmt.Sprintf(" %s ", strings.Join(b.parts, b.sep))
pts := fmt.Sprintf(" %s ", strings.Join(b.parts, b.sep))
wh.DrawText(x, b.y, pts, b.style, screen)
x += len(pts) - len(b.parts)
wh.DrawText(x, b.y, b.end, b.style.Reverse(true), screen)

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"strings"
"sync"
h "git.bullercodeworks.com/brian/expds/helpers"
t "git.bullercodeworks.com/brian/tcell-widgets"
@@ -36,7 +37,12 @@ type TreeBrowser struct {
keyMap *t.KeyMap
vimMode bool
searching bool
searchStr string
logger func(string, ...any)
m sync.Mutex
}
var _ t.Widget = (*TreeBrowser)(nil)
@@ -55,6 +61,10 @@ func (w *TreeBrowser) Init(id string, style tcell.Style) {
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() }),
t.NewKey(t.BuildEK(tcell.KeyEnter), func(ev *tcell.EventKey) bool {
if w.searching {
w.searching = !w.searching
return true
}
if w.onSelect != nil {
n, err := w.GetActiveNode()
if err != nil || n == nil {
@@ -97,6 +107,14 @@ func (w *TreeBrowser) Init(id string, style tcell.Style) {
}
return false
}),
t.NewKey(t.BuildEKr('/'), func(ev *tcell.EventKey) bool {
if !w.searching {
w.searching = true
w.searchStr = ""
return true
}
return false
}),
)
}
@@ -111,12 +129,20 @@ func (w *TreeBrowser) HandleKey(ev *tcell.EventKey) bool {
if !w.active || !w.focusable {
return false
}
return w.keyMap.Handle(ev)
if w.keyMap.Handle(ev) {
return true
} else if w.searching {
w.updateSearch(ev)
return true
}
return false
}
func (w *TreeBrowser) HandleTime(ev *tcell.EventTime) {}
func (w *TreeBrowser) Draw(screen tcell.Screen) {
w.m.Lock()
defer w.m.Unlock()
if !w.visible {
return
}
@@ -170,9 +196,23 @@ func (w *TreeBrowser) Draw(screen tcell.Screen) {
if len(txt) > w.w-brdSz && w.w-brdSz >= 0 {
txt = txt[:(w.w - brdSz)]
}
wh.DrawText(x, y, txt, dS.Reverse(rev), screen)
if w.searching && strings.Contains(txt, w.searchStr) {
// TODO: Fix multi-byte depth indicator
srchIdx := strings.Index(txt, w.searchStr)
endSrchIdx := srchIdx + len(w.searchStr)
wh.DrawText(x, y, txt[:srchIdx], dS.Reverse(rev), screen)
wh.DrawText(x+srchIdx, y, txt[srchIdx:endSrchIdx], dS.Reverse(!rev), screen)
if len(txt) > endSrchIdx {
wh.DrawText(x+endSrchIdx, y, txt[endSrchIdx:], dS.Reverse(rev), screen)
}
} else {
wh.DrawText(x, y, txt, dS.Reverse(rev), screen)
}
y += 1
}
if w.searching {
wh.DrawText(w.x, w.y+w.h, fmt.Sprintf("Searching: %s", w.searchStr), dS, screen)
}
}
func (w *TreeBrowser) SetStyle(s tcell.Style) { w.style = s }
@@ -197,6 +237,8 @@ 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 {
w.m.Lock()
defer w.m.Unlock()
var want int
for i := range w.list {
want = h.MaxI(want, len(w.list[i]))
@@ -204,6 +246,8 @@ func (w *TreeBrowser) WantW() int {
return w.w
}
func (w *TreeBrowser) WantH() int {
w.m.Lock()
defer w.m.Unlock()
want := len(w.list)
if len(w.border) > 0 {
return want + 2
@@ -225,8 +269,8 @@ func (w *TreeBrowser) ClearBorder() { w.border = []rune{}
func (w *TreeBrowser) SetOnChange(c func(*TreeNode) bool) { w.onChange = c }
func (w *TreeBrowser) SetOnSelect(s func(*TreeNode) bool) { w.onSelect = s }
func (w *TreeBrowser) SetVimMode(b bool) { w.vimMode = b }
func (w *TreeBrowser) GetActiveNode() (*TreeNode, error) {
if len(w.listNodes) < 0 {
func (w *TreeBrowser) nmGetActiveNode() (*TreeNode, error) {
if len(w.listNodes) <= 0 {
return nil, errors.New("no nodes")
}
if w.cursor < 0 {
@@ -237,6 +281,11 @@ func (w *TreeBrowser) GetActiveNode() (*TreeNode, error) {
}
return w.listNodes[w.cursor], nil
}
func (w *TreeBrowser) GetActiveNode() (*TreeNode, error) {
w.m.Lock()
defer w.m.Unlock()
return w.nmGetActiveNode()
}
func (w *TreeBrowser) SetCursorWrap(b bool) { w.cursorWrap = b }
func (w *TreeBrowser) MoveUp() bool {
if w.cursor > 0 {
@@ -305,22 +354,30 @@ func (w *TreeBrowser) PageDn() bool {
func (w *TreeBrowser) Title() string { return w.title }
func (w *TreeBrowser) SetTitle(ttl string) { w.title = ttl }
func (w *TreeBrowser) SetTree(l []*TreeNode) {
w.m.Lock()
defer w.m.Unlock()
w.nodes = l
w.UpdateList()
w.nmUpdateList()
}
func (w *TreeBrowser) Clear() {
w.m.Lock()
defer w.m.Unlock()
w.nodes = []*TreeNode{}
w.UpdateList()
w.nmUpdateList()
}
func (w *TreeBrowser) Add(n *TreeNode) {
w.m.Lock()
defer w.m.Lock()
if n.depthIndic == "" {
n.depthIndic = w.depthIndic
}
w.nodes = append(w.nodes, n)
w.UpdateList()
w.nmUpdateList()
}
func (w *TreeBrowser) UpdateList() {
// Update the list, intended to be called locally within other functions that
// handle the mutex
func (w *TreeBrowser) nmUpdateList() {
w.list = []string{}
w.listNodes = []*TreeNode{}
for i := range w.nodes {
@@ -335,6 +392,98 @@ func (w *TreeBrowser) UpdateList() {
}
}
func (w *TreeBrowser) UpdateList() {
w.m.Lock()
defer w.m.Unlock()
w.nmUpdateList()
}
func (w *TreeBrowser) nmSetNodeActive(tn *TreeNode) {
// Make sure that the selected node is visible
wrk := tn.parent
for wrk != nil {
wrk.expanded = true
wrk = wrk.parent
}
w.nmUpdateList()
for i := range w.listNodes {
if w.listNodes[i] == tn {
w.cursor = i
return
}
}
}
func (w *TreeBrowser) SetNodeActive(tn *TreeNode) {
w.m.Lock()
defer w.m.Unlock()
w.nmSetNodeActive(tn)
}
func (w *TreeBrowser) updateSearch(ev *tcell.EventKey) {
w.m.Lock()
defer w.m.Unlock()
if len(w.nodes) == 0 {
return
}
if wh.IsBS(*ev) {
if len(w.searchStr) > 0 {
w.searchStr = w.searchStr[:len(w.searchStr)-1]
if len(w.searchStr) == 0 {
w.searching = false
}
}
return
}
w.searchStr = fmt.Sprintf("%s%s", w.searchStr, string(ev.Rune()))
wrk, _ := w.nmGetActiveNode()
if wrk == nil {
wrk = w.nodes[0]
}
// Check the ative node & it's children for the search
if fnd := wrk.SearchLabels(w.searchStr); fnd != nil {
w.nmSetNodeActive(fnd)
return
}
// Didn't find a child of the active node that matched, look for a sibling
if wrk.parent != nil {
if fnd := wrk.parent.SearchLabels(w.searchStr); fnd != nil {
w.nmSetNodeActive(fnd)
return
}
}
// Check the next browser node
var stIdx int
for i := range w.nodes {
if w.nodes[i] == wrk {
stIdx = i + 1
break
}
}
for i := 0; i < len(w.nodes); i++ {
idx := (i + stIdx) % len(w.nodes)
if fnd := w.nodes[idx].SearchLabels(w.searchStr); fnd != nil {
w.nmSetNodeActive(fnd)
return
}
}
}
func (w *TreeBrowser) getCurrentLine() string {
w.m.Lock()
defer w.m.Unlock()
l := len(w.list)
if l == 0 {
return ""
}
if w.cursor < 0 || w.cursor >= l {
return ""
}
return w.list[w.cursor]
}
/*
* Tree Node
*/
@@ -370,6 +519,7 @@ func (tn *TreeNode) GetLabelPath() []string {
}
return append(path, tn.Label())
}
func (tn *TreeNode) getList() []string {
pre := strings.Repeat(tn.depthIndic, tn.Depth())
ret := []string{fmt.Sprintf("%s%s", pre, tn.label)}
@@ -380,6 +530,7 @@ func (tn *TreeNode) getList() []string {
}
return ret
}
func (tn *TreeNode) getVisibleNodeList() []*TreeNode {
ret := []*TreeNode{tn}
if tn.expanded {
@@ -390,6 +541,19 @@ func (tn *TreeNode) getVisibleNodeList() []*TreeNode {
return ret
}
func (tn *TreeNode) SearchLabels(f string) *TreeNode {
if strings.Contains(tn.label, f) {
return tn
}
for i := 0; i < len(tn.children); i++ {
fnd := tn.children[i].SearchLabels(f)
if fnd != nil {
return fnd
}
}
return nil
}
func (tn *TreeNode) ToggleExpand() { tn.expanded = !tn.expanded }
func (tn *TreeNode) AddChild(t *TreeNode, rest ...*TreeNode) {
@@ -415,3 +579,44 @@ func (tn *TreeNode) GetPath() []string {
}
return append(path, tn.value)
}
func (tn *TreeNode) GetChildren() []*TreeNode { return tn.children }
func (tn *TreeNode) GetFirstChild() *TreeNode {
if !tn.HasChildren() {
return nil
}
return tn.children[0]
}
func (tn *TreeNode) GetPrevChild(before *TreeNode) *TreeNode {
if !tn.HasChildren() {
return nil
}
var found bool
for i := len(tn.children) - 1; i >= 0; i-- {
if found {
return tn.children[i]
}
if tn.children[i] == before {
found = true
}
}
return nil
}
func (tn *TreeNode) GetNextChild(after *TreeNode) *TreeNode {
if !tn.HasChildren() {
return nil
}
var found bool
for i := range tn.children {
if found {
return tn.children[i]
}
if tn.children[i] == after {
found = true
}
}
return nil
}