Class: Tqdm::Printer

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

Overview

Prints a status line, handling the deletion of previously printed lines with carriage returns as necessary. Instantiated by a Decorator.

Defined Under Namespace

Classes: DefaultFormat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Printer

Initialize a new Printer.

Parameters:

  • options (Hash)

    the options for the instantiating Tqdm::Decorator

See Also:



20
21
22
23
24
25
# File 'lib/tqdm/printer.rb', line 20

def initialize(options)
  @total = options[:total]
  @format = Printer::DefaultFormat.new(options)
  @file = options[:file] || $stderr
  @last_printed_length = 0
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/tqdm/printer.rb', line 13

def file
  @file
end

#formatObject (readonly)

Returns the value of attribute format.



13
14
15
# File 'lib/tqdm/printer.rb', line 13

def format
  @format
end

#totalObject (readonly)

Returns the value of attribute total.



13
14
15
# File 'lib/tqdm/printer.rb', line 13

def total
  @total
end

Instance Method Details

#finish(iteration, elapsed_time, reprint) ⇒ Object

Prints a line of text to @file, after deleting the previously printed line

Parameters:

  • iteration (Integer)

    number of iterations, out of the total, that are completed

  • elapsed_time (Float)

    number of seconds passed since start

  • reprint (Boolean)

    do we need to reprint the line one last time?



53
54
55
56
57
# File 'lib/tqdm/printer.rb', line 53

def finish(iteration, elapsed_time, reprint)
  file.write("\r" + padded_line(iteration, elapsed_time)) if reprint
  file.write("\n")
  file.flush
end

#null_finishObject

Disappear without a trace.



60
61
62
# File 'lib/tqdm/printer.rb', line 60

def null_finish
  file.write("\r" + ' ' * @last_printed_length + "\r")
end

#padded_line(iteration, elapsed_time) ⇒ String

Pads a status line so that it is long enough to overwrite the previously written line

Parameters:

  • iteration (Integer)

    number of iterations, out of the total, that are completed

  • elapsed_time (Float)

    number of seconds passed since start

Returns:

  • (String)

    the padded line



32
33
34
35
36
37
# File 'lib/tqdm/printer.rb', line 32

def padded_line(iteration, elapsed_time)
  meter_line = line(iteration, elapsed_time)
  pad_size = [@last_printed_length - meter_line.size, 0].max
  @last_printed_length = meter_line.size
  meter_line + ' ' * pad_size
end

#status(iteration, elapsed_time) ⇒ Object

Prints a line of text to @file, after deleting the previously printed line

Parameters:

  • iteration (Integer)

    number of iterations, out of the total, that are completed

  • elapsed_time (Float)

    number of seconds passed since start



43
44
45
46
# File 'lib/tqdm/printer.rb', line 43

def status(iteration, elapsed_time)
  file.write("\r" + padded_line(iteration, elapsed_time))
  file.flush
end