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

@@ -5,6 +5,8 @@ import (
"strconv"
"strings"
"image/color"
"fmt"
"time"
"tasksquire/internal/taskwarrior"
@@ -82,10 +84,10 @@ func NewStyles(config *taskwarrior.TWConfig) *Styles {
styles.Base = lipgloss.NewStyle().Foreground(styles.Palette.Text.GetForeground())
styles.TableStyle = TableStyle{
// Header: lipgloss.NewStyle().Bold(true).Padding(0, 1).BorderBottom(true),
Cell: lipgloss.NewStyle().Padding(0, 1, 0, 0),
Header: lipgloss.NewStyle().Bold(true).Padding(0, 1, 0, 0).Underline(true).Foreground(styles.Palette.Primary.GetForeground()),
Selected: lipgloss.NewStyle().Bold(true).Reverse(true).Foreground(styles.Palette.Accent.GetForeground()),
// Selected: lipgloss.NewStyle().Bold(true).Reverse(true).Foreground(styles.Palette.Accent.GetForeground()),
Selected: lipgloss.NewStyle().Bold(true).Reverse(true),
}
// formTheme := huh.ThemeBase()
@@ -232,3 +234,77 @@ var colorStrings = map[string]int{
"bright cyan": 14,
"bright white": 15,
}
func GetTaskTabelStyle(task *taskwarrior.Task, com Common) lipgloss.Style {
if task.Status == "deleted" {
if c, ok := com.Styles.Colors["deleted"]; ok && c != nil {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
if task.Status == "completed" {
if c, ok := com.Styles.Colors["completed"]; ok && c != nil {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
if task.Status == "pending" && task.Start != "" {
if c, ok := com.Styles.Colors["active"]; ok && c != nil {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
// TODO: implement keyword
// TODO: implement tag
if task.HasTag("next") {
if c, ok := com.Styles.Colors["tag.next"]; ok && c != nil {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
// TODO: implement project
if !task.GetDate("due").IsZero() && task.GetDate("due").Before(time.Now()) {
if c, ok := com.Styles.Colors["overdue"]; ok && c != nil {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
if task.Scheduled != "" {
if c, ok := com.Styles.Colors["scheduled"]; ok && c != nil {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
if !task.GetDate("due").IsZero() && task.GetDate("due").Truncate(24*time.Hour).Equal(time.Now().Truncate(24*time.Hour)) {
if c, ok := com.Styles.Colors["due.today"]; ok && c != nil {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
if task.Due != "" {
if c, ok := com.Styles.Colors["due"]; ok && c != nil {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
if len(task.Depends) > 0 {
if c, ok := com.Styles.Colors["blocked"]; ok && c != nil {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
// TODO implement blocking
if task.Recur != "" {
if c, ok := com.Styles.Colors["recurring"]; ok && c != nil {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
// TODO: make styles optional and discard if empty
if len(task.Tags) > 0 {
if c, ok := com.Styles.Colors["tagged"]; ok && c != nil {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
if len(com.Udas) > 0 {
for _, uda := range com.Udas {
if u, ok := task.Udas[uda.Name]; ok {
if c, ok := com.Styles.Colors[fmt.Sprintf("uda.%s.%s", uda.Name, u)]; ok {
return c.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}
}
}
}
return com.Styles.Base.Inherit(com.Styles.TableStyle.Cell).Margin(com.Styles.TableStyle.Cell.GetMargin()).Padding(com.Styles.TableStyle.Cell.GetPadding())
}

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)
}

View File

@@ -4,7 +4,7 @@ import (
"tasksquire/internal/common"
tea "charm.land/bubbletea/v2"
// "charm.land/bubbles/v2/key"
"charm.land/bubbles/v2/key"
"charm.land/lipgloss/v2"
)
@@ -34,33 +34,30 @@ func NewMainPage(common *common.Common) *MainPage {
}
func (m *MainPage) Init() tea.Cmd {
return tea.Batch(m.taskPage.Init())
// return tea.Batch(m.taskPage.Init(), m.timePage.Init())
return tea.Batch()
}
func (m *MainPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
// switch msg := msg.(type) {
// case tea.WindowSizeMsg:
// m.width = msg.Width
// m.height = msg.Height
// m.common.SetSize(msg.Width, msg.Height)
//
// tabHeight := lipgloss.Height(m.renderTabBar())
// contentHeight := msg.Height - tabHeight
// if contentHeight < 0 {
// contentHeight = 0
// }
//
// newMsg := tea.WindowSizeMsg{Width: msg.Width, Height: contentHeight}
// activePage, cmd := m.activePage.Update(newMsg)
// m.activePage = activePage.(common.Component)
// return m, cmd
//
// case tea.KeyMsg:
// // Only handle tab key for page switching when at the top level (no subpages active)
// if key.Matches(msg, m.common.Keymap.Next) && !m.common.HasSubpages() {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
m.common.SetSize(msg.Width, msg.Height)
tabHeight := lipgloss.Height(m.renderTabBar())
contentHeight := max(msg.Height - tabHeight, 0)
newMsg := tea.WindowSizeMsg{Width: msg.Width, Height: contentHeight}
activePage, cmd := m.activePage.Update(newMsg)
m.activePage = activePage.(common.Component)
return m, cmd
case tea.KeyMsg:
// Only handle tab key for page switching when at the top level (no subpages active)
if key.Matches(msg, m.common.Keymap.Next) && !m.common.HasSubpages() {
// if m.activePage == m.taskPage {
// m.activePage = m.timePage
// m.currentTab = 1
@@ -69,18 +66,16 @@ func (m *MainPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// m.currentTab = 0
// }
//
// tabHeight := lipgloss.Height(m.renderTabBar())
// contentHeight := m.height - tabHeight
// if contentHeight < 0 {
// contentHeight = 0
// }
// m.activePage.SetSize(m.width, contentHeight)
//
// // Trigger a refresh/init on switch? Maybe not needed if we keep state.
// // But we might want to refresh data.
// return m, m.activePage.Init()
// }
// }
tabHeight := lipgloss.Height(m.renderTabBar())
contentHeight := m.height - tabHeight
if contentHeight < 0 {
contentHeight = 0
}
m.activePage.SetSize(m.width, contentHeight)
return m, m.activePage.Init()
}
}
//
activePage, cmd := m.activePage.Update(msg)
m.activePage = activePage.(common.Component)

View File

@@ -3,11 +3,11 @@ package pages
import (
"tasksquire/internal/common"
"tasksquire/internal/components/tasktable"
"tasksquire/internal/components/table"
"tasksquire/internal/taskwarrior"
tea "charm.land/bubbletea/v2"
// "charm.land/lipgloss/v2"
"charm.land/lipgloss/v2"
"charm.land/bubbles/v2/key"
)
@@ -22,7 +22,7 @@ type TaskPage struct {
tasks taskwarrior.Tasks
taskTable tasktable.Model
taskTable table.Model
// Details panel state
// detailsPanelActive bool
@@ -37,7 +37,7 @@ func NewTaskPage(com *common.Common, report *taskwarrior.Report) *TaskPage {
activeReport: report,
activeContext: com.TW.GetActiveContext(),
activeProject: "",
taskTable: tasktable.New(),
taskTable: table.New(),
// detailsPanelActive: false,
// detailsViewer: detailsviewer.New(com),
}
@@ -84,13 +84,13 @@ func (p *TaskPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return p, tea.Batch(cmds...)
case common.TaskMsg:
p.tasks = taskwarrior.Tasks(msg)
p.populateTaskTable(p.tasks)
// case UpdateReportMsg:
// p.activeReport = msg
// cmds = append(cmds, p.getTasks())
// case UpdateContextMsg:
// p.activeContext = msg
// p.common.TW.SetContext(msg)
// p.populateTaskTable(p.tasks)
// cmds = append(cmds, p.getTasks())
// case UpdateProjectMsg:
// p.activeProject = string(msg)
@@ -227,7 +227,7 @@ func (p *TaskPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// }
// }
//
// var cmd tea.Cmd
var cmd tea.Cmd
//
// // Route keyboard messages to details viewer when panel is active
// if p.detailsPanelActive {
@@ -238,15 +238,15 @@ func (p *TaskPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// cmds = append(cmds, viewerCmd)
// } else {
// // Route to table when details panel not active
// p.taskTable, cmd = p.taskTable.Update(msg)
// cmds = append(cmds, cmd)
//
// if p.tasks != nil && len(p.tasks) > 0 {
// p.selectedTask = p.tasks[p.taskTable.Cursor()]
// } else {
// p.selectedTask = nil
// }
// }
p.taskTable, cmd = p.taskTable.Update(msg)
cmds = append(cmds, cmd)
if len(p.tasks) > 0 {
p.selectedTask = p.tasks[p.taskTable.Cursor()]
} else {
p.selectedTask = nil
}
// }
}
return p, tea.Batch(cmds...)
@@ -274,75 +274,99 @@ func (p *TaskPage) View() tea.View {
// )
// }
//
// func (p *TaskPage) populateTaskTable(tasks taskwarrior.Tasks) {
// if len(tasks) == 0 {
// return
// }
//
// // Build task tree for hierarchical display
// taskTree := taskwarrior.BuildTaskTree(tasks)
//
// // Use flattened tree list for display order
// orderedTasks := make(taskwarrior.Tasks, len(taskTree.FlatList))
// for i, node := range taskTree.FlatList {
// orderedTasks[i] = node.Task
// }
//
// selected := p.taskTable.Cursor()
//
// // Adjust cursor for tree ordering
// if p.selectedTask != nil {
// for i, task := range orderedTasks {
// if task.Uuid == p.selectedTask.Uuid {
// selected = i
// break
// }
// }
// }
// if selected > len(orderedTasks)-1 {
// selected = len(orderedTasks) - 1
// }
//
// // Calculate proper dimensions based on whether details panel is active
// baseHeight := p.common.Height() - p.common.Styles.Base.GetVerticalFrameSize()
// baseWidth := p.common.Width() - p.common.Styles.Base.GetHorizontalFrameSize()
//
// var tableHeight int
func (p *TaskPage) populateTaskTable(tasks taskwarrior.Tasks) {
if len(tasks) == 0 {
return
}
selected := p.taskTable.Cursor()
// Calculate proper dimensions based on whether details panel is active
baseHeight := p.common.Height() - p.common.Styles.Base.GetVerticalFrameSize()
baseWidth := p.common.Width() - p.common.Styles.Base.GetHorizontalFrameSize()
var tableHeight int
// if p.detailsPanelActive {
// // Allocate 60% for table, 40% for details panel
// // Minimum 5 lines for details, minimum 10 lines for table
// detailsHeight := max(min(baseHeight*2/5, baseHeight-10), 5)
// tableHeight = baseHeight - detailsHeight - 1 // -1 for spacing
// detailsHeight := max(min(baseHeight*2/5, baseHeight-10), 5)
// tableHeight = baseHeight - detailsHeight - 1 // -1 for spacing
// } else {
// tableHeight = baseHeight
tableHeight = baseHeight
// }
//
// p.taskTable = table.New(
// p.common,
// able.WithReport(p.activeReport),
// table.WithTasks(orderedTasks),
// table.WithTaskTree(taskTree),
// table.WithFocused(true),
// table.WithWidth(baseWidth),
// table.WithHeight(tableHeight),
// table.WithStyles(p.common.Styles.TableStyle),
// )
//
// if selected == 0 {
// selected = p.taskTable.Cursor()
// }
// if selected < len(orderedTasks) {
// p.taskTable.SetCursor(selected)
// } else {
// p.taskTable.SetCursor(len(p.tasks) - 1)
// }
numCols := len(p.activeReport.Columns)
taskRows := make([]table.Row, len(tasks))
taskStyles := make([]lipgloss.Style, len(tasks))
widths := make([]int, numCols)
for i, task := range tasks {
row := make(table.Row, numCols)
for j, colKey := range p.activeReport.Columns {
val := task.GetString(colKey)
row[j] = val
widths[j] = max(widths[j], lipgloss.Width(val))
}
taskRows[i] = row
taskStyles[i] = common.GetTaskTabelStyle(task, *p.common)
}
var columns []table.Column
for j, w := range widths {
title := p.activeReport.Labels[j]
width := 0
if w > 0 {
width = max(w, lipgloss.Width(title))
}
columns = append(columns, table.Column{
Title: title,
Width: width,
})
}
if len(columns) > 0 {
usedWidth := 0
for i := 0; i < len(columns)-1; i++ {
usedWidth += columns[i].Width + 1 // padding/border offset
}
remaining := p.taskTable.Width() - usedWidth - 1
lastIdx := len(columns) - 1
columns[lastIdx].Width = max(columns[lastIdx].Width, remaining)
}
p.taskTable = table.New(
table.WithColumns(columns),
table.WithRows(taskRows),
table.WithRowStyles(taskStyles),
table.WithFocused(true),
table.WithWidth(baseWidth),
table.WithHeight(tableHeight),
table.WithStyles(table.Styles{
Header: p.common.Styles.TableStyle.Header ,
Cell: p.common.Styles.TableStyle.Cell,
Selected: p.common.Styles.TableStyle.Selected,
}),
)
if selected == 0 {
selected = p.taskTable.Cursor()
}
if selected < len(tasks) {
p.taskTable.SetCursor(selected)
} else {
p.taskTable.SetCursor(len(p.tasks) - 1)
}
//
// // Refresh details content if panel is active
// if p.detailsPanelActive && p.selectedTask != nil {
// p.detailsViewer.SetTask(p.selectedTask)
// }
// }
//
}
func (p *TaskPage) getTasks() tea.Cmd {
return func() tea.Msg {
filters := []string{}