Class: Progress
- Inherits:
-
Object
- Object
- Progress
- Defined in:
- lib/progress-meter.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.
Constant Summary collapse
- @@progress_meters =
Array.new
- @@monitor =
false- @@desc =
""
Class Method Summary collapse
-
.active? ⇒ Boolean
Returns true if next loop must be monitored.
-
.monitor(desc = "", num_reports = 100) ⇒ Object
This function will activate monitoring of the next supported loop.
- .progress_meters ⇒ Object
Instance Method Summary collapse
-
#initialize(max, depth) ⇒ Progress
constructor
Creates a new instance.
-
#report ⇒ Object
Prints de progress report.
- #set(step) ⇒ Object
-
#tick(steps = 1) ⇒ Object
Used to register a new completed loop iteration.
Constructor Details
#initialize(max, depth) ⇒ Progress
Creates a new instance. Max is the total number of iterations of the loop. The depth represents how many other loops are above this one, this information is used to find the place to print the progress report.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/progress-meter.rb', line 44 def initialize(max,depth) @max = max @max = 1 if @max < 1 @current = 0 @time = Time.now @last_report = 0 @num_reports = @@num_reports @depth = depth @desc = @@desc @@monitor = false report end |
Class Method Details
.active? ⇒ Boolean
Returns true if next loop must be monitored.
34 35 36 |
# File 'lib/progress-meter.rb', line 34 def Progress.active? @@monitor end |
.monitor(desc = "", num_reports = 100) ⇒ 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.
26 27 28 29 30 |
# File 'lib/progress-meter.rb', line 26 def Progress.monitor(desc = "", num_reports = 100) @@monitor = true @@desc = desc @@num_reports=num_reports end |
.progress_meters ⇒ Object
15 16 17 |
# File 'lib/progress-meter.rb', line 15 def self.progress_meters @@progress_meters end |
Instance Method Details
#report ⇒ Object
Prints de progress report. It backs up as many lines as the meters depth. Prints the progress as a line of dots, a percentage, time spent, and time left. And then goes moves the cursor back to its original line. Everything is printed to stderr.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/progress-meter.rb', line 83 def report percent = @current.to_f/ @max.to_f percent = 0.001 if percent < 0.001 if @desc != "" indicator = @desc + ": " else indicator = "Progress " end indicator += "[" 10.times{|i| if i < percent * 10 then indicator += "." else indicator += " " end } indicator += "] done #{(percent * 100).to_i}% " eta = (Time.now - @time)/percent * (1-percent) eta = eta.to_i eta = [eta/3600, eta/60 % 60, eta % 60].map{|t| t.to_s.rjust(2, '0')}.join(':') used = (Time.now - @time).to_i used = [used/3600, used/60 % 60, used % 60].map{|t| t.to_s.rjust(2, '0')}.join(':') indicator += " (Time left #{eta} seconds) (Started #{used} seconds ago)" $stderr.print("\033[#{@depth + 1}F\033[2K" + indicator + "\033[#{@depth + 1}E" ) end |
#set(step) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/progress-meter.rb', line 68 def set(step) @current = step percent = @current.to_f/ @max.to_f if percent - @last_report > 1.to_f/@num_reports.to_f then report @last_report=percent end end |
#tick(steps = 1) ⇒ Object
Used to register a new completed loop iteration.
59 60 61 62 63 64 65 66 |
# File 'lib/progress-meter.rb', line 59 def tick(steps = 1) @current += steps percent = @current.to_f/ @max.to_f if percent - @last_report > 1.to_f/@num_reports.to_f then report @last_report=percent end end |