Skip to content

Commit

Permalink
Improved pre-defined pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbata committed Mar 10, 2021
1 parent 88d79c2 commit 04dc72e
Show file tree
Hide file tree
Showing 5 changed files with 619 additions and 618 deletions.
22 changes: 11 additions & 11 deletions bin/todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def execute(arguments)
rescue => error
puts "#{colorize('ERROR:', :red)} #{error}"
end
self
end

private
Expand Down Expand Up @@ -177,15 +178,15 @@ def setup
due_dates_for_queries = next_7_days.map do |day| day.strftime(DATE_FORMAT) end

@queries = {
':active' => 'state=(new|started|blocked)',
':done' => 'state=done',
':blocked' => 'state=blocked',
':started' => 'state=started',
':new' => 'state=new',
':all' => 'state=\w+',
':today' => "due=#{due_dates_for_queries[0]}",
':tomorrow' => "due=#{due_dates_for_queries[1]}",
':next7days' => "due=(#{due_dates_for_queries.join('|')})"
':active' => lambda do |task| /(new|started|blocked)/.match(task[:state]) end,
':done' => lambda do |task| 'done' == task[:state] end,
':blocked' => lambda do |task| 'blocked' == task[:state] end,
':started' => lambda do |task| 'started' == task[:state] end,
':new' => lambda do |task| 'new' == task[:state] end,
':all' => lambda do |task| /\w+/.match(task[:state]) end,
':today' => lambda do |task| due_dates_for_queries[0] == task[:due] end,
':tomorrow' => lambda do |task| due_dates_for_queries[1] == task[:due] end,
':next7days' => lambda do |task| /(#{due_dates_for_queries.join('|')})/.match(task[:due]) end
}
end

Expand Down Expand Up @@ -374,10 +375,9 @@ def cleanup(patterns)
def filter_tasks(tasks, patterns)
items = {}
tasks.each do |num, task|
normalized_task = "state=#{task[:state]} due=#{task[:due]} #{task[:title]}"
match = true
patterns.each do |pattern|
match = false unless /#{@queries[pattern] || pattern}/ix.match(normalized_task)
match = false unless @queries[pattern] ? @queries[pattern].call(task) : /#{pattern}/ix.match(task[:title])
end
items[num] = task if match
end
Expand Down
4 changes: 2 additions & 2 deletions todo.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = 'todo-jsonl'
s.version = '0.1.28'
s.date = '2021-03-09'
s.version = '0.1.29'
s.date = '2021-03-10'
s.summary = 'todo list manager inspired by todo.txt using the jsonl format'
s.authors = ['Gabor Bata']
s.homepage = 'https://github.com/gaborbata/todo'
Expand Down
Loading

0 comments on commit 04dc72e

Please sign in to comment.