-
Notifications
You must be signed in to change notification settings - Fork 1
/
taskCmds.go
191 lines (159 loc) · 4.66 KB
/
taskCmds.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
package main
import (
"errors"
"fmt"
"slices"
"strings"
)
func AddCmd(f TaskForm) ([]string, error) {
if f.description.Value() == "" {
return []string{}, errors.New("cannot create a task without a description")
}
var due string
var project string
var tags string
var recur string
var until string
if f.due.Value() != "" {
due = fmt.Sprintf("due:%s ", f.due.Value())
}
if f.project.Value() != "" {
project = fmt.Sprintf("project:%s ", f.project.Value())
}
if f.label.Value() != "" {
labels := strings.Split(f.label.Value(), " ")
var labelStrings []string
for _, l := range labels {
labelStrings = append(labelStrings, fmt.Sprintf("+%s", l))
}
labelStrings = append(labelStrings, "")
tags = strings.Join(labelStrings, " ")
}
if f.recur.Value() != "" {
recur = fmt.Sprintf("recur:%s ", f.recur.Value())
}
if f.until.Value() != "" {
until = fmt.Sprintf("until:%s ", f.until.Value())
}
if recur != "" && due == "" {
return []string{}, errors.New("cannot create a recurring task without a due date")
}
str := fmt.Sprintf("task add %s %s%s%s%s%s", f.description.Value(), project, due, tags, recur, until)
return strings.Split(strings.TrimSuffix(str, " "), " "), nil
}
func StartCmd(t *Task) ([]string, error) {
if t.id == 0 {
return []string{}, errors.New("cannot start a task with ID 0")
}
return []string{"task", fmt.Sprint(t.id), "start"}, nil
}
func StopCmd(t *Task) ([]string, error) {
if t.id == 0 {
return []string{}, errors.New("cannot stop a task with ID 0")
}
return []string{"task", fmt.Sprint(t.id), "stop"}, nil
}
func DoneCmd(t *Task) ([]string, error) {
if t.id == 0 {
return []string{}, errors.New("cannot finish a task with ID 0")
}
return []string{"task", "rc.confirmation=no", fmt.Sprint(t.id), "done"}, nil
}
func DeleteCmd(t *Task) ([]string, error) {
if t.id == 0 {
return []string{}, errors.New("cannot delete a task with ID 0")
}
return []string{"task", "rc.confirmation=no", fmt.Sprint(t.id), "delete"}, nil
}
func ModifyCmd(t Task, f *TaskForm) ([]string, error) {
if t.id == 0 {
return []string{}, errors.New("cannot modify a task with ID 0")
}
var changedLabels []string
var changedDescription string
var changedDue string
var changedProject string
if f.description.Value() != "" && f.description.Value() != t.description {
changedDescription = f.description.Value()
}
if f.due.Value() != "" && f.due.Value() != t.due {
changedDue = fmt.Sprintf("due:%s ", f.due.Value())
}
if f.project.Value() != t.project {
changedProject = fmt.Sprintf("project:%s ", f.project.Value())
}
if f.label.Value() != "" {
formValues := strings.Split(f.label.Value(), " ")
addedLabels := []string{}
currLabels := t.tags
for _, label := range formValues {
idx := slices.Index(currLabels, label)
if idx == -1 {
addedLabels = append(addedLabels, label)
} else {
currLabels = slices.Delete(currLabels, idx, idx+1)
}
}
for _, label := range addedLabels {
changedLabels = append(changedLabels, fmt.Sprintf("+%s", label))
}
for _, label := range currLabels {
changedLabels = append(changedLabels, fmt.Sprintf("-%s", label))
}
}
str := fmt.Sprintf(
"task rc.confirmation=no %d modify %s %s%s%s",
t.id,
changedDescription,
changedProject,
changedDue,
strings.Join(changedLabels, " "),
)
cmdArgs := []string{}
// remove the nil values if there are any present
for _, arg := range strings.Split(strings.TrimSuffix(str, " "), " ") {
if arg == "" {
continue
}
cmdArgs = append(cmdArgs, arg)
}
return cmdArgs, nil
}
func BlockCmd(t *Task, blocked *[]Task) ([]string, error) {
if len(*blocked) == 0 {
return []string{}, errors.New("need to select at least 1 task")
}
if t.id == 0 {
return []string{}, errors.New("blocking task cannot have ID 0")
}
cmd := []string{"task"}
if len(*blocked) > 2 {
cmd = append(cmd, "rc.bulk=0")
}
var taskIds []string
for _, task := range *blocked {
if task.id == 0 {
return []string{}, errors.New("cannot block a task with ID 0")
}
if task.id == t.id {
return []string{}, errors.New("cannot block a task with same ID")
}
if task.status == done {
return []string{}, errors.New("cannot block a task that is already done")
}
taskIds = append(taskIds, fmt.Sprint(task.id))
}
cmd = append(cmd, strings.Join(taskIds, ","))
cmd = append(cmd, "modify")
cmd = append(cmd, fmt.Sprintf("depends:%d", t.id))
return cmd, nil
}
func UnblockCmd(t *Task) ([]string, error) {
if t.id == 0 {
return []string{}, errors.New("cannot unblock task with ID 0")
}
if !t.blocked {
return []string{}, errors.New("cannot unblock a task that is not blocked")
}
return []string{"task", fmt.Sprint(t.id), "modify", "depends:"}, nil
}