progress 0.0.9.3 → 0.1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -42,9 +42,18 @@ Class to show progress during console script run
42
42
  end
43
43
  end
44
44
 
45
+ symbols = []
46
+ Progress.start('Input 100 symbols', 100) do
47
+ while symbols.length < 100
48
+ input = gets.scan(/\S/)
49
+ symbols += input
50
+ Progress.step input.length
51
+ end
52
+ end
53
+
45
54
  == REQUIREMENTS:
46
55
 
47
- * ruby
56
+ * ruby )))
48
57
 
49
58
  == INSTALL:
50
59
 
@@ -73,4 +82,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
73
82
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
74
83
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
75
84
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
76
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
85
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/VERSION.yml CHANGED
@@ -1 +1 @@
1
- [0, 0, 9, 3]
1
+ [0, 1, 0, 0]
data/lib/progress.rb CHANGED
@@ -6,123 +6,112 @@ require 'singleton'
6
6
  class Progress
7
7
  include Singleton
8
8
 
9
- # start progress indication
10
- # ==== Procedural example
11
- # Progress.start('Test', 1000)
12
- # 1000.times{ Progress.step }
13
- # Progress.stop
14
- # ==== Block example
15
- # Progress.start('Test', 1000) do
16
- # 1000.times{ Progress.step }
17
- # end
18
- # ==== Step must not always be one
19
- # Progress.start('Test', 10) do
20
- # (1..10).to_a.each_slice do |slice|
21
- # Progress.step(slice.length)
22
- # end
23
- # end
24
- # ==== Enclosed block example
25
- # [1, 2, 3].each_with_progress('1 2 3') do |one_of_1_2_3|
26
- # 10.times_with_progress('10') do |one_of_10|
27
- # sleep(0.001)
28
- # end
29
- # end
30
- # ==== To output progress as lines (not trying to stay on line)
31
- # Progress.lines = true
32
- # ==== To force highlight
33
- # Progress.highlight = true
34
- def self.start(title, total = 1, options = {})
35
- levels << new(title, total, levels.length, options)
36
- print_message
37
- if block_given?
38
- result = yield
39
- stop
40
- result
9
+ module InstanceMethods # :nodoc:
10
+ attr_accessor :title, :current, :total
11
+ def initialize(title, total)
12
+ @title, @current, @total = title, 0, total.to_f
41
13
  end
42
- end
43
-
44
- attr_reader :message, :options
45
- def initialize(title, total, level, options) # :nodoc:
46
- @title = title + ': %s'
47
- @total = total
48
- @level = level
49
- @options = options
50
- @current = 0
51
- start
52
- end
53
-
54
- def start # :nodoc:
55
- self.message = '.' * 6
56
- end
57
-
58
- def step(steps) # :nodoc:
59
- @current += steps
60
- self.message = percent
61
- end
62
-
63
- def stop # :nodoc:
64
- self.message = percent
65
- end
66
-
67
- protected
68
14
 
69
- def percent
70
- '%5.1f%%' % (@current * 100.0 / @total)
71
- end
15
+ def step_if_blank
16
+ self.current = 1 if current == 0 && total == 1
17
+ end
72
18
 
73
- def message=(s)
74
- formatted = s.ljust(6)[0, 6]
75
- @message = @title % formatted
19
+ def to_f(inner)
20
+ (current + [inner, 1].min) / total
21
+ end
76
22
  end
23
+ include InstanceMethods
24
+
25
+ class << self
26
+ # start progress indication
27
+ # ==== Procedural example
28
+ # Progress.start('Test', 1000)
29
+ # 1000.times{ Progress.step }
30
+ # Progress.stop
31
+ # ==== Block example
32
+ # Progress.start('Test', 1000) do
33
+ # 1000.times{ Progress.step }
34
+ # end
35
+ # ==== Step must not always be one
36
+ # symbols = []
37
+ # Progress.start('Input 100 symbols', 100) do
38
+ # while symbols.length < 100
39
+ # input = gets.scan(/\S/)
40
+ # symbols += input
41
+ # Progress.step input.length
42
+ # end
43
+ # end
44
+ # ==== Enclosed block example
45
+ # [1, 2, 3].each_with_progress('1 2 3') do |one_of_1_2_3|
46
+ # 10.times_with_progress('10') do |one_of_10|
47
+ # sleep(0.001)
48
+ # end
49
+ # end
50
+ # ==== To output progress as lines (not trying to stay on line)
51
+ # Progress.lines = true
52
+ # ==== To force highlight
53
+ # Progress.highlight = true
54
+ def start(title, total = 1)
55
+ levels << new(title, total)
56
+ print_message
57
+ if block_given?
58
+ result = yield
59
+ stop
60
+ result
61
+ end
62
+ end
77
63
 
78
- module ClassMethods
79
64
  def step(steps = 1)
80
- levels[-1].step(steps)
65
+ levels.last.current += steps
66
+ print_message
67
+ end
68
+
69
+ def set(value)
70
+ levels.last.current = value
81
71
  print_message
82
72
  end
83
73
 
84
74
  def stop
85
- levels.pop.stop
86
- @io.puts if levels.empty?
75
+ print_message if levels.last.step_if_blank
76
+ levels.pop
77
+ io.puts if levels.empty?
78
+ end
79
+
80
+ attr_writer :io, :lines, :highlight # :nodoc:
81
+
82
+ private
83
+
84
+ def levels
85
+ @levels ||= []
87
86
  end
88
87
 
89
88
  def io
90
89
  @io ||= $stderr
90
+ @io.sync = true
91
+ @io
91
92
  end
92
93
 
93
94
  def io_tty?
94
- ENV['PROGRESS_TTY'] || io.tty?
95
+ io.tty? || ENV['PROGRESS_TTY']
95
96
  end
96
97
 
97
- def io=(io)
98
- @io = io
99
- end
100
-
101
- def lines=(value)
102
- @lines = value
103
- end
104
98
  def lines?
105
- if @lines.nil?
106
- @lines = !io_tty?
107
- end
108
- @lines
99
+ @lines.nil? ? !io_tty? : @lines
109
100
  end
110
101
 
111
- def highlight=(value)
112
- @highlight = value
113
- end
114
102
  def highlight?
115
- if @highlight.nil?
116
- @highlight = io_tty?
117
- end
118
- @highlight
103
+ @highlight.nil? ? io_tty? : @highlight
119
104
  end
120
105
 
121
- protected
122
-
123
106
  def print_message
124
- message = levels.map{ |level| level.message } * ' > '
125
- io.sync = true
107
+ messages = []
108
+ inner = 0
109
+ levels.reverse.each do |l|
110
+ current = l.to_f(inner)
111
+ messages << "#{l.title}: #{(current == 0 ? '......' : '%5.1f%%' % (current * 100.0))[0, 6]}"
112
+ inner = current
113
+ end
114
+ message = messages.reverse * ' > '
126
115
 
127
116
  unless lines?
128
117
  previous_length = @previous_length || 0
@@ -130,22 +119,11 @@ protected
130
119
  message = message.ljust(previous_length, ' ') + "\r"
131
120
  end
132
121
 
133
- if highlight?
134
- message.gsub!(/\d+\.\d+/){ |s| s == '100.0' ? s : "\e[1m#{s}\e[0m" }
135
- end
136
-
137
- unless lines?
138
- io.print message
139
- else
140
- io.puts message
141
- end
142
- end
122
+ message.gsub!(/\d+\.\d+/){ |s| s == '100.0' ? s : "\e[1m#{s}\e[0m" } if highlight?
143
123
 
144
- def levels
145
- @levels ||= []
124
+ lines? ? io.puts(message) : io.print(message)
146
125
  end
147
126
  end
148
- extend ClassMethods
149
127
  end
150
128
 
151
129
  require 'progress/with_progress'
@@ -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, options = {})
8
- Progress.start(name, self, options) do
7
+ def times_with_progress(name)
8
+ Progress.start(name, self) 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.9.3"
5
+ s.version = "0.1.0.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"]
9
- s.date = %q{2009-08-06}
9
+ s.date = %q{2009-08-19}
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/with_progress.rb", "lib/progress.rb", "README.rdoc", "tasks/rspec.rake"]
@@ -1,5 +1,11 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper.rb'
2
2
 
3
+ Progress.start('Test') do
4
+ Progress.start('Test') do
5
+ 'qwerty'
6
+ end
7
+ end
8
+
3
9
  describe Progress do
4
10
  before :each do
5
11
  @io = StringIO.new
@@ -138,9 +144,23 @@ describe Progress do
138
144
  10.times_with_progress('A') do |a|
139
145
  io_pop.should =~ /#{Regexp.quote(a == 0 ? '......' : (a * 10.0).to_s)}/
140
146
  10.times_with_progress('B') do |b|
141
- io_pop.should =~ /#{Regexp.quote(a == 0 ? '......' : (a * 10.0).to_s)}.*#{Regexp.quote(b == 0 ? '......' : (b * 10.0).to_s)}/
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
142
162
  end
143
- io_pop.should =~ /#{Regexp.quote(a == 0 ? '......' : (a * 10.0).to_s)}.*100\.0/
163
+ io_pop.should =~ /#{Regexp.quote(((a + 1) * 10.0).to_s)}.*200\.0/
144
164
  end
145
165
  io_pop.should =~ /100\.0.*\n$/
146
166
  end
@@ -150,4 +170,12 @@ describe Progress do
150
170
  'qwerty'
151
171
  end.should == 'qwerty'
152
172
  end
173
+
174
+ it "should pipe result from nested block" do
175
+ [1, 2, 3].with_progress('a').map do |a|
176
+ [1, 2, 3].with_progress('b').map do |b|
177
+ a * b
178
+ end
179
+ end.should == [[1, 2, 3], [2, 4, 6], [3, 6, 9]]
180
+ end
153
181
  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.0.9.3
4
+ version: 0.1.0.0
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-08-06 00:00:00 +04:00
12
+ date: 2009-08-19 00:00:00 +04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15