progress 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,42 +1,73 @@
1
1
  # progress
2
2
 
3
- http://github.com/toy/progress/tree/master
3
+ Show progress during console script run.
4
4
 
5
- ## DESCRIPTION:
5
+ ## Installation:
6
6
 
7
- Class to show progress during console script run
7
+ gem install progress
8
8
 
9
9
  ## SYNOPSIS:
10
10
 
11
- 1000.times_with_progress('Wait') do |time| # title is optional
12
- puts time
11
+ 1000.times_with_progress('Counting to 1000') do |i|
12
+ # do something with i
13
13
  end
14
14
 
15
- [1, 2, 3].with_progress('Wait').each do |i|
16
- puts i
15
+ or without title:
16
+
17
+ 1000.times_with_progress do |i|
18
+ # do something with i
17
19
  end
18
20
 
19
- (1..100).with_progress('Wait').each do |i|
20
- puts i
21
+ With array:
22
+
23
+ [1, 2, 3].with_progress('1…2…3').each do |i|
24
+ # still counting
21
25
  end
22
26
 
23
- {
24
- :a => 'a',
25
- :b => 'b',
26
- :c => 'c',
27
- :d => 'd',
28
- }.with_progress('Wait').each do |k, v|
29
- puts "#{k} => #{v}"
27
+ `.each` is optional:
28
+
29
+ [1, 2, 3].with_progress('1…2…3') do |i|
30
+ # =||=
30
31
  end
31
32
 
33
+ Nested progress
34
+
32
35
  (1..10).with_progress('Outer').map do |a|
33
36
  (1..10).with_progress('Middle').map do |b|
34
37
  (1..10).with_progress('Inner').map do |c|
35
- [a, b, c]
38
+ # do something with a, b and c
36
39
  end
37
40
  end
38
41
  end
39
42
 
43
+ You can also show note:
44
+
45
+ [1, 2, 3].with_progress do |i|
46
+ Progress.note = i
47
+ sleep 5
48
+ end
49
+
50
+ You can use any enumerable method:
51
+
52
+ [1, 2, 3].with_progress.map{ |i| 'do stuff' }
53
+ [1, 2, 3].with_progress.each_cons(3){ |i| 'do stuff' }
54
+ [1, 2, 3].with_progress.each_slice(2){ |i| 'do stuff' }
55
+ # …
56
+
57
+ Any enumerable will work:
58
+
59
+ (1..100).with_progress('Wait') do |i|
60
+ # ranges are good
61
+ end
62
+
63
+ Dir.new('.').with_progress do |path|
64
+ # check path
65
+ end
66
+
67
+ NOTE: progress gets number of objects using `length`, `size`, `to_a.length` or just `inject` and if used on objects which needs rewind (like opened File), cycle itself will not work.
68
+
69
+ Use simple blocks:
70
+
40
71
  symbols = []
41
72
  Progress.start('Input 100 symbols', 100) do
42
73
  while symbols.length < 100
@@ -57,7 +88,7 @@ or just
57
88
  end
58
89
  end
59
90
 
60
- Note - you will get WRONG progress if you use something like this:
91
+ NOTE: you will get WRONG progress if you use something like this:
61
92
 
62
93
  10.times_with_progress('A') do |time|
63
94
  10.times_with_progress('B') do
@@ -98,22 +129,6 @@ Or if you know that B runs 10 times faster than C:
98
129
  end
99
130
  end
100
131
 
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
-
109
- ## REQUIREMENTS:
110
-
111
- ruby )))
112
-
113
- ## INSTALL:
114
-
115
- sudo gem install progress
116
-
117
132
  ## Copyright
118
133
 
119
134
  Copyright (c) 2010-2011 Ivan Kuchin. See LICENSE.txt for details.
@@ -4,11 +4,14 @@ class Progress
4
4
  class WithProgress
5
5
  include Enumerable
6
6
 
7
- def initialize(enumerable, title, &block)
8
- @enumerable, @title = enumerable, title
7
+ # initialize with object responding to each, title and optional length
8
+ # if block is provided, it is passed to each
9
+ def initialize(enumerable, title, length = nil, &block)
10
+ @enumerable, @title, @length = enumerable, title, length
9
11
  each(&block) if block
10
12
  end
11
13
 
14
+ # each object with progress
12
15
  def each
13
16
  Progress.start(@title, length) do
14
17
  @enumerable.each do |object|
@@ -19,6 +22,7 @@ class Progress
19
22
  end
20
23
  end
21
24
 
25
+ # determine number of objects
22
26
  def length
23
27
  @length ||= if @enumerable.respond_to?(:length) && !@enumerable.is_a?(String)
24
28
  @enumerable.length
@@ -31,7 +35,9 @@ class Progress
31
35
  end
32
36
  end
33
37
 
38
+ # returns self but changes title
34
39
  def with_progress(title = nil)
40
+ @title = title
35
41
  self
36
42
  end
37
43
  end
data/progress.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'progress'
5
- s.version = '2.0.0'
5
+ s.version = '2.1.0'
6
6
  s.summary = %q{Show progress of long running tasks}
7
7
  s.homepage = "http://github.com/toy/#{s.name}"
8
8
  s.authors = ['Ivan Kuchin']
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: 15
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 2.0.0
10
+ version: 2.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ivan Kuchin
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-28 00:00:00 Z
18
+ date: 2011-11-29 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec