lib/progress_bar.rb in progress_bar-0.3.4 vs lib/progress_bar.rb in progress_bar-0.4.0
- old
+ new
@@ -2,30 +2,34 @@
require 'options'
require 'highline'
class ProgressBar
- attr_accessor :count, :max, :start, :meters
+ attr_accessor :count, :max, :meters
- def initialize(max = 100, *meters)
+ def initialize(*args)
- @count = 0
- @max = max
- @start = Time.now
+ @count = 0
+ @max = 100
+ @meters = [:bar, :counter, :percentage, :elapsed, :eta, :rate]
- if meters.empty?
- @meters = [:bar, :counter, :percentage, :elapsed, :eta, :rate]
- else
- @meters = meters
- end
+ @max = args.shift if args.first.is_a? Numeric
+ @meters = args unless args.empty?
- @hl = HighLine.new
+ @last_write = Time.at(0)
+ @start = Time.now
+
+ @hl = HighLine.new
end
def increment!(count = 1)
self.count += count
- write
+ now = Time.now
+ if (now - @last_write) > 0.2 || self.count >= max
+ write
+ @last_write = now
+ end
end
def write
clear!
print to_s
@@ -42,10 +46,10 @@
def percentage
ratio * 100
end
def elapsed
- Time.now - start
+ Time.now - @start
end
def rate
if count > 0
count / elapsed