Sha256: 19f74256c9a11f6fc9f5185a47d2fb639fc916e57e7a379f8ab78b7c8c6022c1
Contents?: true
Size: 870 Bytes
Versions: 26
Compression:
Stored size: 870 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 silence? 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 silence? STDOUT.print " " * 79 + "\r" end def calc_ratio(num) num / @max end def silence? $debug || ENV["NAROU_ENV"] == "test" end end
Version data entries
26 entries across 26 versions & 1 rubygems