Sha256: c68a6f157694d716712f253e06e45713769d09da7be29141e9b41773a6d81132

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

require 'ruby-progressbar/time'

class ProgressBar
  module Components
    module Timer
      TIME_FORMAT = '%02d:%02d:%02d'

      def start
        @started_at = stopped? ? now - (@stopped_at - @started_at) : now
        @stopped_at = nil
      end

      def stop
        @stopped_at = now
      end

      def pause
        stop
      end

      def resume
        start
      end

      def started?
        !!@started_at
      end

      def stopped?
        !!@stopped_at
      end

      def reset
        @started_at = nil
        @stopped_at = nil
      end

    private
      def now
        ProgressBar::Time.now
      end

      def elapsed_seconds
        ((@stopped_at || now) - @started_at)
      end

      def elapsed_whole_seconds
        elapsed_seconds.floor
      end

      def elapsed_time
        return '--:--:--' unless started?

        hours, seconds = elapsed_whole_seconds.divmod(3600)
        minutes, seconds = seconds.divmod(60)

        sprintf TIME_FORMAT, hours, minutes, seconds
      end

      def divide_seconds(seconds)
        hours, seconds = seconds.divmod(3600)
        minutes, seconds = seconds.divmod(60)

        [hours, minutes, seconds]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-progressbar-1.3.2 lib/ruby-progressbar/components/timer.rb
ruby-progressbar-1.3.1 lib/ruby-progressbar/components/timer.rb
ruby-progressbar-1.3.0 lib/ruby-progressbar/components/timer.rb
ruby-progressbar-1.2.0 lib/ruby-progressbar/components/timer.rb