Class: Tqdm::Printer::DefaultFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/tqdm/printer/default_format.rb

Constant Summary collapse

PROGRESS_BAR_WIDTH =
10
SPACE =
'-'
PROGRESS =
'#'

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DefaultFormat

Initialize a new DefaultFormat.

Parameters:

  • options (Hash)

    the options for the Tqdm::Decorator

See Also:



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

def initialize(options)
  @total = options[:total]
  @prefix = options[:desc] ? options[:desc] + ': ' : ''
end

Instance Method Details

#line(iteration, elapsed_time) ⇒ String

Formats the prefix, progress bar and meter as a complete line to be printed

Parameters:

  • iteration (Integer)

    number of finished iterations

  • elapsed_time (Float)

    total number of seconds passed since start

Returns:

  • (String)

    the complete line to print

See Also:



25
26
27
# File 'lib/tqdm/printer/default_format.rb', line 25

def line(iteration, elapsed_time)
  prefix + meter(iteration, total, elapsed_time)
end

#meter(n, total, elapsed) ⇒ String

Formats a count (n) of total items processed + an elapsed time into a textual progress bar + meter.

Parameters:

  • n (Integer)

    number of finished iterations

  • total (Integer, nil)

    total number of iterations, or nil

  • elapsed (Float)

    number of seconds passed since start

Returns:

  • (String)

    a textual progress bar + meter



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tqdm/printer/default_format.rb', line 36

def meter(n, total, elapsed)
  total = (n > total ? nil : total) if total

  elapsed_str = interval(elapsed)
  rate = elapsed && elapsed > 0 ? ('%5.2f' % (n / elapsed)) : '?'

  if total && total > 0
    frac = n.to_f / total

    bar_length = (frac * PROGRESS_BAR_WIDTH).to_i
    bar = PROGRESS * bar_length + SPACE * (PROGRESS_BAR_WIDTH - bar_length)

    percentage = '%3d%%' % (frac * 100)

    left_str = n > 0 ? (interval(elapsed / n * (total - n))) : '?'

    '|%s| %d/%d %s [elapsed: %s left: %s, %s iters/sec]' % [bar, n, total,
        percentage, elapsed_str, left_str, rate]
  else
    '%d [elapsed: %s, %s iters/sec]' % [n, elapsed_str, rate]
  end
end