Class: Tqdm::Decorator
- Inherits:
-
Object
- Object
- Tqdm::Decorator
- Extended by:
- Forwardable
- Defined in:
- lib/tqdm/decorator.rb
Overview
The Enumerable
is cloned before it is enhanced; it is not modified directly.
Decorates the #each method of an Enumerable
by wrapping it so that each
iteration produces a pretty progress bar printed to the console or a file handle.
Instance Attribute Summary collapse
-
#enumerable ⇒ Object
readonly
Returns the value of attribute enumerable.
-
#iteration ⇒ Object
readonly
Returns the value of attribute iteration.
-
#printer ⇒ Object
readonly
Returns the value of attribute printer.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Instance Method Summary collapse
-
#enhance ⇒ Enumerable
Enhances the wrapped
Enumerable
. -
#finish! ⇒ Object
Prints the final state of the textual progress bar.
-
#increment! ⇒ Object
Called everytime the textual progress bar might need to be updated (i.e. on every iteration).
-
#initialize(enumerable, options = {}) ⇒ Decorator
constructor
Initialize a new Decorator.
-
#start! ⇒ Object
Starts the textual progress bar.
Constructor Details
#initialize(enumerable, options = {}) ⇒ Decorator
Initialize a new Decorator. Typically you wouldn't use this object, but
would immediately call #enhance
to retrieve the enhanced Enumerable
.
40 41 42 43 44 45 46 47 |
# File 'lib/tqdm/decorator.rb', line 40 def initialize(enumerable, ={}) @enumerable = enumerable .merge!(total: total!) unless [:total] @printer = Printer.new() @min_iterations = [:min_iters] || 1 @min_interval = [:min_interval] || 0.5 @leave = [:leave] || false end |
Instance Attribute Details
#enumerable ⇒ Object (readonly)
Returns the value of attribute enumerable.
18 19 20 |
# File 'lib/tqdm/decorator.rb', line 18 def enumerable @enumerable end |
#iteration ⇒ Object (readonly)
Returns the value of attribute iteration.
18 19 20 |
# File 'lib/tqdm/decorator.rb', line 18 def iteration @iteration end |
#printer ⇒ Object (readonly)
Returns the value of attribute printer.
18 19 20 |
# File 'lib/tqdm/decorator.rb', line 18 def printer @printer end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
18 19 20 |
# File 'lib/tqdm/decorator.rb', line 18 def start_time @start_time end |
Instance Method Details
#enhance ⇒ Enumerable
The Enumerable
is cloned (shallow copied) before it is enhanced; it is not modified directly.
Enhances the wrapped Enumerable
.
88 89 90 91 |
# File 'lib/tqdm/decorator.rb', line 88 def enhance decorate_enumerable_each enhanced end |
#finish! ⇒ Object
Prints the final state of the textual progress bar. Based on the :leave
option, this
may include deleting it entirely.
76 77 78 79 80 |
# File 'lib/tqdm/decorator.rb', line 76 def finish! return printer.null_finish unless @leave printer.finish(iteration, elapsed_time!, reprint?) end |
#increment! ⇒ Object
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
options.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/tqdm/decorator.rb', line 61 def increment! @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!) @last_printed_iteration = iteration @last_print_time = current_time end |
#start! ⇒ Object
Starts the textual progress bar.
50 51 52 53 |
# File 'lib/tqdm/decorator.rb', line 50 def start! @iteration = @last_printed_iteration = 0 @start_time = @last_print_time = current_time! end |