progress 0.1.0.3 → 0.1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -51,6 +51,49 @@ Class to show progress during console script run
51
51
  end
52
52
  end
53
53
 
54
+ or just
55
+
56
+ Progress('Input 100 symbols', 100) do
57
+ while symbols.length < 100
58
+ input = gets.scan(/\S/)
59
+ symbols += input
60
+ Progress.step input.length
61
+ end
62
+ end
63
+
64
+ Note - you will get WRONG progress if you use something like this:
65
+
66
+ 10.times_with_progress('A') do |time|
67
+ 10.times_with_progress('B'){ … }
68
+ 10.times_with_progress('C'){ … }
69
+ end
70
+
71
+ But you can use this:
72
+
73
+ 10.times_with_progress('A') do |time|
74
+ Progress('B,C', 2) do
75
+ Progress.step do
76
+ 10.times_with_progress('B'){ … }
77
+ end
78
+ Progress.step do
79
+ 10.times_with_progress('C'){ … }
80
+ end
81
+ end
82
+ end
83
+
84
+ Or if you know that B runs 10 times faster than C:
85
+
86
+ 10.times_with_progress('A') do |time|
87
+ Progress('B,C', 11) do
88
+ Progress.step 1 do
89
+ 10.times_with_progress('B'){ … }
90
+ end
91
+ Progress.step 10 do
92
+ 10.times_with_progress('C'){ … }
93
+ end
94
+ end
95
+ end
96
+
54
97
  == REQUIREMENTS:
55
98
 
56
99
  * ruby )))
data/VERSION.yml CHANGED
@@ -1 +1 @@
1
- [0, 1, 0, 3]
1
+ [0, 1, 1, 0]
data/lib/progress.rb CHANGED
@@ -8,6 +8,7 @@ class Progress
8
8
 
9
9
  module InstanceMethods # :nodoc:
10
10
  attr_accessor :title, :current, :total
11
+ attr_reader :current_step
11
12
  def initialize(title, total)
12
13
  total = Float(total)
13
14
  @title, @current, @total = title, 0.0, total == 0.0 ? 1.0 : total
@@ -18,7 +19,16 @@ class Progress
18
19
  end
19
20
 
20
21
  def to_f(inner)
21
- (current + (inner < 1.0 ? inner : 1.0)) / total
22
+ inner = [inner, 1.0].min
23
+ inner *= current_step if current_step
24
+ (current + inner) / total
25
+ end
26
+
27
+ def step(steps)
28
+ @current_step = steps
29
+ yield
30
+ ensure
31
+ @current_step = nil
22
32
  end
23
33
  end
24
34
  include InstanceMethods
@@ -56,16 +66,25 @@ class Progress
56
66
  levels << new(title, total)
57
67
  print_message
58
68
  if block_given?
59
- result = yield
60
- stop
61
- result
69
+ begin
70
+ yield
71
+ ensure
72
+ stop
73
+ end
62
74
  end
63
75
  end
64
76
 
65
77
  def step(steps = 1)
66
78
  if levels.last
79
+ if block_given?
80
+ levels.last.step(steps) do
81
+ yield
82
+ end
83
+ end
67
84
  levels.last.current += Float(steps)
68
85
  print_message
86
+ elsif block_given?
87
+ yield
69
88
  end
70
89
  end
71
90
 
@@ -137,3 +156,11 @@ require 'progress/with_progress'
137
156
 
138
157
  require 'progress/enumerable'
139
158
  require 'progress/integer'
159
+
160
+ # like Pathname
161
+ module Kernel
162
+ def Progress(title, total = 1, &block)
163
+ Progress.start(title, total, &block)
164
+ end
165
+ private :Progress
166
+ end
data/progress.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{progress}
5
- s.version = "0.1.0.3"
5
+ s.version = "0.1.1.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["toy"]
@@ -140,31 +140,6 @@ describe Progress do
140
140
  end
141
141
  end
142
142
 
143
- it "should allow enclosed progress" do
144
- 10.times_with_progress('A') do |a|
145
- io_pop.should =~ /#{Regexp.quote(a == 0 ? '......' : (a * 10.0).to_s)}/
146
- 10.times_with_progress('B') do |b|
147
- io_pop.should =~ /#{Regexp.quote(a == 0 && b == 0 ? '......' : (a * 10.0 + b).to_s)}.*#{Regexp.quote(b == 0 ? '......' : (b * 10.0).to_s)}/
148
- end
149
- io_pop.should =~ /#{Regexp.quote(((a + 1) * 10.0).to_s)}.*100\.0/
150
- end
151
- io_pop.should =~ /100\.0.*\n$/
152
- end
153
-
154
- it "should not overlap outer progress if inner exceeds" do
155
- 10.times_with_progress('A') do |a|
156
- io_pop.should =~ /#{Regexp.quote(a == 0 ? '......' : (a * 10.0).to_s)}/
157
- Progress.start('B', 10) do
158
- 20.times do |b|
159
- io_pop.should =~ /#{Regexp.quote(a == 0 && b == 0 ? '......' : (a * 10.0 + [b, 10].min).to_s)}.*#{Regexp.quote(b == 0 ? '......' : (b * 10.0).to_s)}/
160
- Progress.step
161
- end
162
- end
163
- io_pop.should =~ /#{Regexp.quote(((a + 1) * 10.0).to_s)}.*200\.0/
164
- end
165
- io_pop.should =~ /100\.0.*\n$/
166
- end
167
-
168
143
  it "should pipe result from block" do
169
144
  Progress.start('Test') do
170
145
  'qwerty'
@@ -192,4 +167,86 @@ describe Progress do
192
167
  end
193
168
  end.should == [[1, 2, 3], [2, 4, 6], [3, 6, 9]]
194
169
  end
170
+
171
+ it "should kill progress on cycle break" do
172
+ 2.times do
173
+ catch(:lalala) do
174
+ 2.times_with_progress('A') do |a|
175
+ io_pop.should == "A: ......\n"
176
+ 2.times_with_progress('B') do |b|
177
+ io_pop.should == "A: ...... > B: ......\n"
178
+ throw(:lalala)
179
+ end
180
+ end
181
+ end
182
+ io_pop.should == "\n"
183
+ end
184
+ end
185
+
186
+ [[2, 2000], [20, 200], [200, 20], [2000, 2]].each do |_a, _b|
187
+ it "should allow enclosed progress [#{_a}, #{_b}]" do
188
+ _a.times_with_progress('A') do |a|
189
+ io_pop.should == "A: #{a == 0 ? '......' : '%5.1f%%'}\n" % [a / _a.to_f * 100.0]
190
+ _b.times_with_progress('B') do |b|
191
+ io_pop.should == "A: #{a == 0 && b == 0 ? '......' : '%5.1f%%'} > B: #{b == 0 ? '......' : '%5.1f%%'}\n" % [(a + b / _b.to_f) / _a.to_f * 100.0, b / _b.to_f * 100.0]
192
+ end
193
+ io_pop.should == "A: %5.1f%% > B: 100.0%%\n" % [(a + 1) / _a.to_f * 100.0]
194
+ end
195
+ io_pop.should == "A: 100.0%\n\n"
196
+ end
197
+
198
+ it "should not overlap outer progress if inner exceeds [#{_a}, #{_b}]" do
199
+ _a.times_with_progress('A') do |a|
200
+ io_pop.should == "A: #{a == 0 ? '......' : '%5.1f%%'}\n" % [a / _a.to_f * 100.0]
201
+ Progress.start('B', _b) do
202
+ (_b * 2).times do |b|
203
+ io_pop.should == "A: #{a == 0 && b == 0 ? '......' : '%5.1f%%'} > B: #{b == 0 ? '......' : '%5.1f%%'}\n" % [(a + [b / _b.to_f, 1].min) / _a.to_f * 100.0, b / _b.to_f * 100.0]
204
+ Progress.step
205
+ end
206
+ end
207
+ io_pop.should == "A: %5.1f%% > B: 200.0%%\n" % [(a + 1) / _a.to_f * 100.0]
208
+ end
209
+ io_pop.should == "A: 100.0%\n\n"
210
+ end
211
+
212
+ it "should allow step with block to validly count custom progresses [#{_a}, #{_b}]" do
213
+ a_step = 99
214
+ Progress.start('A', _a * 100) do
215
+ io_pop.should == "A: ......\n"
216
+ _a.times do |a|
217
+ Progress.step(a_step) do
218
+ _b.times_with_progress('B') do |b|
219
+ io_pop.should == "A: #{a == 0 && b == 0 ? '......' : '%5.1f%%'} > B: #{b == 0 ? '......' : '%5.1f%%'}\n" % [(a * a_step + b / _b.to_f * a_step) / (_a * 100).to_f * 100.0, b / _b.to_f * 100.0]
220
+ end
221
+ io_pop.should == "A: %5.1f%% > B: 100.0%\n" % [(a + 1) * a_step.to_f / (100.0 * _a.to_f) * 100.0]
222
+ end
223
+ io_pop.should == "A: %5.1f%%\n" % [(a + 1) * a_step.to_f / (100.0 * _a.to_f) * 100.0]
224
+ end
225
+ Progress.step _a
226
+ end
227
+ io_pop.should == "A: 100.0%\n\n"
228
+ end
229
+ end
230
+
231
+ describe "using Progress instead of Progress.start" do
232
+ it "should show valid output for procedural version" do
233
+ Progress('Test', 1000)
234
+ 1000.times do |i|
235
+ verify_output_before_step(i)
236
+ Progress.step
237
+ end
238
+ Progress.stop
239
+ verify_output_after_stop
240
+ end
241
+
242
+ it "should show valid output for block version" do
243
+ Progress('Test', 1000) do
244
+ 1000.times do |i|
245
+ verify_output_before_step(i)
246
+ Progress.step
247
+ end
248
+ end
249
+ verify_output_after_stop
250
+ end
251
+ end
195
252
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: progress
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.3
4
+ version: 0.1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - toy