Sha256: a63693eef54fd2f761b09abbf8cbe99b0724d07c4cccfafab9a0394de09a98a1

Contents?: true

Size: 870 Bytes

Versions: 6

Compression:

Stored size: 870 Bytes

Contents

# frozen_string_literal: true

require_relative '../converter'

module TTY
  class ProgressBar
    # Used by {Pipeline} to format :eta token
    #
    # @api private
    class EstimatedFormatter
      MATCHER = /:eta/.freeze

      def initialize(progress)
        @progress  = progress
      end

      # Determines whether this formatter is applied or not.
      #
      # @param [Object] value
      #
      # @return [Boolean]
      #
      # @api private
      def matches?(value)
        !!(value.to_s =~ MATCHER)
      end

      def format(value)
        elapsed   = Time.now - @progress.start_at
        estimated = (elapsed / @progress.ratio).to_f - elapsed
        estimated = (estimated.infinite? || estimated < 0) ? 0.0 : estimated
        value.gsub(MATCHER, Converter.to_time(estimated))
      end
    end # ElapsedFormatter
  end # ProgressBar
end # TTY

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
tty-progressbar-0.17.0 lib/tty/progressbar/formatter/estimated.rb
tty-progressbar-0.16.0 lib/tty/progressbar/formatter/estimated.rb
tty-progressbar-0.15.1 lib/tty/progressbar/formatter/estimated.rb
tty-progressbar-0.15.0 lib/tty/progressbar/formatter/estimated.rb
tty-progressbar-0.14.0 lib/tty/progressbar/formatter/estimated.rb
tty-progressbar-0.13.0 lib/tty/progressbar/formatter/estimated.rb