Display/remove progress bars during processing arrays that contain a lot of elements. Sample usage:
Progress.new(@msgs, :each) { |msg| # Do something with msg }
Create a progress bar object based on the number of elements in the
list array. Update the progress bar before each invocation of the
block called by method meth. The displayed length of the
bar can also be supplied. Note that list should be an
Array
. Hashes should be converted to arrays with
to_a
before calling the constructor.
# File buryspam.rb, line 4271 def initialize(list, meth, length = 16) super("", :log => false) @total = list.size.to_f @len = length @complete = 0 @fill = nil @result = list.send(meth) { |arg| step yield(arg) } finish end
Increment the progress bar one unit towards total. Update the bar on the display only if there was a change in the length of the completion bar.
# File buryspam.rb, line 4289 def step(incr = 1) @complete += incr pcnt = @complete / @total fill = (pcnt * @len).to_i return if fill == @fill @fill = fill bar = "[#{"=" * fill}#{" " * (@len - fill)}]" pcnt = "%5.1f%%" % (pcnt * 100) # To save time, don't display "complete/total" if # total is too high. if @total < 10000 bar += " %5d/%d (%6s)" % [@complete, @total, pcnt] else bar += "%7s" % pcnt end update(bar) end