Really figuring some things out

This commit is contained in:
2025-08-07 11:18:03 -05:00
parent 7c96fbb187
commit b476c74683
23 changed files with 737 additions and 249 deletions

View File

@@ -25,7 +25,7 @@ import (
"fmt"
"strings"
h "git.bullercodeworks.com/brian/tcell-widgets/helpers"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
)
@@ -48,9 +48,8 @@ type Table struct {
border []rune
x, y int
w, h int
wantW, wantH int
x, y int
w, h int
columnWidths []int
}
@@ -82,11 +81,17 @@ func (w *Table) Init(id string, style tcell.Style) {
w.id = id
w.style = style
w.visible = true
w.border = h.BRD_CSIMPLE
w.border = wh.BRD_CSIMPLE
w.tabbable = true
}
func (w *Table) Id() string { return w.id }
func (w *Table) HandleResize(ev *tcell.EventResize) {}
func (w *Table) Id() string { return w.id }
func (w *Table) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size()
w.w = wh.Min(w.w, w.WantW())
w.h = wh.Min(w.h, w.WantH())
}
func (w *Table) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
return false
@@ -115,29 +120,29 @@ func (w *Table) Draw(screen tcell.Screen) {
dStyle = dStyle.Dim(true)
}
if w.minimized {
h.DrawText(x, y, fmt.Sprintf("├%s (%d rows)┤", w.title, len(dat)), dStyle, screen)
wh.DrawText(x, y, fmt.Sprintf("├%s (%d rows)┤", w.title, len(dat)), dStyle, screen)
return
}
if w.title != "" {
h.TitledBorder(x, y, w.x+width, w.y+height, w.title, h.BRD_CSIMPLE, dStyle, screen)
wh.TitledBorder(x, y, w.x+width, w.y+height, w.title, wh.BRD_CSIMPLE, dStyle, screen)
} else {
h.Border(x, y, w.x+width, w.y+height, h.BRD_CSIMPLE, dStyle, screen)
wh.Border(x, y, w.x+width, w.y+height, wh.BRD_CSIMPLE, dStyle, screen)
}
if len(w.header) > 0 {
h.DrawText(x, y, fmt.Sprintf("│%s│", strings.Join(w.header, "│")), dStyle, screen)
wh.DrawText(x, y, fmt.Sprintf("│%s│", strings.Join(w.header, "│")), dStyle, screen)
y++
}
if len(dat) == 0 {
h.DrawText(x, y, "No data in table", dStyle, screen)
wh.DrawText(x, y, "No data in table", dStyle, screen)
y++
} else {
for i := range dat {
h.DrawText(x, y, fmt.Sprintf("│%s│", strings.Join(dat[i], "│")), dStyle, screen)
wh.DrawText(x, y, fmt.Sprintf("│%s│", strings.Join(dat[i], "│")), dStyle, screen)
y++
}
}
if len(w.footer) > 0 {
h.DrawText(x, y, fmt.Sprintf("│%s│", strings.Join(w.header, "│")), dStyle, screen)
wh.DrawText(x, y, fmt.Sprintf("│%s│", strings.Join(w.header, "│")), dStyle, screen)
y++
}
}
@@ -169,9 +174,9 @@ func (w *Table) WantW() int {
}
// For each column, find the longest (in header, data, and footer)
var totalW int
colCnt := h.Max(len(w.header), len(w.footer))
colCnt := wh.Max(len(w.header), len(w.footer))
for i := range w.data {
colCnt = h.Max(colCnt, len(w.data[i]))
colCnt = wh.Max(colCnt, len(w.data[i]))
}
for i := 0; i < colCnt; i++ {
var cols []int
@@ -186,7 +191,7 @@ func (w *Table) WantW() int {
if len(w.footer) > i {
cols = append(cols, len(w.footer[i]))
}
totalW += h.Max(cols...)
totalW += wh.Max(cols...)
}
return totalW
}
@@ -214,9 +219,9 @@ func (w *Table) MinW() int {
}
// For each column, find the longest (in header, data, and footer)
var totalW int
colCnt := h.Max(len(w.header), len(w.footer))
colCnt := wh.Max(len(w.header), len(w.footer))
for i := range w.data {
colCnt = h.Max(colCnt, len(w.data[i]))
colCnt = wh.Max(colCnt, len(w.data[i]))
}
for i := 0; i < colCnt; i++ {
var cols []int
@@ -231,7 +236,7 @@ func (w *Table) MinW() int {
if len(w.footer) > i {
cols = append(cols, len(w.footer[i]))
}
totalW += h.Max(cols...)
totalW += wh.Max(cols...)
}
return totalW
}
@@ -240,7 +245,7 @@ func (w *Table) MinH() int {
if w.minimized {
return 1
}
datLen := h.Min(len(w.data)+2, 7) // Data length + Border
datLen := wh.Min(len(w.data)+2, 7) // Data length + Border
if len(w.header) > 0 {
datLen += len(w.header) + 1 // Header length + separator
}