Module: Progress

Defined in:
lib/progress-monitor.rb,
lib/progress-bar.rb

Overview

Tracks the progress of a loop. It holds information about how many iterations it has to go through and how many have been executed already. Every now and then, it prints the progress report.

Defined Under Namespace

Modules: MonitorableProgress Classes: Bar

Constant Summary collapse

@@progress_meters =
Array.new
@@monitor =
false
@@announce =
false

Class Method Summary collapse

Class Method Details

.active?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/progress-monitor.rb', line 159

def self.active?
  return (monitor? || announce?) && this_loop?
end

.add_progress_meter(max) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/progress-monitor.rb', line 163

def self.add_progress_meter(max)
  progress_meter = Bar.new(max, progress_meters.size, @@num_reports, @@desc)
  @@monitor = false

  progress_meters.push(progress_meter)

  progress_meter
end

.announce(announcement, options = {}) ⇒ Object



123
124
125
126
127
128
# File 'lib/progress-monitor.rb', line 123

def self.announce(announcement, options = {})
  @@announce     = true
  @@call_info   = caller_info(caller)
  @@announcement = announcement
  process_options(options)
end

.announce?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/progress-monitor.rb', line 155

def self.announce?
  return @@announce
end

.caller_info(callers, depth = 0) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/progress-monitor.rb', line 83

def self.caller_info(callers, depth = 0)
  return [nil, nil] if callers.length <= depth

  line = callers[depth]
  if line.match(/(.*):\d+(?::in `(.*)')/)
    return [$1, $2]
  end

  if line.match(/(.*):\d+/)
    return [$1, nil ]
  end

  info
end

.get_announcementObject



130
131
132
133
# File 'lib/progress-monitor.rb', line 130

def self.get_announcement
  @@announce    = false
  @@announcement
end

.monitor(desc = "", options = {}) ⇒ Object

This function will activate monitoring of the next supported loop.

If a description is given as a parameter it will show at the beginning of the progress report.



115
116
117
118
119
120
121
# File 'lib/progress-monitor.rb', line 115

def self.monitor(desc = "", options = {})
  @@monitor     = true
  @@desc        = desc
  @@num_reports = options[:num_reports] || 100
  @@call_info   = caller_info(caller)
  process_options(options)
end

.monitor?Boolean

Returns true if next loop must be monitored.

Returns:

  • (Boolean)


151
152
153
# File 'lib/progress-monitor.rb', line 151

def self.monitor?
  return @@monitor
end


176
177
178
179
180
# File 'lib/progress-monitor.rb', line 176

def self.print_announcement(message = nil)
  return if message.nil?
  total_depth = @@progress_meters.length + 1
  $stderr.print("\033[#{total_depth}F\033[2K" + message + "\033[#{total_depth}E")
end

.process_options(options) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/progress-monitor.rb', line 101

def self.process_options(options)
  @@stack_depth = options[:stack_depth] 
  @@skip        = options[:skip] || 0

  if options[:announcement]
    @@announce     = true
    @@announcement = options[:announcement]
  end
end

.progress_metersObject



79
80
81
# File 'lib/progress-monitor.rb', line 79

def self.progress_meters
  @@progress_meters
end

.remove_last_meterObject



172
173
174
# File 'lib/progress-monitor.rb', line 172

def self.remove_last_meter
  progress_meters.pop
end

.this_loop?Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/progress-monitor.rb', line 135

def self.this_loop?
  if @@stack_depth != nil
    call_info = caller_info(caller, @@stack_depth + 2)
    return false if call_info != @@call_info
  end

  if @@skip > 0
    @@skip -= 1
    return false
  else
    return true
  end
end