Integrate tasktable

This commit is contained in:
Martin Pander
2026-02-26 22:49:00 +01:00
parent 9eda92503e
commit f6ce2e30dc
8 changed files with 778 additions and 109 deletions

View File

@@ -1,7 +1,8 @@
package tasktable
// Package table provides a simple table component for Bubble Tea applications.
package table
import (
taskw "tasksquire/internal/taskwarrior"
"strings"
"charm.land/bubbles/v2/help"
"charm.land/bubbles/v2/key"
@@ -18,6 +19,7 @@ type Model struct {
cols []Column
rows []Row
rowStyles []lipgloss.Style
cursor int
focus bool
styles Styles
@@ -28,15 +30,11 @@ type Model struct {
}
// Row represents one line in the table.
type Row struct {
task taskw.Task
style lipgloss.Style
}
type Row []string
// Column defines the table structure.
type Column struct {
Title string
Name string
Width int
}
@@ -166,6 +164,13 @@ func WithRows(rows []Row) Option {
}
}
// WithRowStyles sets the per row styles
func WithRowStyles(styles []lipgloss.Style) Option {
return func(m *Model) {
m.rowStyles = styles
}
}
// WithHeight sets the height of the table.
func WithHeight(h int) Option {
return func(m *Model) {
@@ -289,7 +294,7 @@ func (m *Model) UpdateViewport() {
// You can cast it to your own implementation.
func (m Model) SelectedRow() Row {
if m.cursor < 0 || m.cursor >= len(m.rows) {
return Row{}
return nil
}
return m.rows[m.cursor]
@@ -402,6 +407,22 @@ func (m *Model) GotoBottom() {
m.MoveDown(len(m.rows))
}
// FromValues create the table rows from a simple string. It uses `\n` by
// default for getting all the rows and the given separator for the fields on
// each row.
func (m *Model) FromValues(value, separator string) {
rows := []Row{} //nolint:prealloc
for _, line := range strings.Split(value, "\n") {
r := Row{}
for _, field := range strings.Split(line, separator) {
r = append(r, field)
}
rows = append(rows, r)
}
m.SetRows(rows)
}
func (m Model) headersView() string {
s := make([]string, 0, len(m.cols))
for _, col := range m.cols {
@@ -417,26 +438,28 @@ func (m Model) headersView() string {
func (m *Model) renderRow(r int) string {
s := make([]string, 0, len(m.cols))
for i, col := range m.cols {
for i, value := range m.rows[r] {
if m.cols[i].Width <= 0 {
continue
}
cellStyle := m.rows[r].style
style := lipgloss.NewStyle().
Width(m.cols[i].Width + m.styles.Cell.GetHorizontalPadding()).
MaxWidth(m.cols[i].Width + m.styles.Cell.GetHorizontalPadding()).
Inherit(m.rowStyles[r]).
Inherit(m.styles.Cell).
Inline(true)
if r == m.cursor {
cellStyle = cellStyle.Inherit(m.styles.Selected)
style = style.Inherit(m.styles.Selected)
}
style := lipgloss.NewStyle().Width(m.cols[i].Width).MaxWidth(m.cols[i].Width).Inline(true)
renderedCell := cellStyle.Render(style.Render(ansi.Truncate(m.rows[r].task.GetString(col.Name), m.cols[i].Width, "…")))
renderedCell := style.Render(ansi.Truncate(value, m.cols[i].Width, "…"))
s = append(s, renderedCell)
}
row := lipgloss.JoinHorizontal(lipgloss.Top, s...)
if r == m.cursor {
return m.styles.Selected.Render(row)
}
return row
}

View File

@@ -0,0 +1,80 @@
package table
import (
"strings"
"testing"
"charm.land/lipgloss/v2"
)
func TestRenderRowSelection(t *testing.T) {
cols := []Column{
{Title: "ID", Width: 5},
{Title: "Task", Width: 10},
}
rows := []Row{
{"1", "Task 1"},
{"2", "Task 2"},
}
m := New(
WithColumns(cols),
WithRows(rows),
WithStyles(Styles{
Cell: lipgloss.NewStyle().Padding(0, 1),
Selected: lipgloss.NewStyle().Background(lipgloss.Color("212")),
}),
)
m.SetWidth(30)
m.SetCursor(0)
rendered := m.renderRow(0)
// The rendered row should contain the background color 212
// AND the background should be present in the padding area.
// Since we are using lipgloss, we check for the color code.
if !strings.Contains(rendered, "212") {
t.Errorf("expected rendered row to contain background color 212, got %s", rendered)
}
// Verify it has the full width
if lipgloss.Width(rendered) != 30 {
t.Errorf("expected width 30, got %d", lipgloss.Width(rendered))
}
}
func TestRenderRowOutOfBounds(t *testing.T) {
cols := []Column{
{Title: "ID", Width: 5},
}
rows := []Row{
{"1", "Task 1 Extra Column"},
}
m := New(
WithColumns(cols),
WithRows(rows),
)
// This should not panic
m.renderRow(0)
}
func TestRenderRowNoStyles(t *testing.T) {
cols := []Column{
{Title: "ID", Width: 5},
}
rows := []Row{
{"1"},
}
m := New(
WithColumns(cols),
WithRows(rows),
)
m.rowStyles = nil // Ensure rowStyles is nil
// This should not panic
m.renderRow(0)
}