-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtasks.go
1424 lines (1125 loc) · 31.1 KB
/
tasks.go
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sncli
import (
"errors"
"fmt"
"slices"
"sort"
"strconv"
"strings"
"time"
"github.com/alexeyco/simpletable"
"github.com/asdine/storm/v3/q"
"github.com/dustin/go-humanize"
"github.com/gookit/color"
"github.com/jonhadfield/gosn-v2/cache"
"github.com/jonhadfield/gosn-v2/common"
"github.com/jonhadfield/gosn-v2/items"
"github.com/pterm/pterm"
)
const (
txtOrderingLastUpdated = "last-updated"
defaultMaxLength = 80
selectListHeight = 10
)
type ListTasklistsInput struct {
Session *cache.Session
Ordering string
Debug bool
}
func outputTime(updated time.Time, created time.Time) string {
updatedAt := humanize.Time(updated)
if updated.IsZero() {
updatedAt = humanize.Time(created)
}
return updatedAt
}
func (ci *ListTasklistsInput) Run() error {
stdLists, advLists, err := getAllLists(ci.Session)
if err != nil {
return err
}
table := simpletable.New()
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignCenter, Text: color.Bold.Text("title")},
{Align: simpletable.AlignCenter, Text: color.Bold.Text("type")},
{Align: simpletable.AlignCenter, Text: color.Bold.Text("updated")},
{Align: simpletable.AlignCenter, Text: color.Bold.Text("uuid")},
},
}
if ci.Ordering == txtOrderingLastUpdated {
advLists.Sort()
}
// get advanced checklist rows
for _, row := range stdLists {
r := []*simpletable.Cell{
{Align: simpletable.AlignLeft, Text: row.Title},
{Align: simpletable.AlignLeft, Text: "std"},
{Align: simpletable.AlignLeft, Text: outputTime(row.UpdatedAt, time.Time{})},
// {Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", taskListsConflictedWarning(row.Duplicates))},
{Align: simpletable.AlignLeft, Text: row.UUID},
}
table.Body.Cells = append(table.Body.Cells, r)
}
// get advanced checklist rows
for _, row := range advLists {
r := []*simpletable.Cell{
{Align: simpletable.AlignLeft, Text: row.Title},
{Align: simpletable.AlignLeft, Text: "adv"},
{Align: simpletable.AlignLeft, Text: outputTime(row.UpdatedAt, time.Time{})},
// {Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", conflictedWarning(row.Duplicates))},
{Align: simpletable.AlignLeft, Text: row.UUID},
}
table.Body.Cells = append(table.Body.Cells, r)
}
table.SetStyle(simpletable.StyleRounded)
fmt.Println(table.String())
return nil
}
func itemsToListNotes(sess *cache.Session, agitems cache.Items, listType string) (items.Notes, error) {
gis, err := agitems.ToItems(sess)
if err != nil {
return nil, err
}
gis.Filter(items.ItemFilters{
Filters: []items.Filter{
{
Type: common.SNItemTypeNote,
Key: "editor",
Comparison: "==",
Value: listType,
},
},
})
return gis.Notes(), nil
}
func getTasklists(sess *cache.Session, cacheItems cache.Items) (items.Tasklists, error) {
allItemUUIDs := cacheItems.UUIDs()
var gitems items.Items
gitems, err := cacheItems.ToItems(sess)
if err != nil {
return items.Tasklists{}, err
}
gitems.Filter(items.ItemFilters{
Filters: []items.Filter{
{
Type: "Note",
Key: "editor",
Comparison: "==",
Value: items.SimpleTaskEditorNoteType,
},
},
})
var checklists items.Tasklists
checklistNotes := gitems.Notes()
duplicatesMap, err := getTasklistsDuplicatesMap(checklistNotes)
if err != nil {
return nil, err
}
// strip any duplicated items that no longer exist
for k := range duplicatesMap {
if !slices.Contains(allItemUUIDs, k) {
delete(duplicatesMap, k)
}
}
// second pass to get all non-deleted and non-trashed checklists
for x := range checklistNotes {
// strip deleted and trashed
if checklistNotes[x].Deleted || checklistNotes[x].Content.Trashed != nil && *checklistNotes[x].Content.Trashed {
continue
}
var cl items.Tasklist
cl, err = checklistNotes[x].Content.ToTaskList()
if err != nil {
return items.Tasklists{}, err
}
cl.UUID = checklistNotes[x].UUID
cl.UpdatedAt, err = time.Parse(timeLayout, checklistNotes[x].UpdatedAt)
if err != nil {
return items.Tasklists{}, err
}
cl.Duplicates = duplicatesMap[checklistNotes[x].UUID]
checklists = append(checklists, cl)
}
return checklists, nil
}
func getAllLists(sess *cache.Session) (items.Tasklists, items.AdvancedChecklists, error) {
var so cache.SyncOutput
so, err := Sync(cache.SyncInput{
Session: sess,
}, true)
if err != nil {
return nil, nil, err
}
defer so.DB.Close()
var cacheItems cache.Items
if err = so.DB.All(&cacheItems); err != nil {
return nil, nil, err
}
std, err := getTasklists(sess, cacheItems)
if err != nil {
return nil, nil, err
}
adv, err := getAdvancedChecklists(sess, cacheItems)
if err != nil {
return nil, nil, err
}
return std, adv, nil
}
func getAllMatchingLists(sess *cache.Session, title, uuid string) (items.Tasklists, items.AdvancedChecklists, error) {
var so cache.SyncOutput
so, err := Sync(cache.SyncInput{
Session: sess,
}, true)
if err != nil {
return nil, nil, err
}
var cacheItems cache.Items
if err = so.DB.All(&cacheItems); err != nil {
return nil, nil, err
}
allStd, err := getTasklists(sess, cacheItems)
if err != nil {
return nil, nil, err
}
var std items.Tasklists
for x := range allStd {
if allStd[x].Title == title || allStd[x].UUID == uuid {
std = append(std, allStd[x])
}
}
allAdv, err := getAdvancedChecklists(sess, cacheItems)
if err != nil {
return nil, nil, err
}
var adv items.AdvancedChecklists
for x := range allAdv {
if allAdv[x].Title == title || allAdv[x].UUID == uuid {
adv = append(adv, allAdv[x])
}
}
if len(std) == 0 && len(adv) == 0 {
return nil, nil, errors.New("list not found")
}
return std, adv, nil
}
func getAdvancedChecklists(sess *cache.Session, cacheItems cache.Items) (items.AdvancedChecklists, error) {
allItemUUIDs := cacheItems.UUIDs()
var checklists items.AdvancedChecklists
listNotes, err := itemsToListNotes(sess, cacheItems, items.AdvancedChecklistNoteType)
if err != nil {
return nil, err
}
duplicatesMap, err := getAdvancedChecklistsDuplicatesMap(listNotes)
if err != nil {
return nil, err
}
// strip any duplicated items that no longer exist
for k := range duplicatesMap {
if !slices.Contains(allItemUUIDs, k) {
delete(duplicatesMap, k)
}
}
// second pass to get all non-deleted and non-trashed checklists
for x := range listNotes {
// strip deleted and trashed
if listNotes[x].Deleted || listNotes[x].Content.Trashed != nil && *listNotes[x].Content.Trashed {
continue
}
var cl items.AdvancedChecklist
cl, err = listNotes[x].Content.ToAdvancedCheckList()
if err != nil {
return items.AdvancedChecklists{}, err
}
cl.UUID = listNotes[x].UUID
cl.UpdatedAt, err = time.Parse(timeLayout, listNotes[x].UpdatedAt)
if err != nil {
return items.AdvancedChecklists{}, err
}
cl.Duplicates = duplicatesMap[listNotes[x].UUID]
checklists = append(checklists, cl)
}
return checklists, nil
}
func taskListsConflictedWarning([]items.Tasklist) string {
if len(items.Tasklists{}) > 0 {
return color.Yellow.Sprintf("%d conflicted versions", len(items.Tasklists{}))
}
return "-"
}
// construct a map of duplicates.
func getAdvancedChecklistsDuplicatesMap(checklistNotes items.Notes) (map[string][]items.AdvancedChecklist, error) {
duplicates := make(map[string][]items.AdvancedChecklist)
for x := range checklistNotes {
if checklistNotes[x].DuplicateOf == "" {
continue
}
// checklist is a duplicate
// get the checklist content
cl, err := checklistNotes[x].Content.ToAdvancedCheckList()
if err != nil {
return map[string][]items.AdvancedChecklist{}, err
}
// skip trashed content
if cl.Trashed {
continue
}
cl.UUID = checklistNotes[x].UUID
cl.UpdatedAt, err = time.Parse(timeLayout, checklistNotes[x].UpdatedAt)
if err != nil {
return map[string][]items.AdvancedChecklist{}, err
}
duplicates[checklistNotes[x].DuplicateOf] = append(duplicates[checklistNotes[x].DuplicateOf], cl)
}
return duplicates, nil
}
// construct a map of duplicates.
func getTasklistsDuplicatesMap(checklistNotes items.Notes) (map[string][]items.Tasklist, error) {
duplicates := make(map[string][]items.Tasklist)
for x := range checklistNotes {
if checklistNotes[x].DuplicateOf == "" {
continue
}
// checklist is a duplicate
// get the checklist content
cl, err := checklistNotes[x].Content.ToTaskList()
if err != nil {
return map[string][]items.Tasklist{}, err
}
// skip trashed content
if cl.Trashed {
continue
}
cl.UUID = checklistNotes[x].UUID
cl.UpdatedAt, err = time.Parse(timeLayout, checklistNotes[x].UpdatedAt)
if err != nil {
return map[string][]items.Tasklist{}, err
}
duplicates[checklistNotes[x].DuplicateOf] = append(duplicates[checklistNotes[x].DuplicateOf], cl)
}
return duplicates, nil
}
type AddAdvancedChecklistTaskInput struct {
Session *cache.Session
Debug bool
UUID string
Title string
Group string
Tasklist string
}
type AddTaskInput struct {
Session *cache.Session
Debug bool
Title string
UUID string
Tasklist string
}
func (ci *AddTaskInput) Run() (err error) {
tasklist, err := getTasklist(ci.Session, ci.Tasklist, ci.UUID)
if err != nil {
return err
}
// add task to the tasklist
if err = tasklist.AddTask(ci.Title); err != nil {
return
}
// get corresponding note
note, err := getNoteByUUID(ci.Session, tasklist.UUID)
if err != nil {
return err
}
clnt := items.TasksToNoteText(tasklist.Tasks)
now := time.Now().UTC()
note.Content.SetUpdateTime(now)
note.Content.SetText(clnt)
// save note to db
si := cache.SyncInput{
Session: ci.Session,
Close: false,
}
_, err = cache.Sync(si)
if err != nil {
return err
}
ci.Session.CacheDB = si.CacheDB
if err = cache.SaveNotes(ci.Session, ci.Session.CacheDB, items.Notes{note}, true); err != nil {
return
}
si = cache.SyncInput{
Session: ci.Session,
Close: true,
}
if _, err = cache.Sync(si); err != nil {
return
}
return nil
}
func (ci *AddAdvancedChecklistTaskInput) Run() (err error) {
// sync to get db
_, err = Sync(cache.SyncInput{
Session: ci.Session,
}, true)
if err != nil {
return err
}
// get matching note
notes, err := getNotesByTitleUUID(ci.Session, ci.Tasklist, ci.UUID, items.AdvancedChecklistNoteType)
if err != nil {
return err
}
var cl items.AdvancedChecklist
cl, err = notes[0].Content.ToAdvancedCheckList()
if err != nil {
return err
}
// add task to the checklist
if err = cl.AddTask(ci.Group, ci.Title); err != nil {
return
}
taskNoteText := items.AdvancedCheckListToNoteText(cl)
now := time.Now().UTC()
notes[0].Content.SetUpdateTime(now)
notes[0].Content.SetText(taskNoteText)
notes[0].Content.SetUpdateTime(now)
// save note to db
si := cache.SyncInput{
Session: ci.Session,
Close: false,
}
if err = cache.SaveNotes(ci.Session, ci.Session.CacheDB, items.Notes{notes[0]}, true); err != nil {
return
}
if _, err = cache.Sync(si); err != nil {
return
}
return nil
}
func getNoteByUUID(sess *cache.Session, uuid string) (note items.Note, err error) {
if sess.CacheDBPath == "" {
return items.Note{}, errors.New("CacheDBPath missing from session")
}
if uuid == "" {
return items.Note{}, errors.New("uuid not supplied")
}
var so cache.SyncOutput
si := cache.SyncInput{
Session: sess,
Close: false,
}
so, err = cache.Sync(si)
if err != nil {
return
}
defer func() {
_ = so.DB.Close()
}()
var encNotes cache.Items
query := so.DB.Select(q.And(q.Eq("UUID", uuid), q.Eq("Deleted", false)))
if err = query.Find(&encNotes); err != nil {
if strings.Contains(err.Error(), "not found") {
return note, fmt.Errorf("could not find note with inUUID %s", uuid)
}
return
}
var rawEncItems items.Items
rawEncItems, err = encNotes.ToItems(sess)
return *rawEncItems[0].(*items.Note), err
}
func getNotesByTitleUUID(sess *cache.Session, title string, uuid string, editor string) (items.Notes, error) {
if title == "" && uuid == "" {
return nil, errors.New("title or uuid required")
}
var allPersistedItems cache.Items
if err := sess.CacheDB.All(&allPersistedItems); err != nil {
return nil, err
}
var gitems items.Items
gitems, err := allPersistedItems.ToItems(sess)
if err != nil {
return nil, err
}
filters := []items.Filter{
{
Type: common.SNItemTypeNote,
Key: "editor",
Comparison: "==",
Value: editor,
},
}
if title != "" {
filters = append(filters, items.Filter{
Type: common.SNItemTypeNote,
Key: "title",
Comparison: "==",
Value: title,
})
}
if uuid != "" {
filters = append(filters, items.Filter{
Type: common.SNItemTypeNote,
Key: "uuid",
Comparison: "==",
Value: uuid,
})
}
gitems.Filter(items.ItemFilters{
Filters: filters,
})
tasklistNotes := gitems.Notes()
if len(tasklistNotes) == 0 {
return nil, errors.New("list not found")
}
if len(tasklistNotes) > 1 {
return nil, fmt.Errorf("%d lists found with same title/uuid", len(tasklistNotes))
}
return tasklistNotes, nil
}
func getTasklist(sess *cache.Session, title string, uuid string) (items.Tasklist, error) {
if title == "" && uuid == "" {
return items.Tasklist{}, fmt.Errorf("title and/or uuid must be specified")
}
var so cache.SyncOutput
so, err := Sync(cache.SyncInput{
Session: sess,
}, true)
if err != nil {
return items.Tasklist{}, err
}
defer func() {
_ = so.DB.Close()
}()
var allPersistedItems cache.Items
if err = so.DB.All(&allPersistedItems); err != nil {
return items.Tasklist{}, err
}
allItemUUIDs := allPersistedItems.UUIDs()
var gitems items.Items
gitems, err = allPersistedItems.ToItems(sess)
if err != nil {
return items.Tasklist{}, err
}
filters := []items.Filter{
{
Type: common.SNItemTypeNote,
Key: "editor",
Comparison: "==",
Value: items.SimpleTaskEditorNoteType,
},
}
if title != "" {
filters = append(filters, items.Filter{
Type: common.SNItemTypeNote,
Key: "title",
Comparison: "==",
Value: title,
})
}
if uuid != "" {
filters = append(filters, items.Filter{
Type: common.SNItemTypeNote,
Key: "uuid",
Comparison: "==",
Value: uuid,
})
}
gitems.Filter(items.ItemFilters{
Filters: filters,
})
var tasklists items.Tasklists
tasklistNotes := gitems.Notes()
duplicatesMap, err := getTasklistsDuplicatesMap(tasklistNotes)
// strip any duplicated items that no longer exist
for k := range duplicatesMap {
if !slices.Contains(allItemUUIDs, k) {
delete(duplicatesMap, k)
}
}
// second pass to get all non-deleted and non-trashed checklists
for x := range tasklistNotes {
// strip deleted and trashed
if tasklistNotes[x].Deleted || tasklistNotes[x].Content.Trashed != nil && *tasklistNotes[x].Content.Trashed {
continue
}
var cl items.Tasklist
cl, err = tasklistNotes[x].Content.ToTaskList()
if err != nil {
return items.Tasklist{}, err
}
cl.UUID = tasklistNotes[x].UUID
cl.UpdatedAt, err = time.Parse(timeLayout, tasklistNotes[x].UpdatedAt)
if err != nil {
return items.Tasklist{}, err
}
cl.Duplicates = duplicatesMap[tasklistNotes[x].UUID]
tasklists = append(tasklists, cl)
}
numTasklists := len(tasklists)
if numTasklists == 0 {
return items.Tasklist{}, fmt.Errorf("tasklist not found")
}
if numTasklists > 1 {
return items.Tasklist{}, fmt.Errorf("duplicate tasklists found")
}
return tasklists[0], nil
}
type ShowTasklistInput struct {
Session *cache.Session
Debug bool
Group string
Title string
UUID string
ShowCompleted bool
Ordering string
}
func outputChars(s string, max int) string {
if len(s) > max {
return s[:max] + "..."
}
return s
}
// func outputTime(updated time.Time, created time.Time) string {
// updatedAt := humanize.Time(updated)
// if updated.IsZero() {
// updatedAt = humanize.Time(created)
// }
//
// return updatedAt
// }
func getShowGroupTaskRows(name string, tasks items.AdvancedChecklistTasks, completed bool) [][]*simpletable.Cell {
var rowsCells [][]*simpletable.Cell
var rowCells []*simpletable.Cell
var nameOutput bool
for y, task := range tasks {
if !nameOutput {
// add group row
// adding rowCells to output
rowCells = []*simpletable.Cell{
// {Align: simpletable.AlignLeft, Text: ""},
{Span: 2, Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("%s", name)},
// {Align: simpletable.AlignLeft, Text: ""},
{Align: simpletable.AlignLeft, Text: ""},
}
// output completed column if we're showing completed tasks
if completed {
rowCells = append(rowCells, &simpletable.Cell{
Align: simpletable.AlignLeft, Text: boolToText(task.Completed, "yes", "no"),
})
}
rowsCells = append(rowsCells, rowCells)
nameOutput = true
}
// adding rowCells to output
rowCells = []*simpletable.Cell{
{Align: simpletable.AlignRight, Text: strconv.Itoa(y + 1)},
{Align: simpletable.AlignLeft, Text: outputChars(task.Description, defaultMaxLength)},
{Align: simpletable.AlignLeft, Text: " " + outputTime(task.UpdatedAt, task.CreatedAt) + " "},
}
// output completed column if we're showing completed tasks
if completed {
rowCells = append(rowCells, &simpletable.Cell{
Align: simpletable.AlignLeft, Text: boolToText(task.Completed, "yes", "no"),
})
}
rowsCells = append(rowsCells, rowCells)
}
return rowsCells
}
func getShowGroupsTaskRows(group items.AdvancedChecklistGroup, showCompleted bool) [][]*simpletable.Cell {
var rowsCells [][]*simpletable.Cell
// output open and then completed tasks (if specified)
seqs := []bool{false}
if showCompleted {
seqs = append(seqs, true)
}
// TODO: output empty groups
for x := range seqs {
// filter complete/incomplete tasks
filtered := filterAdvancedChecklistTasks(group.Tasks, seqs[x])
taskRows := getShowGroupTaskRows(group.Name, filtered, showCompleted)
rowsCells = append(rowsCells, taskRows...)
}
return rowsCells
}
func filterAdvancedChecklistTasks(tasks items.AdvancedChecklistTasks, completed bool) items.AdvancedChecklistTasks {
var filtered items.AdvancedChecklistTasks
for x := range tasks {
if tasks[x].Completed == completed {
filtered = append(filtered, tasks[x])
}
}
return filtered
}
func countCompletedTasks(group *items.AdvancedChecklistGroup) int {
completedTaskCount := 0
for taskIndex := range group.Tasks {
if group.Tasks[taskIndex].Completed {
completedTaskCount++
}
}
return completedTaskCount
}
func getShowGroupsRows(groups []items.AdvancedChecklistGroup, showCompleted bool) [][]*simpletable.Cell {
var rows [][]*simpletable.Cell
for x := range groups {
rows = append(rows, getShowGroupsTaskRows(groups[x], showCompleted)...)
}
return rows
}
func getShowTaskRows(tasklist items.Tasklist, showCompleted bool) [][]*simpletable.Cell {
var rowsCells [][]*simpletable.Cell // table
var rowCells []*simpletable.Cell // row
// output open and then completed tasks (if specified)
seqs := []bool{false}
if showCompleted {
seqs = append(seqs, true)
}
// for incomplete and then (if selected) completed tasks
for _, seq := range seqs {
// filter complete/incomplete tasks
filtered := filterTasks(tasklist.Tasks, seq)
for x, task := range filtered {
// adding rowCells to output
rowCells = []*simpletable.Cell{
{Align: simpletable.AlignRight, Text: strconv.Itoa(x + 1)},
{Align: simpletable.AlignLeft, Text: outputChars(task.Title, defaultMaxLength)},
}
// output completed column if we're showing completed tasks
if showCompleted {
rowCells = append(rowCells, &simpletable.Cell{
Align: simpletable.AlignLeft, Text: boolToText(task.Completed, "yes", "no"),
})
}
rowsCells = append(rowsCells, rowCells)
}
}
return rowsCells
}
func (ci *ShowTasklistInput) Run() error {
if ci.Title == "" && ci.UUID == "" {
var std items.Tasklists
var adv items.AdvancedChecklists
std, adv, err := getAllLists(ci.Session)
if err != nil {
return err
}
// Initialize an empty slice to hold the options
var options []string
for x := range std {
options = append(options, std[x].Title)
}
for x := range adv {
options = append(options, adv[x].Title)
}
selectedOption, _ := pterm.InteractiveSelectPrinter{
TextStyle: &pterm.ThemeDefault.TreeTextStyle,
DefaultText: "choose list to display",
Options: []string{},
OptionStyle: &pterm.ThemeDefault.DefaultText,
DefaultOption: "",
MaxHeight: selectListHeight,
Selector: ">",
SelectorStyle: &pterm.ThemeDefault.SecondaryStyle,
OnInterruptFunc: nil,
Filter: true,
}.WithOptions(options).Show()
ci.Title = selectedOption
}
std, adv, err := getAllMatchingLists(ci.Session, ci.Title, ci.UUID)
if err != nil {
return err
}
if len(std)+len(adv) > 1 {
return errors.New("more than one match found. use --uuid flag to specify.")
// TODO: output table with all the item titles, type, uuid, and last updated
}
if len(std) > 0 {
showTaskList(std[0], ci.ShowCompleted)
} else if len(adv) > 0 {
showAdvancedChecklist(adv[0], ci.ShowCompleted)
}
return nil
}
func showTaskList(tasklist items.Tasklist, showCompleted bool) {
table := simpletable.New()
headerCells := []*simpletable.Cell{
{Align: simpletable.AlignCenter, Text: color.Bold.Text("-")},
{Align: simpletable.AlignCenter, Text: color.Bold.Text("title")},
}
if showCompleted {
headerCells = append(headerCells, &simpletable.Cell{Align: simpletable.AlignLeft, Text: color.Bold.Text("completed")})
}
table.Header = &simpletable.Header{Cells: headerCells}
table.Body.Cells = getShowTaskRows(tasklist, showCompleted)
table.SetStyle(simpletable.StyleRounded)
fmt.Println(table.String())
}
func showAdvancedChecklist(tasklist items.AdvancedChecklist, showCompleted bool) {
table := simpletable.New()
headerCells := []*simpletable.Cell{
{Span: 1, Align: simpletable.AlignCenter, Text: color.Bold.Text("group")},
{Span: 1, Align: simpletable.AlignCenter, Text: color.Bold.Text("title")},
// {Align: simpletable.AlignCenter, Text: color.Bold.Text("title")},
{Align: simpletable.AlignCenter, Text: color.Bold.Text("updated")},
}
if showCompleted {
headerCells = append(headerCells, &simpletable.Cell{Align: simpletable.AlignLeft, Text: color.Bold.Text("completed")})
}
table.Header = &simpletable.Header{Cells: headerCells}
table.Body.Cells = getShowGroupsRows(tasklist.Groups, showCompleted)
table.SetStyle(simpletable.StyleRounded)
fmt.Println(table.String())
}
type DefaultSection struct {
Id string `json:"id"`
Name string `json:"name"`
}
type AdvancedChecklistTasks []AdvancedChecklistTask
type AdvancedChecklistTask struct {
Id string `json:"id"`
Description string `json:"description"`
Completed bool `json:"completed"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type AdvancedChecklistSection struct {