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 cursor int
cursorWrap bool cursorWrap bool
list []string list []string
itemsStyle map[int]tcell.Style
onSelect func(string) bool onSelect func(int, string) bool
keyMap KeyMap keyMap KeyMap
} }
@@ -63,11 +64,12 @@ func (w *List) Init(id string, style tcell.Style) {
tcell.KeyDown: w.MoveDown, tcell.KeyDown: w.MoveDown,
tcell.KeyEnter: func() bool { tcell.KeyEnter: func() bool {
if w.onSelect != nil && w.cursor < len(w.list) { 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 return false
}, },
}) })
w.itemsStyle = make(map[int]tcell.Style)
} }
func (w *List) Id() string { return w.id } func (w *List) Id() string { return w.id }
func (w *List) HandleResize(ev *tcell.EventResize) {} func (w *List) HandleResize(ev *tcell.EventResize) {}
@@ -76,7 +78,7 @@ func (w *List) HandleKey(ev *tcell.EventKey) bool {
if !w.active || !w.focusable { if !w.active || !w.focusable {
return false return false
} }
return false return w.keyMap.Handle(ev)
} }
func (w *List) HandleTime(ev *tcell.EventTime) {} func (w *List) HandleTime(ev *tcell.EventTime) {}
func (w *List) Draw(screen tcell.Screen) { func (w *List) Draw(screen tcell.Screen) {
@@ -96,14 +98,21 @@ func (w *List) Draw(screen tcell.Screen) {
} }
x, y = x+1, y+1 x, y = x+1, y+1
for i := range w.list { for i := range w.list {
rev := false
if i == w.cursor { if i == w.cursor {
dS = dS.Reverse(true) rev = true
} }
txt := w.list[i] txt := w.list[i]
if len(txt) > w.w-brdSz { if len(txt) > w.w-brdSz {
txt = 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 } 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) SetFocusable(f bool) { w.focusable = f }
func (w *List) SetCursorWrap(b bool) { w.cursorWrap = b } func (w *List) SetCursorWrap(b bool) { w.cursorWrap = b }
func (w *List) MoveUp() bool { func (w *List) MoveUp() bool {
if w.cursor > 0 { if w.cursor > 0 {
w.cursor-- w.cursor--
@@ -153,7 +161,7 @@ func (w *List) MoveUp() bool {
} }
func (w *List) MoveDown() bool { func (w *List) MoveDown() bool {
if w.cursor < len(w.list) { if w.cursor < len(w.list)-1 {
w.cursor++ w.cursor++
return true return true
} else if w.cursorWrap { } else if w.cursorWrap {
@@ -162,7 +170,6 @@ func (w *List) MoveDown() bool {
} }
return false return false
} }
func (w *List) SetTitle(ttl string) { w.title = ttl } func (w *List) SetTitle(ttl string) { w.title = ttl }
func (w *List) SetList(l []string) { w.list = l } func (w *List) SetList(l []string) { w.list = l }
func (w *List) Add(l string) { w.list = append(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 }

View File

@@ -45,6 +45,8 @@ type Table struct {
cursorX, cursorY int cursorX, cursorY int
wrapColumns bool wrapColumns bool
border []rune
x, y int x, y int
w, h int w, h int
wantW, wantH int wantW, wantH int
@@ -79,6 +81,7 @@ func (w *Table) Init(id string, style tcell.Style) {
w.id = id w.id = id
w.style = style w.style = style
w.visible = true w.visible = true
w.border = h.BRD_CSIMPLE
} }
func (w *Table) Id() string { return w.id } func (w *Table) Id() string { return w.id }
func (w *Table) HandleResize(ev *tcell.EventResize) {} func (w *Table) HandleResize(ev *tcell.EventResize) {}
@@ -200,3 +203,8 @@ func (w *Table) Focusable() bool { return w.focusable }
func (w *Table) SetTitle(ttl string) { w.title = ttl } func (w *Table) SetTitle(ttl string) { w.title = ttl }
func (w *Table) SetFocusable(f bool) { w.focusable = f } func (w *Table) SetFocusable(f bool) { w.focusable = f }
func (w *Table) SetBorder(b []rune) { w.border = b }
func (w *Table) AddRow(row []string) { w.data = append(w.data, row) }
func (w *Table) ClearData() { w.data = [][]string{} }
func (w *Table) GetData() [][]string { return w.data }