progress 0.0.1 → 0.0.3

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/History.txt ADDED
@@ -0,0 +1,14 @@
1
+ == 0.0.3 2008-11-01
2
+
3
+ * recreated with newgem
4
+ * created specs
5
+ * autospec now runs well
6
+ * created documentation
7
+
8
+ == 0.0.2 2008-10-31
9
+
10
+ * added enclosed progress possibility
11
+
12
+ == 0.0.1
13
+
14
+ * initial release
data/Manifest.txt ADDED
@@ -0,0 +1,14 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/progress.rb
6
+ lib/progress/enumerable.rb
7
+ lib/progress/integer.rb
8
+ script/console
9
+ script/destroy
10
+ script/generate
11
+ spec/progress_spec.rb
12
+ spec/spec.opts
13
+ spec/spec_helper.rb
14
+ tasks/rspec.rake
data/README.rdoc ADDED
@@ -0,0 +1,51 @@
1
+ = progress
2
+
3
+ * http://github.com/toy/progress/tree/master
4
+
5
+ == DESCRIPTION:
6
+
7
+ Class to show progress during console script run
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Progress
12
+ * Enclosed progress
13
+
14
+ == SYNOPSIS:
15
+
16
+ 1000.times_with_progress('Wait') do |time|
17
+ sleep(time * 0.0001)
18
+ end
19
+
20
+ == REQUIREMENTS:
21
+
22
+ * ruby
23
+
24
+ == INSTALL:
25
+
26
+ * sudo gem install progress
27
+
28
+ == LICENSE:
29
+
30
+ (The MIT License)
31
+
32
+ Copyright (c) 2008 toy full name
33
+
34
+ Permission is hereby granted, free of charge, to any person obtaining
35
+ a copy of this software and associated documentation files (the
36
+ 'Software'), to deal in the Software without restriction, including
37
+ without limitation the rights to use, copy, modify, merge, publish,
38
+ distribute, sublicense, and/or sell copies of the Software, and to
39
+ permit persons to whom the Software is furnished to do so, subject to
40
+ the following conditions:
41
+
42
+ The above copyright notice and this permission notice shall be
43
+ included in all copies or substantial portions of the Software.
44
+
45
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
46
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
47
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
48
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
49
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
50
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
51
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,47 +1,21 @@
1
- require 'rubygems'
2
- require 'rake/gempackagetask'
3
- require 'rubygems/specification'
4
- require 'date'
5
-
6
- GEM = "progress"
7
- GEM_VERSION = "0.0.1"
8
- AUTHOR = "toy"
9
- EMAIL = "ivan@workisfun.ru"
10
- HOMEPAGE = ""
11
- SUMMARY = "Class to show progress during script run"
12
-
13
- spec = Gem::Specification.new do |s|
14
- s.name = GEM
15
- s.version = GEM_VERSION
16
- s.platform = Gem::Platform::RUBY
17
- s.has_rdoc = true
18
- s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
19
- s.summary = SUMMARY
20
- s.description = s.summary
21
- s.author = AUTHOR
22
- s.email = EMAIL
23
- s.homepage = HOMEPAGE
24
-
25
- # Uncomment this to add a dependency
26
- # s.add_dependency "foo"
27
-
28
- s.require_path = 'lib'
29
- s.autorequire = GEM
30
- s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
31
- end
32
-
33
- Rake::GemPackageTask.new(spec) do |pkg|
34
- pkg.gem_spec = spec
35
- end
36
-
37
- desc "install the gem locally"
38
- task :install => [:package] do
39
- sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
40
- end
41
-
42
- desc "create a gemspec file"
43
- task :make_spec do
44
- File.open("#{GEM}.gemspec", "w") do |file|
45
- file.puts spec.to_ruby
46
- end
47
- end
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/progress'
3
+
4
+ $hoe = Hoe.new('progress', Progress::VERSION) do |p|
5
+ p.developer('toy', 'email')
6
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
7
+ p.rubyforge_name = p.name
8
+ # p.extra_deps = [
9
+ # ['activesupport','>= 2.0.2'],
10
+ # ]
11
+ p.extra_dev_deps = [
12
+ ['newgem', ">= #{::Newgem::VERSION}"]
13
+ ]
14
+
15
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
16
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
17
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
18
+ p.rsync_args = '-av --delete --ignore-errors'
19
+ end
20
+
21
+ require 'newgem/tasks'
@@ -0,0 +1,29 @@
1
+ module Enumerable
2
+ # note that Progress.step is called automatically
3
+ # ==== Example
4
+ # [1, 2, 3].each_with_progress('Numbers') do |number|
5
+ # sleep(number)
6
+ # end
7
+ def each_with_progress(name)
8
+ Progress.start(name, length) do
9
+ each do |item|
10
+ yield item
11
+ Progress.step
12
+ end
13
+ end
14
+ end
15
+
16
+ # note that Progress.step is called automatically
17
+ # ==== Example
18
+ # [1, 2, 3].each_with_index_and_progress('Numbers') do |number, index|
19
+ # sleep(number)
20
+ # end
21
+ def each_with_index_and_progress(name)
22
+ Progress.start(name, length) do
23
+ each_with_index do |item, index|
24
+ yield item, index
25
+ Progress.step
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,4 +1,9 @@
1
1
  class Integer
2
+ # note that Progress.step is called automatically
3
+ # ==== Example
4
+ # 100.times_with_progress('Numbers') do |number|
5
+ # sleep(number)
6
+ # end
2
7
  def times_with_progress(name)
3
8
  Progress.start(name, self) do
4
9
  times do |i|
data/lib/progress.rb CHANGED
@@ -1,42 +1,99 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'singleton'
5
+
1
6
  class Progress
7
+ VERSION = '0.0.3'
8
+
9
+ include Singleton
10
+
11
+ # start progress indication
12
+ # ==== Procedural example
13
+ # Progress.start('Test', 1000)
14
+ # 1000.times{ Progress.step }
15
+ # Progress.stop
16
+ # ==== Block example
17
+ # Progress.start('Test', 1000) do
18
+ # 1000.times{ Progress.step }
19
+ # end
20
+ # ==== Enclosed block example
21
+ # [1, 2, 3].each_with_progress('1 2 3') do |one_of_1_2_3|
22
+ # 10.times_with_progress('10') do |one_of_10|
23
+ # sleep(0.001)
24
+ # end
25
+ # end
2
26
  def self.start(name, total = 100)
3
- @io ||= $stdout
4
- @io.sync = true
5
-
6
- @each = total / 1000
7
- @count = 0
27
+ levels << new(name, total, levels.length)
28
+ print_message
29
+ if block_given?
30
+ yield
31
+ stop
32
+ end
33
+ end
8
34
 
9
- @current = 0
10
- @total = total
35
+ def self.step
36
+ levels[-1].step
37
+ print_message
38
+ end
39
+
40
+ def self.stop
41
+ levels.pop.stop
42
+ @io.puts if levels.empty?
43
+ end
44
+
45
+ def self.io=(io) # :nodoc:
46
+ @io = io
47
+ end
48
+
49
+ def initialize(name, total, level) # :nodoc:
11
50
  @name = name + ': %s'
12
- message highight('...')
13
- yield
14
- message percent
15
- @io.puts
51
+ @total = total
52
+ @level = level
53
+ @current = 0
54
+ start
16
55
  end
17
56
 
18
- def self.step
57
+ def start # :nodoc:
58
+ self.message = '.' * 6
59
+ end
60
+
61
+ def step # :nodoc:
19
62
  @current += 1
20
- if (@count += 1) >= @each
21
- message highight(percent)
22
- @count = 0
23
- end
63
+ self.message = percent
24
64
  end
25
65
 
26
- private
66
+ def stop # :nodoc:
67
+ self.message = percent
68
+ end
27
69
 
28
- def self.percent
29
- '%5.1f%%' % (@current * 100.0 / @total)
70
+ def message # :nodoc:
71
+ @message
30
72
  end
31
-
32
- def self.message(s)
33
- @io.print "\r\e[0K#{@name % s}"
73
+
74
+ protected
75
+
76
+ def self.print_message
77
+ message = levels.map{ |level| level.message } * ' > '
78
+ @io ||= $stdout
79
+ @io.sync = true
80
+ @io.print "\r" + message.ljust(@previous_length || 0).gsub(/\d+\.\d+/){ |s| s == '100.0' ? s : "\e[1m#{s}\e[0m" }
81
+ @previous_length = message.length
82
+ end
83
+
84
+ def self.levels
85
+ @levels ||= []
86
+ end
87
+
88
+ def percent
89
+ '%5.1f%%' % (@current * 100.0 / @total)
34
90
  end
35
91
 
36
- def self.highight(s)
37
- "\e[1m#{s}\e[0m"
92
+ def message=(s)
93
+ formatted = s.ljust(6)[0, 6]
94
+ @message = @name % formatted
38
95
  end
39
96
  end
40
97
 
41
- require 'enumerable'
42
- require 'integer'
98
+ require 'progress/enumerable'
99
+ require 'progress/integer'
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/progress.rb'}"
9
+ puts "Loading progress gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,114 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe Progress do
4
+ before :each do
5
+ @io = StringIO.new
6
+ Progress.io = @io
7
+ end
8
+
9
+ def io_pop
10
+ @io.seek(0)
11
+ s = @io.read
12
+ @io.truncate(0)
13
+ @io.seek(0)
14
+ s
15
+ end
16
+
17
+ def verify_output_before_step(i)
18
+ io_pop.should =~ /#{Regexp.quote(i == 0 ? '......' : (i / 10.0).to_s)}/
19
+ end
20
+ def verify_output_after_stop
21
+ io_pop.should =~ /100\.0.*\n$/
22
+ end
23
+
24
+ it "should show valid output for procedural version" do
25
+ Progress.start('Test', 1000)
26
+ 1000.times do |i|
27
+ verify_output_before_step(i)
28
+ Progress.step
29
+ end
30
+ Progress.stop
31
+ verify_output_after_stop
32
+ end
33
+
34
+ it "should show valid output for block version" do
35
+ Progress.start('Test', 1000) do
36
+ 1000.times do |i|
37
+ verify_output_before_step(i)
38
+ Progress.step
39
+ end
40
+ end
41
+ verify_output_after_stop
42
+ end
43
+
44
+ describe Enumerable do
45
+ before :each do
46
+ @a = Array.new(1000){ |n| n }
47
+ end
48
+
49
+ describe 'with each_with_progress' do
50
+ it "should not break each" do
51
+ c = 0
52
+ @a.each_with_progress('Test') do |n|
53
+ n.should == @a[c]
54
+ c += 1
55
+ end
56
+ end
57
+
58
+ it "should show valid output for each_with_progress" do
59
+ @a.each_with_progress('Test') do |n|
60
+ verify_output_before_step(n)
61
+ end
62
+ verify_output_after_stop
63
+ end
64
+ end
65
+
66
+ describe 'with each_with_index_and_progress' do
67
+ it "should not break each_with_index" do
68
+ c = 0
69
+ @a.each_with_index_and_progress('Test') do |n, i|
70
+ n.should == @a[c]
71
+ i.should == @a[c]
72
+ c += 1
73
+ end
74
+ end
75
+
76
+ it "should show valid output for each_with_progress" do
77
+ @a.each_with_index_and_progress('Test') do |n, i|
78
+ verify_output_before_step(n)
79
+ end
80
+ verify_output_after_stop
81
+ end
82
+ end
83
+ end
84
+
85
+ describe Integer do
86
+ describe 'with times_with_progress' do
87
+ it "should not break times" do
88
+ c = 0
89
+ 1000.times_with_progress('Test') do |i|
90
+ i.should == c
91
+ c += 1
92
+ end
93
+ end
94
+
95
+ it "should show valid output for each_with_progress" do
96
+ 1000.times_with_progress('Test') do |i|
97
+ verify_output_before_step(i)
98
+ end
99
+ verify_output_after_stop
100
+ end
101
+ end
102
+ end
103
+
104
+ it "should allow enclosed progress" do
105
+ 10.times_with_progress('A') do |a|
106
+ io_pop.should =~ /#{Regexp.quote(a == 0 ? '......' : (a * 10.0).to_s)}/
107
+ 10.times_with_progress('B') do |b|
108
+ io_pop.should =~ /#{Regexp.quote(a == 0 ? '......' : (a * 10.0).to_s)}.*#{Regexp.quote(b == 0 ? '......' : (b * 10.0).to_s)}/
109
+ end
110
+ io_pop.should =~ /#{Regexp.quote(a == 0 ? '......' : (a * 10.0).to_s)}.*100\.0/
111
+ end
112
+ io_pop.should =~ /100\.0.*\n$/
113
+ end
114
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'progress'
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata CHANGED
@@ -1,41 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: progress
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - toy
8
- autorequire: progress
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-11 00:00:00 +04:00
12
+ date: 2008-11-01 00:00:00 +03:00
13
13
  default_executable:
14
- dependencies: []
15
-
16
- description: Class to show progress during script run
17
- email: ivan@workisfun.ru
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ description: Class to show progress during console script run
36
+ email:
37
+ - email
18
38
  executables: []
19
39
 
20
40
  extensions: []
21
41
 
22
42
  extra_rdoc_files:
23
- - README
24
- - LICENSE
25
- - TODO
43
+ - History.txt
44
+ - Manifest.txt
45
+ - README.rdoc
26
46
  files:
27
- - LICENSE
28
- - README
47
+ - History.txt
48
+ - Manifest.txt
49
+ - README.rdoc
29
50
  - Rakefile
30
- - TODO
31
- - lib/enumerable.rb
32
- - lib/integer.rb
33
51
  - lib/progress.rb
52
+ - lib/progress/enumerable.rb
53
+ - lib/progress/integer.rb
54
+ - script/console
55
+ - script/destroy
56
+ - script/generate
57
+ - spec/progress_spec.rb
58
+ - spec/spec.opts
59
+ - spec/spec_helper.rb
60
+ - tasks/rspec.rake
34
61
  has_rdoc: true
35
- homepage: ""
62
+ homepage: http://github.com/toy/progress/tree/master
36
63
  post_install_message:
37
- rdoc_options: []
38
-
64
+ rdoc_options:
65
+ - --main
66
+ - README.rdoc
39
67
  require_paths:
40
68
  - lib
41
69
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -52,10 +80,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
80
  version:
53
81
  requirements: []
54
82
 
55
- rubyforge_project:
83
+ rubyforge_project: progress
56
84
  rubygems_version: 1.2.0
57
85
  signing_key:
58
86
  specification_version: 2
59
- summary: Class to show progress during script run
87
+ summary: Class to show progress during console script run
60
88
  test_files: []
61
89
 
data/LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright (c) 2008 YOUR NAME
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README DELETED
@@ -1,4 +0,0 @@
1
- progress
2
- ========
3
-
4
- Class to show progress during script run
data/TODO DELETED
@@ -1 +0,0 @@
1
- TODO:
data/lib/enumerable.rb DELETED
@@ -1,10 +0,0 @@
1
- module Enumerable
2
- def each_with_progress(name)
3
- Progress.start(name, length) do
4
- each do |item|
5
- yield item
6
- Progress.step
7
- end
8
- end
9
- end
10
- end