-
Notifications
You must be signed in to change notification settings - Fork 2
/
executable_todoist
337 lines (275 loc) · 8.72 KB
/
executable_todoist
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env ruby
# mise description="Connect to Todoist API"
require 'io/console'
require 'optparse'
require 'csv'
require 'json'
def fetch_todolist
`todoist --color --namespace --csv l -p`
end
def fetch_donelist
`todoist --color --namespace --csv cl`
end
def select_todolist(csv)
`echo '#{csv}' | gum choose --height 25 --limit 100`
end
def complete_todos(csv)
rows = select_todolist(csv)
rows.each_line do |line|
task_id, priority, deadline, project, label, task_name = line.split(',')
system("todoist c #{task_id}")
puts "Completed [#{task_id}] #{project} #{task_name}"
end
end
ESTIMATION_POINTS = {
"1 point" => "point:1",
"2 points" => "point:2",
"3 points" => "point:3",
"5 points" => "point:5",
"8 points" => "point:8",
"13 points" => "point:13",
"21 points" => "point:21",
"34 points" => "point:34",
}
def compose_modify_command(deadline: nil, task_name: nil, priority: "p4", project_id: nil, labels: "", task_id:)
deadline_param = ""
deadline_param = "-d '#{deadline}'" unless deadline.to_s.empty?
priority_param = "-p #{priority.scan(/\d+/).first.to_i}"
project_id_param = ""
project_id_param = "-P #{project_id}" unless project_id.to_s.empty?
labels_param = ""
labels_param = "--label-names '#{labels.to_s.gsub("@", "")}'" unless labels.to_s.empty?
task_name_param = ""
task_name_param = "-c '#{task_name}'" unless task_name.to_s.empty?
[
"todoist",
"modify",
deadline_param,
priority_param,
project_id_param,
labels_param,
task_name_param,
task_id
].join(" ")
end
options = {}
OptionParser.new do |opts|
# opts.on('-sTAG', '--search=TAG', 'Search image tag perfix') { options[:search] = _1 }
end.parse!
subcommand = ARGV.pop
available_commands = [
'all',
'breakdown',
'bulk_add',
'bulk_complete',
'estimate',
'import',
'listup',
'plan',
'prioritize',
'schedule',
]
if subcommand.nil?
subcommand = `echo '#{available_commands.join("\n")}' | gum choose`.chomp
puts "Selected #{subcommand} command"
puts "======"
end
# modify 연산을 수행할때, project_id를 제외한 모든 값들이 assign 되어 있어야 한다.
DATE_OPTIONS = <<~HERE
#{Date.today.strftime("%Y/%m/%d")} (today)
#{(Date.today + 1).strftime("%Y/%m/%d")} (tomorrow)
#{(Date.today + 7).strftime("%Y/%m/%d")} (next week)
#{(Date.today + 14).strftime("%Y/%m/%d")} (next 2 weeks)
#{(Date.today + 30).strftime("%Y/%m/%d")} (next month)
HERE
def quickadd_with_auto_reminder(task)
config_path = File.expand_path("~/.config/todoist/config.json")
json = File.read(config_path)
todoist_token = JSON.parse(json)['token']
command = <<~SHELL
curl https://api.todoist.com/sync/v9/quick/add \\
-H \"Authorization: Bearer #{todoist_token}\" \\
-d text='#{task}' \\
-d auto_reminder='true' \\
-s
SHELL
`#{command}`
end
case subcommand
when "import"
$<.read.lines.each do |line|
task = line.chomp
quickadd_with_auto_reminder(task)
puts task
end
when "all"
system("todoist --color --namespace --header l -p")
when proc { _1 == 'finale' }
donelist = fetch_donelist
rows = select_todolist(donelist)
table = []
rows.each_line do |line|
task_id, _date, project, task_name_with_labels = line.parse_csv
task_name, point = task_name_with_labels.split("@point:")
point_column = "unknown point"
if !point.nil? && point.scan(/\d+/).any?
point_column = "#{point.scan(/\d+/).first.to_i} point"
end
display_name = task_name.split('|>').reverse.map(&:strip).map { "[#{_1}]" }.join(" |> ")
table << "#{project},\"#{display_name}\",#{point_column}"
end
width, height = IO.console.winsize
system "echo 'project,task_name,score\n#{table.join("\n")}' | gum table -w 15,#{[150,(width*1.5).ceil].min},10"
when proc { _1 == 'plan' || _1 == 'listup' }
todolist = fetch_todolist
rows = select_todolist(todolist)
table = []
rows.each_line do |line|
task_id, priority, _, project, labels, task_name = line.parse_csv
display_name = task_name.split('|>').reverse.map(&:strip).map { "[#{_1}]" }.join(" |> ")
point_column = "unknown point"
if labels.to_s.split(',').any? && labels.include?("@point")
point_column = "#{labels.split(',').select { _1.start_with?("@point:") }&.first&.scan(/\d+/)&.first&.to_s} point"
end
# Handles edge case of including comma within column
table << "#{project},\"#{display_name}\",#{point_column}"
end
width, height = IO.console.winsize
system "echo 'project,task_name,score\n#{table.join("\n")}' | gum table -w 15,#{[150,(width*1.5).ceil].min},10"
when "schedule"
todolist = fetch_todolist
rows = select_todolist(todolist)
puts "Choose Deadline"
puts "======"
deadline = `echo '#{DATE_OPTIONS}' | gum choose`.chomp.split.first
rows.each_line do |line|
task_id, priority, _, __, labels, task_name = line.parse_csv
system(
compose_modify_command(
deadline:,
priority:,
labels:,
task_id:
)
)
puts "Scheduled [Task #{task_name}(##{task_id})] to #{deadline}"
end
`todoist sync`
when "breakdown"
todolist = fetch_todolist
line = `echo '#{todolist}' | gum choose --height 25`.chomp
_, priority, deadline, project_name, labels, parent_task_name = line.parse_csv
puts "Selected #{parent_task_name}} in #{project_name}"
puts "======"
tasks = []
while true
task_name = `gum input --placeholder="Leave blank character if you want to quit"`.chomp
break if task_name.empty?
tasks << task_name
puts "Added SubTask [#{task_name}] to Task [#{parent_task_name}]"
end
tasks.each do |task|
system("todoist q '#{task}'")
end
puts "Syncing....."
`todoist sync`
lines = `todoist --csv l -p | tail -n #{tasks.size}`.chomp
latest_tasks = lines.split("\n").map { |line| line.parse_csv }
project_name = project_name.split('/').first
projects = `todoist --csv projects | grep '#{project_name}'`.split("\n").map { |line| line.split(',').reverse }.to_h
latest_tasks.each do |task_id, *_, task_name|
system(
compose_modify_command(
deadline:,
priority:,
project_id: projects[project_name],
labels:,
task_name: "#{task_name} |> #{parent_task_name}",
task_id:
)
)
puts "Assigned [Task #{task_name}(##{task_id})] to [Project #{project_name}]"
end
`todoist sync`
when "estimate"
todoist = fetch_todolist
rows = select_todolist(todoist)
puts "Choose Estimation Points"
puts "======"
estimation_point = `echo '#{ESTIMATION_POINTS.keys.join("\n")}' | gum choose`.chomp
rows.each_line do |line|
task_id, priority, deadline, project, labels, task_name = line.parse_csv
result = labels.to_s.split(',')&.reject { _1.start_with?("@point:") }.to_a
result = [*result, ESTIMATION_POINTS[estimation_point]]
system(
compose_modify_command(
deadline:,
priority:,
labels: result.join(','),
task_id:
)
)
puts "Assigned [Task #{task_name}(##{task_id})] to #{estimation_point}"
end
`todoist sync`
when "prioritize"
todoist = fetch_todolist
rows = select_todolist(todoist)
puts "Assign Priority"
puts "======"
priority = `echo 'p1\np2\np3\np4' | gum choose`.chomp
rows.each_line do |line|
task_id, _, deadline, *__, labels, task_name = line.parse_csv
system(
compose_modify_command(
deadline:,
priority:,
labels:,
task_id:
)
)
puts "Assigned [Task #{task_name}(##{task_id})] to priority #{priority}"
end
`todoist sync`
when "bulk_add"
tasks = []
while true
task_name = `gum input --placeholder="Leave blank character if you want to quit"`.chomp
break if task_name.empty?
tasks << task_name
puts "Added Task [#{task_name}]"
end
line = `todoist --csv projects | gum choose`
project_id, project_name = line.split(',')
puts "Syncing....."
tasks.each do |task|
system("todoist q '#{task}'")
end
lines = `todoist --csv l | tail -n #{tasks.size}`.chomp
latest_tasks = lines.split("\n").map { |line| line.parse_csv }
latest_tasks.each do |task_id, *_, task_name|
system(
compose_modify_command(
project_id:,
task_id:,
)
)
puts "Assigned [Task #{task_name}(##{task_id})] to #{project_name}"
end
`todoist sync`
when "bulk_update"
todolist = fetch_todolist
rows = select_todolist(todoist)
rows.each_line do |line|
task_id, priority, deadline, project, labels, task_name = line.parse_csv
# content
# priority
# date
system("todoist m #{task_id} --content #{content} --priority #{priority} --date #{date}")
puts "Modifies [#{task_id}] #{project} #{task_name}"
end
when "bulk_complete"
todolist = fetch_todolist
complete_todos(todolist)
`todoist sync`
end