Sha256: 7d177b78149357d3aa6b3d4f2701b7c1d419c86c1ff96de14255d3c2684aecd5

Contents?: true

Size: 893 Bytes

Versions: 3

Compression:

Stored size: 893 Bytes

Contents

require 'cap-util/say'

module CapUtil
  class Timer

    def self.pretty_time(elapsed)
      "#{elapsed / 60}:#{(elapsed % 60).to_s.rjust(2, '0')}"
    end

    attr_reader :name, :start_time, :end_time, :elapsed_time

    def initialize(name, quiet = nil)
      @name, @start_time, @end_time, @elapsed_time = name, 0, 0, 0
      @quiet = !!(quiet == :quiet)
    end

    def start(time = nil)
      CapUtil.say "Starting #{CapUtil.color @name, :cyan}." if !@quiet
      @start_time = (time || ::Time.now)
    end

    def end(time = nil)
      @end_time = (time || ::Time.now)
      @elapsed_time = @end_time - @start_time
      if !@quiet
        elapsed = self.class.pretty_time(@elapsed_time.to_i)
        CapUtil.say "#{CapUtil.color @name, :bold, :yellow} completed in"\
                    " #{CapUtil.color elapsed, :underline, :yellow}."
      end
      @end_time
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cap-util-1.5.2 lib/cap-util/timer.rb
cap-util-1.5.1 lib/cap-util/timer.rb
cap-util-1.5.0 lib/cap-util/timer.rb