-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskops.go
127 lines (116 loc) · 2.99 KB
/
taskops.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
package main
import (
"fmt"
"os/exec"
tea "github.com/charmbracelet/bubbletea"
todoist "github.com/psethwick/todoist/lib"
fltr "github.com/psethwick/todoist/lib/filter"
"github.com/psethwick/tuidoist/task"
"github.com/psethwick/tuidoist/tasklist"
)
func (m *mainModel) setTasksFromProject(p *project) {
lists := []tasklist.List{}
tasks := []task.Task{}
var selectedList int
for _, i := range m.local.Items {
// no section tasks should be first
if i.ProjectID == p.project.ID && i.SectionID == "" {
tasks = append(tasks, task.New(m.local, i))
}
}
if len(tasks) > 0 {
lists = append(
lists,
tasklist.List{
Title: p.project.Name, Tasks: tasks, ListId: project{p.project, todoist.Section{}},
})
}
for _, s := range m.local.Sections {
tasks = []task.Task{}
if s.ProjectID == p.project.ID {
for _, item := range m.local.Items {
if item.ProjectID == p.project.ID && s.ID == item.SectionID {
tasks = append(tasks, task.New(m.local, item))
}
}
lists = append(lists, tasklist.List{
Title: fmt.Sprintf("%s/%s", p.project.Name, s.Name),
Tasks: tasks,
ListId: project{p.project, s},
})
if s.ID == p.section.ID {
selectedList = len(lists) - 1
}
}
}
if len(lists) == 0 {
lists = append(lists, tasklist.List{Title: p.project.Name})
}
m.taskList.ResetItems(lists, selectedList)
m.statusBarModel.SetTitle(m.taskList.Title())
// m.statusBarModel.SetNumber(m.taskList.Len())
}
type filterTitle struct {
Title string
Expr fltr.Expression
}
type filterSelection struct {
lists []filterTitle
index int
}
func (m *mainModel) setTasksFromFilter(fs filterSelection) {
// this can't be the right place to do this
m.projectId = ""
m.sectionId = ""
var tls []tasklist.List
for i, l := range fs.lists {
tasks := []task.Task{}
for _, i := range m.local.Items {
if res, _ := fltr.Eval(l.Expr, &i, m.local); res {
tasks = append(tasks, task.New(m.local, i))
}
}
tls = append(
tls,
tasklist.List{Title: l.Title, Tasks: tasks, ListId: fmt.Sprint(i, l.Title)},
)
}
m.taskList.ResetItems(tls, fs.index)
}
func (tm *mainModel) OpenUrl(url string) func() tea.Msg {
return func() tea.Msg {
// todo mac: open, win: ???
openCmd := exec.Command("xdg-open", url)
if err := openCmd.Run(); err != nil {
dbg(err)
}
return nil
}
}
func (m *mainModel) openInbox() {
for _, tp := range m.local.Projects {
if tp.ID == m.local.User.InboxProjectID {
p := project{tp, todoist.Section{}}
m.refresh = func() {
m.setTasksFromProject(&p)
}
m.refresh()
m.sub <- struct{}{}
return
}
}
// possible to end up here if InboxProjectID isn't set for whatever reason
// empty cache, mostly
m.projectId = "CHANGEME"
m.refresh = func() {
// one empty list in the list is better than an empty list of lists
dbg("empty list refresh")
if m.local.User.InboxProjectID != "" {
dbg("attempt fix loading project")
m.openInbox()
return
}
m.taskList.ResetItems([]tasklist.List{{Title: "Loading..."}}, 0)
}
m.refresh()
}