Sha256: 0636088eb4f003dbfd08b07747284e1eaaa3d10152ee3dca32f61e033591c9b0

Contents?: true

Size: 841 Bytes

Versions: 41

Compression:

Stored size: 841 Bytes

Contents

# -*- coding: utf-8 -*-
#
# Copyright 2013 whiteleaf. All rights reserved.
#

class ProgressBar
  class OverRangeError < StandardError; end
  
  def initialize(max, interval = 1, width = 50, char = "*")
    @max = max == 0 ? 1.0 : max.to_f
    @interval = interval
    @width = width
    @char = char
    @counter = 0
  end

  def output(num)
    return if $debug
    if num > @max
      raise OverRangeError, "`#{num}` over `#{@max}(max)`"
    end
    @counter += 1
    return unless @counter % @interval == 0
    ratio = calc_ratio(num)
    now = (@width * ratio).round
    rest = @width - now
    STDOUT.print "[" + @char * now + ' ' * rest + "] #{(ratio * 100).round}%\r"
  end

  def clear
    return if $debug
    STDOUT.print " " * 79 + "\r"
  end

  def calc_ratio(num)
    num / @max
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
narou-1.7.1 lib/progressbar.rb
narou-1.6.4 lib/progressbar.rb
narou-1.6.3 lib/progressbar.rb
narou-1.6.1 lib/progressbar.rb
narou-1.6.0 lib/progressbar.rb
narou-1.5.11 lib/progressbar.rb
narou-1.5.10.1 lib/progressbar.rb
narou-1.5.10 lib/progressbar.rb
narou-1.5.9 lib/progressbar.rb
narou-1.5.8 lib/progressbar.rb
narou-1.5.7.1 lib/progressbar.rb
narou-1.5.7 lib/progressbar.rb
narou-1.5.6.1 lib/progressbar.rb
narou-1.5.6 lib/progressbar.rb
narou-1.5.5.1 lib/progressbar.rb
narou-1.5.5 lib/progressbar.rb
narou-1.5.4 lib/progressbar.rb
narou-1.5.3 lib/progressbar.rb
narou-1.5.2.1 lib/progressbar.rb
narou-1.5.2 lib/progressbar.rb