Files
gems/opal-task/cmd/reports.go
T
joakim 5b660c3c1c Fix working set IDs to match display order
BREAKING CHANGE: BuildWorkingSet() now accepts []*Task instead of *Filter

Problem:
- Working set IDs were assigned based on database query order (unsorted)
- Reports displayed tasks in sorted order (by urgency)
- Result: IDs didn't match displayed task positions (ID 1 wasn't first task)

Solution:
- Changed BuildWorkingSet() to accept pre-sorted task slice
- Reports now pass sorted tasks to BuildWorkingSet()
- IDs are assigned sequentially to match display order (1, 2, 3...)

Behavior:
- Reports rebuild working set on every execution with fresh IDs
- Task operations (done, modify, info) use saved working set IDs
- After completing a task, re-running report renumbers remaining tasks

Example:
  Before:  opal list shows ID 1 = low urgency task (wrong)
  After:   opal list shows ID 1 = highest urgency task (correct)

Tested scenarios:
✓ List report: IDs 1-N match urgency order
✓ Next report: IDs 1-5 match top urgent tasks
✓ Task completion: IDs renumber correctly after removal
✓ Multiple operations: Use saved working set (correct behavior)
✓ Different reports: Each builds own sequential IDs
2026-01-06 14:54:01 +01:00

98 lines
2.5 KiB
Go

package cmd
import (
"fmt"
"os"
"sort"
"git.jnss.me/joakim/opal/internal/engine"
"github.com/spf13/cobra"
)
// CreateReportCommands generates commands for all reports dynamically
func CreateReportCommands() []*cobra.Command {
reports := engine.AllReports()
commands := make([]*cobra.Command, 0, len(reports))
// Create a command for each report
for name, report := range reports {
// Capture in closure
reportName := name
reportObj := report
cmd := &cobra.Command{
Use: reportName + " [filter...]",
Short: reportObj.Description,
Long: fmt.Sprintf("%s\n\nThis is a report that shows: %s", reportObj.Description, reportObj.Description),
Run: func(cmd *cobra.Command, args []string) {
parsed := getParsedArgs(cmd)
if err := runReport(reportName, parsed.Filters); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
},
}
commands = append(commands, cmd)
}
return commands
}
// runReport executes a report by name with optional filters
func runReport(reportName string, filters []string) error {
// Get the report
report, err := engine.GetReport(reportName)
if err != nil {
return err
}
// Execute the report (returns sorted tasks)
tasks, err := report.Execute(filters)
if err != nil {
return fmt.Errorf("failed to execute report: %w", err)
}
// Build working set from sorted tasks - IDs will match display order
ws, err := engine.BuildWorkingSet(tasks)
if err != nil {
return fmt.Errorf("failed to build working set: %w", err)
}
// Display tasks based on format
var output string
if report.DisplayFormat == engine.DisplayFormatMinimal {
output = engine.FormatTaskListWithFormat(tasks, ws, "minimal")
} else {
output = engine.FormatTaskListWithFormat(tasks, ws, "table")
}
fmt.Println(output)
return nil
}
// reportsCmd shows all available reports
var reportsCmd = &cobra.Command{
Use: "reports",
Short: "List all available reports",
Long: `Display a list of all available task reports with their descriptions.`,
Run: func(cmd *cobra.Command, args []string) {
reports := engine.AllReports()
// Sort by name
names := make([]string, 0, len(reports))
for name := range reports {
names = append(names, name)
}
sort.Strings(names)
fmt.Println("Available reports:\n")
for _, name := range names {
report := reports[name]
fmt.Printf(" %-12s %s\n", name, report.Description)
}
fmt.Println("\nUsage: opal <report-name> [filters...]")
fmt.Println("Example: opal ready +home")
},
}