todo-jsonl 0.1.19 → 0.1.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/todo.rb +80 -68
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fd0447c1a7546c396ad8c792f7b7da0d2a72dcf9bf0d20aefd554cffb4c40cb
4
- data.tar.gz: 49428f5380ed3c56b4c548e80661c77a1a83e6129a69dae7292457f423532390
3
+ metadata.gz: d47bb16f60f84f6c3e55951169bdb0871f915b7a6948844897fa8a150df05456
4
+ data.tar.gz: '09600be32199c25bc946036ee947290c7c77472d00933fee39c67d9bd2dc0649'
5
5
  SHA512:
6
- metadata.gz: 0cdc1df93f9b9c194fbe48486dc0b6d92b8f2613af3d2b9d398ac2bd2b0478025ce8a495e98c1860888cbe7335d33997ce23ef5e7b9d1c2be53bbaf400a6e967
7
- data.tar.gz: 8d04c7605fdc2e96ec5b8adcd88b115e805a5441cc32ec50b3c7839beb350e1fb215f07874f34a14301ffd8b36cfe6f1f735731e65e17b8d98235a56e6c61052
6
+ metadata.gz: 3ae62a56b667ebcb55e661d1ac64cd4c2b62db7d7d4f106c79a034ca58264d4d7c3ea20a4d262e4386806baa88d472c88dcc5a6946c4d3ca8eb01bbc3d99363e
7
+ data.tar.gz: ede2f1f9f155ce5ff5acacdd784b030c01e11f14a4ef6d85e5223663dc3a0dea5a2ab8a3eac95f498808747fa9588723d1feef36828e4657ee23cd41736aa4e0
data/bin/todo.rb CHANGED
@@ -115,9 +115,14 @@ class Todo
115
115
  raise action + ' command requires exactly one parameter' if args.length != 1
116
116
  show(args.first.to_i)
117
117
  when 'help'
118
+ raise action + ' command has no parameters' if args.length > 0
118
119
  puts usage
119
120
  when 'repl'
121
+ raise action + ' command has no parameters' if args.length > 0
120
122
  start_repl
123
+ when 'cleanup'
124
+ raise action + ' command requires at least one parameter' if args.nil? || args.empty?
125
+ cleanup(args)
121
126
  else
122
127
  list(nil, arguments)
123
128
  end
@@ -150,6 +155,7 @@ class Todo
150
155
  * list <regex> [regex...] list tasks (only active tasks by default)
151
156
  * show <tasknumber> show all task details
152
157
  * repl enter read-eval-print loop mode
158
+ * cleanup <regex> [regex...] cleanup completed tasks by regex
153
159
  * help this help screen
154
160
 
155
161
  With list command the following pre-defined regex patterns can be also used:
@@ -165,8 +171,8 @@ class Todo
165
171
  end
166
172
 
167
173
  def setup
168
- @today = Time.now
169
- next_7_days = (0..6).map do |day| (@today.to_date + day) end
174
+ @today = Date.today
175
+ next_7_days = (0..6).map do |day| @today + day end
170
176
  @due_date_days = next_7_days.map do |day| day.strftime('%A').downcase end
171
177
  due_dates_for_queries = next_7_days.map do |day| day.strftime(DATE_FORMAT) end
172
178
 
@@ -195,9 +201,7 @@ class Todo
195
201
  end
196
202
  end
197
203
  end
198
- if item_to_check && !tasks.has_key?(item_to_check)
199
- raise "#{item_to_check}: No such todo"
200
- end
204
+ raise "#{item_to_check}: No such todo" if item_to_check && !tasks.has_key?(item_to_check)
201
205
  tasks
202
206
  end
203
207
 
@@ -231,78 +235,68 @@ class Todo
231
235
  list
232
236
  end
233
237
 
234
- def append(item, text = '')
238
+ def update_task(item, post_action, update_function)
235
239
  tasks = load_tasks(item)
236
- tasks[item][:title] = [tasks[item][:title], text].join(' ')
240
+ update_function.call(tasks[item])
237
241
  tasks[item][:modified] = @today.strftime(DATE_FORMAT)
238
- postprocess_tags(tasks[item])
239
242
  write_tasks(tasks)
240
- list(tasks)
243
+ send(post_action, tasks) if post_action
244
+ end
245
+
246
+ def append(item, text = '')
247
+ update_task item, :list, lambda { |task|
248
+ task[:title] = [task[:title], text].join(' ')
249
+ postprocess_tags(task)
250
+ }
241
251
  end
242
252
 
243
253
  def rename(item, text)
244
- tasks = load_tasks(item)
245
- tasks[item][:title] = text
246
- tasks[item][:modified] = @today.strftime(DATE_FORMAT)
247
- postprocess_tags(tasks[item])
248
- write_tasks(tasks)
249
- list(tasks)
254
+ update_task item, :list, lambda { |task|
255
+ task[:title] = text
256
+ postprocess_tags(task)
257
+ }
250
258
  end
251
259
 
252
260
  def delete(item)
253
261
  tasks = load_tasks(item)
254
262
  tasks.delete(item)
255
263
  write_tasks(tasks)
256
- list
264
+ list(tasks)
257
265
  end
258
266
 
259
267
  def change_state(item, state, note = nil)
260
- tasks = load_tasks(item)
261
- tasks[item][:state] = state
262
- tasks[item][:modified] = @today.strftime(DATE_FORMAT)
263
- if !note.nil? && !note.empty?
264
- tasks[item][:note] ||= []
265
- tasks[item][:note].push(note)
266
- end
267
- write_tasks(tasks)
268
- list(tasks)
268
+ update_task item, :list, lambda { |task|
269
+ task[:state] = state
270
+ if !note.nil? && !note.empty?
271
+ task[:note] ||= []
272
+ task[:note].push(note)
273
+ end
274
+ }
269
275
  end
270
276
 
271
277
  def set_priority(item, note = nil)
272
- tasks = load_tasks(item)
273
- tasks[item][:priority] = !tasks[item][:priority]
274
- tasks[item].delete(:priority) if !tasks[item][:priority]
275
- tasks[item][:modified] = @today.strftime(DATE_FORMAT)
276
- if !note.nil? && !note.empty?
277
- tasks[item][:note] ||= []
278
- tasks[item][:note].push(note)
279
- end
280
- write_tasks(tasks)
281
- list(tasks)
278
+ update_task item, :list, lambda { |task|
279
+ task[:priority] = !task[:priority]
280
+ task.delete(:priority) if !task[:priority]
281
+ if !note.nil? && !note.empty?
282
+ task[:note] ||= []
283
+ task[:note].push(note)
284
+ end
285
+ }
282
286
  end
283
287
 
284
288
  def due_date(item, date = '')
285
- tasks = load_tasks(item)
286
- tasks[item][:due] = convert_due_date(date)
287
- tasks[item].delete(:due) if tasks[item][:due].nil?
288
- tasks[item][:modified] = @today.strftime(DATE_FORMAT)
289
- write_tasks(tasks)
290
- list(tasks)
289
+ update_task item, :list, lambda { |task|
290
+ task[:due] = convert_due_date(date)
291
+ task.delete(:due) if task[:due].nil?
292
+ }
291
293
  end
292
294
 
293
295
  def list(tasks = nil, patterns = nil)
294
- items = {}
295
296
  tasks = tasks || load_tasks
296
297
  task_indent = [tasks.keys.max.to_s.size, 4].max
297
- patterns = patterns.nil? || patterns.empty? ? [@queries[':active']] : patterns
298
- tasks.each do |num, task|
299
- normalized_task = "state=#{task[:state]} due=#{task[:due]} #{task[:title]}"
300
- match = true
301
- patterns.each do |pattern|
302
- match = false unless /#{@queries[pattern] || pattern}/ix.match(normalized_task)
303
- end
304
- items[num] = task if match
305
- end
298
+ patterns = patterns.nil? || patterns.empty? ? [':active'] : patterns
299
+ items = filter_tasks(tasks, patterns)
306
300
  items = items.sort_by do |num, task|
307
301
  [task[:priority] && task[:state] != 'done' ? 0 : 1, ORDER[task[:state] || 'default'], task[:due] || 'n/a', num]
308
302
  end
@@ -316,7 +310,7 @@ class Todo
316
310
  priority_flag = task[:priority] ? colorize(PRIORITY_FLAG, :red) : ' '
317
311
  due_date = ''
318
312
  if task[:due] && state != 'done'
319
- date_diff = (Date.parse(task[:due]) - @today.to_date).to_i
313
+ date_diff = (Date.strptime(task[:due], DATE_FORMAT) - @today).to_i
320
314
  if date_diff < 0
321
315
  due_date = colorize("(#{date_diff.abs}d overdue)", :red)
322
316
  elsif date_diff == 0 || date_diff == 1
@@ -332,24 +326,20 @@ class Todo
332
326
  end
333
327
 
334
328
  def add_note(item, text)
335
- tasks = load_tasks(item)
336
- tasks[item][:note] ||= []
337
- tasks[item][:note].push(text)
338
- tasks[item][:modified] = @today.strftime(DATE_FORMAT)
339
- write_tasks(tasks)
340
- show(item)
329
+ update_task item, :show, lambda { |task|
330
+ task[:note] ||= []
331
+ task[:note].push(text)
332
+ }
341
333
  end
342
334
 
343
335
  def delete_note(item)
344
- tasks = load_tasks(item)
345
- tasks[item].delete(:note)
346
- tasks[item][:modified] = @today.strftime(DATE_FORMAT)
347
- write_tasks(tasks)
348
- show(item)
336
+ update_task item, :show, lambda { |task|
337
+ task.delete(:note)
338
+ }
349
339
  end
350
340
 
351
- def show(item)
352
- tasks = load_tasks(item)
341
+ def show(item, tasks = nil)
342
+ tasks = tasks || load_tasks(item)
353
343
  tasks[item].each do |key, value|
354
344
  val = value.kind_of?(Array) ? "\n" + value.join("\n") : value
355
345
  puts "#{colorize(key.to_s.rjust(10, ' ') + ':', :cyan)} #{val}"
@@ -369,19 +359,41 @@ class Todo
369
359
  end
370
360
  end
371
361
 
362
+ def cleanup(patterns)
363
+ tasks = load_tasks
364
+ patterns = [':done'] + patterns.to_a
365
+ items = filter_tasks(tasks, patterns)
366
+ items.keys.each do |num| tasks.delete(num) end
367
+ write_tasks(tasks)
368
+ puts "Deleted #{items.size} todo(s)"
369
+ end
370
+
371
+ def filter_tasks(tasks, patterns)
372
+ items = {}
373
+ tasks.each do |num, task|
374
+ normalized_task = "state=#{task[:state]} due=#{task[:due]} #{task[:title]}"
375
+ match = true
376
+ patterns.each do |pattern|
377
+ match = false unless /#{@queries[pattern] || pattern}/ix.match(normalized_task)
378
+ end
379
+ items[num] = task if match
380
+ end
381
+ return items
382
+ end
383
+
372
384
  def colorize(text, color)
373
385
  "\e[#{COLOR_CODES[color]}m#{text}\e[0m"
374
386
  end
375
387
 
376
- def convert_due_date(date = '')
388
+ def convert_due_date(date)
377
389
  due = nil
378
390
  day_index = @due_date_days.index(date.to_s.downcase) ||
379
391
  DUE_DATE_DAYS_SIMPLE.index(date.to_s.downcase) ||
380
392
  @due_date_days.map do |day| day[0..2] end.index(date.to_s.downcase)
381
393
  if day_index
382
- due = (@today.to_date + day_index).strftime(DATE_FORMAT)
394
+ due = (@today + day_index).strftime(DATE_FORMAT)
383
395
  else
384
- due = date.nil? || date.empty? ? nil : Date.parse(date).strftime(DATE_FORMAT)
396
+ due = date.nil? || date.empty? ? nil : Date.strptime(date, DATE_FORMAT).strftime(DATE_FORMAT)
385
397
  end
386
398
  return due
387
399
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: todo-jsonl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.1.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabor Bata
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-01 00:00:00.000000000 Z
11
+ date: 2021-03-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: