From d3f93ca9dbff5b08d8659b60d77b75d4c4af291d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Str=C3=B6m?= Date: Wed, 3 Feb 2010 18:38:02 +0100 Subject: [PATCH] make it possible to filter tasks by tag, both from within the lib and the cmd line tool --- bin/things | 7 ++++--- lib/things/focus.rb | 4 ++++ test/test_focus.rb | 5 +++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/bin/things b/bin/things index 6a59477..48c5f7b 100755 --- a/bin/things +++ b/bin/things @@ -4,10 +4,10 @@ require "optparse" require 'things' options = { :tasks => { :completed => false } } + opts = OptionParser.new do |opts| opts.separator '' opts.separator 'Options:' - opts.banner = "Usage: things [options] today|next|inbox|someday|logbook|trash" def opts.show_usage @@ -20,8 +20,9 @@ opts = OptionParser.new do |opts| end opts.on("-c", "--completed", 'Shows only completed tasks') { options[:tasks] = { :completed => true } } - opts.on("-a", "--all", 'Shows all tasks in the focus') { |f| options[:tasks] = { } } - + opts.on("-a", "--all", 'Shows all tasks in the focus') { options[:tasks].delete(:completed) } + opts.on("-t TAG", "--tag TAG", 'Filter tasks by tag') { |f| options[:tasks][:tag] = f } + opts.on_tail("-h", "--help", "Shows this help message") { opts.show_usage } opts.on_tail("-v", "--version", "Shows version") do puts Things::Version::STRING diff --git a/lib/things/focus.rb b/lib/things/focus.rb index 7066c38..ee54f5c 100644 --- a/lib/things/focus.rb +++ b/lib/things/focus.rb @@ -58,6 +58,10 @@ def filter_tasks!(options) end end end + + if tag = options[:tag] + @tasks = @tasks.select { |t| t.tag?(tag) } + end end end end \ No newline at end of file diff --git a/test/test_focus.rb b/test/test_focus.rb index 34510f3..93db53c 100644 --- a/test/test_focus.rb +++ b/test/test_focus.rb @@ -95,6 +95,11 @@ def setup assert_equal 0, tasks.select(&:completed?).length end + test "should filter tasks by tag" do + tasks = @things.focus(:next).tasks(:tag => 'home') + assert tasks.all? { |e| e.tag?('home') } + end + test "should not include projects when listing tasks" do with_children = @things.focus(:next).tasks.select(&:children?) assert_equal 0, with_children.length