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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
|
package ui
import (
"fmt"
"os/exec"
"strings"
"time"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"codeberg.org/snonux/tasksamurai/internal/task"
)
// handleNormalMode handles keyboard input in normal mode (not editing)
func (m *Model) handleNormalMode(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
// If help is shown, handle special cases
if m.showHelp {
switch msg.String() {
case "H", "esc", "q":
return m.handleQuitOrEscape()
case "/", "?":
return m.handleHelpSearch()
case "n":
return m.handleNextHelpSearchMatch()
case "N":
return m.handlePrevHelpSearchMatch()
case "up", "k":
m.helpViewport.LineUp(1)
return m, nil
case "down", "j":
m.helpViewport.LineDown(1)
return m, nil
case "pgup", "b":
m.helpViewport.ViewUp()
return m, nil
case "pgdown", " ":
m.helpViewport.ViewDown()
return m, nil
case "g", "home":
m.helpViewport.GotoTop()
return m, nil
case "G", "end":
m.helpViewport.GotoBottom()
return m, nil
default:
// Ignore other keys in help mode
return m, nil
}
}
switch msg.String() {
case "H":
return m.handleToggleHelp()
case "q", "esc":
return m.handleQuitOrEscape()
case "e", "E":
return m.handleEditTask()
case "s":
return m.handleToggleStart()
case "d":
return m.handleMarkDone()
case "o":
return m.handleOpenURL()
case "U":
return m.handleUndo()
case "w":
return m.handleSetDueDate()
case "W":
return m.handleRemoveDueDate()
case "r":
return m.handleRandomDueDate()
case "R":
return m.handleSetRecurrence()
case "p":
return m.handleSetPriority()
case "a":
return m.handleAnnotate(false)
case "A":
return m.handleAnnotate(true)
case "f":
return m.handleFilter()
case "+":
return m.handleAddTask()
case "t":
return m.handleEditTags()
case "J":
return m.handleEditProject()
case "T":
return m.handleTagToProject()
case "c":
return m.handleRandomTheme()
case "C":
return m.handleResetTheme()
case "x":
return m.handleToggleDisco()
case " ":
return m.handleRefresh()
case "/", "?":
return m.handleSearch()
case "n":
return m.handleNextSearchMatch()
case "N":
return m.handlePrevSearchMatch()
case "enter":
return m.handleShowTaskDetail()
case "i":
return m.handleEnterOrEdit()
default:
// Pass through to table for navigation
return m.handleTableNavigation(msg)
}
}
func (m *Model) handleToggleHelp() (tea.Model, tea.Cmd) {
m.showHelp = true
// Initialize help viewport with proper dimensions
width := m.tbl.Width() - 4 // Account for padding
height := m.windowHeight - 6 // Leave room for status bars and search input
if width <= 0 {
width = 80 // Default width
}
if height <= 0 {
height = 20 // Default height
}
m.helpViewport = viewport.New(width, height)
// Set the content immediately
content := m.buildHelpContent()
m.helpViewport.SetContent(content)
return m, nil
}
func (m *Model) handleQuitOrEscape() (tea.Model, tea.Cmd) {
if m.showTaskDetail {
m.showTaskDetail = false
m.currentTaskDetail = nil
m.detailSearching = false
m.detailSearchRegex = nil
m.detailSearchInput.SetValue("")
return m, nil
}
if m.cellExpanded {
m.cellExpanded = false
m.updateTableHeight()
return m, nil
}
if m.showHelp {
m.showHelp = false
// Clear help search state
m.helpSearchRegex = nil
m.helpSearchMatches = nil
m.helpSearchIndex = 0
m.helpSearchInput.SetValue("")
// Reset help viewport
m.helpViewport = viewport.Model{}
return m, nil
}
if m.searchRegex != nil {
m.searchRegex = nil
m.searchMatches = nil
m.searchIndex = 0
m.reload()
return m, nil
}
return m, tea.Quit
}
func (m *Model) handleEditTask() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
m.editID = id
return m, editCmd(id)
}
func (m *Model) handleToggleStart() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
// Check if task is started
started := false
for _, tsk := range m.tasks {
if tsk.ID == id {
started = tsk.Start != ""
break
}
}
if started {
if err := task.Stop(id); err != nil {
m.showError(err)
return m, nil
}
} else {
if err := task.Start(id); err != nil {
m.showError(err)
return m, nil
}
}
m.reload()
return m, m.startBlink(id, false)
}
func (m *Model) handleMarkDone() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
return m, m.startBlink(id, true)
}
func (m *Model) handleOpenURL() (tea.Model, tea.Cmd) {
task := m.getTaskAtCursor()
if task == nil {
return m, nil
}
url := urlRegex.FindString(task.Description)
if url == "" {
return m, nil
}
if err := exec.Command(m.browserCmd, url).Run(); err != nil {
m.showError(fmt.Errorf("opening browser: %w", err))
return m, nil
}
return m, m.startBlink(task.ID, false)
}
func (m *Model) handleUndo() (tea.Model, tea.Cmd) {
if len(m.undoStack) == 0 {
return m, nil
}
uuid := m.undoStack[len(m.undoStack)-1]
m.undoStack = m.undoStack[:len(m.undoStack)-1]
if err := task.SetStatusUUID(uuid, "pending"); err != nil {
m.showError(err)
return m, nil
}
m.reload()
// Find the task ID for blinking
var id int
for _, tsk := range m.tasks {
if tsk.UUID == uuid {
id = tsk.ID
break
}
}
return m, m.startBlink(id, false)
}
func (m *Model) handleSetDueDate() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
m.clearEditingModes()
m.dueID = id
m.dueEditing = true
m.dueDate = time.Now()
m.updateTableHeight()
return m, nil
}
func (m *Model) handleRemoveDueDate() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
// In Taskwarrior, passing an empty value to due: removes the due date
if err := task.SetDueDate(id, ""); err != nil {
m.showError(err)
return m, nil
}
m.reload()
return m, m.startBlink(id, false)
}
func (m *Model) handleRandomDueDate() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
days := rng.Intn(31) + 7
due := time.Now().AddDate(0, 0, days).Format("2006-01-02")
if err := task.SetDueDate(id, due); err != nil {
m.showError(err)
return m, nil
}
m.reload()
return m, m.startBlink(id, false)
}
func (m *Model) handleSetRecurrence() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
task := m.getTaskAtCursor()
if task == nil {
return m, nil
}
m.clearEditingModes()
m.recurID = id
m.recurEditing = true
m.recurInput.SetValue(task.Recur)
m.recurInput.Focus()
m.updateTableHeight()
return m, nil
}
func (m *Model) handleSetPriority() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
m.clearEditingModes()
m.priorityID = id
m.prioritySelecting = true
m.priorityIndex = 0
m.updateTableHeight()
return m, nil
}
func (m *Model) handleAnnotate(replace bool) (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
m.clearEditingModes()
m.annotateID = id
m.annotating = true
m.replaceAnnotations = replace
m.annotateInput.SetValue("")
m.annotateInput.Focus()
m.updateTableHeight()
return m, nil
}
func (m *Model) handleFilter() (tea.Model, tea.Cmd) {
m.clearEditingModes()
m.filterEditing = true
m.filterInput.SetValue(strings.Join(m.filters, " "))
m.filterInput.Focus()
m.updateTableHeight()
return m, nil
}
func (m *Model) handleAddTask() (tea.Model, tea.Cmd) {
m.clearEditingModes()
m.addingTask = true
m.addInput.SetValue("")
m.addInput.Focus()
m.updateTableHeight()
return m, nil
}
func (m *Model) handleEditTags() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
m.clearEditingModes()
m.tagsID = id
m.tagsEditing = true
m.tagsInput.SetValue("")
m.tagsInput.Focus()
m.updateTableHeight()
return m, nil
}
func (m *Model) handleEditProject() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
m.clearEditingModes()
m.projID = id
m.projEditing = true
// Get current project value
task := m.getTaskAtCursor()
if task != nil {
m.projInput.SetValue(task.Project)
} else {
m.projInput.SetValue("")
}
m.projInput.Focus()
m.updateTableHeight()
return m, nil
}
func (m *Model) handleTagToProject() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
// Get the task at cursor
currentTask := m.getTaskAtCursor()
if currentTask == nil || len(currentTask.Tags) == 0 {
// No tags to convert
return m, nil
}
// Get the first tag
firstTag := currentTask.Tags[0]
// Set the tag as project
if err := task.SetProject(id, firstTag); err != nil {
m.showError(err)
return m, nil
}
// Remove the tag from the task
if err := task.RemoveTags(id, []string{firstTag}); err != nil {
m.showError(err)
return m, nil
}
m.reload()
return m, m.startBlink(id, false)
}
func (m *Model) handleRandomTheme() (tea.Model, tea.Cmd) {
m.theme = RandomTheme()
m.applyTheme()
return m, nil
}
func (m *Model) handleResetTheme() (tea.Model, tea.Cmd) {
m.theme = m.defaultTheme
m.applyTheme()
return m, nil
}
func (m *Model) handleToggleDisco() (tea.Model, tea.Cmd) {
m.disco = !m.disco
return m, nil
}
func (m *Model) handleRefresh() (tea.Model, tea.Cmd) {
m.reload()
return m, nil
}
func (m *Model) handleSearch() (tea.Model, tea.Cmd) {
m.clearEditingModes()
m.searching = true
m.searchIndex = 0
m.searchMatches = nil
m.searchInput.SetValue("")
m.searchInput.Focus()
m.updateTableHeight()
return m, nil
}
func (m *Model) handleNextSearchMatch() (tea.Model, tea.Cmd) {
if len(m.searchMatches) == 0 {
return m, nil
}
m.searchIndex = (m.searchIndex + 1) % len(m.searchMatches)
match := m.searchMatches[m.searchIndex]
prevRow := m.tbl.Cursor()
prevCol := m.tbl.ColumnCursor()
m.tbl.SetCursor(match.row)
m.tbl.SetColumnCursor(match.col)
m.updateSelectionHighlight(prevRow, m.tbl.Cursor(), prevCol, m.tbl.ColumnCursor())
return m, nil
}
func (m *Model) handlePrevSearchMatch() (tea.Model, tea.Cmd) {
if len(m.searchMatches) == 0 {
return m, nil
}
m.searchIndex = (m.searchIndex - 1 + len(m.searchMatches)) % len(m.searchMatches)
match := m.searchMatches[m.searchIndex]
prevRow := m.tbl.Cursor()
prevCol := m.tbl.ColumnCursor()
m.tbl.SetCursor(match.row)
m.tbl.SetColumnCursor(match.col)
m.updateSelectionHighlight(prevRow, m.tbl.Cursor(), prevCol, m.tbl.ColumnCursor())
return m, nil
}
func (m *Model) handleHelpSearch() (tea.Model, tea.Cmd) {
m.helpSearching = true
m.helpSearchIndex = 0
m.helpSearchMatches = nil
m.helpSearchInput.SetValue("")
m.helpSearchInput.Focus()
return m, nil
}
func (m *Model) handleNextHelpSearchMatch() (tea.Model, tea.Cmd) {
if len(m.helpSearchMatches) == 0 {
return m, nil
}
m.helpSearchIndex = (m.helpSearchIndex + 1) % len(m.helpSearchMatches)
// In the future, we could add visual indication of current match
return m, nil
}
func (m *Model) handlePrevHelpSearchMatch() (tea.Model, tea.Cmd) {
if len(m.helpSearchMatches) == 0 {
return m, nil
}
m.helpSearchIndex = (m.helpSearchIndex - 1 + len(m.helpSearchMatches)) % len(m.helpSearchMatches)
// In the future, we could add visual indication of current match
return m, nil
}
func (m *Model) handleShowTaskDetail() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
return m, nil
}
// Find the task with this ID
for i := range m.tasks {
if m.tasks[i].ID == id {
m.showTaskDetail = true
m.currentTaskDetail = &m.tasks[i]
m.detailSearching = false
m.detailSearchRegex = nil
m.detailFieldIndex = 0
m.detailBlinkField = -1
m.detailBlinkOn = false
m.detailBlinkCount = 0
m.detailSearchInput = textinput.New()
m.detailSearchInput.Placeholder = "Search..."
m.detailSearchInput.Width = 30
break
}
}
return m, nil
}
func (m *Model) handleEnterOrEdit() (tea.Model, tea.Cmd) {
id, err := m.getSelectedTaskID()
if err != nil {
// No task selected, toggle cell expansion
m.cellExpanded = !m.cellExpanded
m.updateTableHeight()
return m, nil
}
col := m.tbl.ColumnCursor()
switch col {
case 0: // Priority
m.clearEditingModes()
m.priorityID = id
m.prioritySelecting = true
// Set current priority index
task := m.getTaskAtCursor()
if task != nil {
switch task.Priority {
case "H":
m.priorityIndex = 0
case "M":
m.priorityIndex = 1
case "L":
m.priorityIndex = 2
default:
m.priorityIndex = 3
}
}
m.updateTableHeight()
return m, nil
case 3: // Project
m.clearEditingModes()
m.projID = id
m.projEditing = true
task := m.getTaskAtCursor()
if task != nil {
m.projInput.SetValue(task.Project)
}
m.projInput.Focus()
m.updateTableHeight()
return m, nil
case 4: // Due date
m.dueID = id
task := m.getTaskAtCursor()
if task != nil && task.Due != "" {
if ts, err := parseTaskDate(task.Due); err == nil {
m.dueDate = ts
} else {
m.dueDate = time.Now()
}
} else {
m.dueDate = time.Now()
}
m.clearEditingModes()
m.dueEditing = true
m.updateTableHeight()
return m, nil
case 5: // Recurrence
m.clearEditingModes()
m.recurID = id
m.recurEditing = true
task := m.getTaskAtCursor()
if task != nil {
m.recurInput.SetValue(task.Recur)
}
m.recurInput.Focus()
m.updateTableHeight()
return m, nil
case 6: // Tags
m.clearEditingModes()
m.tagsID = id
m.tagsEditing = true
m.tagsInput.SetValue("")
m.tagsInput.Focus()
m.updateTableHeight()
return m, nil
case 7: // Annotations
m.clearEditingModes()
m.annotateID = id
m.annotating = true
m.replaceAnnotations = true
// Get current annotations
task := m.getTaskAtCursor()
if task != nil {
var anns []string
for _, a := range task.Annotations {
anns = append(anns, a.Description)
}
m.annotateInput.SetValue(strings.Join(anns, "; "))
}
m.annotateInput.Focus()
m.updateTableHeight()
return m, nil
case 8: // Description
m.clearEditingModes()
m.descID = id
m.descEditing = true
task := m.getTaskAtCursor()
if task != nil {
m.descInput.SetValue(task.Description)
}
m.descInput.Focus()
m.updateTableHeight()
return m, nil
default:
// Toggle cell expansion for other columns
m.cellExpanded = !m.cellExpanded
m.updateTableHeight()
return m, nil
}
}
func (m *Model) handleTableNavigation(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
prevRow := m.tbl.Cursor()
prevCol := m.tbl.ColumnCursor()
var cmd tea.Cmd
m.tbl, cmd = m.tbl.Update(msg)
if prevRow != m.tbl.Cursor() || prevCol != m.tbl.ColumnCursor() {
m.updateSelectionHighlight(prevRow, m.tbl.Cursor(), prevCol, m.tbl.ColumnCursor())
}
return m, cmd
}
// showError displays an error in the status bar
func (m *Model) showError(err error) {
m.statusMsg = fmt.Sprintf("Error: %v", err)
// Note: we can't return a Cmd from here, so the error will stay until next update
}
|