lib/tqdm/decorator.rb in tqdm-0.2.0 vs lib/tqdm/decorator.rb in tqdm-0.3.0
- old
+ new
@@ -10,10 +10,12 @@
# @example Enhances `arr` so that an animated progress bar prints while iterating.
# arr = (0...1000)
# arr_tqdm = Decorator.new(arr).enhance
# arr_tqdm.each { |x| sleep 0.01 }
class Decorator
+
+ extend Forwardable
attr_reader :printer, :enumerable, :iteration, :start_time
# Initialize a new Decorator. Typically you wouldn't use this object, but
# would immediately call `#enhance` to retrieve the enhanced `Enumerable`.
@@ -44,13 +46,12 @@
@leave = options[:leave] || false
end
# Starts the textual progress bar.
def start!
- @iteration = 0
- @start_time = Time.now
- printer.start
+ @iteration = @last_printed_iteration = 0
+ @start_time = @last_print_time = current_time!
end
# Called everytime the textual progress bar might need to be updated (i.e. on
# every iteration). We still check whether the update is appropriate to print to
# the progress bar before doing so, according to the `:min_iters` and `:min_interval`
@@ -61,12 +62,13 @@
@iteration += 1
return unless (iteration - last_printed_iteration) >= @min_iterations
# We check the counter first, to reduce the overhead of Time.now
return unless (current_time! - last_print_time) >= @min_interval
+ return if iteration == total && !@leave
- printer.status(iteration, elapsed_time!)
+ printer.status(iteration, elapsed_time!)
@last_printed_iteration = iteration
@last_print_time = current_time
end
# Prints the final state of the textual progress bar. Based on the `:leave` option, this
@@ -132,7 +134,9 @@
end
def reprint?
last_printed_iteration < iteration
end
+
+ def_delegator :printer, :total
end
end