Skip to content
This repository has been archived by the owner on Jul 10, 2018. It is now read-only.

Commit

Permalink
Allow an initial time to be specified when starting a timer, closes z…
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack Hobson committed Jul 30, 2009
1 parent 31a3a51 commit d2de12e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
11 changes: 10 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
14 changes: 11 additions & 3 deletions lib/hcl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'trollop'
require 'highline/import'

require 'hcl/utility'
require 'hcl/timesheet_resource'
require 'hcl/project'
require 'hcl/task'
Expand All @@ -25,6 +26,8 @@ def initialize(*args)
end

class HCl
include Utility

VERSION_FILE = File.dirname(__FILE__) + '/../VERSION.yml'
SETTINGS_FILE = "#{ENV['HOME']}/.hcl_settings"
CONFIG_FILE = "#{ENV['HOME']}/.hcl_config"
Expand Down Expand Up @@ -172,6 +175,11 @@ def aliases
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+/)
Expand All @@ -183,8 +191,8 @@ def start *args
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
Expand Down Expand Up @@ -217,7 +225,7 @@ def show *args
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
Expand Down
10 changes: 3 additions & 7 deletions lib/hcl/day_entry.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,7 +16,7 @@ def to_s
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
Expand Down Expand Up @@ -52,13 +53,8 @@ def toggle

# 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
12 changes: 9 additions & 3 deletions lib/hcl/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ def to_s
"#{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>
Expand All @@ -43,6 +44,11 @@ def start *args
days.first
end

def start opts
day = add opts
DayEntry.from_xml(Task.get("daily/timer/#{day.id}")).first
end

end
end

18 changes: 18 additions & 0 deletions lib/hcl/utility.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d2de12e

Please sign in to comment.