From d2de12ef6cec9d8de1e06fc6096a1dff75f96ab1 Mon Sep 17 00:00:00 2001 From: Zack Hobson Date: Thu, 30 Jul 2009 11:37:18 -0700 Subject: Allow an initial time to be specified when starting a timer, closes #9. --- lib/hcl.rb | 14 +++++++++++--- lib/hcl/day_entry.rb | 10 +++------- lib/hcl/task.rb | 12 +++++++++--- lib/hcl/utility.rb | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 lib/hcl/utility.rb (limited to 'lib') diff --git a/lib/hcl.rb b/lib/hcl.rb index 1dced54..f5889ab 100644 --- a/lib/hcl.rb +++ b/lib/hcl.rb @@ -7,6 +7,7 @@ require 'chronic' require 'trollop' require 'highline/import' +require 'hcl/utility' require 'hcl/timesheet_resource' require 'hcl/project' require 'hcl/task' @@ -25,6 +26,8 @@ class Net::HTTP end class HCl + include Utility + VERSION_FILE = File.dirname(__FILE__) + '/../VERSION.yml' SETTINGS_FILE = "#{ENV['HOME']}/.hcl_settings" CONFIG_FILE = "#{ENV['HOME']}/.hcl_config" @@ -172,6 +175,11 @@ EOM end def start *args + starting_time = args.detect {|x| x =~ /^\+\d*(\.|:)\d+$/ } + if starting_time + args.delete(starting_time) + starting_time = time2float starting_time + end ident = args.shift task_ids = if @settings.key? "task.#{ident}" @settings["task.#{ident}"].split(/\s+/) @@ -183,8 +191,8 @@ EOM puts "Unknown project/task alias, try one of the following: #{aliases.join(', ')}." exit 1 end - task.start(*args) - puts "Started timer for #{task}." + timer = task.start(:starting_time => starting_time, :note => args.join(' ')) + puts "Started timer for #{timer}." end def stop @@ -217,7 +225,7 @@ EOM total_hours = total_hours + day.hours.to_f end puts "\t" + '-' * 13 - puts "\t#{HCl::DayEntry.as_hours total_hours}\ttotal" + puts "\t#{as_hours total_hours}\ttotal" end end diff --git a/lib/hcl/day_entry.rb b/lib/hcl/day_entry.rb index 6e5f7bc..9948250 100644 --- a/lib/hcl/day_entry.rb +++ b/lib/hcl/day_entry.rb @@ -1,6 +1,7 @@ class HCl class DayEntry < TimesheetResource + include Utility # Get the time sheet entries for a given day. If no date is provided # defaults to today. def self.all date = nil @@ -15,7 +16,7 @@ class HCl def self.from_xml xml doc = REXML::Document.new xml Task.cache_tasks doc - doc.root.elements.collect('*/day_entry') do |day| + doc.root.elements.collect('//day_entry') do |day| new xml_to_hash(day) end end @@ -52,13 +53,8 @@ class HCl # Returns the hours formatted as "HH:MM" def formatted_hours - self.class.as_hours hours + as_hours hours end - # Convert from decimal to a string of the form HH:MM. - def self.as_hours hours - minutes = hours.to_f * 60.0 - sprintf "%d:%02d", (minutes / 60).to_i, (minutes % 60).to_i - end end end diff --git a/lib/hcl/task.rb b/lib/hcl/task.rb index e5dc267..8a4769a 100644 --- a/lib/hcl/task.rb +++ b/lib/hcl/task.rb @@ -29,12 +29,13 @@ class HCl "#{project.name} #{name}" end - def start *args - notes = args.join ' ' + def add opts + notes = opts[:note] + starting_time = opts[:starting_time] || 0 days = DayEntry.from_xml Task.post("daily/add", <<-EOT) #{notes} - + #{starting_time} #{project.id} #{id} #{Date.today} @@ -43,6 +44,11 @@ class HCl days.first end + def start opts + day = add opts + DayEntry.from_xml(Task.get("daily/timer/#{day.id}")).first + end + end end diff --git a/lib/hcl/utility.rb b/lib/hcl/utility.rb new file mode 100644 index 0000000..2d8c28c --- /dev/null +++ b/lib/hcl/utility.rb @@ -0,0 +1,18 @@ +class HCl + module Utility + # Convert from decimal to a string of the form HH:MM. + def as_hours hours + minutes = hours.to_f * 60.0 + sprintf "%d:%02d", (minutes / 60).to_i, (minutes % 60).to_i + end + + def time2float time_string + if time_string =~ /:/ + hours, minutes = time_string.split(':') + hours.to_f + (minutes.to_f / 60.0) + elsif time_string =~ /./ + time_string.to_f + end + end + end +end -- cgit v1.2.3