Class: Tqdm::Decorator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tqdm/decorator.rb

Overview

Note:

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.

Examples:

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 }

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Examples:

a = (1...1000)
Decorator.new(a).enhance.each { |x| sleep 0.01 }
a = [1, 2, 3, 4]
Decorator.new(a, file: $stdout, leave: true)

Parameters:

  • enumerable (Enumerable)

    the Enumerable object to be enhanced

  • options (Hash) (defaults to: {})

    more options used to control behavior of the progress bar

Options Hash (options):

  • :desc (String)

    a short description added to the beginning of the progress bar

  • :total (Integer) — default: self.size

    the expected number of iterations

  • :file (File, IO) — default: $stderr

    a file-like object to output the progress bar to

  • :leave (Boolean) — default: false

    should the progress bar should stay on screen after it's done?

  • :min_iters (Integer)

    see :min_interval

  • :min_interval (Float)

    If less than min_interval seconds or min_iters iterations have passed since the last progress meter update, it is not updated again.



40
41
42
43
44
45
46
47
# File 'lib/tqdm/decorator.rb', line 40

def initialize(enumerable, options={})
  @enumerable = enumerable
  options.merge!(total: total!) unless options[:total]
  @printer = Printer.new(options)
  @min_iterations = options[:min_iters] || 1
  @min_interval = options[:min_interval] || 0.5
  @leave = options[:leave] || false
end

Instance Attribute Details

#enumerableObject (readonly)

Returns the value of attribute enumerable.



18
19
20
# File 'lib/tqdm/decorator.rb', line 18

def enumerable
  @enumerable
end

#iterationObject (readonly)

Returns the value of attribute iteration.



18
19
20
# File 'lib/tqdm/decorator.rb', line 18

def iteration
  @iteration
end

#printerObject (readonly)

Returns the value of attribute printer.



18
19
20
# File 'lib/tqdm/decorator.rb', line 18

def printer
  @printer
end

#start_timeObject (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

#enhanceEnumerable

Note:

The Enumerable is cloned (shallow copied) before it is enhanced; it is not modified directly.

Enhances the wrapped Enumerable.

Returns:

  • (Enumerable)

    a clone of Enumerable enhanced so that every call to #each animates the progress bar.



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.

See Also:



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