Working on Layout Stuff

This commit is contained in:
2025-10-16 11:39:06 -05:00
parent f823a24fe8
commit 6aaaa68228
5 changed files with 222 additions and 74 deletions

View File

@@ -47,6 +47,7 @@ type Searcher struct {
disableBorder bool
selectFunc func(idx int, s string) bool
onChange func(idx int, s string) bool
hideOnSelect bool
logger func(string, ...any)
@@ -116,11 +117,16 @@ func (w *Searcher) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
return false
}
sel := w.cursor
b1, b2 := w.keyMap.Handle(ev), w.customKeyMap.Handle(ev)
if b1 || b2 {
return true
var ret bool
if !b1 && !b2 {
ret = w.search.HandleKey(ev)
}
return w.search.HandleKey(ev)
if w.cursor != sel && w.onChange != nil {
w.onChange(w.cursor, w.filteredData[w.cursor])
}
return b1 || b2 || ret
}
func (w *Searcher) handleKeyUp(ev *tcell.EventKey) bool {
@@ -209,11 +215,11 @@ func (w *Searcher) Draw(screen tcell.Screen) {
} else {
wh.BorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, wh.BRD_CSIMPLE, dStyle, screen)
}
} else if len(w.title) > 0 {
// TODO: Output the label
}
w.GetPos().DrawOffset(w.search, screen)
//w.GetPos().DrawOffset(w.search, screen)
x, y := w.x+1, w.y+2
w.search.SetPos(Coord{X: w.x + 1, Y: w.y + 1})
w.search.Draw(screen)
var stIdx int
if w.cursor > w.h/2 {
stIdx = w.cursor - (w.h / 2)
@@ -285,7 +291,6 @@ func (w *Searcher) MinH() int {
func (w *Searcher) SetHideOnSelect(t bool) { w.hideOnSelect = t }
func (w *Searcher) SetTitle(ttl string) { w.title = ttl }
func (w *Searcher) SetData(data []string) {
// w.data = data
w.data = data
w.filteredData = data
w.updateFilter()
@@ -318,22 +323,31 @@ func (w *Searcher) updateFilter() {
return
}
}
// If we're here, then the selected value changed
if w.cursor > len(w.filteredData) {
w.cursor = len(w.filteredData) - 1
w.doChange()
return
}
w.cursor = 0
w.doChange()
}
func (w *Searcher) SelectedValue() string { return w.filteredData[w.cursor] }
func (w *Searcher) SelectedIndex() int { return w.cursor }
func (w *Searcher) SetSearchValue(val string) {
w.search.SetValue(val)
}
func (w *Searcher) SetSearchValue(val string) { w.search.SetValue(val) }
func (w *Searcher) SetSelectFunc(f func(idx int, s string) bool) {
w.selectFunc = f
func (w *Searcher) SetSelectFunc(f func(idx int, s string) bool) { w.selectFunc = f }
func (w *Searcher) SetOnChange(f func(idx int, s string) bool) { w.onChange = f }
func (w *Searcher) doChange() {
l := len(w.filteredData)
if w.onChange == nil || l == 0 {
return
}
if w.cursor < l {
w.onChange(w.cursor, w.filteredData[w.cursor])
}
}
func (w *Searcher) ClearSearch() {