-
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathlist.go
More file actions
152 lines (125 loc) · 3.47 KB
/
Copy pathlist.go
File metadata and controls
152 lines (125 loc) · 3.47 KB
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
package main
import (
"context"
"fmt"
"os"
"github.com/acarl005/stripansi"
todoist "github.com/sachaos/todoist/lib"
"github.com/urfave/cli/v2"
)
func traverseItems(item *todoist.Item, f func(item *todoist.Item, depth int), depth int) {
f(item, depth)
if item.ChildItem != nil {
traverseItems(item.ChildItem, f, depth+1)
}
if item.BrotherItem != nil {
traverseItems(item.BrotherItem, f, depth)
}
}
func sortItems(itemListPtr *[][]string, byIndex int) {
itemList := *itemListPtr
length := len(itemList)
for i := 0; i < length-1; i++ {
for j := 0; j < length-1-i; j++ {
if stripansi.Strip(itemList[j][byIndex]) > stripansi.Strip(itemList[j+1][byIndex]) {
tmp := itemList[j]
itemList[j] = itemList[j+1]
itemList[j+1] = tmp
}
}
}
}
func List(c *cli.Context) error {
if c.Bool("remote") {
return listRemote(c)
}
return listLocal(c)
}
func listLocal(c *cli.Context) error {
client := GetClient(c)
colorList := ColorList()
projectsCount := len(client.Store.Projects)
projectIds := make([]string, projectsCount)
for i, project := range client.Store.Projects {
projectIds[i] = project.GetID()
}
projectColorHash := GenerateColorHash(projectIds, colorList)
ex := Filter(c.String("filter"))
itemList := [][]string{}
rootItem := client.Store.RootItem
if rootItem == nil {
fmt.Fprintln(os.Stderr, "There is no task. You can fetch latest tasks by `todoist sync`.")
return nil
}
traverseItems(rootItem, func(item *todoist.Item, depth int) {
r, err := Eval(ex, item, client.Store.Projects, client.Store.Labels)
if err != nil {
return
}
if !r || item.Checked {
return
}
itemList = append(itemList, []string{
IdFormat(item),
PriorityFormat(item.Priority),
DueDateFormat(item.DateTime(), item.AllDay),
ProjectFormat(item.ProjectID, client.Store, projectColorHash, c) +
SectionFormat(item.SectionID, client.Store, c),
item.LabelsString(),
ContentPrefix(client.Store, item, depth, c) + ContentFormat(item),
})
}, 0)
if c.Bool("priority") == true {
// sort output by priority
// and no need to use "else block" as items returned by API are already sorted by task id
sortItems(&itemList, 1)
}
defer writer.Flush()
if c.Bool("header") {
writer.Write([]string{"ID", "Priority", "DueDate", "Project", "Labels", "Content"})
}
for _, strings := range itemList {
writer.Write(strings)
}
return nil
}
func listRemote(c *cli.Context) error {
filter := c.String("filter")
if filter == "" {
return fmt.Errorf("--remote requires --filter")
}
client := GetClient(c)
items, err := client.FilterItems(context.Background(), filter, c.Int("limit"))
if err != nil {
return err
}
colorList := ColorList()
var projectIds []string
for _, project := range client.Store.Projects {
projectIds = append(projectIds, project.GetID())
}
projectColorHash := GenerateColorHash(projectIds, colorList)
itemList := [][]string{}
for _, item := range items {
itemList = append(itemList, []string{
IdFormat(item),
PriorityFormat(item.Priority),
DueDateFormat(item.DateTime(), item.AllDay),
ProjectFormat(item.ProjectID, client.Store, projectColorHash, c) +
SectionFormat(item.SectionID, client.Store, c),
item.LabelsString(),
ContentFormat(item),
})
}
if c.Bool("priority") {
sortItems(&itemList, 1)
}
defer writer.Flush()
if c.Bool("header") {
writer.Write([]string{"ID", "Priority", "DueDate", "Project", "Labels", "Content"})
}
for _, strings := range itemList {
writer.Write(strings)
}
return nil
}