Sha256: 57f63d35615c5eaf25e689d8e65307ec5d93daa1ba11563fbb3478df5d9a7973

Contents?: true

Size: 973 Bytes

Versions: 1

Compression:

Stored size: 973 Bytes

Contents

module Fortschritt
  class Printer
    def print(meter, stream = STDOUT)
      stream.print output(meter) if stream.tty?
    end

    def output(meter)
      result = [
        slice(meter),
        remaining(meter),
        eta(meter),
        finished_after(meter),
      ].compact.join ' → '
      "\r#{result}#{eol(meter)}"
    end

    def slice(meter)
      "#{meter.done}/#{meter.total}"
    end

    def eta(meter)
      result = Time.now + meter.remaining_seconds
      "ETA #{result.strftime "%Y-%m-%d %H:%M:%S"}"
    end

    def finished_after(meter)
      meter.completed? or return
      "took #{format_seconds(meter.total_elapsed_seconds)}"
    end

    def remaining(meter)
      format_seconds meter.remaining_seconds
    end

    def eol(meter)
      meter.completed? ? "\n" : ''
    end

    private

    def format_seconds(seconds)
      mm, ss = seconds.divmod(60)
      hh, mm = mm.divmod(60)
      "%02d:%02d:%02d" % [hh, mm, ss]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fortschritt-1.0.3 lib/fortschritt/printer.rb