diff options
| -rw-r--r-- | README.markdown | 11 | ||||
| -rw-r--r-- | lib/hcl.rb | 14 | ||||
| -rw-r--r-- | lib/hcl/day_entry.rb | 10 | ||||
| -rw-r--r-- | lib/hcl/task.rb | 12 | ||||
| -rw-r--r-- | lib/hcl/utility.rb | 18 |
5 files changed, 51 insertions, 14 deletions
diff --git a/README.markdown b/README.markdown index 9c5f547..936ebec 100644 --- a/README.markdown +++ b/README.markdown @@ -27,7 +27,7 @@ HCl is a command-line tool for interacting with Harvest time sheets using the hcl tasks hcl set <key> <value ...> hcl unset <key> - hcl start (<task_alias> | <project_id> <task_id>) [msg ...] + hcl start (<task_alias> | <project_id> <task_id>) [+time] [msg ...] hcl note <msg ...> hcl stop @@ -54,6 +54,15 @@ identify a task, HCl supports task aliases: $ hcl set task.xdev 1234 5678 $ hcl start xdev adding a new feature +### Starting a Timer with Initial Time + +You can also provide an initial time when starting a new timer. +This can be expressed in floating-point or HH:MM. The following two +commands are identical: + + $ hcl start xdev +0:15 adding a new feature + $ hcl start +.25 xdev adding a new feature + ### Adding Notes to a Running Task While a task is running you can append strings to the note for that task: @@ -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) <request> <notes>#{notes}</notes> - <hours></hours> + <hours>#{starting_time}</hours> <project_id type="integer">#{project.id}</project_id> <task_id type="integer">#{id}</task_id> <spent_at type="date">#{Date.today}</spent_at> @@ -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 |
