-
Notifications
You must be signed in to change notification settings - Fork 8
/
segment.go
102 lines (89 loc) · 2.64 KB
/
segment.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
package todotxt
import (
"fmt"
"sort"
)
// TaskSegmentType represents type of segment in task string.
//go:generate stringer -type TaskSegmentType -trimprefix Segment -output segment_type.go
type TaskSegmentType uint8
// Flags for indicating type of segment in task string.
const (
SegmentIsCompleted TaskSegmentType = iota + 1
SegmentCompletedDate
SegmentPriority
SegmentCreatedDate
SegmentTodoText
SegmentContext
SegmentProject
SegmentTag
SegmentDueDate
)
// TaskSegment represents a segment in task string.
type TaskSegment struct {
Type TaskSegmentType
Originals []string
Display string
}
// Segments returns a segmented task string in todo.txt format. The order of segments is the same as String().
func (task *Task) Segments() []*TaskSegment {
var segs []*TaskSegment
newBasicTaskSeg := func(t TaskSegmentType, s string) *TaskSegment {
return &TaskSegment{
Type: t,
Originals: []string{s},
Display: s,
}
}
newTaskSeg := func(t TaskSegmentType, so, sd string) *TaskSegment {
return &TaskSegment{
Type: t,
Originals: []string{so},
Display: sd,
}
}
if task.Completed {
segs = append(segs, newBasicTaskSeg(SegmentIsCompleted, "x"))
if task.HasCompletedDate() {
segs = append(segs, newBasicTaskSeg(SegmentCompletedDate, task.CompletedDate.Format(DateLayout)))
}
}
if task.HasPriority() && (!task.Completed || !RemoveCompletedPriority) {
segs = append(segs, newTaskSeg(SegmentPriority, task.Priority, fmt.Sprintf("(%s)", task.Priority)))
}
if task.HasCreatedDate() {
segs = append(segs, newBasicTaskSeg(SegmentCreatedDate, task.CreatedDate.Format(DateLayout)))
}
segs = append(segs, newBasicTaskSeg(SegmentTodoText, task.Todo))
if task.HasContexts() {
sort.Strings(task.Contexts)
for _, context := range task.Contexts {
segs = append(segs, newTaskSeg(SegmentContext, context, fmt.Sprintf("@%s", context)))
}
}
if task.HasProjects() {
sort.Strings(task.Projects)
for _, project := range task.Projects {
segs = append(segs, newTaskSeg(SegmentProject, project, 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 {
val := task.AdditionalTags[key]
segs = append(segs, &TaskSegment{
Type: SegmentTag,
Originals: []string{key, val},
Display: fmt.Sprintf("%s:%s", key, val),
})
}
}
if task.HasDueDate() {
segs = append(segs, newBasicTaskSeg(SegmentDueDate, fmt.Sprintf("due:%s", task.DueDate.Format(DateLayout))))
}
return segs
}