summaryrefslogtreecommitdiff
path: root/internal/task/task.go
diff options
context:
space:
mode:
authorPaul Bütow <1224732+snonux@users.noreply.github.com>2025-06-21 23:54:08 +0300
committerGitHub <noreply@github.com>2025-06-21 23:54:08 +0300
commit05124d641791083612043159f36fada257e03468 (patch)
tree224f417c19f324674f5a240dc98583725061049a /internal/task/task.go
parentfde95e1ca2acb202b3e2698562ab0a3ca360c0cc (diff)
parentc1585e94cf4a0eeb8ebd49103291119b97e097a8 (diff)
Merge pull request #81 from snonux/codex/fix-row-alignment-issue
Fix spacing in highlighted table rows
Diffstat (limited to 'internal/task/task.go')
-rw-r--r--internal/task/task.go56
1 files changed, 55 insertions, 1 deletions
diff --git a/internal/task/task.go b/internal/task/task.go
index 4acfec9..603002d 100644
--- a/internal/task/task.go
+++ b/internal/task/task.go
@@ -167,6 +167,50 @@ func RemoveTags(id int, tags []string) error {
return run(args...)
}
+// SetTags sets the tags of the task with the given id to exactly the provided set.
+// Tags not present will be removed and new tags added as needed.
+func SetTags(id int, tags []string) error {
+ tasks, err := Export(strconv.Itoa(id))
+ if err != nil {
+ return err
+ }
+ if len(tasks) == 0 {
+ return fmt.Errorf("task %d not found", id)
+ }
+ current := make(map[string]struct{})
+ for _, t := range tasks[0].Tags {
+ current[t] = struct{}{}
+ }
+ desired := make(map[string]struct{})
+ for _, t := range tags {
+ desired[t] = struct{}{}
+ }
+
+ var adds, removes []string
+ for t := range desired {
+ if _, ok := current[t]; !ok {
+ adds = append(adds, t)
+ }
+ }
+ for t := range current {
+ if _, ok := desired[t]; !ok {
+ removes = append(removes, t)
+ }
+ }
+
+ if len(adds) > 0 {
+ if err := AddTags(id, adds); err != nil {
+ return err
+ }
+ }
+ if len(removes) > 0 {
+ if err := RemoveTags(id, removes); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
// SetRecurrence sets the recurrence for the task with the given id.
func SetRecurrence(id int, rec string) error {
return run(strconv.Itoa(id), "modify", "recur:"+rec)
@@ -242,7 +286,8 @@ func Edit(id int) error {
// SortTasks orders tasks by start status, priority, due date, tag names and id.
// Started tasks are always placed before non-started ones. Tasks without a due
-// date are placed after tasks with a due date.
+// date are placed after tasks with a due date. Overdue tasks are placed at the
+// very top regardless of other properties.
func SortTasks(tasks []Task) {
joinTags := func(tags []string) string {
if len(tags) == 0 {
@@ -277,9 +322,18 @@ func SortTasks(tasks []Task) {
return t, true
}
+ overdue := func(t Task) bool {
+ du, ok := parseDue(t.Due)
+ return ok && time.Now().After(du)
+ }
+
sort.Slice(tasks, func(i, j int) bool {
ti, tj := tasks[i], tasks[j]
+ if oi, oj := overdue(ti), overdue(tj); oi != oj {
+ return oi
+ }
+
startedI := ti.Start != "" && ti.Status != "completed"
startedJ := tj.Start != "" && tj.Status != "completed"
if startedI != startedJ {