[WIP] table formatting
This commit is contained in:
136
pages/report.go
136
pages/report.go
@ -2,14 +2,14 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"log/slog"
|
||||
"tasksquire/common"
|
||||
"tasksquire/components/table"
|
||||
"tasksquire/taskwarrior"
|
||||
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
"github.com/charmbracelet/bubbles/table"
|
||||
// "github.com/charmbracelet/bubbles/table"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
type ReportPage struct {
|
||||
@ -22,71 +22,27 @@ type ReportPage struct {
|
||||
|
||||
tasks taskwarrior.Tasks
|
||||
|
||||
taskTable table.Model
|
||||
tableStyle table.Styles
|
||||
keymap ReportKeys
|
||||
taskTable table.Model
|
||||
|
||||
subpage tea.Model
|
||||
subpageActive bool
|
||||
}
|
||||
|
||||
type ReportKeys struct {
|
||||
Quit key.Binding
|
||||
Up key.Binding
|
||||
Down key.Binding
|
||||
Select key.Binding
|
||||
ToggleFocus key.Binding
|
||||
}
|
||||
|
||||
func NewReportPage(com *common.Common, report *taskwarrior.Report) *ReportPage {
|
||||
s := table.DefaultStyles()
|
||||
s.Header = s.Header.
|
||||
BorderStyle(lipgloss.NormalBorder()).
|
||||
BorderForeground(com.Styles.Active.GetForeground()).
|
||||
BorderBottom(true).
|
||||
Bold(true)
|
||||
s.Selected = s.Selected.
|
||||
Reverse(true).
|
||||
Bold(true)
|
||||
|
||||
keys := ReportKeys{
|
||||
Quit: key.NewBinding(
|
||||
key.WithKeys("q", "ctrl+c"),
|
||||
key.WithHelp("q, ctrl+c", "Quit"),
|
||||
),
|
||||
Up: key.NewBinding(
|
||||
key.WithKeys("k", "up"),
|
||||
key.WithHelp("↑/k", "Up"),
|
||||
),
|
||||
Down: key.NewBinding(
|
||||
key.WithKeys("j", "down"),
|
||||
key.WithHelp("↓/j", "Down"),
|
||||
),
|
||||
Select: key.NewBinding(
|
||||
key.WithKeys("enter"),
|
||||
key.WithHelp("enter", "Select"),
|
||||
),
|
||||
ToggleFocus: key.NewBinding(
|
||||
key.WithKeys("esc"),
|
||||
key.WithHelp("esc", "Toggle focus"),
|
||||
),
|
||||
}
|
||||
|
||||
return &ReportPage{
|
||||
common: com,
|
||||
activeReport: report,
|
||||
activeContext: com.TW.GetActiveContext(),
|
||||
activeProject: "",
|
||||
tableStyle: s,
|
||||
keymap: keys,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ReportPage) SetSize(width int, height int) {
|
||||
p.common.SetSize(width, height)
|
||||
slog.Info("FramSize", "vert", p.common.Styles.Main.GetVerticalFrameSize(), "horz", p.common.Styles.Main.GetHorizontalFrameSize())
|
||||
|
||||
p.taskTable.SetWidth(width - 2)
|
||||
p.taskTable.SetHeight(height - 4)
|
||||
p.taskTable.SetWidth(width - p.common.Styles.Main.GetVerticalFrameSize())
|
||||
p.taskTable.SetHeight(height - p.common.Styles.Main.GetHorizontalFrameSize())
|
||||
}
|
||||
|
||||
func (p *ReportPage) Init() tea.Cmd {
|
||||
@ -180,77 +136,33 @@ func (p *ReportPage) View() string {
|
||||
}
|
||||
|
||||
func (p *ReportPage) populateTaskTable(tasks taskwarrior.Tasks) {
|
||||
var selected int
|
||||
if len(tasks) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
nCols := len(p.activeReport.Columns)
|
||||
columns := make([]table.Column, 0)
|
||||
columnSizes := make([]int, nCols)
|
||||
fullRows := make([]table.Row, len(tasks))
|
||||
rows := make([]table.Row, len(tasks))
|
||||
descIndex := -1
|
||||
selected := 0
|
||||
|
||||
for i, task := range tasks {
|
||||
if p.selectedTask != nil && task.Uuid == p.selectedTask.Uuid {
|
||||
selected = i
|
||||
}
|
||||
|
||||
row := table.Row{}
|
||||
for i, col := range p.activeReport.Columns {
|
||||
if strings.Contains(col, "description") {
|
||||
descIndex = i
|
||||
if p.selectedTask != nil {
|
||||
for i, task := range tasks {
|
||||
if task.Uuid == p.selectedTask.Uuid {
|
||||
selected = i
|
||||
}
|
||||
field := task.GetString(col)
|
||||
columnSizes[i] = max(columnSizes[i], len(field))
|
||||
row = append(row, field)
|
||||
}
|
||||
fullRows[i] = row
|
||||
}
|
||||
|
||||
for i, r := range fullRows {
|
||||
row := table.Row{}
|
||||
for j, size := range columnSizes {
|
||||
if size == 0 {
|
||||
continue
|
||||
}
|
||||
row = append(row, r[j])
|
||||
}
|
||||
rows[i] = row
|
||||
}
|
||||
|
||||
combinedSize := 0
|
||||
for i, label := range p.activeReport.Labels {
|
||||
if columnSizes[i] == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
width := max(columnSizes[i], len(label))
|
||||
columns = append(columns, table.Column{Title: label, Width: width})
|
||||
|
||||
if i == descIndex {
|
||||
descIndex = len(columns) - 1
|
||||
continue
|
||||
}
|
||||
combinedSize += width
|
||||
}
|
||||
|
||||
if descIndex >= 0 {
|
||||
columns[descIndex].Width = p.taskTable.Width() - combinedSize - 14
|
||||
}
|
||||
p.taskTable = table.New(
|
||||
table.WithReport(p.activeReport),
|
||||
table.WithTasks(tasks),
|
||||
table.WithFocused(true),
|
||||
table.WithWidth(p.common.Width()-p.common.Styles.Main.GetVerticalFrameSize()),
|
||||
table.WithHeight(p.common.Height()-p.common.Styles.Main.GetHorizontalFrameSize()-10),
|
||||
table.WithStyles(p.common.Styles.TableStyle),
|
||||
)
|
||||
|
||||
if selected == 0 {
|
||||
selected = p.taskTable.Cursor()
|
||||
}
|
||||
|
||||
p.taskTable = table.New(
|
||||
table.WithColumns(columns),
|
||||
table.WithRows(rows),
|
||||
table.WithFocused(true),
|
||||
// table.WithHeight(7),
|
||||
// table.WithWidth(100),
|
||||
)
|
||||
p.taskTable.SetStyles(p.tableStyle)
|
||||
|
||||
if selected < len(p.tasks) {
|
||||
if selected < len(tasks) {
|
||||
p.taskTable.SetCursor(selected)
|
||||
} else {
|
||||
p.taskTable.SetCursor(len(p.tasks) - 1)
|
||||
|
||||
Reference in New Issue
Block a user