progress 2.4.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -6,6 +6,7 @@
6
6
  /.yardoc/
7
7
  /coverage/
8
8
 
9
+ Gemfile.lock
9
10
  Makefile
10
11
  *.o
11
12
  *.bundle
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - jruby-18mode
8
+ - jruby-19mode
9
+ - rbx-18mode
10
+ - rbx-19mode
11
+ - ree
12
+ script: "bundle exec rspec"
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010-2011 Ivan Kuchin
1
+ Copyright (c) 2010-2013 Ivan Kuchin
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
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 1, 2 do
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 1, 2 do
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 10 times faster than C:
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, 11 do
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 10, 11 do
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-2011 Ivan Kuchin. See LICENSE.txt for details.
136
+ Copyright (c) 2010-2013 Ivan Kuchin. See LICENSE.txt for details.
@@ -1,33 +1,31 @@
1
1
  require 'progress'
2
2
 
3
- if defined?(ActiveRecord::Base)
4
- module ActiveRecord
5
- module BatchesWithProgress
6
- # run `find_each` with progress
7
- def find_each_with_progress(options = {})
8
- Progress.start(name.tableize, count(options)) do
9
- find_each do |model|
10
- Progress.step do
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
- # run `find_in_batches` with progress
18
- def find_in_batches_with_progress(options = {})
19
- Progress.start(name.tableize, count(options)) do
20
- find_in_batches do |batch|
21
- Progress.step batch.length do
22
- yield batch
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
- class Base
30
- extend BatchesWithProgress
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
@@ -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, length = case
20
+ enumerable = case
19
21
  when @length
20
- [@enumerable, @length]
21
- when !@enumerable.respond_to?(:length) || @enumerable.is_a?(String) || (defined?(StringIO) && @enumerable.is_a?(StringIO)) || (defined?(TempFile) && @enumerable.is_a?(TempFile))
22
- elements = @enumerable.each.to_a
23
- [elements, elements.length]
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
- [@enumerable, @enumerable.length]
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