Implement report system and fix template task filtering

- Fix template task filtering bug: templates now hidden from all reports
  except 'template' and 'all' reports, even when using custom filters
- Add support for status:template filter to explicitly show templates
- Implement comprehensive report system with 12 predefined reports:
  * active - Started tasks
  * all - All tasks including templates
  * completed - Completed tasks
  * list - Pending tasks (default)
  * minimal - Pending tasks in minimal format
  * newest - Most recent pending tasks
  * oldest - Oldest pending tasks
  * overdue - Overdue tasks
  * ready - Tasks ready to work on
  * recurring - Pending recurring instances
  * template - Recurring template tasks
  * waiting - Hidden/waiting tasks
- Replace list command with report-based architecture
- Add configurable default_report option (defaults to 'list')
- Add minimal display format (ID + description only)
- Support flexible syntax: 'opal <report> [filters]' or 'opal [filters] <report>'
- Add 'opal reports' command to list all available reports
This commit is contained in:
2026-01-05 21:17:07 +01:00
parent f5f7bc3ad7
commit 59861bc3bf
7 changed files with 568 additions and 68 deletions
-58
View File
@@ -1,58 +0,0 @@
package cmd
import (
"fmt"
"os"
"git.jnss.me/joakim/opal/internal/engine"
"github.com/spf13/cobra"
)
var listCmd = &cobra.Command{
Use: "list [filter...]",
Short: "List tasks",
Long: `List tasks matching the filter criteria.
Examples:
opal list # List all pending tasks
opal list +home # List tasks with +home tag
opal list project:backend # List backend project tasks
opal list priority:H # List high priority tasks
opal 2 list # List using filter 2 (flexible syntax)`,
Run: func(cmd *cobra.Command, args []string) {
parsed := getParsedArgs(cmd)
if err := listTasks(parsed.Filters); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
},
}
func listTasks(args []string) error {
// Parse filter
var filter *engine.Filter
var err error
if len(args) == 0 {
filter = engine.DefaultFilter()
} else {
filter, err = engine.ParseFilter(args)
if err != nil {
return fmt.Errorf("failed to parse filter: %w", err)
}
}
// Build working set
ws, err := engine.BuildWorkingSet(filter)
if err != nil {
return fmt.Errorf("failed to build working set: %w", err)
}
// Get tasks
tasks := ws.GetTasks()
// Display
fmt.Println(engine.FormatTaskList(tasks, ws))
return nil
}
+97
View File
@@ -0,0 +1,97 @@
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
tasks, err := report.Execute(filters)
if err != nil {
return fmt.Errorf("failed to execute report: %w", err)
}
// Build working set for display IDs
ws, err := engine.BuildWorkingSet(report.BaseFilter)
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")
},
}
+44 -7
View File
@@ -23,9 +23,16 @@ const parsedArgsKey contextKey = "parsedArgs"
// Command classification
var commandNames = []string{
"add", "list", "done", "modify", "delete",
"add", "done", "modify", "delete",
"start", "stop", "count", "projects", "tags",
"info", "edit", "server", "sync",
"info", "edit", "server", "sync", "reports",
}
// Report names (dynamically populated)
var reportNames = []string{
"active", "all", "completed", "list", "minimal",
"newest", "oldest", "overdue", "ready", "recurring",
"template", "waiting",
}
var commandsWithModifiers = map[string]bool{
@@ -39,9 +46,22 @@ var rootCmd = &cobra.Command{
Long: `Opal is a powerful command-line task manager inspired by taskwarrior.
It supports filtering, tags, priorities, projects, and recurring tasks.`,
Run: func(cmd *cobra.Command, args []string) {
// Default behavior: list tasks
// Default behavior: run configured default report (defaults to "list")
parsed := getParsedArgs(cmd)
if err := listTasks(parsed.Filters); err != nil {
// Get default report from config
cfg, err := engine.GetConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
os.Exit(1)
}
defaultReport := cfg.DefaultReport
if defaultReport == "" {
defaultReport = "list"
}
if err := runReport(defaultReport, parsed.Filters); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
@@ -102,11 +122,12 @@ func preprocessArgs(args []string) *ParsedArgs {
}
}
// Find command position
// Find command position (check both regular commands and reports)
cmdIdx := -1
cmdName := ""
for i, arg := range args {
// Check regular commands
for _, name := range commandNames {
if arg == name {
cmdIdx = i
@@ -114,6 +135,16 @@ func preprocessArgs(args []string) *ParsedArgs {
break
}
}
// Check report names
if cmdIdx < 0 {
for _, name := range reportNames {
if arg == name {
cmdIdx = i
cmdName = name
break
}
}
}
if cmdIdx >= 0 {
break
}
@@ -159,9 +190,8 @@ func preprocessArgs(args []string) *ParsedArgs {
func init() {
cobra.OnInitialize(initializeApp)
// Add subcommands
// Add regular subcommands
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(doneCmd)
rootCmd.AddCommand(modifyCmd)
rootCmd.AddCommand(deleteCmd)
@@ -172,6 +202,13 @@ func init() {
rootCmd.AddCommand(tagsCmd)
rootCmd.AddCommand(infoCmd)
rootCmd.AddCommand(editCmd)
rootCmd.AddCommand(reportsCmd)
// Add report commands dynamically
reportCommands := CreateReportCommands()
for _, cmd := range reportCommands {
rootCmd.AddCommand(cmd)
}
}
func initializeApp() {