Class: Rmega::Progress

Inherits:
Object
  • Object
show all
Includes:
Options
Defined in:
lib/rmega/progress.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Options

included, #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, 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

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

#columnsObject



44
45
46
# File 'lib/rmega/progress.rb', line 44

def columns
  stty_size_columns || 80
end

#elapsed_timeObject



65
66
67
# File 'lib/rmega/progress.rb', line 65

def elapsed_time
  (Time.now - @start_time).round(2)
end

#ended?Boolean

Returns:

  • (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, options = {})
  @mutex.synchronize do
    @bytes += bytes
    @real_bytes += bytes unless options[:real] == false
    show
  end
end

#percentageObject



57
58
59
# File 'lib/rmega/progress.rb', line 57

def percentage
  (100.0 * @bytes / @total).round(2)
end


48
49
50
51
52
53
54
55
# 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

#showObject



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?

  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?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/rmega/progress.rb', line 20

def show?
  options.show_progress
end

#speedObject



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_columnsObject



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