-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
509 lines (423 loc) · 13.3 KB
/
main.go
File metadata and controls
509 lines (423 loc) · 13.3 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/scttymn/todo-cli/pkg"
"github.com/spf13/cobra"
)
func requiresInit() bool {
// Just ensure .todo directory exists
if err := pkg.EnsureTodoDirectory(); err != nil {
fmt.Printf("Failed to create .todo directory: %v\n", err)
return true
}
return false
}
var rootCmd = &cobra.Command{
Use: "todo [command] [flags]",
Short: "A CLI tool for managing todo lists",
Long: `todo is a CLI tool that manages todo lists in markdown files, helping you track tasks for different projects or features.`,
}
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize todo management in the current directory",
Long: `Initialize the current directory for todo management by creating the .todo directory.`,
Run: func(cmd *cobra.Command, args []string) {
err := pkg.EnsureTodoDirectory()
if err != nil {
fmt.Printf("Failed to initialize todo directory: %v\n", err)
return
}
fmt.Println("✅ Todo management initialized successfully!")
fmt.Println("You can now create todo lists with: todo list <name>")
},
}
var addCmd = &cobra.Command{
Use: "add [todo-item]",
Short: "Add a todo item to the current list",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if requiresInit() {
return
}
todoItem := args[0]
currentList, err := pkg.GetCurrentList()
if err != nil {
fmt.Printf("Error getting current list: %v\n", err)
return
}
err = pkg.AddTodoItem(currentList, todoItem)
if err != nil {
fmt.Printf("Error adding todo item: %v\n", err)
return
}
fmt.Printf("Added todo item to list '%s': %s\n", currentList, todoItem)
},
}
var checkCmd = &cobra.Command{
Use: "check [item-number]",
Short: "Mark a todo item as completed",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if requiresInit() {
return
}
itemNumber := args[0]
currentList, err := pkg.GetCurrentList()
if err != nil {
fmt.Printf("Error getting current list: %v\n", err)
return
}
itemID, err := strconv.Atoi(itemNumber)
if err != nil {
fmt.Printf("Invalid item number: %s\n", itemNumber)
return
}
err = pkg.CheckTodoItem(currentList, itemID)
if err != nil {
fmt.Printf("Error checking todo item: %v\n", err)
return
}
fmt.Printf("Marked item %d as completed in list '%s'\n", itemID, currentList)
},
}
var uncheckCmd = &cobra.Command{
Use: "uncheck [item-number]",
Short: "Mark a todo item as not completed",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if requiresInit() {
return
}
itemNumber := args[0]
currentList, err := pkg.GetCurrentList()
if err != nil {
fmt.Printf("Error getting current list: %v\n", err)
return
}
itemID, err := strconv.Atoi(itemNumber)
if err != nil {
fmt.Printf("Invalid item number: %s\n", itemNumber)
return
}
err = pkg.UncheckTodoItem(currentList, itemID)
if err != nil {
fmt.Printf("Error unchecking todo item: %v\n", err)
return
}
fmt.Printf("Marked item %d as not completed in list '%s'\n", itemID, currentList)
},
}
var progressCmd = &cobra.Command{
Use: "progress [list-name]",
Short: "Show progress for current list, specific list, or all lists\n Available flags: --all",
Long: `Show todo progress:\n\n todo progress Current list progress\n todo progress <name> Specific list progress\n todo progress --all All lists progress`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if requiresInit() {
return
}
showAll, _ := cmd.Flags().GetBool("all")
if showAll {
if len(args) > 0 {
fmt.Println("Error: Cannot use --all flag with list name")
return
}
err := pkg.ListAllFeatures()
if err != nil {
fmt.Printf("Error showing progress: %v\n", err)
return
}
} else if len(args) == 1 {
// Show progress for specific list
listName := args[0]
// Check if the list exists by checking if todo file exists
if !pkg.TodoFileExists(listName) {
fmt.Printf("List '%s' does not exist\n", listName)
return
}
err := pkg.DisplayTodoList(listName)
if err != nil {
fmt.Printf("Error displaying todo list: %v\n", err)
return
}
} else {
// Show progress for current list
currentList, err := pkg.GetCurrentList()
if err != nil {
fmt.Printf("Error getting current list: %v\n", err)
return
}
err = pkg.DisplayTodoList(currentList)
if err != nil {
fmt.Printf("Error displaying todo list: %v\n", err)
return
}
}
},
}
var listCmd = &cobra.Command{
Use: "list [list-name]",
Short: "Show all lists, switch to lists, or create new lists\n Available flags: --delete",
Long: `Manage todo lists:\n\n todo list Show all lists with progress\n todo list <name> Switch to or create list\n todo list --delete <name> Delete list (requires confirmation)`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if requiresInit() {
return
}
deleteFlag, _ := cmd.Flags().GetBool("delete")
if deleteFlag {
if len(args) == 0 {
fmt.Println("Error: --delete requires a list name")
return
}
listName := args[0]
// Check if we're currently on the list we're trying to delete
currentList, err := pkg.GetCurrentList()
if err != nil {
fmt.Printf("Error getting current list: %v\n", err)
return
}
if currentList == listName {
fmt.Printf("Error: Cannot delete list '%s' because it is currently active.\n", listName)
fmt.Println("Switch to another list first (e.g., 'todo list main')")
return
}
// Check if list exists
if !pkg.ListExists(listName) {
fmt.Printf("List '%s' does not exist\n", listName)
return
}
// Confirmation prompt
fmt.Printf("Are you sure you want to delete list '%s'? This will remove the todo file. (y/N): ", listName)
reader := bufio.NewReader(os.Stdin)
response, err := reader.ReadString('\n')
if err != nil {
fmt.Printf("Error reading input: %v\n", err)
return
}
response = strings.TrimSpace(strings.ToLower(response))
if response != "y" && response != "yes" {
fmt.Println("Delete cancelled.")
return
}
// Delete the todo file
err = pkg.DeleteList(listName)
if err != nil {
fmt.Printf("Error deleting list: %v\n", err)
return
}
fmt.Printf("Successfully deleted list '%s'\n", listName)
return
}
if len(args) == 0 {
// Show all lists
err := pkg.ListAllFeatures()
if err != nil {
fmt.Printf("Error showing lists: %v\n", err)
return
}
} else {
// Switch to or create specific list
listName := args[0]
// Set as current list
err := pkg.SetCurrentList(listName)
if err != nil {
fmt.Printf("Error setting current list: %v\n", err)
return
}
// Create todo file if it doesn't exist
if !pkg.TodoFileExists(listName) {
err = pkg.CreateTodoFile(listName)
if err != nil {
fmt.Printf("Error creating todo file: %v\n", err)
return
}
fmt.Printf("Created todo list '%s'\n", listName)
} else {
fmt.Printf("Switched to list '%s'\n", listName)
}
// Display current todos
err = pkg.DisplayTodoList(listName)
if err != nil {
fmt.Printf("Error displaying todo list: %v\n", err)
return
}
}
},
}
var historyCmd = &cobra.Command{
Use: "history",
Short: "Show history of completed todos across all lists",
Long: `Display a chronological history of all completed todos with timestamps, organized by date.`,
Run: func(cmd *cobra.Command, args []string) {
if requiresInit() {
return
}
err := pkg.ShowHistory()
if err != nil {
fmt.Printf("Failed to show history: %v\n", err)
return
}
},
}
var infoCmd = &cobra.Command{
Use: "info",
Short: "Output comprehensive information about todo CLI for LLM assistants",
Long: `Outputs detailed information about the todo CLI structure, commands, and usage patterns designed for LLM assistants to understand how to use the tool effectively.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(`# Todo CLI - LLM Assistant Guide
## Overview
This is a CLI tool for managing todo lists. Each todo list is stored as a markdown file in the .todo directory.
## Core Concepts
- **Lists**: Each todo list is a separate markdown file
- **Storage**: Todo items stored in .todo/<list-name>.md files
- **Current List**: Track which list is currently active via .current-list file
## Available Commands
### 1. todo init
Initialize todo management in the current directory.
- Use when: Directory lacks .todo setup
- Creates: .todo directory for storing todo files
### 2. todo list [list-name]
Manage todo lists (create, switch, view, delete).
- 'todo list' - Show all lists with progress percentages
- 'todo list <name>' - Switch to or create list
- 'todo list --delete <name>' - Delete list (requires confirmation)
### 3. todo add <item>
Add todo item to current list.
- Takes: Single quoted string argument
- Example: todo add "Implement user authentication"
### 4. todo check <number>
Mark todo item as completed.
- Takes: Item number (1-based indexing)
- Example: todo check 1
### 5. todo uncheck <number>
Mark todo item as incomplete.
- Takes: Item number (1-based indexing)
- Example: todo uncheck 2
### 6. todo progress [list-name]
Show progress for lists.
- 'todo progress' - Current list progress
- 'todo progress <name>' - Specific list progress
- 'todo progress --all' - All lists progress
### 7. todo history
Show chronological history of completed todos across all lists.
### 8. todo edit
Open current list in your configured editor ($EDITOR).
### 9. todo version
Show CLI version.
## File Structure
` + "```" + `
project/
├── .todo/
│ ├── main.md
│ ├── feature-auth.md
│ └── bug-fixes.md
├── .current-list
└── [project files]
` + "```" + `
## Todo File Format
Standard markdown with checkboxes:
` + "```" + `
# Todo List for feature-auth
- [ ] Incomplete task
- [x] Completed task (completed: 2024-01-15 10:30)
` + "```" + `
## Common Workflows
### Starting New Work
1. 'todo list my-feature' (creates or switches to list)
2. 'todo add "First task"'
3. 'todo add "Second task"'
4. Work and mark complete: 'todo check 1'
### Checking Progress
- 'todo progress' (current list)
- 'todo list' (all lists overview)
- 'todo progress --all' (detailed all lists)
### Switching Between Lists
- 'todo list other-feature' (switches to different list)
### Cleanup
- 'todo list --delete completed-feature' (removes todo file)
## Error Handling
- Creates .todo directory automatically if missing
- Prevents deleting currently active list
- Validates item numbers for check/uncheck
- Confirms before deleting lists
## Tips for LLM Assistants
1. Always check current status with 'todo progress' or 'todo list'
2. Use descriptive names for lists (feature names, project areas)
3. Add todos in logical order (dependencies first)
4. Check progress regularly to track completion
5. Clean up completed lists to maintain organization
## Safety Features
- Delete confirmations
- Current list protection
- Automatic directory creation
- Timestamp tracking for completed items
This tool is designed for developers who want flexible todo management.
`)
},
}
var editCmd = &cobra.Command{
Use: "edit",
Short: "Open the current todo list in your configured editor",
Long: `Open the current todo list file in your configured editor (set via $EDITOR environment variable).`,
Run: func(cmd *cobra.Command, args []string) {
if requiresInit() {
return
}
currentList, err := pkg.GetCurrentList()
if err != nil {
fmt.Printf("Error getting current list: %v\n", err)
return
}
err = pkg.EditTodoFile(currentList)
if err != nil {
fmt.Printf("Error opening editor: %v\n", err)
return
}
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Show the version of todo CLI",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("todo CLI v0.3.0")
},
}
func init() {
// Add the --all flag to progress command
progressCmd.Flags().BoolP("all", "a", false, "Show progress for all features")
// Add the --delete flag to list command
listCmd.Flags().BoolP("delete", "d", false, "Delete the specified list")
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(checkCmd)
rootCmd.AddCommand(uncheckCmd)
rootCmd.AddCommand(progressCmd)
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(historyCmd)
rootCmd.AddCommand(infoCmd)
rootCmd.AddCommand(editCmd)
rootCmd.AddCommand(versionCmd)
}
func main() {
rootCmd.SetUsageTemplate(`Usage:
{{.UseLine}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}