Sha256: 97b5f1a381b53d0717ca6f1c6c8a3b0f126e70e4cbf5ee5bf763f5d4b2ab4a4b

Contents?: true

Size: 813 Bytes

Versions: 1

Compression:

Stored size: 813 Bytes

Contents

module Rmega
  class Progress

    def initialize(params)
      @filesize = params[:filesize]
      @verb = params[:verb].capitalize
      @progress = 0

      render
    end

    def render
      percentage = (100.0 * @progress / @filesize).round(2)
      message = "#{@verb} in progress #{format_bytes(@progress)} of #{format_bytes(@filesize)} (#{percentage}%)"
      rtrn = "\n" if @filesize == @progress

      print "\r#{' '*(message.size + 15)}\r#{message}#{rtrn}"
    end

    def increment(bytes)
      @progress += bytes

      render
    end

    def format_bytes(bytes, round = 2)
      units = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB']
      e = (bytes == 0 ? 0 : Math.log(bytes)) / Math.log(1024)
      value = bytes.to_f / (1024 ** e.floor)

      "#{value.round(round)}#{units[e]}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rmega-0.1.0 lib/rmega/progress.rb