diff options
| author | Zack Hobson | 2009-07-21 10:24:31 -0700 | 
|---|---|---|
| committer | Zack Hobson | 2009-07-21 10:24:31 -0700 | 
| commit | 00f5c6cf9c061ecf2dca4e8c2a558ffcaed041ed (patch) | |
| tree | d00c170030d3b561c0e0d5eccb972c8762914a9f /lib | |
| parent | e0a2e5b1e9cfe7ea81b335e62d33ef6aadc55964 (diff) | |
| download | hcl-00f5c6cf9c061ecf2dca4e8c2a558ffcaed041ed.tar.bz2 | |
Implement task aliases, closes #2
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/hcl.rb | 46 | ||||
| -rw-r--r-- | lib/hcl/task.rb | 8 | 
2 files changed, 46 insertions, 8 deletions
@@ -14,6 +14,7 @@ require 'hcl/day_entry'  class HCl    VERSION = "0.1.0" +  SETTINGS_FILE = "#{ENV['HOME']}/.hcl_settings"    class UnknownCommand < StandardError; end @@ -40,6 +41,7 @@ class HCl    def initialize conf_file      config = YAML::load File.read(conf_file)      TimesheetResource.configure config +    read_settings    end    def process_args *args @@ -64,7 +66,7 @@ Examples:  Options:  EOM -      stop_on %w[ show tasks add rm start stop ] +      stop_on %w[ show tasks set unset add rm start stop ]      end      @command = args.shift      @args = args @@ -74,12 +76,48 @@ EOM    def tasks      Task.all.each do |task|        # TODO more information and formatting options -      puts "#{task.id}\t#{task}" +      puts "#{task.project.id} #{task.id}\t#{task}"      end    end +  def read_settings +    settings_file = "#{ENV['HOME']}/.hcl_settings" +    if File.exists? settings_file +      @settings = YAML.load(File.read(settings_file)) +    end +  end + +  def write_settings +    File.open(SETTINGS_FILE, 'w') do |f| +     f.write @settings.to_yaml +    end +  end + +  def set key = nil, *args +    if key.nil? +      @settings.each do |k, v| +        puts "#{k}: #{v}" +      end +    else +      value = args.join(' ') +      @settings ||= {} +      @settings[key] = value +      write_settings +    end +  end + +  def unset key +    @settings.delete key +    write_settings +  end +    def start *args -    task = Task.find args.shift +    ident = args.shift +    task = if @settings["task.#{ident}"] +      Task.find *@settings["task.#{ident}"].split(/\s+/) +    else +      Task.find ident, args.shift +    end      puts "Starting timer for #{task}"      day_entry = task.start(*args)      puts "Time is running on #{day_entry}" @@ -100,7 +138,7 @@ EOM    # Convert from decimal to a string of the form HH:MM.    def as_hours hours      minutes = hours.to_f * 60.0 -    "#{(minutes / 60).to_i}:#{(minutes % 60).to_i}" +    sprintf "%d:%02d", (minutes / 60).to_i, (minutes % 60).to_i    end    def not_implemented *args diff --git a/lib/hcl/task.rb b/lib/hcl/task.rb index 0874f51..1a742ce 100644 --- a/lib/hcl/task.rb +++ b/lib/hcl/task.rb @@ -17,8 +17,8 @@ class HCl        YAML.load File.read(File.join(ENV['HOME'],'.hcl_tasks'))      end -    def self.find id -      all.detect {|t| t.id == id } +    def self.find project_id, id +      all.detect {|t| t.project.id == project_id && t.id == id }      end      def to_s @@ -27,7 +27,7 @@ class HCl      def start *args        notes = args.join ' ' -      day = DayEntry.from_xml Task.post("daily/add", <<-EOT) +      days = DayEntry.from_xml Task.post("daily/add", <<-EOT)        <request>          <notes>#{notes}</notes>          <hours></hours> @@ -36,7 +36,7 @@ class HCl          <spent_at type="date">#{Date.today}</spent_at>        </request>        EOT -      return day +      days.first      end    end  end  | 
