From bbf2ddd56169aa184e38b3779038258b03556f16 Mon Sep 17 00:00:00 2001 From: David Dollar Date: Mon, 16 Jun 2008 16:50:10 -0400 Subject: [PATCH] getting initial command 'tasks' working --- lib/core_ext/datetime.rb | 9 +++++++++ lib/core_ext/enumerable.rb | 11 +--------- lib/core_ext/nil.rb | 7 +++++++ lib/core_ext/string.rb | 8 ++++++++ lib/remember-the-ruby/api.rb | 4 ++++ lib/remember-the-ruby/entity.rb | 11 ++++++++++ lib/remember-the-ruby/entity_list.rb | 2 +- lib/rtr/api.rb | 28 +++++++++++++++----------- lib/rtr/commands.rb | 30 ++++++++++++++++++++++++++-- 9 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 lib/core_ext/datetime.rb create mode 100644 lib/core_ext/nil.rb diff --git a/lib/core_ext/datetime.rb b/lib/core_ext/datetime.rb new file mode 100644 index 0000000..043c1e8 --- /dev/null +++ b/lib/core_ext/datetime.rb @@ -0,0 +1,9 @@ +require 'date' + +class DateTime + + def blank? + false + end + +end \ No newline at end of file diff --git a/lib/core_ext/enumerable.rb b/lib/core_ext/enumerable.rb index 052cd5b..32b5cd6 100644 --- a/lib/core_ext/enumerable.rb +++ b/lib/core_ext/enumerable.rb @@ -1,16 +1,7 @@ module Enumerable - def path_lookup(path) - value = self - parts = path.to_s.split('/') - parts.each do |part| - value = hash[part] - end - value - end - def sorted_by(attribute) - self.sort_by { |e| e.path_lookup(attribute) } + self.sort_by { |e| e[attribute] } end end \ No newline at end of file diff --git a/lib/core_ext/nil.rb b/lib/core_ext/nil.rb new file mode 100644 index 0000000..f5cb8a0 --- /dev/null +++ b/lib/core_ext/nil.rb @@ -0,0 +1,7 @@ +class NilClass + + def blank? + true + end + +end \ No newline at end of file diff --git a/lib/core_ext/string.rb b/lib/core_ext/string.rb index e49a168..a680b41 100644 --- a/lib/core_ext/string.rb +++ b/lib/core_ext/string.rb @@ -1,7 +1,11 @@ require 'date' +require 'rubygems' +require 'term/ansicolor' class String + include Term::ANSIColor + def to_date begin date = DateTime.parse(self) @@ -11,4 +15,8 @@ def to_date date end + def blank? + self == "" + end + end \ No newline at end of file diff --git a/lib/remember-the-ruby/api.rb b/lib/remember-the-ruby/api.rb index e074103..a27c29a 100644 --- a/lib/remember-the-ruby/api.rb +++ b/lib/remember-the-ruby/api.rb @@ -35,6 +35,10 @@ def tasks # derivative objects ###################################################### + def default_list + lists.find(settings["defaultlist"]) + end + def tags found_tags = EntityList.new(@transport, Tag) tasks.each do |task| diff --git a/lib/remember-the-ruby/entity.rb b/lib/remember-the-ruby/entity.rb index ef6e1fb..0ffb20a 100644 --- a/lib/remember-the-ruby/entity.rb +++ b/lib/remember-the-ruby/entity.rb @@ -34,6 +34,17 @@ def [](key) def []=(key, value) regular_writer(key.to_s, value) end + + alias_method :original_lookup, :[] + + def [](key) + value = self + parts = key.to_s.split('/') + parts.each do |part| + value = value.original_lookup(part) + end + value + end end end \ No newline at end of file diff --git a/lib/remember-the-ruby/entity_list.rb b/lib/remember-the-ruby/entity_list.rb index b9b83a5..039d822 100644 --- a/lib/remember-the-ruby/entity_list.rb +++ b/lib/remember-the-ruby/entity_list.rb @@ -53,6 +53,6 @@ def self.from_rsp(transport, type, rsp, xpath) end entity_list end - + end end \ No newline at end of file diff --git a/lib/rtr/api.rb b/lib/rtr/api.rb index 41eb389..94c9829 100644 --- a/lib/rtr/api.rb +++ b/lib/rtr/api.rb @@ -1,18 +1,22 @@ -def api_connection +def api - api = RememberTheRuby::API.new(:frob => $storage[:frob], - :token => $storage[:token]) + @api ||= begin - unless $storage[:frob] - system %{open "#{api.authorization_url}"} - $storage[:frob] = api.frob - exit - end + api = RememberTheRuby::API.new(:frob => $storage[:frob], + :token => $storage[:token]) - unless $storage[:token] - $storage[:token] = api.authenticate - end + unless $storage[:frob] + system %{open "#{api.authorization_url}"} + $storage[:frob] = api.frob + exit + end + + unless $storage[:token] + $storage[:token] = api.authenticate + end - api + api + + end end diff --git a/lib/rtr/commands.rb b/lib/rtr/commands.rb index 135a3b8..647143b 100644 --- a/lib/rtr/commands.rb +++ b/lib/rtr/commands.rb @@ -1,9 +1,35 @@ +require 'term/ansicolor' + module RTR class Commands register_method :tasks do |options| - pp api_connection.tasks.select do |t| - t.next.due.to_date && t.next.due.to_date > DateTime.now + + puts "Oustanding Tasks".white.bold + puts + api.default_list.tasks.sorted_by('next/due').reverse.each do |task| + + now = DateTime.now + + due = task['next/due'] + due = due.to_date unless due.blank? + + diff = due.blank? ? nil : due - now + + color = case + when diff.nil? then :white + when diff.abs < 1 && due.day == now.day then :yellow + when diff < 0 then :red + else :white + end + + title = task.name + title = title.send(color).bold if color + + prefix = " * " + prefix = prefix.send(color).bold if color + + puts prefix + title end end