Class: Rmega::Progress
- Inherits:
-
Object
- Object
- Rmega::Progress
- Includes:
- Options
- Defined in:
- lib/rmega/progress.rb
Class Method Summary collapse
Instance Method Summary collapse
- #columns ⇒ Object
- #elapsed_time ⇒ Object
- #ended? ⇒ Boolean
- #humanize_bytes(*args) ⇒ Object
- #increment(bytes, options = {}) ⇒ Object
-
#initialize(total, options = {}) ⇒ Progress
constructor
A new instance of Progress.
- #percentage ⇒ Object
- #print_r(message) ⇒ Object
- #show ⇒ Object
- #show? ⇒ Boolean
- #speed ⇒ Object
- #stty_size_columns ⇒ Object
Methods included from Options
Constructor Details
#initialize(total, options = {}) ⇒ Progress
Returns a new instance of Progress.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rmega/progress.rb', line 5 def initialize(total, = {}) @total = total @caption = [:caption] @bytes = 0 @real_bytes = 0 @mutex = Mutex.new @start_time = Time.now if show? and [:filename] puts [:filename] end show end |
Class Method Details
.humanize_bytes(bytes, round = 2) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/rmega/progress.rb', line 85 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 |
Instance Method Details
#columns ⇒ Object
44 45 46 |
# File 'lib/rmega/progress.rb', line 44 def columns stty_size_columns || 80 end |
#elapsed_time ⇒ Object
65 66 67 |
# File 'lib/rmega/progress.rb', line 65 def elapsed_time (Time.now - @start_time).round(2) end |
#ended? ⇒ Boolean
69 70 71 |
# File 'lib/rmega/progress.rb', line 69 def ended? @total == @bytes end |
#humanize_bytes(*args) ⇒ Object
81 82 83 |
# File 'lib/rmega/progress.rb', line 81 def humanize_bytes(*args) self.class.humanize_bytes(*args) end |
#increment(bytes, options = {}) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/rmega/progress.rb', line 73 def increment(bytes, = {}) @mutex.synchronize do @bytes += bytes @real_bytes += bytes unless [:real] == false show end end |
#percentage ⇒ Object
57 58 59 |
# File 'lib/rmega/progress.rb', line 57 def percentage (100.0 * @bytes / @total).round(2) end |
#print_r(message) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/rmega/progress.rb', line 48 def print_r() if .size + 10 > columns puts else blank_line = ' ' * (.size + 10) print "\r#{blank_line}\r#{}" end end |
#show ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rmega/progress.rb', line 24 def show return unless show? = @caption ? "[#{@caption}] " : "" << "#{humanize_bytes(@bytes)} of #{humanize_bytes(@total)}" if ended? << ". Completed in #{elapsed_time} sec.\n" else << ", #{percentage}% @ #{humanize_bytes(speed, 1)}/s, #{.thread_pool_size} threads" end print_r() end |
#show? ⇒ Boolean
20 21 22 |
# File 'lib/rmega/progress.rb', line 20 def show? .show_progress end |
#speed ⇒ Object
61 62 63 |
# File 'lib/rmega/progress.rb', line 61 def speed @real_bytes.to_f / (Time.now - @start_time).to_f end |
#stty_size_columns ⇒ Object
39 40 41 42 |
# 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 |