List/Table Work

This commit is contained in:
2025-07-20 10:28:56 -05:00
parent c152a52304
commit 174fab2c6d
2 changed files with 32 additions and 9 deletions

33
list.go
View File

@@ -42,8 +42,9 @@ type List struct {
cursor int
cursorWrap bool
list []string
itemsStyle map[int]tcell.Style
onSelect func(string) bool
onSelect func(int, string) bool
keyMap KeyMap
}
@@ -63,11 +64,12 @@ func (w *List) Init(id string, style tcell.Style) {
tcell.KeyDown: w.MoveDown,
tcell.KeyEnter: func() bool {
if w.onSelect != nil && w.cursor < len(w.list) {
return w.onSelect(w.list[w.cursor])
return w.onSelect(w.cursor, w.list[w.cursor])
}
return false
},
})
w.itemsStyle = make(map[int]tcell.Style)
}
func (w *List) Id() string { return w.id }
func (w *List) HandleResize(ev *tcell.EventResize) {}
@@ -76,7 +78,7 @@ func (w *List) HandleKey(ev *tcell.EventKey) bool {
if !w.active || !w.focusable {
return false
}
return false
return w.keyMap.Handle(ev)
}
func (w *List) HandleTime(ev *tcell.EventTime) {}
func (w *List) Draw(screen tcell.Screen) {
@@ -96,14 +98,21 @@ func (w *List) Draw(screen tcell.Screen) {
}
x, y = x+1, y+1
for i := range w.list {
rev := false
if i == w.cursor {
dS = dS.Reverse(true)
rev = true
}
txt := w.list[i]
if len(txt) > w.w-brdSz {
txt = txt[:(w.w - brdSz)]
h.DrawText(x, y, txt, dS, screen)
}
var ok bool
var s tcell.Style
if s, ok = w.itemsStyle[i]; !ok {
s = dS
}
h.DrawText(x, y, txt, s.Reverse(rev), screen)
y += 1
}
}
func (w *List) Active() bool { return w.active }
@@ -140,7 +149,6 @@ 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 {
if w.cursor > 0 {
w.cursor--
@@ -153,7 +161,7 @@ func (w *List) MoveUp() bool {
}
func (w *List) MoveDown() bool {
if w.cursor < len(w.list) {
if w.cursor < len(w.list)-1 {
w.cursor++
return true
} else if w.cursorWrap {
@@ -162,7 +170,6 @@ func (w *List) MoveDown() bool {
}
return false
}
func (w *List) SetTitle(ttl string) { w.title = ttl }
func (w *List) SetList(l []string) { w.list = l }
func (w *List) Add(l string) { w.list = append(w.list, l) }
@@ -188,4 +195,12 @@ func (w *List) SetBorder(brd []rune) {
}
}
func (w *List) ClearBorder() { w.border = []rune{} }
func (w *List) SetItemStyle(idx int, s tcell.Style) { w.itemsStyle[idx] = s }
func (w *List) SetItem(idx int, txt string) {
if len(w.list) < idx {
w.list[idx] = txt
}
}
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 }