progress 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -65,18 +65,26 @@ or just
65
65
  Note - you will get WRONG progress if you use something like this:
66
66
 
67
67
  10.times_with_progress('A') do |time|
68
- 10.times_with_progress('B'){ … }
69
- 10.times_with_progress('C'){ … }
68
+ 10.times_with_progress('B') do
69
+ # code
70
+ end
71
+ 10.times_with_progress('C') do
72
+ # code
73
+ end
70
74
  end
71
75
 
72
76
  But you can use this:
73
77
 
74
78
  10.times_with_progress('A') do |time|
75
79
  Progress.step 1, 2 do
76
- 10.times_with_progress('B'){ … }
80
+ 10.times_with_progress('B') do
81
+ # code
82
+ end
77
83
  end
78
84
  Progress.step 1, 2 do
79
- 10.times_with_progress('C'){ … }
85
+ 10.times_with_progress('C') do
86
+ # code
87
+ end
80
88
  end
81
89
  end
82
90
 
@@ -84,10 +92,14 @@ Or if you know that B runs 10 times faster than C:
84
92
 
85
93
  10.times_with_progress('A') do |time|
86
94
  Progress.step 1, 11 do
87
- 10.times_with_progress('B'){ … }
95
+ 10.times_with_progress('B') do
96
+ # code
97
+ end
88
98
  end
89
99
  Progress.step 10, 11 do
90
- 10.times_with_progress('C'){ … }
100
+ 10.times_with_progress('C') do
101
+ # code
102
+ end
91
103
  end
92
104
  end
93
105
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.1
1
+ 1.1.2
data/lib/progress.rb CHANGED
@@ -1,81 +1,74 @@
1
1
  require 'singleton'
2
2
 
3
+ # ==== Procedural example
4
+ # Progress.start('Test', 1000)
5
+ # 1000.times{ Progress.step }
6
+ # Progress.stop
7
+ # ==== Block example
8
+ # Progress.start('Test', 1000) do
9
+ # 1000.times{ Progress.step }
10
+ # end
11
+ # ==== Step must not always be one
12
+ # symbols = []
13
+ # Progress.start('Input 100 symbols', 100) do
14
+ # while symbols.length < 100
15
+ # input = gets.scan(/\S/)
16
+ # symbols += input
17
+ # Progress.step input.length
18
+ # end
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
3
26
  class Progress
4
27
  include Singleton
5
28
 
6
- module InstanceMethods # :nodoc:
7
- attr_accessor :title, :current, :total, :note
8
- attr_reader :current_step
9
- def initialize(title, total)
10
- if title.is_a?(Numeric) && total.nil?
11
- title, total = nil, title
12
- elsif total.nil?
13
- total = 1
14
- end
15
- @title = title
16
- @current = 0.0
17
- @total = total == 0.0 ? 1.0 : Float(total)
18
- end
29
+ attr_accessor :title, :current, :total, :note
30
+ attr_reader :current_step
31
+ def initialize(title, total)
32
+ if title.is_a?(Numeric) && total.nil?
33
+ title, total = nil, title
34
+ elsif total.nil?
35
+ total = 1
36
+ end
37
+ @title = title
38
+ @current = 0.0
39
+ @total = total == 0.0 ? 1.0 : Float(total)
40
+ end
19
41
 
20
- def step_if_blank
21
- if current == 0.0 && total == 1.0
22
- self.current = 1.0
23
- end
42
+ def step_if_blank
43
+ if current == 0.0 && total == 1.0
44
+ self.current = 1.0
24
45
  end
46
+ end
25
47
 
26
- def to_f(inner)
27
- inner = [inner, 1.0].min
28
- if current_step
29
- inner *= current_step
30
- end
31
- (current + inner) / total
48
+ def to_f(inner)
49
+ inner = [inner, 1.0].min
50
+ if current_step
51
+ inner *= current_step
32
52
  end
53
+ (current + inner) / total
54
+ end
33
55
 
34
- def step(steps)
35
- @current_step = steps
36
- yield
37
- ensure
38
- @current_step = nil
39
- end
56
+ def step(steps)
57
+ @current_step = steps
58
+ yield
59
+ ensure
60
+ @current_step = nil
40
61
  end
41
- include InstanceMethods
42
62
 
43
63
  class << self
44
64
  # start progress indication
45
- # ==== Procedural example
46
- # Progress.start('Test', 1000)
47
- # 1000.times{ Progress.step }
48
- # Progress.stop
49
- # ==== Block example
50
- # Progress.start('Test', 1000) do
51
- # 1000.times{ Progress.step }
52
- # end
53
- # ==== Step must not always be one
54
- # symbols = []
55
- # Progress.start('Input 100 symbols', 100) do
56
- # while symbols.length < 100
57
- # input = gets.scan(/\S/)
58
- # symbols += input
59
- # Progress.step input.length
60
- # end
61
- # end
62
- # ==== Enclosed block example
63
- # [1, 2, 3].each_with_progress('1 2 3') do |one_of_1_2_3|
64
- # 10.times_with_progress('10') do |one_of_10|
65
- # sleep(0.001)
66
- # end
67
- # end
68
- # ==== To output progress as lines (not trying to stay on line)
69
- # Progress.lines = true
70
- # ==== To force highlight
71
- # Progress.highlight = true
72
65
  def start(title = nil, total = nil)
73
66
  if levels.empty?
74
67
  @started_at = Time.now
75
68
  @eta = nil
76
69
  end
77
70
  levels << new(title, total)
78
- print_message(true)
71
+ print_message true
79
72
  if block_given?
80
73
  begin
81
74
  yield
@@ -85,6 +78,7 @@ class Progress
85
78
  end
86
79
  end
87
80
 
81
+ # step current progress by `num / den`
88
82
  def step(num = 1, den = 1, &block)
89
83
  if levels.last
90
84
  set(levels.last.current + Float(num) / den, &block)
@@ -93,6 +87,7 @@ class Progress
93
87
  end
94
88
  end
95
89
 
90
+ # set current progress to `value`
96
91
  def set(value, &block)
97
92
  if levels.last
98
93
  ret = if block
@@ -109,11 +104,12 @@ class Progress
109
104
  end
110
105
  end
111
106
 
107
+ # stop progress
112
108
  def stop
113
109
  if levels.last
114
110
  if levels.last.step_if_blank || levels.length == 1
115
- print_message(true)
116
- set_title(nil)
111
+ print_message true
112
+ set_title nil
117
113
  end
118
114
  levels.pop
119
115
  if levels.empty?
@@ -122,13 +118,20 @@ class Progress
122
118
  end
123
119
  end
124
120
 
121
+ # set note (will be shown after progress message)
125
122
  def note=(s)
126
123
  if levels.last
127
124
  levels.last.note = s
128
125
  end
129
126
  end
130
127
 
131
- attr_writer :lines, :highlight # :nodoc:
128
+ # output progress as lines (not trying to stay on line)
129
+ # Progress.lines = true
130
+ attr_writer :lines
131
+
132
+ # force highlight
133
+ # Progress.highlight = true
134
+ attr_writer :highlight
132
135
 
133
136
  private
134
137
 
@@ -187,26 +190,23 @@ class Progress
187
190
 
188
191
  def set_title(title)
189
192
  if io_tty?
190
- io.print("\e]0;#{title}\a")
193
+ io.print "\e]0;#{title}\a"
191
194
  end
192
195
  end
193
196
 
194
197
  def print_message(force = false)
195
198
  if force || time_to_print?
196
199
  inner = 0
197
- parts = []
198
- parts_cl = []
199
- levels.reverse.each do |l|
200
- current = l.to_f(inner)
201
- value = current == 0 ? '......' : "#{'%5.1f' % (current * 100.0)}%"
202
- inner = current
203
-
204
- title = l.title ? "#{l.title}: " : ''
205
- highlighted = "\e[1m#{value}\e[0m"
200
+ parts, parts_cl = [], []
201
+ levels.reverse.each do |level|
202
+ inner = current = level.to_f(inner)
203
+ value = current.zero? ? '......' : "#{'%5.1f' % (current * 100.0)}%"
204
+
205
+ title = level.title ? "#{level.title}: " : nil
206
206
  if !highlight? || value == '100.0%'
207
207
  parts << "#{title}#{value}"
208
208
  else
209
- parts << "#{title}#{highlighted}"
209
+ parts << "#{title}\e[1m#{value}\e[0m"
210
210
  end
211
211
  parts_cl << "#{title}#{value}"
212
212
  end
@@ -220,14 +220,13 @@ class Progress
220
220
  message_cl << " - #{note}"
221
221
  end
222
222
 
223
- unless lines?
224
- previous_length = @previous_length || 0
225
- @previous_length = message_cl.length
226
- message << "#{' ' * [previous_length - message_cl.length, 0].max}\r"
223
+ if lines?
224
+ io.puts message
225
+ else
226
+ io << message << "\e[K\r"
227
227
  end
228
228
 
229
- lines? ? io.puts(message) : io.print(message)
230
- set_title(message_cl)
229
+ set_title message_cl
231
230
  end
232
231
  end
233
232
  end
@@ -1,6 +1,7 @@
1
1
  if defined?(ActiveRecord::Base)
2
2
  module ActiveRecord
3
3
  module BatchesWithProgress
4
+ # run `find_each` with progress
4
5
  def find_each_with_progress(options = {})
5
6
  Progress.start(name.tableize, count(options)) do
6
7
  find_each do |model|
@@ -11,6 +12,7 @@ if defined?(ActiveRecord::Base)
11
12
  end
12
13
  end
13
14
 
15
+ # run `find_in_batches` with progress
14
16
  def find_in_batches_with_progress(options = {})
15
17
  Progress.start(name.tableize, count(options)) do
16
18
  find_in_batches do |batch|
@@ -2,32 +2,32 @@ require 'enumerator'
2
2
  require 'progress/with_progress'
3
3
 
4
4
  module Enumerable
5
- # executes any Enumerable method with progress
6
- # note that methods which don't necessarily go through all items (like find or any?) will not show 100%
5
+ # run any Enumerable method with progress
6
+ # methods which don't necessarily go through all items (like find, any? or all?) will not show 100%
7
7
  # ==== Example
8
8
  # [1, 2, 3].with_progress('Numbers').each do |number|
9
- # sleep(number)
9
+ # # code
10
10
  # end
11
11
  # [1, 2, 3].with_progress('Numbers').each_cons(2) do |numbers|
12
- # p numbers
12
+ # # code
13
13
  # end
14
14
  def with_progress(title = nil)
15
15
  Progress::WithProgress.new(self, title)
16
16
  end
17
17
 
18
- # note that Progress.step is called automatically
18
+ # run `each` with progress
19
19
  # ==== Example
20
20
  # [1, 2, 3].each_with_progress('Numbers') do |number|
21
- # sleep(number)
21
+ # # code
22
22
  # end
23
23
  def each_with_progress(title = nil, &block)
24
24
  with_progress(title).each(&block)
25
25
  end
26
26
 
27
- # note that Progress.step is called automatically
27
+ # run `each_with_index` with progress
28
28
  # ==== Example
29
29
  # [1, 2, 3].each_with_index_and_progress('Numbers') do |number, index|
30
- # sleep(number)
30
+ # # code
31
31
  # end
32
32
  def each_with_index_and_progress(title = nil, &block)
33
33
  with_progress(title).each_with_index(&block)
@@ -1,8 +1,7 @@
1
1
  class Integer
2
- # note that Progress.step is called automatically
3
- # ==== Example
2
+ # run `times` with progress
4
3
  # 100.times_with_progress('Numbers') do |number|
5
- # sleep(number)
4
+ # # code
6
5
  # end
7
6
  def times_with_progress(title = nil)
8
7
  Progress.start(title, self) do
data/progress.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{progress}
8
- s.version = "1.1.1"
8
+ s.version = "1.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Boba Fat"]
12
- s.date = %q{2010-12-07}
12
+ s.date = %q{2010-12-09}
13
13
  s.extra_rdoc_files = [
14
14
  "README.rdoc"
15
15
  ]
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: 17
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 1
10
- version: 1.1.1
9
+ - 2
10
+ version: 1.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Boba Fat
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-07 00:00:00 +03:00
18
+ date: 2010-12-09 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency