-
Notifications
You must be signed in to change notification settings - Fork 8
/
task.go
294 lines (253 loc) · 8.78 KB
/
task.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
package todotxt
import (
"fmt"
"regexp"
"sort"
"strings"
"time"
)
var (
// DateLayout is used for formatting time.Time into todo.txt date format and vice-versa.
DateLayout = "2006-01-02"
priorityRx = regexp.MustCompile(`^(x|x \d{4}-\d{2}-\d{2}|)\s*\(([A-Z])\)\s+`) // Match priority: '(A) ...' or 'x (A) ...' or 'x 2012-12-12 (A) ...'
// Match created date: '(A) 2012-12-12 ...' or 'x 2012-12-12 (A) 2012-12-12 ...' or 'x (A) 2012-12-12 ...'or 'x 2012-12-12 2012-12-12 ...' or '2012-12-12 ...'
createdDateRx = regexp.MustCompile(`^(\([A-Z]\)|x \d{4}-\d{2}-\d{2} \([A-Z]\)|x \([A-Z]\)|x \d{4}-\d{2}-\d{2}|)\s*(\d{4}-\d{2}-\d{2})\s+`)
completedRx = regexp.MustCompile(`^x\s+`) // Match completed: 'x ...'
completedDateRx = regexp.MustCompile(`^x\s*(\d{4}-\d{2}-\d{2})\s+`) // Match completed date: 'x 2012-12-12 ...'
addonTagRx = regexp.MustCompile(`(^|\s+)([^:\s]+):([^:\s]+)`) // Match additional tags date: '... due:2012-12-12 ...'
contextRx = regexp.MustCompile(`(^|\s+)@(\S+)`) // Match contexts: '@Context ...' or '... @Context ...'
projectRx = regexp.MustCompile(`(^|\s+)\+(\S+)`) // Match projects: '+Project...' or '... +Project ...')
)
// Task represents a todo.txt task entry.
type Task struct {
ID int // Internal task ID.
Original string // Original raw task text.
Todo string // Todo part of task text.
Priority string
Projects []string
Contexts []string
AdditionalTags map[string]string // Addon tags will be available here.
CreatedDate time.Time
DueDate time.Time
CompletedDate time.Time
Completed bool
}
// NewTask creates a new empty Task with default values. (CreatedDate is set to Now())
func NewTask() Task {
task := Task{}
task.CreatedDate = time.Now()
return task
}
// Task returns a complete task string in todo.txt format.
// See *Task.String() for further information.
func (task *Task) Task() string {
return task.String()
}
// String returns a complete task string in todo.txt format.
//
// Contexts, Projects and additional tags are alphabetically sorted,
// and appended at the end in the following order:
// Contexts, Projects, Tags
//
// For example:
// "(A) 2013-07-23 Call Dad @Home @Phone +Family due:2013-07-31 customTag1:Important!"
func (task Task) String() string {
var sb strings.Builder
if task.Completed {
sb.WriteString("x ")
if task.HasCompletedDate() {
sb.WriteString(fmt.Sprintf("%s ", task.CompletedDate.Format(DateLayout)))
}
}
if task.HasPriority() && (!task.Completed || !RemoveCompletedPriority) {
sb.WriteString(fmt.Sprintf("(%s) ", task.Priority))
}
if task.HasCreatedDate() {
sb.WriteString(fmt.Sprintf("%s ", task.CreatedDate.Format(DateLayout)))
}
sb.WriteString(task.Todo)
if task.HasContexts() {
sort.Strings(task.Contexts)
for _, context := range task.Contexts {
sb.WriteString(fmt.Sprintf(" @%s", context))
}
}
if task.HasProjects() {
sort.Strings(task.Projects)
for _, project := range task.Projects {
sb.WriteString(fmt.Sprintf(" +%s", project))
}
}
if task.HasAdditionalTags() {
// Sort map alphabetically by keys
keys := make([]string, 0, len(task.AdditionalTags))
for key := range task.AdditionalTags {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
sb.WriteString(fmt.Sprintf(" %s:%s", key, task.AdditionalTags[key]))
}
}
if task.HasDueDate() {
sb.WriteString(fmt.Sprintf(" due:%s", task.DueDate.Format(DateLayout)))
}
return sb.String()
}
// ParseTask parses the input text string into a Task struct.
func ParseTask(text string) (*Task, error) {
var err error
oriText := strings.Trim(text, whitespaces)
task := Task{}
task.Original = oriText
task.Todo = oriText
// Check for completed
if completedRx.MatchString(oriText) {
task.Completed = true
// Check for completed date
if completedDateRx.MatchString(oriText) {
if date, err := parseTime(completedDateRx.FindStringSubmatch(oriText)[1]); err == nil {
task.CompletedDate = date
} else {
return nil, err
}
}
// Remove from Todo text
task.Todo = completedDateRx.ReplaceAllString(task.Todo, emptyStr) // Strip CompletedDate first, otherwise it wouldn't match anymore (^x date...)
task.Todo = completedRx.ReplaceAllString(task.Todo, emptyStr) // Strip 'x '
}
// Check for priority
if priorityRx.MatchString(oriText) {
task.Priority = priorityRx.FindStringSubmatch(oriText)[2]
task.Todo = priorityRx.ReplaceAllString(task.Todo, emptyStr) // Remove from Todo text
}
// Check for created date
if createdDateRx.MatchString(oriText) {
if date, err := parseTime(createdDateRx.FindStringSubmatch(oriText)[2]); err == nil {
task.CreatedDate = date
task.Todo = createdDateRx.ReplaceAllString(task.Todo, emptyStr) // Remove from Todo text
} else {
return nil, err
}
}
// function for collecting projects/contexts as slices from text
getSlice := func(rx *regexp.Regexp) []string {
matches := rx.FindAllStringSubmatch(oriText, -1)
slice := make([]string, 0, len(matches))
seen := make(map[string]bool, len(matches))
for _, match := range matches {
word := strings.Trim(match[2], whitespaces)
if _, found := seen[word]; !found {
slice = append(slice, word)
seen[word] = true
}
}
sort.Strings(slice)
return slice
}
// Check for contexts
if contextRx.MatchString(oriText) {
task.Contexts = getSlice(contextRx)
task.Todo = contextRx.ReplaceAllString(task.Todo, emptyStr) // Remove from Todo text
}
// Check for projects
if projectRx.MatchString(oriText) {
task.Projects = getSlice(projectRx)
task.Todo = projectRx.ReplaceAllString(task.Todo, emptyStr) // Remove from Todo text
}
// Check for additional tags
if addonTagRx.MatchString(oriText) {
matches := addonTagRx.FindAllStringSubmatch(oriText, -1)
tags := make(map[string]string, len(matches))
for _, match := range matches {
key, value := match[2], match[3]
if key == "due" { // due date is a known addon tag, it has its own struct field
if date, err := parseTime(value); err == nil {
task.DueDate = date
} else {
return nil, err
}
} else if isNotEmpty(key) && isNotEmpty(value) {
tags[key] = value
}
}
task.AdditionalTags = tags
task.Todo = addonTagRx.ReplaceAllString(task.Todo, emptyStr) // Remove from Todo text
}
// Trim any remaining whitespaces from Todo text
task.Todo = strings.Trim(task.Todo, "\t\n\r\f ")
return &task, err
}
// HasProjects returns true if the task has any projects.
func (task *Task) HasProjects() bool {
return len(task.Projects) > 0
}
// HasContexts returns true if the task has any contexts.
func (task *Task) HasContexts() bool {
return len(task.Contexts) > 0
}
// HasAdditionalTags returns true if the task has any additional tags.
func (task *Task) HasAdditionalTags() bool {
return len(task.AdditionalTags) > 0
}
// HasPriority returns true if the task has a priority.
func (task *Task) HasPriority() bool {
return isNotEmpty(task.Priority)
}
// HasCreatedDate returns true if the task has a created date.
func (task *Task) HasCreatedDate() bool {
return !task.CreatedDate.IsZero()
}
// HasCompletedDate returns true if the task has a completed date.
func (task *Task) HasCompletedDate() bool {
return !task.CompletedDate.IsZero() && task.Completed
}
// IsCompleted returns true if the task has already been completed.
func (task *Task) IsCompleted() bool {
return task.Completed
}
// Complete sets Task.Completed to 'true' if the task was not already completed.
// Also sets Task.CompletedDate to time.Now()
func (task *Task) Complete() {
if !task.Completed {
task.Completed = true
task.CompletedDate = time.Now()
}
}
// Reopen sets Task.Completed to 'false' if the task was completed.
// Also resets Task.CompletedDate.
func (task *Task) Reopen() {
if task.Completed {
task.Completed = false
task.CompletedDate = time.Time{} // time.IsZero() value
}
}
// HasDueDate returns true if the task has a due date.
func (task *Task) HasDueDate() bool {
return !task.DueDate.IsZero()
}
// IsOverdue returns true if due date is in the past.
//
// This function does not take the Completed flag into consideration.
// You should check Task.Completed first if needed.
func (task *Task) IsOverdue() bool {
if task.HasDueDate() {
return task.Due() < 0
}
return false
}
// IsDueToday returns true if the task is due todasy.
func (task *Task) IsDueToday() bool {
if task.HasDueDate() {
due := task.Due()
return 0 < due && due <= oneDay
}
return false
}
// Due returns the duration left until due date from now. The duration is negative if the task is overdue.
//
// Just as with IsOverdue(), this function does also not take the Completed flag into consideration.
// You should check Task.Completed first if needed.
func (task *Task) Due() time.Duration {
return time.Until(task.DueDate.AddDate(0, 0, 1))
}