Sha256: ed86d2576cc49de3a0d2c829bbd283c03a5da40967c1868b2d867cb597a2d6d7

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

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

    # "%d days, %d hours, %d minutes and %d seconds" % [dd, hh, mm, ss]
    #=> 3 days, 3 hours, 15 minutes and 21 seconds
    def format_seconds(seconds)
      mm, ss = seconds.divmod(60)
      hh, mm = mm.divmod(60)
      dd, hh = hh.divmod(24)
      "%02d:%02d:%02d" % [hh, mm, ss]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fortschritt-1.0.2 lib/fortschritt/printer.rb
fortschritt-1.0.1 lib/fortschritt/printer.rb
fortschritt-1.0.0 lib/fortschritt/printer.rb
fortschritt-0.3.6 lib/fortschritt/printer.rb
fortschritt-0.2.6 lib/fortschritt/printer.rb