KeyMap Expansion

This commit is contained in:
2025-07-31 13:31:14 -05:00
parent 174fab2c6d
commit 9e88799391
7 changed files with 111 additions and 54 deletions

22
list.go
View File

@@ -46,6 +46,7 @@ type List struct {
onSelect func(int, string) bool
keyMap KeyMap
vimMode bool
}
var _ Widget = (*List)(nil)
@@ -59,16 +60,28 @@ func NewList(id string, style tcell.Style) *List {
func (w *List) Init(id string, style tcell.Style) {
w.id = id
w.style = style
w.keyMap = NewKeyMap(map[tcell.Key]func() bool{
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
tcell.KeyUp: w.MoveUp,
tcell.KeyDown: w.MoveDown,
tcell.KeyEnter: func() bool {
tcell.KeyEnter: func(ev *tcell.EventKey) bool {
if w.onSelect != nil && w.cursor < len(w.list) {
return w.onSelect(w.cursor, w.list[w.cursor])
}
return false
},
})
w.keyMap.AddRune('j', func(ev *tcell.EventKey) bool {
if w.vimMode {
return w.MoveDown(ev)
}
return false
})
w.keyMap.AddRune('k', func(ev *tcell.EventKey) bool {
if w.vimMode {
return w.MoveUp(ev)
}
return false
})
w.itemsStyle = make(map[int]tcell.Style)
}
func (w *List) Id() string { return w.id }
@@ -149,7 +162,7 @@ func (w *List) WantH() int {
func (w *List) SetFocusable(f bool) { w.focusable = f }
func (w *List) SetCursorWrap(b bool) { w.cursorWrap = b }
func (w *List) MoveUp() bool {
func (w *List) MoveUp(ev *tcell.EventKey) bool {
if w.cursor > 0 {
w.cursor--
return true
@@ -160,7 +173,7 @@ func (w *List) MoveUp() bool {
return false
}
func (w *List) MoveDown() bool {
func (w *List) MoveDown(ev *tcell.EventKey) bool {
if w.cursor < len(w.list)-1 {
w.cursor++
return true
@@ -204,3 +217,4 @@ func (w *List) SetItem(idx int, txt string) {
func (w *List) GetIdx() int { return w.cursor }
func (w *List) ClearBorder() { w.border = []rune{} }
func (w *List) SetOnSelect(s func(int, string) bool) { w.onSelect = s }
func (w *List) SetVimMode(b bool) { w.vimMode = b }