This commit is contained in:
2025-11-15 14:43:52 -06:00
parent 4b179ba2f5
commit 197df3e029
3 changed files with 151 additions and 1 deletions

138
wdget_simplelistwithhelp.go Normal file
View File

@@ -0,0 +1,138 @@
/*
Copyright © Brian Buller <brian@bullercodeworks.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package widgets
import (
"fmt"
h "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
)
type SimpleListWithHelp struct {
id string
style tcell.Style
x, y int
w, h int
visible bool
list *SimpleList
help *Text
}
func NewSimpleListWithHelp(id string, s tcell.Style) *SimpleListWithHelp {
ret := &SimpleListWithHelp{}
ret.Init(id, s)
return ret
}
func (w *SimpleListWithHelp) Init(id string, s tcell.Style) {
w.id = id
w.style = s
w.visible = true
w.list = NewSimpleList(fmt.Sprintf("%s.list", id), s)
w.help = NewText(fmt.Sprintf("%s.help", id), s)
}
func (w *SimpleListWithHelp) Id() string { return w.id }
func (w *SimpleListWithHelp) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size()
hh := 0
if w.list.Active() {
hh = w.help.WantH()
}
w.help.HandleResize((Coord{X: w.w, Y: hh}).ResizeEvent())
w.help.SetPos(Coord{X: 1, Y: w.h - hh})
w.list.HandleResize((Coord{X: w.w, Y: w.h - hh}).ResizeEvent())
w.list.SetPos(Coord{X: 0, Y: 0})
}
func (w *SimpleListWithHelp) GetKeyMap() *KeyMap { return w.list.GetKeyMap() }
func (w *SimpleListWithHelp) SetKeyMap(km *KeyMap) { w.list.SetKeyMap(km) }
func (w *SimpleListWithHelp) HandleKey(ev *tcell.EventKey) bool { return w.list.HandleKey(ev) }
func (w *SimpleListWithHelp) HandleTime(ev *tcell.EventTime) {
w.list.HandleTime(ev)
w.help.HandleTime(ev)
}
func (w *SimpleListWithHelp) Draw(screen tcell.Screen) {
if !w.visible {
return
}
w.GetPos().DrawOffset(w.list, screen)
w.GetPos().DrawOffset(w.help, screen)
}
func (w *SimpleListWithHelp) Active() bool { return w.list.Active() }
func (w *SimpleListWithHelp) SetActive(a bool) {
w.list.SetActive(a)
w.help.SetVisible(a)
}
func (w *SimpleListWithHelp) Visible() bool { return w.visible }
func (w *SimpleListWithHelp) SetVisible(a bool) { w.visible = a }
func (w *SimpleListWithHelp) Focusable() bool { return w.list.Focusable() }
func (w *SimpleListWithHelp) SetFocusable(b bool) { w.list.SetFocusable(b) }
func (w *SimpleListWithHelp) SetX(x int) { w.x = x }
func (w *SimpleListWithHelp) SetY(y int) { w.y = y }
func (w *SimpleListWithHelp) GetX() int { return w.x }
func (w *SimpleListWithHelp) GetY() int { return w.y }
func (w *SimpleListWithHelp) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
func (w *SimpleListWithHelp) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
func (w *SimpleListWithHelp) GetW() int { return w.w }
func (w *SimpleListWithHelp) GetH() int { return w.h }
func (w *SimpleListWithHelp) SetW(wd int) { w.w = wd }
func (w *SimpleListWithHelp) SetH(h int) { w.h = h }
func (w *SimpleListWithHelp) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *SimpleListWithHelp) WantW() int { return w.list.WantW() + w.help.WantW() }
func (w *SimpleListWithHelp) WantH() int { return h.Max(w.list.WantH(), w.help.WantH()) }
func (w *SimpleListWithHelp) MinW() int { return h.Max(w.list.MinW(), w.help.MinW()) }
func (w *SimpleListWithHelp) MinH() int { return w.list.MinH() + w.help.MinH() }
// Help Text Widget
func (w *SimpleListWithHelp) SetHelp(txt string) { w.help.SetText(txt) }
func (w *SimpleListWithHelp) GetHelp() string { return w.help.GetText() }
// SimpleList Widget
func (w *SimpleListWithHelp) SetCursorWrap(b bool) { w.list.SetCursorWrap(b) }
func (w *SimpleListWithHelp) MoveUp() bool { return w.list.MoveUp() }
func (w *SimpleListWithHelp) MoveDown() bool { return w.list.MoveDown() }
func (w *SimpleListWithHelp) SetTitle(ttl string) { w.list.SetTitle(ttl) }
func (w *SimpleListWithHelp) SetList(l []string) { w.list.SetList(l) }
func (w *SimpleListWithHelp) Clear() { w.list.Clear() }
func (w *SimpleListWithHelp) Add(l string) { w.list.Add(l) }
func (w *SimpleListWithHelp) Remove(l string) { w.list.Remove(l) }
func (w *SimpleListWithHelp) SetBorder(brd []rune) { w.list.SetBorder(brd) }
func (w *SimpleListWithHelp) SetItemStyle(idx int, s tcell.Style) { w.list.SetItemStyle(idx, s) }
func (w *SimpleListWithHelp) SetItem(idx int, txt string) { w.list.SetItem(idx, txt) }
func (w *SimpleListWithHelp) SetSelectedIndex(i int) { w.list.SetSelectedIndex(i) }
func (w *SimpleListWithHelp) SelectedIndex() int { return w.list.SelectedIndex() }
func (w *SimpleListWithHelp) ClearBorder() { w.list.ClearBorder() }
func (w *SimpleListWithHelp) SetOnSelect(s func(int, string) bool) { w.list.SetOnSelect(s) }
func (w *SimpleListWithHelp) SetVimMode(b bool) { w.list.SetVimMode(b) }
func (w *SimpleListWithHelp) SetLogger(l func(string, ...any)) { w.list.SetLogger(l) }
func (w *SimpleListWithHelp) Log(txt string, args ...any) { w.list.Log(txt, args...) }
func (w *SimpleListWithHelp) SetOnChange(c func(idx int, nm string) bool) { w.list.SetOnChange(c) }
func (w *SimpleListWithHelp) GetSelectedItem() string { return w.list.GetSelectedItem() }
func (w *SimpleListWithHelp) GetAllItems() []string { return w.list.GetAllItems() }
func (w *SimpleListWithHelp) GetAllItemStyles() map[int]tcell.Style { return w.list.GetAllItemStyles() }

View File

@@ -388,6 +388,18 @@ func (w *Menu) CreateMenuItem(lbl string, do func() bool, hotKey rune, subItems
return d
}
func (w *Menu) CreateMenuItemWithId(id, lbl string, do func() bool, hotKey rune, subItems ...*MenuItem) *MenuItem {
d := NewMenuItem(fmt.Sprintf("menuitem-%s", id), tcell.StyleDefault)
d.SetMenuType(MenuTypeV)
d.SetHotKey(hotKey)
d.SetLabel(lbl)
d.SetOnPressed(do)
if len(subItems) > 0 {
d.AddItems(subItems...)
}
return d
}
func (w *Menu) FindItem(id string) *MenuItem {
for _, itm := range w.items {
if itm.Id() == id {

View File

@@ -229,7 +229,7 @@ func (w *Searcher) oldDraw(screen tcell.Screen) {
if w.cursor+w.h/2 > fD {
stIdx = fD - w.h/2
}
stIdx = wh.Max(stIdx, 0)
stIdx = wh.Max(0, wh.Min(stIdx, len(w.filteredData)-(w.h-3)))
for i := stIdx; i < fD; i++ {
st := dStyle
if i == w.cursor {