Sha256: 94aa1319217bb6b8327a35d2bd9f409557334a9bc121e83e330b0fb78472b7cf

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require "tqdm"

module Tqdm
  
  # Utility functions related to `Tqdm`.
  module Utils
  
    # Formats a number of seconds into an hh:mm:ss string.
    #
    # @param t [Integer] a number of seconds
    # @return [String] an hh:mm:ss string
    def format_interval(t)
      mins, s = t.to_i.divmod(60)
      h, m = mins.divmod(60)
      if h > 0 then '%d:%02d:%02d' % [h, m, s]; else '%02d:%02d' % [m, s]; end
    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 [Integer] number of seconds passed since start
    # @return [String] a textual progress bar + meter
    def format_meter(n, total, elapsed)
      total = (n > total ? nil : total) if total
  
      elapsed_str = format_interval(elapsed)
      rate = elapsed && elapsed > 0 ? ('%5.2f' % (n / elapsed)) : '?'
  
      if total
        frac = n.to_f / total
    
        bar_length = (frac * N_BARS).to_i
        bar = '#' * bar_length + '-' * (N_BARS - bar_length)
    
        percentage = '%3d%%' % (frac * 100)
    
        left_str = n > 0 ? (format_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
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tqdm-0.1.0 lib/tqdm/utils.rb