progress-meter 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +50 -0
- data/lib/progress-meter.rb +137 -0
- data/samples/two-loops.rb +14 -0
- metadata +48 -0
data/README
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
= Progress Meter
|
2
|
+
|
3
|
+
This program allows monitoring of loop. Only Array.each and Hash.each are
|
4
|
+
supported right now, each type of loop must be supported separately.
|
5
|
+
|
6
|
+
To use it, just require 'progress-meter'. This will hook all supported loop
|
7
|
+
functions with the monitoring code. To actually activate the monitoring call
|
8
|
+
Progress.monitor, this will activate monitoring of the next loop that is of a
|
9
|
+
_supported_ type.
|
10
|
+
|
11
|
+
See samples/ directory for an example.
|
12
|
+
|
13
|
+
== CAVEATS
|
14
|
+
|
15
|
+
Progress meters are printed in successive lines above the current one.
|
16
|
+
So they might erase what was written before. The cursor does not really
|
17
|
+
return to the original position after printing the reports, just to the
|
18
|
+
original line.
|
19
|
+
|
20
|
+
|
21
|
+
== BUGS
|
22
|
+
|
23
|
+
Please report bugs to mikisvaz at yahoo com.
|
24
|
+
|
25
|
+
== LICENSE
|
26
|
+
|
27
|
+
|
28
|
+
(The MIT License)
|
29
|
+
|
30
|
+
Copyright (c) 2007 Miguel Vazquez Garcia
|
31
|
+
|
32
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
33
|
+
a copy of this software and associated documentation files (the
|
34
|
+
'Software'), to deal in the Software without restriction, including
|
35
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
36
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
37
|
+
permit persons to whom the Software is furnished to do so, subject to
|
38
|
+
the following conditions:
|
39
|
+
|
40
|
+
The above copyright notice and this permission notice shall be
|
41
|
+
included in all copies or substantial portions of the Software.
|
42
|
+
|
43
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
44
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
45
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
46
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
47
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
48
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
49
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
50
|
+
|
@@ -0,0 +1,137 @@
|
|
1
|
+
# Allow the possibility of monitoring the progress of some loop.
|
2
|
+
# Currently only Array.each and Hash.each are supported, but this should
|
3
|
+
# be easy to extend.
|
4
|
+
#
|
5
|
+
# Author:: Miguel Vazquez Garcia
|
6
|
+
# License:: MIT
|
7
|
+
#
|
8
|
+
|
9
|
+
# Tracks the progress of a loop. It holds information about how many
|
10
|
+
# iterations it has to go through and how many have been executed
|
11
|
+
# already. Every now and then, it prints the progress report.
|
12
|
+
class Progress
|
13
|
+
|
14
|
+
@@monitor = false
|
15
|
+
@@desc = ""
|
16
|
+
|
17
|
+
# This function will activate monitoring of the next _supported_ loop.
|
18
|
+
#
|
19
|
+
# If a description is given as a parameter it will show at the
|
20
|
+
# beginning of the progress report.
|
21
|
+
def Progress.monitor(desc = "")
|
22
|
+
@@monitor = true
|
23
|
+
@@desc = desc
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns true if next loop must be monitored.
|
27
|
+
#
|
28
|
+
def Progress.active?
|
29
|
+
@@monitor
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
# Creates a new instance. Max is the total number of iterations of the
|
34
|
+
# loop. The depth represents how many other loops are above this one,
|
35
|
+
# this information is used to find the place to print the progress
|
36
|
+
# report.
|
37
|
+
#
|
38
|
+
def initialize(max,depth)
|
39
|
+
@max = max
|
40
|
+
@current = 1
|
41
|
+
@time = Time.now
|
42
|
+
@last_report = 0
|
43
|
+
@num_reports = 100
|
44
|
+
@depth = depth
|
45
|
+
@desc = @@desc
|
46
|
+
@@monitor = false
|
47
|
+
report
|
48
|
+
end
|
49
|
+
|
50
|
+
# Used to register a new completed loop iteration.
|
51
|
+
#
|
52
|
+
def tick
|
53
|
+
@current += 1
|
54
|
+
percent = @current.to_f/ @max.to_f
|
55
|
+
if percent - @last_report > 1.to_f/@num_reports.to_f then
|
56
|
+
report
|
57
|
+
@last_report=percent
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Prints de progress report. It backs up as many lines as the meters
|
62
|
+
# depth. Prints the progress as a line of dots, a percentage, time
|
63
|
+
# spent, and time left. And then goes moves the cursor back to its
|
64
|
+
# original line. Everything is printed to stderr.
|
65
|
+
#
|
66
|
+
def report
|
67
|
+
|
68
|
+
percent = @current.to_f/ @max.to_f
|
69
|
+
percent = 0.000001 if percent == 0
|
70
|
+
if @desc != ""
|
71
|
+
indicator = @desc + ": "
|
72
|
+
else
|
73
|
+
indicator = "Progress "
|
74
|
+
end
|
75
|
+
indicator += "["
|
76
|
+
10.times{|i|
|
77
|
+
if i < percent * 10 then
|
78
|
+
indicator += "."
|
79
|
+
else
|
80
|
+
indicator += " "
|
81
|
+
end
|
82
|
+
}
|
83
|
+
indicator += "] done #{(percent * 100).to_i}% "
|
84
|
+
|
85
|
+
eta = (Time.now - @time)/percent * (1-percent)
|
86
|
+
eta = eta.to_i
|
87
|
+
eta = [eta/3600, eta/60 % 60, eta % 60].map{|t| t.to_s.rjust(2, '0')}.join(':')
|
88
|
+
|
89
|
+
used = (Time.now - @time).to_i
|
90
|
+
used = [used/3600, used/60 % 60, used % 60].map{|t| t.to_s.rjust(2, '0')}.join(':')
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
indicator += " (Time left #{eta} seconds) (Started #{used} seconds ago)"
|
95
|
+
|
96
|
+
$stderr.print("\033[#{@depth + 1}F\033[2K" + indicator + "\033[#{@depth + 1}E" )
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
class Array
|
106
|
+
@@progress_meters = Array.new
|
107
|
+
alias :orig_each :each
|
108
|
+
|
109
|
+
def each (&block)
|
110
|
+
if Progress.active? then
|
111
|
+
@@progress_meters.push(Progress.new(self.length, @@progress_meters.size ))
|
112
|
+
orig_each {|w|block.call(w);@@progress_meters.last.tick;}
|
113
|
+
@@progress_meters.pop
|
114
|
+
else
|
115
|
+
orig_each &block
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
class Hash
|
124
|
+
@@progress_meters = Array.new
|
125
|
+
alias :orig_each :each
|
126
|
+
def each (&block)
|
127
|
+
if Progress.active? then
|
128
|
+
$stderr.print "\n"
|
129
|
+
@@progress_meters.push(Progress.new(self.length, @@progress_meters.size ))
|
130
|
+
orig_each {|k,v|block.call(k,v);@@progress_meters.last.tick;}
|
131
|
+
@@progress_meters.pop
|
132
|
+
else
|
133
|
+
orig_each &block
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: progress-meter
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2007-12-05 00:00:00 +01:00
|
8
|
+
summary: Allows to monitor the progress of some loop. Currently only Array.each and Hash.each
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: mikisvaz@yahoo.com
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Miguel Vazquez
|
31
|
+
files:
|
32
|
+
- lib/progress-meter.rb
|
33
|
+
- samples/two-loops.rb
|
34
|
+
- README
|
35
|
+
test_files: []
|
36
|
+
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- README
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
dependencies: []
|
48
|
+
|