Skip to content

Commit

Permalink
Add cleanup command
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbata committed Mar 5, 2021
1 parent c56b905 commit 8d678d3
Show file tree
Hide file tree
Showing 11 changed files with 813 additions and 26,810 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.jar
*.gem
Gemfile.lock
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Commands:
* list <regex> [regex...] list tasks (only active tasks by default)
* show <tasknumber> show all task details
* repl enter read-eval-print loop mode
* cleanup <regex> [regex...] cleanup completed tasks by regexp
* help this help screen
With list command the following pre-defined regex patterns can be also used:
Expand Down
36 changes: 27 additions & 9 deletions bin/todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def execute(arguments)
when 'repl'
raise action + ' command has no parameters' if args.length > 0
start_repl
when 'cleanup'
raise action + ' command requires at least one parameter' if args.nil? || args.empty?
cleanup(args)
else
list(nil, arguments)
end
Expand Down Expand Up @@ -152,6 +155,7 @@ def usage
* list <regex> [regex...] list tasks (only active tasks by default)
* show <tasknumber> show all task details
* repl enter read-eval-print loop mode
* cleanup <regex> [regex...] cleanup completed tasks by regexp
* help this help screen
With list command the following pre-defined regex patterns can be also used:
Expand Down Expand Up @@ -293,18 +297,10 @@ def due_date(item, date = '')
end

def list(tasks = nil, patterns = nil)
items = {}
tasks = tasks || load_tasks
task_indent = [tasks.keys.max.to_s.size, 4].max
patterns = patterns.nil? || patterns.empty? ? [@queries[':active']] : patterns
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)
end
items[num] = task if match
end
items = filter_tasks(tasks, patterns)
items = items.sort_by do |num, task|
[task[:priority] && task[:state] != 'done' ? 0 : 1, ORDER[task[:state] || 'default'], task[:due] || 'n/a', num]
end
Expand Down Expand Up @@ -371,6 +367,28 @@ def start_repl
end
end

def cleanup(patterns)
tasks = load_tasks
patterns = [@queries[':done']] + patterns.to_a
items = filter_tasks(tasks, patterns)
items.keys.each do |num| tasks.delete(num) end
write_tasks(tasks)
puts "deleted #{items.size} todo(s)"
end

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)
end
items[num] = task if match
end
return items
end

def colorize(text, color)
"\e[#{COLOR_CODES[color]}m#{text}\e[0m"
end
Expand Down
24 changes: 24 additions & 0 deletions test/test_todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,28 @@ def test_list_by_due_date
assert_equal(" 1: \e[37m[ ]\e[0m Buy Milk \e[33m(today)\e[0m\n", $stdout.string)
end

def test_cleanup_with_non_matching_todos
@todo.execute ['rename', '1', 'Buy Bread @breakfast']
$stdout = StringIO.new
@todo.execute ['cleanup', '@breakfast']
assert_match(
/{"state":"new","title":"Buy Bread @breakfast","modified":"\d{4}-\d{2}-\d{2}"}\r?\n/,
File.read(@todo_file)
)
assert_equal("deleted 0 todo(s)\n", $stdout.string)
end

def test_cleanup
@todo.execute ['rename', '1', 'Buy Bread @breakfast']
@todo.execute ['add', 'Buy Eggs @breakfast']
@todo.execute ['done', '1']
$stdout = StringIO.new
@todo.execute ['cleanup', '@breakfast']
assert_match(
/{"state":"new","title":"Buy Eggs @breakfast","modified":"\d{4}-\d{2}-\d{2}"}\r?\n/,
File.read(@todo_file)
)
assert_equal("deleted 1 todo(s)\n", $stdout.string)
end

end
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.21'
s.date = '2021-03-03'
s.version = '0.1.22'
s.date = '2021-03-05'
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 8d678d3

Please sign in to comment.