class Rmega::Progress
Public Class Methods
humanize_bytes(bytes, round = 2)
click to toggle source
# File lib/rmega/progress.rb, line 86 def self.humanize_bytes(bytes, round = 2) units = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB'] e = (bytes == 0 ? 0 : Math.log(bytes)) / Math.log(1024) value = bytes.to_f / (1024 ** e.floor) return "#{value.round(round)} #{units[e]}" end
new(total, options = {})
click to toggle source
# File lib/rmega/progress.rb, line 5 def initialize(total, options = {}) @total = total @caption = options[:caption] @bytes = 0 @real_bytes = 0 @mutex = Mutex.new @start_time = Time.now if show? and options[:filename] puts options[:filename] end show end
Public Instance Methods
columns()
click to toggle source
# File lib/rmega/progress.rb, line 44 def columns stty_size_columns || 80 end
elapsed_time()
click to toggle source
# File lib/rmega/progress.rb, line 65 def elapsed_time (Time.now - @start_time).round(2) end
ended?()
click to toggle source
# File lib/rmega/progress.rb, line 69 def ended? @total == @bytes end
humanize_bytes(*args)
click to toggle source
# File lib/rmega/progress.rb, line 82 def humanize_bytes(*args) self.class.humanize_bytes(*args) end
increment(bytes, options = {})
click to toggle source
# File lib/rmega/progress.rb, line 73 def increment(bytes, options = {}) @mutex.synchronize do @caption = options[:caption] if options[:caption] @bytes += bytes @real_bytes += bytes unless options[:real] == false show end end
percentage()
click to toggle source
# File lib/rmega/progress.rb, line 57 def percentage (100.0 * @bytes / @total).round(2) end
print_r(message)
click to toggle source
# File lib/rmega/progress.rb, line 48 def print_r(message) if message.size + 10 > columns puts message else blank_line = ' ' * (message.size + 10) print "\r#{blank_line}\r#{message}" end end
show()
click to toggle source
# File lib/rmega/progress.rb, line 24 def show return unless show? message = @caption ? "[#{@caption}] " : "" message << "#{humanize_bytes(@bytes)} of #{humanize_bytes(@total)}" if ended? message << ". Completed in #{elapsed_time} sec.\n" else message << ", #{percentage}% @ #{humanize_bytes(speed, 1)}/s, #{options.thread_pool_size} threads" end print_r(message) end
show?()
click to toggle source
# File lib/rmega/progress.rb, line 20 def show? options.show_progress end
speed()
click to toggle source
# File lib/rmega/progress.rb, line 61 def speed @real_bytes.to_f / (Time.now - @start_time).to_f end
stty_size_columns()
click to toggle source
# File lib/rmega/progress.rb, line 39 def stty_size_columns return @stty_size_columns unless @stty_size_columns.nil? @stty_size_columns ||= (`stty size`.split[1].to_i rescue false) end