progress 1.2.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /pkg/
2
+
3
+ /doc/
4
+ /rdoc/
5
+ /.yardoc/
6
+ /coverage/
7
+
8
+ Makefile
9
+ *.o
10
+ *.bundle
11
+ /tmp/
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Ivan Kuchin
1
+ Copyright (c) 2010-2011 Ivan Kuchin
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.markdown CHANGED
@@ -98,6 +98,14 @@ Or if you know that B runs 10 times faster than C:
98
98
  end
99
99
  end
100
100
 
101
+ You can also show note:
102
+
103
+ [1, 2, 3].with_progress('Wait').each do |i|
104
+ sleep 1
105
+ Progress.note = i
106
+ end
107
+
108
+
101
109
  ## REQUIREMENTS:
102
110
 
103
111
  ruby )))
@@ -108,4 +116,4 @@ ruby )))
108
116
 
109
117
  ## Copyright
110
118
 
111
- Copyright (c) 2010 Ivan Kuchin. See LICENSE.txt for details.
119
+ Copyright (c) 2010-2011 Ivan Kuchin. See LICENSE.txt for details.
data/lib/progress.rb CHANGED
@@ -3,11 +3,19 @@ require 'thread'
3
3
 
4
4
  # ==== Procedural example
5
5
  # Progress.start('Test', 1000)
6
- # 1000.times{ Progress.step }
6
+ # 1000.times do
7
+ # Progress.step do
8
+ # # do something
9
+ # end
10
+ # end
7
11
  # Progress.stop
8
12
  # ==== Block example
9
13
  # Progress.start('Test', 1000) do
10
- # 1000.times{ Progress.step }
14
+ # 1000.times do
15
+ # Progress.step do
16
+ # # do something
17
+ # end
18
+ # end
11
19
  # end
12
20
  # ==== Step must not always be one
13
21
  # symbols = []
@@ -70,7 +78,7 @@ class Progress
70
78
  @semaphore = Mutex.new
71
79
  @beeper = Thread.new do
72
80
  loop do
73
- sleep 3
81
+ sleep 1
74
82
  print_message
75
83
  end
76
84
  end
@@ -121,12 +129,13 @@ class Progress
121
129
  end
122
130
  levels.pop
123
131
  if levels.empty?
132
+ @beeper.kill
124
133
  io.puts
125
134
  end
126
135
  end
127
136
  end
128
137
 
129
- # set note (will be shown after progress message)
138
+ # set note
130
139
  def note=(s)
131
140
  if levels.last
132
141
  levels.last.note = s
@@ -250,5 +259,11 @@ end
250
259
 
251
260
  require 'progress/enumerable'
252
261
  require 'progress/integer'
253
- require 'progress/kernel'
254
262
  require 'progress/active_record'
263
+
264
+ module Kernel
265
+ def Progress(title = nil, total = nil, &block)
266
+ Progress.start(title, total, &block)
267
+ end
268
+ private :Progress
269
+ end
@@ -1,3 +1,5 @@
1
+ require 'progress'
2
+
1
3
  if defined?(ActiveRecord::Base)
2
4
  module ActiveRecord
3
5
  module BatchesWithProgress
@@ -8,28 +8,19 @@ module Enumerable
8
8
  # [1, 2, 3].with_progress('Numbers').each do |number|
9
9
  # # code
10
10
  # end
11
+ #
11
12
  # [1, 2, 3].with_progress('Numbers').each_cons(2) do |numbers|
12
13
  # # code
13
14
  # end
14
- def with_progress(title = nil)
15
- Progress::WithProgress.new(self, title)
16
- end
17
-
18
- # run `each` with progress
19
- # ==== Example
20
- # [1, 2, 3].each_with_progress('Numbers') do |number|
15
+ #
16
+ # (0...100).with_progress('Numbers').select do |numbers|
21
17
  # # code
22
18
  # end
23
- def each_with_progress(title = nil, &block)
24
- with_progress(title).each(&block)
25
- end
26
-
27
- # run `each_with_index` with progress
28
- # ==== Example
29
- # [1, 2, 3].each_with_index_and_progress('Numbers') do |number, index|
19
+ #
20
+ # (0...100).with_progress('Numbers').all? do |numbers|
30
21
  # # code
31
22
  # end
32
- def each_with_index_and_progress(title = nil, &block)
33
- with_progress(title).each_with_index(&block)
23
+ def with_progress(title = nil, &block)
24
+ Progress::WithProgress.new(self, title, &block)
34
25
  end
35
26
  end
@@ -1,3 +1,5 @@
1
+ require 'progress'
2
+
1
3
  class Integer
2
4
  # run `times` with progress
3
5
  # 100.times_with_progress('Numbers') do |number|
@@ -1,42 +1,38 @@
1
- require 'delegate'
1
+ require 'progress'
2
2
 
3
3
  class Progress
4
4
  class WithProgress
5
- attr_reader :object, :title
6
- def initialize(object, title)
7
- @object = Progress::Enhancer.new(object)
8
- @title = title
9
- end
5
+ include Enumerable
10
6
 
11
- def with_progress(title)
12
- self
7
+ def initialize(enumerable, title, &block)
8
+ @enumerable, @title = enumerable, title
9
+ each(&block) if block
13
10
  end
14
11
 
15
- def method_missing(method, *args, &block)
16
- Progress.start(title, object.length) do
17
- object.send(method, *args, &block)
18
- end
19
- end
20
- end
21
-
22
- class Enhancer < SimpleDelegator
23
- include Enumerable
24
- def each(*args, &block)
25
- __getobj__.each(*args) do |*yielded|
26
- Progress.step do
27
- block.call(*yielded)
12
+ def each
13
+ Progress.start(@title, length) do
14
+ @enumerable.each do |object|
15
+ Progress.step do
16
+ yield object
17
+ end
28
18
  end
29
19
  end
30
20
  end
31
21
 
32
22
  def length
33
- if __getobj__.respond_to?(:length) && !__getobj__.is_a?(String)
34
- __getobj__.length
35
- elsif __getobj__.respond_to?(:to_a)
36
- __getobj__.to_a.length
23
+ @length ||= if @enumerable.respond_to?(:length) && !@enumerable.is_a?(String)
24
+ @enumerable.length
25
+ elsif @enumerable.respond_to?(:count)
26
+ @enumerable.count
27
+ elsif @enumerable.respond_to?(:to_a)
28
+ @enumerable.to_a.length
37
29
  else
38
- __getobj__.inject(0){ |length, obj| length + 1 }
30
+ @enumerable.inject(0){ |length, obj| length + 1 }
39
31
  end
40
32
  end
33
+
34
+ def with_progress(title = nil)
35
+ self
36
+ end
41
37
  end
42
38
  end
data/progress.gemspec CHANGED
@@ -1,57 +1,19 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
1
+ # encoding: UTF-8
5
2
 
6
3
  Gem::Specification.new do |s|
7
- s.name = "progress"
8
- s.version = "1.2.1"
4
+ s.name = 'progress'
5
+ s.version = '2.0.0'
6
+ s.summary = %q{Show progress of long running tasks}
7
+ s.homepage = "http://github.com/toy/#{s.name}"
8
+ s.authors = ['Ivan Kuchin']
9
+ s.license = 'MIT'
9
10
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Ivan Kuchin"]
12
- s.date = "2011-10-18"
13
- s.extra_rdoc_files = [
14
- "LICENSE.txt",
15
- "README.markdown"
16
- ]
17
- s.files = [
18
- ".tmignore",
19
- "LICENSE.txt",
20
- "README.markdown",
21
- "Rakefile",
22
- "VERSION",
23
- "lib/progress.rb",
24
- "lib/progress/active_record.rb",
25
- "lib/progress/enumerable.rb",
26
- "lib/progress/integer.rb",
27
- "lib/progress/kernel.rb",
28
- "lib/progress/with_progress.rb",
29
- "progress.gemspec",
30
- "spec/progress_spec.rb",
31
- "spec/spec_helper.rb"
32
- ]
33
- s.homepage = "http://github.com/toy/progress"
34
- s.licenses = ["MIT"]
35
- s.require_paths = ["lib"]
36
- s.rubygems_version = "1.8.11"
37
- s.summary = "Show progress of long running tasks"
11
+ s.rubyforge_project = s.name
38
12
 
39
- if s.respond_to? :specification_version then
40
- s.specification_version = 3
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = %w[lib]
41
17
 
42
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
44
- s.add_development_dependency(%q<rake-gem-ghost>, [">= 0"])
45
- s.add_development_dependency(%q<rspec>, [">= 0"])
46
- else
47
- s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
48
- s.add_dependency(%q<rake-gem-ghost>, [">= 0"])
49
- s.add_dependency(%q<rspec>, [">= 0"])
50
- end
51
- else
52
- s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
53
- s.add_dependency(%q<rake-gem-ghost>, [">= 0"])
54
- s.add_dependency(%q<rspec>, [">= 0"])
55
- end
18
+ s.add_development_dependency 'rspec'
56
19
  end
57
-
@@ -1,10 +1,12 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper.rb'
2
2
 
3
3
  describe Progress do
4
+ let(:count){ 165 }
5
+
4
6
  before :each do
5
7
  @io = StringIO.new
6
- Progress.stub!(:io).and_return(@io)
7
- Progress.stub!(:time_to_print?).and_return(true)
8
+ Progress.instance_variable_set(:@io, @io)
9
+ def Progress.time_to_print?; true; end
8
10
  end
9
11
 
10
12
  def io_pop
@@ -19,8 +21,8 @@ describe Progress do
19
21
  io_pop.sub(/ \(ETA: \d+.\)$/, '')
20
22
  end
21
23
 
22
- def verify_output_before_step(i)
23
- io_pop.should =~ /#{Regexp.quote(i == 0 ? '......' : (i / 10.0).to_s)}/
24
+ def verify_output_before_step(i, count)
25
+ io_pop.should =~ /#{Regexp.quote(i == 0 ? '......' : '%5.1f' % (i / count.to_f * 100.0))}/
24
26
  end
25
27
  def verify_output_after_stop
26
28
  io_pop.should =~ /100\.0.*\n$/
@@ -29,9 +31,9 @@ describe Progress do
29
31
  describe 'direct usage' do
30
32
  describe 'procedural' do
31
33
  it "should show valid output when called as Progress.start" do
32
- Progress.start('Test', 1000)
33
- 1000.times do |i|
34
- verify_output_before_step(i)
34
+ Progress.start('Test', count)
35
+ count.times do |i|
36
+ verify_output_before_step(i, count)
35
37
  Progress.step
36
38
  end
37
39
  Progress.stop
@@ -39,9 +41,9 @@ describe Progress do
39
41
  end
40
42
 
41
43
  it "should show valid output when called as Progress" do
42
- Progress('Test', 1000)
43
- 1000.times do |i|
44
- verify_output_before_step(i)
44
+ Progress('Test', count)
45
+ count.times do |i|
46
+ verify_output_before_step(i, count)
45
47
  Progress.step
46
48
  end
47
49
  Progress.stop
@@ -49,9 +51,9 @@ describe Progress do
49
51
  end
50
52
 
51
53
  it "should show valid output when called without title" do
52
- Progress(1000)
53
- 1000.times do |i|
54
- verify_output_before_step(i)
54
+ Progress(count)
55
+ count.times do |i|
56
+ verify_output_before_step(i, count)
55
57
  Progress.step
56
58
  end
57
59
  Progress.stop
@@ -61,9 +63,9 @@ describe Progress do
61
63
 
62
64
  describe 'block' do
63
65
  it "should show valid output when called as Progress.start" do
64
- Progress.start('Test', 1000) do
65
- 1000.times do |i|
66
- verify_output_before_step(i)
66
+ Progress.start('Test', count) do
67
+ count.times do |i|
68
+ verify_output_before_step(i, count)
67
69
  Progress.step
68
70
  end
69
71
  end
@@ -71,9 +73,9 @@ describe Progress do
71
73
  end
72
74
 
73
75
  it "should show valid output when called as Progress" do
74
- Progress('Test', 1000) do
75
- 1000.times do |i|
76
- verify_output_before_step(i)
76
+ Progress('Test', count) do
77
+ count.times do |i|
78
+ verify_output_before_step(i, count)
77
79
  Progress.step
78
80
  end
79
81
  end
@@ -81,9 +83,9 @@ describe Progress do
81
83
  end
82
84
 
83
85
  it "should show valid output when called without title" do
84
- Progress(1000) do
85
- 1000.times do |i|
86
- verify_output_before_step(i)
86
+ Progress(count) do
87
+ count.times do |i|
88
+ verify_output_before_step(i, count)
87
89
  Progress.step
88
90
  end
89
91
  end
@@ -196,68 +198,18 @@ describe Progress do
196
198
 
197
199
  describe Enumerable do
198
200
  before :each do
199
- @a = (0...1000).to_a
201
+ @a = 0...1000
200
202
  end
201
203
 
202
- describe 'with each_with_progress' do
204
+ describe 'with_progress' do
203
205
  it "should not break each" do
204
- a = []
205
- @a.each_with_progress('Test') do |n|
206
- a << n
207
- end
208
- a.should == @a
209
- end
210
-
211
- it "should show valid output for each_with_progress" do
212
- @a.each_with_progress('Test') do |n|
213
- verify_output_before_step(n)
214
- end
215
- verify_output_after_stop
216
- end
217
-
218
- it "should show valid output for each_with_progress without title" do
219
- @a.each_with_progress do |n|
220
- verify_output_before_step(n)
221
- end
222
- verify_output_after_stop
223
- end
224
- end
225
-
226
- describe 'with each_with_index_and_progress' do
227
- it "should not break each_with_index" do
228
- a = []
229
- @a.each_with_index_and_progress('Test') do |n, i|
230
- n.should == i
231
- a << n
232
- end
233
- a.should == @a
234
- end
235
-
236
- it "should show valid output for each_with_progress" do
237
- @a.each_with_index_and_progress('Test') do |n, i|
238
- verify_output_before_step(n)
239
- end
240
- verify_output_after_stop
241
- end
242
-
243
- it "should show valid output for each_with_progress without title" do
244
- @a.each_with_index_and_progress do |n, i|
245
- verify_output_before_step(n)
246
- end
247
- verify_output_after_stop
248
- end
249
- end
250
-
251
- describe 'with with_progress' do
252
- it "should not break each" do
253
- a = []
254
- @a.with_progress('Test').each do |n|
255
- a << n
256
- end
257
- a.should == @a
206
+ with, without = [], []
207
+ @a.with_progress.each{ |n| with << n }
208
+ @a.each{ |n| without << n }
209
+ with.should == without
258
210
  end
259
211
 
260
- it "should not break any?" do
212
+ it "should not break find" do
261
213
  @a.with_progress('Hello').find{ |n| n == 100 }.should == @a.find{ |n| n == 100 }
262
214
  @a.with_progress('Hello').find{ |n| n == 10000 }.should == @a.find{ |n| n == 10000 }
263
215
  default = proc{ 'default' }
@@ -285,23 +237,23 @@ describe Progress do
285
237
  describe Integer do
286
238
  describe 'with times_with_progress' do
287
239
  it "should not break times" do
288
- c = 0
289
- 1000.times_with_progress('Test') do |i|
290
- i.should == c
291
- c += 1
240
+ ii = 0
241
+ count.times_with_progress('Test') do |i|
242
+ i.should == ii
243
+ ii += 1
292
244
  end
293
245
  end
294
246
 
295
247
  it "should show valid output for each_with_progress" do
296
- 1000.times_with_progress('Test') do |i|
297
- verify_output_before_step(i)
248
+ count.times_with_progress('Test') do |i|
249
+ verify_output_before_step(i, count)
298
250
  end
299
251
  verify_output_after_stop
300
252
  end
301
253
 
302
254
  it "should show valid output for each_with_progress without title" do
303
- 1000.times_with_progress do |i|
304
- verify_output_before_step(i)
255
+ count.times_with_progress do |i|
256
+ verify_output_before_step(i, count)
305
257
  end
306
258
  verify_output_after_stop
307
259
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: progress
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
- - 1
8
7
  - 2
9
- - 1
10
- version: 1.2.1
8
+ - 0
9
+ - 0
10
+ version: 2.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ivan Kuchin
@@ -15,42 +15,12 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-18 00:00:00 Z
18
+ date: 2011-11-28 00:00:00 Z
19
19
  dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: jeweler
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 1
29
- segments:
30
- - 1
31
- - 5
32
- - 1
33
- version: 1.5.1
34
- type: :development
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: rake-gem-ghost
38
- prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 3
45
- segments:
46
- - 0
47
- version: "0"
48
- type: :development
49
- version_requirements: *id002
50
20
  - !ruby/object:Gem::Dependency
51
21
  name: rspec
52
22
  prerelease: false
53
- requirement: &id003 !ruby/object:Gem::Requirement
23
+ requirement: &id001 !ruby/object:Gem::Requirement
54
24
  none: false
55
25
  requirements:
56
26
  - - ">="
@@ -60,27 +30,23 @@ dependencies:
60
30
  - 0
61
31
  version: "0"
62
32
  type: :development
63
- version_requirements: *id003
33
+ version_requirements: *id001
64
34
  description:
65
35
  email:
66
36
  executables: []
67
37
 
68
38
  extensions: []
69
39
 
70
- extra_rdoc_files:
71
- - LICENSE.txt
72
- - README.markdown
40
+ extra_rdoc_files: []
41
+
73
42
  files:
74
- - .tmignore
43
+ - .gitignore
75
44
  - LICENSE.txt
76
45
  - README.markdown
77
- - Rakefile
78
- - VERSION
79
46
  - lib/progress.rb
80
47
  - lib/progress/active_record.rb
81
48
  - lib/progress/enumerable.rb
82
49
  - lib/progress/integer.rb
83
- - lib/progress/kernel.rb
84
50
  - lib/progress/with_progress.rb
85
51
  - progress.gemspec
86
52
  - spec/progress_spec.rb
@@ -113,10 +79,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
79
  version: "0"
114
80
  requirements: []
115
81
 
116
- rubyforge_project:
82
+ rubyforge_project: progress
117
83
  rubygems_version: 1.8.11
118
84
  signing_key:
119
85
  specification_version: 3
120
86
  summary: Show progress of long running tasks
121
- test_files: []
122
-
87
+ test_files:
88
+ - spec/progress_spec.rb
89
+ - spec/spec_helper.rb
90
+ has_rdoc:
data/.tmignore DELETED
@@ -1 +0,0 @@
1
- /*.gemspec
data/Rakefile DELETED
@@ -1,25 +0,0 @@
1
- require 'rake'
2
- require 'jeweler'
3
- require 'rake/gem_ghost_task'
4
- require 'rspec/core/rake_task'
5
-
6
- name = 'progress'
7
-
8
- Jeweler::Tasks.new do |gem|
9
- gem.name = name
10
- gem.summary = %Q{Show progress of long running tasks}
11
- gem.homepage = "http://github.com/toy/#{name}"
12
- gem.license = 'MIT'
13
- gem.authors = ['Ivan Kuchin']
14
- gem.add_development_dependency 'jeweler', '~> 1.5.1'
15
- gem.add_development_dependency 'rake-gem-ghost'
16
- gem.add_development_dependency 'rspec'
17
- end
18
- Jeweler::RubygemsDotOrgTasks.new
19
- Rake::GemGhostTask.new
20
-
21
- RSpec::Core::RakeTask.new(:spec) do |spec|
22
- spec.rspec_opts = ['--colour --format progress']
23
- spec.pattern = 'spec/**/*_spec.rb'
24
- end
25
- task :default => :spec
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.2.1
@@ -1,6 +0,0 @@
1
- module Kernel
2
- def Progress(title = nil, total = nil, &block)
3
- Progress.start(title, total, &block)
4
- end
5
- private :Progress
6
- end