Sha256: f0b43a984177a49654b76c97777ac5abec9590ca20f470b8d4dc6194720e64a5

Contents?: true

Size: 803 Bytes

Versions: 36

Compression:

Stored size: 803 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

36 entries across 36 versions & 1 rubygems

Version Path
narou-2.9.5 lib/progressbar.rb
narou-2.9.4 lib/progressbar.rb
narou-2.9.3.1 lib/progressbar.rb
narou-2.9.3 lib/progressbar.rb
narou-2.9.2 lib/progressbar.rb
narou-2.9.1 lib/progressbar.rb
narou-2.9.0 lib/progressbar.rb
narou-2.8.3.1 lib/progressbar.rb
narou-2.8.3 lib/progressbar.rb
narou-2.8.2 lib/progressbar.rb
narou-2.8.1 lib/progressbar.rb
narou-2.8.0 lib/progressbar.rb
narou-2.7.2 lib/progressbar.rb
narou-2.7.1 lib/progressbar.rb
narou-2.7.0 lib/progressbar.rb
narou-2.6.1 lib/progressbar.rb
narou-2.6.0 lib/progressbar.rb
narou-2.5.2 lib/progressbar.rb
narou-2.5.1 lib/progressbar.rb
narou-2.4.2 lib/progressbar.rb