Module: Trove::Utils
- Defined in:
- lib/trove/utils.rb
Class Method Summary collapse
-
.human_size(size) ⇒ Object
TODO improve performance.
- .progress(stream, filename, current_size, total_size) ⇒ Object
Class Method Details
.human_size(size) ⇒ Object
TODO improve performance
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/trove/utils.rb', line 4 def self.human_size(size) if size < 2**10 units = "B" elsif size < 2**20 size /= (2**10).to_f units = "KB" elsif size < 2**30 size /= (2**20).to_f units = "MB" else size /= (2**30).to_f units = "GB" end round = size < 9.95 ? 1 : 0 "#{size.round(round)}#{units}" end |
.progress(stream, filename, current_size, total_size) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/trove/utils.rb', line 22 def self.progress(stream, filename, current_size, total_size) return unless stream.tty? width = 50 progress = (100.0 * current_size / total_size).floor completed = (width / 100.0 * progress).round remaining = width - completed stream.print "\r#{filename} [#{"=" * completed}#{" " * remaining}] %3s%% %11s " % [progress, "#{Utils.human_size(current_size)}/#{Utils.human_size(total_size)}"] stream.print "\n" if current_size == total_size end |