From 174fab2c6d2246db838a9dbde1a8324521c49184 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sun, 20 Jul 2025 10:28:56 -0500 Subject: [PATCH] List/Table Work --- list.go | 33 ++++++++++++++++++++++++--------- table.go | 8 ++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/list.go b/list.go index a19d2a0..243cfed 100644 --- a/list.go +++ b/list.go @@ -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 } diff --git a/table.go b/table.go index 9f14a1b..578af8a 100644 --- a/table.go +++ b/table.go @@ -45,6 +45,8 @@ type Table struct { cursorX, cursorY int wrapColumns bool + border []rune + x, y int w, h int wantW, wantH int @@ -79,6 +81,7 @@ func (w *Table) Init(id string, style tcell.Style) { w.id = id w.style = style w.visible = true + w.border = h.BRD_CSIMPLE } func (w *Table) Id() string { return w.id } 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) 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 }