progress 2.4.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +1 -0
- data/.travis.yml +12 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +1 -1
- data/README.markdown +8 -6
- data/lib/progress/active_record.rb +18 -20
- data/lib/progress/beeper.rb +24 -0
- data/lib/progress/eta.rb +48 -0
- data/lib/progress/with_progress.rb +42 -7
- data/lib/progress.rb +163 -151
- data/progress.gemspec +1 -1
- data/spec/progress_spec.rb +281 -240
- metadata +39 -57
- data/spec/spec_helper.rb +0 -3
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OWNkMGE2NDNkOTU0NmM5NzVlN2JkNWQ5YjdiMjY2ZjJlZWRjMTA5ZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MmYyYTQ1NGFiNzliM2VhNzE1OWI2OGE0OWZkOWVmMDI2ZjMzNmRmYg==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NWQ5ODQyNTg5YTg4MDhjZGI1Y2EzZmQ1ZTA2NGMzNzRmNzg0NWFjOTJiZDdk
|
10
|
+
OTZkYzIzMDIwMzIxZjhlNTQ1M2M5MDAyOGQ3NmQ5YTM1ZWIxODIxYmQyZDNh
|
11
|
+
OGUwNDlmOWE3MTNlMjAwYmEzN2EyNTEyZjA0YTNjMzVkNGJkNTc=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NTJmNjY4YzJlMmVlYzM2MDMyNjk4NDNjMjFkYTIyMmFiNTQ5NDdiMTkzYTg3
|
14
|
+
ODY4MmU5MTNlMGFjZGViZjhiNGZjOGZjMzBiYTM4MDYzODE3MTY3ZGVkNjJm
|
15
|
+
ZTFjZmMxZTI3ZTUwZDI2ZTQ1NTQ5MjdlZjVkNDI1NjY0NGJjNzM=
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
CHANGED
data/README.markdown
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Show progress during console script run.
|
4
4
|
|
5
|
+
[![Build Status](https://travis-ci.org/toy/progress.png?branch=master)](https://travis-ci.org/toy/progress)
|
6
|
+
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
gem install progress
|
@@ -102,27 +104,27 @@ NOTE: you will get WRONG progress if you use something like this:
|
|
102
104
|
But you can use this:
|
103
105
|
|
104
106
|
10.times_with_progress('A') do |time|
|
105
|
-
Progress.step
|
107
|
+
Progress.step 5 do
|
106
108
|
10.times_with_progress('B') do
|
107
109
|
# code
|
108
110
|
end
|
109
111
|
end
|
110
|
-
Progress.step
|
112
|
+
Progress.step 5 do
|
111
113
|
10.times_with_progress('C') do
|
112
114
|
# code
|
113
115
|
end
|
114
116
|
end
|
115
117
|
end
|
116
118
|
|
117
|
-
Or if you know that B runs
|
119
|
+
Or if you know that B runs 9 times faster than C:
|
118
120
|
|
119
121
|
10.times_with_progress('A') do |time|
|
120
|
-
Progress.step 1
|
122
|
+
Progress.step 1 do
|
121
123
|
10.times_with_progress('B') do
|
122
124
|
# code
|
123
125
|
end
|
124
126
|
end
|
125
|
-
Progress.step
|
127
|
+
Progress.step 9 do
|
126
128
|
10.times_with_progress('C') do
|
127
129
|
# code
|
128
130
|
end
|
@@ -131,4 +133,4 @@ Or if you know that B runs 10 times faster than C:
|
|
131
133
|
|
132
134
|
## Copyright
|
133
135
|
|
134
|
-
Copyright (c) 2010-
|
136
|
+
Copyright (c) 2010-2013 Ivan Kuchin. See LICENSE.txt for details.
|
@@ -1,33 +1,31 @@
|
|
1
1
|
require 'progress'
|
2
2
|
|
3
|
-
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
yield model
|
12
|
-
end
|
3
|
+
module ActiveRecord
|
4
|
+
module BatchesWithProgress
|
5
|
+
# run `find_each` with progress
|
6
|
+
def find_each_with_progress(options = {})
|
7
|
+
Progress.start(name.tableize, count(options)) do
|
8
|
+
find_each do |model|
|
9
|
+
Progress.step do
|
10
|
+
yield model
|
13
11
|
end
|
14
12
|
end
|
15
13
|
end
|
14
|
+
end
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
16
|
+
# run `find_in_batches` with progress
|
17
|
+
def find_in_batches_with_progress(options = {})
|
18
|
+
Progress.start(name.tableize, count(options)) do
|
19
|
+
find_in_batches do |batch|
|
20
|
+
Progress.step batch.length do
|
21
|
+
yield batch
|
24
22
|
end
|
25
23
|
end
|
26
24
|
end
|
27
25
|
end
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
end
|
28
|
+
class Base
|
29
|
+
extend BatchesWithProgress
|
32
30
|
end
|
33
31
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Progress
|
2
|
+
class Beeper
|
3
|
+
class Restart < RuntimeError; end
|
4
|
+
|
5
|
+
def initialize(time, &block)
|
6
|
+
@thread = Thread.new do
|
7
|
+
begin
|
8
|
+
sleep time
|
9
|
+
block.call
|
10
|
+
rescue Restart
|
11
|
+
end
|
12
|
+
redo
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def restart
|
17
|
+
@thread.raise Restart
|
18
|
+
end
|
19
|
+
|
20
|
+
def stop
|
21
|
+
@thread.kill
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/progress/eta.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
class Progress
|
2
|
+
class Eta
|
3
|
+
def initialize
|
4
|
+
@started_at = Time.now
|
5
|
+
end
|
6
|
+
|
7
|
+
def left(completed)
|
8
|
+
seconds = seconds_left(completed)
|
9
|
+
if seconds && seconds > 0
|
10
|
+
seconds_to_string(seconds)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def elapsed
|
15
|
+
seconds_to_string(Time.now - @started_at)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def seconds_to_string(seconds)
|
21
|
+
if seconds
|
22
|
+
case seconds
|
23
|
+
when 0...60
|
24
|
+
'%.0fs' % seconds
|
25
|
+
when 60...3600
|
26
|
+
'%.1fm' % (seconds / 60)
|
27
|
+
when 3600...86400
|
28
|
+
'%.1fh' % (seconds / 3600)
|
29
|
+
else
|
30
|
+
'%.1fd' % (seconds / 86400)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def seconds_left(completed)
|
36
|
+
now = Time.now
|
37
|
+
if completed > 0 && now - @started_at >= 1
|
38
|
+
current_eta = @started_at + (now - @started_at) / completed
|
39
|
+
@left = if @left
|
40
|
+
@left + (current_eta - @left) * (1 + completed) * 0.5
|
41
|
+
else
|
42
|
+
current_eta
|
43
|
+
end
|
44
|
+
@left - now
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'progress'
|
2
|
+
require 'delegate'
|
2
3
|
|
3
4
|
class Progress
|
4
|
-
class WithProgress
|
5
|
+
class WithProgress < Delegator
|
5
6
|
include Enumerable
|
6
7
|
|
7
8
|
attr_reader :enumerable, :title
|
@@ -9,20 +10,36 @@ class Progress
|
|
9
10
|
# initialize with object responding to each, title and optional length
|
10
11
|
# if block is provided, it is passed to each
|
11
12
|
def initialize(enumerable, title, length = nil, &block)
|
13
|
+
super(enumerable)
|
12
14
|
@enumerable, @title, @length = enumerable, title, length
|
13
15
|
each(&block) if block
|
14
16
|
end
|
15
17
|
|
16
18
|
# each object with progress
|
17
19
|
def each
|
18
|
-
enumerable
|
20
|
+
enumerable = case
|
19
21
|
when @length
|
20
|
-
|
21
|
-
when
|
22
|
-
|
23
|
-
|
22
|
+
@enumerable
|
23
|
+
when
|
24
|
+
@enumerable.is_a?(String),
|
25
|
+
@enumerable.is_a?(IO),
|
26
|
+
Object.const_defined?(:StringIO) && @enumerable.is_a?(StringIO),
|
27
|
+
Object.const_defined?(:TempFile) && @enumerable.is_a?(TempFile)
|
28
|
+
warn "Progress: collecting elements for instance of class #{@enumerable.class}"
|
29
|
+
@enumerable.each.to_a
|
24
30
|
else
|
25
|
-
|
31
|
+
@enumerable
|
32
|
+
end
|
33
|
+
|
34
|
+
length = case
|
35
|
+
when @length
|
36
|
+
@length
|
37
|
+
when enumerable.respond_to?(:size)
|
38
|
+
enumerable.size
|
39
|
+
when enumerable.respond_to?(:length)
|
40
|
+
enumerable.length
|
41
|
+
else
|
42
|
+
enumerable.count
|
26
43
|
end
|
27
44
|
|
28
45
|
Progress.start(@title, length) do
|
@@ -39,5 +56,23 @@ class Progress
|
|
39
56
|
def with_progress(title = nil, length = nil, &block)
|
40
57
|
self.class.new(@enumerable, title, length || @length, &block)
|
41
58
|
end
|
59
|
+
|
60
|
+
# befriend with in_threads gem
|
61
|
+
def in_threads(*args, &block)
|
62
|
+
@enumerable.in_threads(*args).with_progress(@title, @length, &block)
|
63
|
+
rescue
|
64
|
+
super
|
65
|
+
end
|
66
|
+
|
67
|
+
protected
|
68
|
+
|
69
|
+
def __getobj__
|
70
|
+
@enumerable
|
71
|
+
end
|
72
|
+
|
73
|
+
def __setobj__(obj)
|
74
|
+
@enumerable = obj
|
75
|
+
end
|
76
|
+
|
42
77
|
end
|
43
78
|
end
|