class Tqdm::Printer::DefaultFormat
Constants
- PROGRESS
- PROGRESS_BAR_WIDTH
- SPACE
Attributes
Public Class Methods
Initialize a new DefaultFormat
.
@param options [Hash] the options for the Tqdm::Decorator
@see Tqdm::Decorator#initialize
# File lib/tqdm/printer/default_format.rb, line 13 def initialize(options) @total = options[:total] @prefix = options[:desc] ? options[:desc] + ': ' : '' end
Public Instance Methods
Formats the prefix, progress bar and meter as a complete line to be printed
@param iteration [Integer] number of finished iterations @param elapsed_time [Float] total number of seconds passed since start @return [String] the complete line to print
@see meter
# File lib/tqdm/printer/default_format.rb, line 25 def line(iteration, elapsed_time) prefix + meter(iteration, total, elapsed_time) end
Formats a count (n) of total items processed + an elapsed time into a textual progress bar + meter.
@param n [Integer] number of finished iterations @param total [Integer, nil] total number of iterations, or nil @param elapsed [Float] number of seconds passed since start @return [String] a textual progress bar + meter
# 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
Private Instance Methods
Formats a number of seconds into an hh:mm:ss string.
@param seconds [Integer] a number of seconds @return [String] an hh:mm:ss string
# File lib/tqdm/printer/default_format.rb, line 67 def interval(seconds) m, s = seconds.to_i.divmod(60) h, m = m.divmod(60) if h > 0 then '%d:%02d:%02d' % [h, m, s]; else '%02d:%02d' % [m, s]; end end