-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand_plan_list.go
More file actions
145 lines (123 loc) · 3.61 KB
/
command_plan_list.go
File metadata and controls
145 lines (123 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package tasked
import (
"fmt"
"github.com/dhamidi/tasked/planner"
"github.com/spf13/cobra"
)
var PlanListCmd = &cobra.Command{
Use: "list",
Short: "List all plans grouped by context",
Long: `List all existing plans showing their names, completion status (DONE/TODO),
and task count information. Plans are grouped by project context, and plans matching
the current directory context are highlighted.`,
RunE: RunPlanList,
}
func RunPlanList(cmd *cobra.Command, args []string) error {
// Get the database file path from settings
dbPath := GlobalSettings.GetDatabaseFile()
// Detect context
currentCtx, err := GlobalSettings.GetOrCreateContext()
if err != nil {
fmt.Printf("Warning: could not detect current context: %v\n", err)
}
// Get current context path for comparison
var currentContextPath string
if currentCtx != nil {
currentContextPath = currentCtx.CurrentPath
if currentCtx.IsGitRepo {
currentContextPath = currentCtx.GitRoot
}
}
// Initialize the planner
p, err := planner.New(dbPath)
if err != nil {
return fmt.Errorf("failed to initialize planner: %w", err)
}
defer p.Close()
// Get all plans using the List method
plans, err := p.List()
if err != nil {
return fmt.Errorf("failed to list plans: %w", err)
}
// Handle empty list gracefully
if len(plans) == 0 {
fmt.Println("No plans found.")
return nil
}
// Group plans by context path and project name
type groupKey struct {
ContextPath string
ProjectName string
}
plansByGroup := make(map[groupKey][]planner.PlanInfo)
for _, plan := range plans {
key := groupKey{
ContextPath: plan.ContextPath,
ProjectName: plan.ProjectName,
}
plansByGroup[key] = append(plansByGroup[key], plan)
}
// Display context info if available
if currentCtx != nil {
fmt.Printf("Current context: %s", currentCtx.CurrentPath)
if currentCtx.IsGitRepo {
fmt.Printf(" (git root: %s)", currentCtx.GitRoot)
}
fmt.Printf("\n\n")
}
// Determine which group is "current" (matches current context)
var currentGroupKey groupKey
if currentCtx != nil {
currentGroupKey = groupKey{
ContextPath: currentContextPath,
ProjectName: currentCtx.ProjectName,
}
}
// Display plans, with current context group first
groupsToDisplay := make([]groupKey, 0, len(plansByGroup))
// Add current context group first if it exists
if _, ok := plansByGroup[currentGroupKey]; ok {
groupsToDisplay = append(groupsToDisplay, currentGroupKey)
}
// Add remaining groups
for key := range plansByGroup {
if key == currentGroupKey {
continue // Skip current group as it's already added
}
groupsToDisplay = append(groupsToDisplay, key)
}
// Display each group
for _, group := range groupsToDisplay {
groupPlans := plansByGroup[group]
// Determine if this is the current context group
var isCurrentContext bool
if currentCtx != nil {
isCurrentContext = (group.ContextPath == currentContextPath && group.ProjectName == currentCtx.ProjectName)
} else {
isCurrentContext = false
}
// Display group header
if isCurrentContext && currentCtx != nil {
fmt.Printf("%s context:\n", currentCtx.ProjectName)
} else {
fmt.Printf("%s context:\n", group.ProjectName)
}
// Display plans in this group
for _, plan := range groupPlans {
// Mark current context plans with *
marker := " "
if isCurrentContext {
marker = "* "
}
status := plan.Status
if plan.TotalTasks == 0 {
fmt.Printf("%s%s [%s] (no tasks)\n", marker, plan.Name, status)
} else {
fmt.Printf("%s%s [%s] (%d/%d tasks completed)\n",
marker, plan.Name, status, plan.CompletedTasks, plan.TotalTasks)
}
}
fmt.Println()
}
return nil
}