progress 0.0.7.1 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/progress.rb CHANGED
@@ -4,7 +4,7 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'singleton'
5
5
 
6
6
  class Progress
7
- VERSION = '0.0.7.1'
7
+ VERSION = '0.0.8'
8
8
 
9
9
  include Singleton
10
10
 
@@ -29,8 +29,12 @@ class Progress
29
29
  # sleep(0.001)
30
30
  # end
31
31
  # end
32
- def self.start(name, total = 1)
33
- levels << new(name, total, levels.length)
32
+ # ==== To output progress as lines (not trying to stay on line)
33
+ # Progress.start('Test', 1000, :lines => true) do
34
+ # 1000.times{ Progress.step }
35
+ # end
36
+ def self.start(name, total = 1, options = {})
37
+ levels << new(name, total, levels.length, options)
34
38
  print_message
35
39
  if block_given?
36
40
  result = yield
@@ -39,24 +43,11 @@ class Progress
39
43
  end
40
44
  end
41
45
 
42
- def self.step(steps = 1)
43
- levels[-1].step(steps)
44
- print_message
45
- end
46
-
47
- def self.stop
48
- levels.pop.stop
49
- @io.puts if levels.empty?
50
- end
51
-
52
- def self.io=(io) # :nodoc:
53
- @io = io
54
- end
55
-
56
- def initialize(name, total, level) # :nodoc:
46
+ def initialize(name, total, level, options) # :nodoc:
57
47
  @name = name + ': %s'
58
48
  @total = total
59
49
  @level = level
50
+ @options = options
60
51
  @current = 0
61
52
  start
62
53
  end
@@ -78,19 +69,11 @@ class Progress
78
69
  @message
79
70
  end
80
71
 
81
- protected
82
-
83
- def self.print_message
84
- message = levels.map{ |level| level.message } * ' > '
85
- @io ||= $stderr
86
- @io.sync = true
87
- @io.print "\r" + message.ljust(@previous_length || 0).gsub(/\d+\.\d+/){ |s| s == '100.0' ? s : "\e[1m#{s}\e[0m" }
88
- @previous_length = message.length
72
+ def options # :nodoc:
73
+ @options
89
74
  end
90
75
 
91
- def self.levels
92
- @levels ||= []
93
- end
76
+ protected
94
77
 
95
78
  def percent
96
79
  '%5.1f%%' % (@current * 100.0 / @total)
@@ -100,6 +83,41 @@ protected
100
83
  formatted = s.ljust(6)[0, 6]
101
84
  @message = @name % formatted
102
85
  end
86
+
87
+ module ClassMethods
88
+ def step(steps = 1)
89
+ levels[-1].step(steps)
90
+ print_message
91
+ end
92
+
93
+ def stop
94
+ levels.pop.stop
95
+ @io.puts if levels.empty?
96
+ end
97
+
98
+ def io=(io) # :nodoc:
99
+ @io = io
100
+ end
101
+
102
+ protected
103
+
104
+ def print_message
105
+ message = levels.map{ |level| level.message } * ' > '
106
+ @io ||= $stderr
107
+ @io.sync = true
108
+ if @io.tty? && !levels.any?{ |level| level.options[:lines] }
109
+ @io.print "\r" + message.ljust(@previous_length || 0).gsub(/\d+\.\d+/){ |s| s == '100.0' ? s : "\e[1m#{s}\e[0m" }
110
+ @previous_length = message.length
111
+ else
112
+ @io.puts message
113
+ end
114
+ end
115
+
116
+ def levels
117
+ @levels ||= []
118
+ end
119
+ end
120
+ extend ClassMethods
103
121
  end
104
122
 
105
123
  require 'progress/enumerable'
@@ -4,8 +4,8 @@ module Enumerable
4
4
  # [1, 2, 3].each_with_progress('Numbers') do |number|
5
5
  # sleep(number)
6
6
  # end
7
- def each_with_progress(name)
8
- Progress.start(name, length) do
7
+ def each_with_progress(name, options = {})
8
+ Progress.start(name, length, options) do
9
9
  each do |item|
10
10
  yield item
11
11
  Progress.step
@@ -18,8 +18,8 @@ module Enumerable
18
18
  # [1, 2, 3].each_with_index_and_progress('Numbers') do |number, index|
19
19
  # sleep(number)
20
20
  # end
21
- def each_with_index_and_progress(name)
22
- Progress.start(name, length) do
21
+ def each_with_index_and_progress(name, options = {})
22
+ Progress.start(name, length, options) do
23
23
  each_with_index do |item, index|
24
24
  yield item, index
25
25
  Progress.step
@@ -4,8 +4,8 @@ class Integer
4
4
  # 100.times_with_progress('Numbers') do |number|
5
5
  # sleep(number)
6
6
  # end
7
- def times_with_progress(name)
8
- Progress.start(name, self) do
7
+ def times_with_progress(name, options = {})
8
+ Progress.start(name, self, options) do
9
9
  times do |i|
10
10
  yield i
11
11
  Progress.step
data/progress.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{progress}
5
- s.version = "0.0.7.1"
5
+ s.version = "0.0.8"
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"]
9
- s.date = %q{2009-04-24}
9
+ s.date = %q{2009-04-27}
10
10
  s.description = %q{A library to show progress of long running tasks.}
11
11
  s.email = %q{}
12
12
  s.extra_rdoc_files = ["CHANGELOG", "lib/progress/enumerable.rb", "lib/progress/integer.rb", "lib/progress.rb", "README.rdoc", "tasks/rspec.rake"]
data/tasks/rspec.rake CHANGED
@@ -14,7 +14,7 @@ EOS
14
14
  exit(0)
15
15
  end
16
16
 
17
- desc "Run the specs under spec/models"
17
+ desc "Run the specs"
18
18
  Spec::Rake::SpecTask.new do |t|
19
19
  t.spec_opts = ['--options', "spec/spec.opts"]
20
20
  t.spec_files = FileList['spec/**/*_spec.rb']
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.0.7.1
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - toy
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-24 00:00:00 +04:00
12
+ date: 2009-04-27 00:00:00 +04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15