diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/hcl/app.rb | 10 | ||||
| -rw-r--r-- | lib/hcl/commands.rb | 61 | ||||
| -rw-r--r-- | lib/hcl/day_entry.rb | 5 | ||||
| -rw-r--r-- | lib/hcl/utility.rb | 34 | 
4 files changed, 62 insertions, 48 deletions
| diff --git a/lib/hcl/app.rb b/lib/hcl/app.rb index 6b3e428..d615891 100644 --- a/lib/hcl/app.rb +++ b/lib/hcl/app.rb @@ -61,14 +61,6 @@ module HCl        Commands.method_defined? command      end -    def start_or_note *args -      if DayEntry.with_timer -        note *args -      else -        start *args -      end -    end -      # Start the application.      def run        begin @@ -83,7 +75,7 @@ module HCl                end              end            else -            start_or_note @command, *@args +            start @command, *@args            end          else            show diff --git a/lib/hcl/commands.rb b/lib/hcl/commands.rb index 2ca3742..3bdfb66 100644 --- a/lib/hcl/commands.rb +++ b/lib/hcl/commands.rb @@ -41,6 +41,7 @@ module HCl          puts "Added alias @#{task_name} for #{task}."        else          puts "Unrecognized project and task ID: #{value.inspect}" +        exit 1        end      end @@ -49,48 +50,27 @@ module HCl      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.detect {|a| a[0] == '@' } -      if ident -        args.delete(ident) -        ident = ident.slice(1..-1) -      else -        ident = args.shift -      end -      if ident -        task_ids = if @settings.key? "task.#{ident}" -            @settings["task.#{ident}"].split(/\s+/) -          else -            [ident, args.shift] -          end -        task = Task.find *task_ids -        if task.nil? -          puts "Unknown task alias, try one of the following: ", aliases.join(', ') -          exit 1 -        end -        timer = task.start( -          :starting_time => starting_time, -          :note => args.join(' ') -        ) -        puts "Started timer for #{timer} (at #{current_time})" -      else -        puts "You must provide a task alias to start a timer:", aliases.join(', ') +      starting_time = get_starting_time args +      task = get_task args +      if task.nil? +        puts "Unknown task alias, try one of the following: ", aliases.join(', ')          exit 1        end +      timer = task.start \ +        :starting_time => starting_time, +        :note => args.join(' ') +      puts "Started timer for #{timer} (at #{current_time})"      end      def stop *args        entry = DayEntry.with_timer        if entry -        entry.append_note(*args.join(' ')) if args.any? +        entry.append_note(args.join(' ')) if args.any?          entry.toggle          puts "Stopped #{entry} (at #{current_time})"        else          puts "No running timers found." +        exit 1        end      end @@ -102,6 +82,7 @@ module HCl          puts "Added note to #{entry}."        else          puts "No running timers found." +        exit 1        end      end @@ -118,19 +99,21 @@ module HCl        puts "\t#{as_hours total_hours}\ttotal (as of #{current_time})"      end -    def resume -      entry = DayEntry.last +    def resume *args +      ident = get_ident args +      entry = if ident +          task_ids = get_task_ids ident +          DayEntry.last_by_task *task_ids +        else +          DayEntry.last +        end        if entry -        puts "Resumed #{entry} (at #{current_time})"          entry.toggle        else -        puts "No timers found" +        puts "No matching timer found." +        exit 1        end      end -  private -    def current_time -      Time.now.strftime('%I:%M %p').downcase -    end    end  end diff --git a/lib/hcl/day_entry.rb b/lib/hcl/day_entry.rb index afcdf08..84b5e3d 100644 --- a/lib/hcl/day_entry.rb +++ b/lib/hcl/day_entry.rb @@ -40,6 +40,11 @@ module HCl        all.detect {|t| t.running? }      end +    def self.last_by_task project_id, task_id +      all.sort {|a,b| b.updated_at<=>a.updated_at}. +        detect {|t| t.project_id == project_id && t.task_id == task_id } +    end +      def self.last        all.sort {|a,b| a.updated_at<=>b.updated_at}[-1]      end diff --git a/lib/hcl/utility.rb b/lib/hcl/utility.rb index 7cf1bfb..c724846 100644 --- a/lib/hcl/utility.rb +++ b/lib/hcl/utility.rb @@ -1,5 +1,39 @@  module HCl    module Utility +    def get_task_ids ident +      if @settings.key? "task.#{ident}" +        @settings["task.#{ident}"].split(/\s+/) +      else +        [ident, args.shift] +      end +    end + +    def get_ident args +      ident = args.detect {|a| a[0] == '@' } +      if ident +        args.delete(ident) +        ident.slice(1..-1) +      else +        args.shift +      end +    end + +    def get_task args +      Task.find *get_task_ids(get_ident(args)) +    end + +    def get_starting_time args +      starting_time = args.detect {|x| x =~ /^\+\d*(\.|:)?\d+$/ } +      if starting_time +        args.delete(starting_time) +        time2float starting_time +      end +    end + +    def current_time +      Time.now.strftime('%I:%M %p').downcase +    end +      # Convert from decimal to a string of the form HH:MM.      #      # @param [#to_f] hours number of hours in decimal | 
