progress_printer 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +10 -2
- data/CHANGELOG.md +9 -0
- data/README.md +45 -20
- data/lib/progress_printer.rb +45 -12
- data/lib/progress_printer/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 66bac07a17f8eafa553b1384da0c1cfb8c353fac09720d5b6cbb20bc53504370
|
4
|
+
data.tar.gz: 9a510a4a9e62f3f6a81e5d1849df04d2838c1a13543cf9a6b544e105c995b22e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cddf35442374d0b86a579ca5411af75af86ce30f4e81f33fd26fd3e5ee13c14309567664c2bf1686c747bf78b2e80aa3b4f0289fecc4abf85cc86e8fa9ac9fc1
|
7
|
+
data.tar.gz: 6f3268e6e4d93141acf90cf8c33eeabc56030576957b00f11fa92310bb5496195337980e1a88e92cac6f714a03742a138cd4fdede7b5c4033c9a7bb256cd0a5c
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
## 0.2.0
|
2
|
+
|
3
|
+
* Print the total runtime on #finish [[PR 6](https://github.com/justincampbell/progress_printer/pull/6/files)]
|
4
|
+
* Added .wrap/#wrap methods which take blocks [[PR 4](https://github.com/justincampbell/progress_printer/pull/4/files)]
|
5
|
+
|
6
|
+
## 0.1.0
|
7
|
+
|
8
|
+
* Initial release
|
9
|
+
|
data/README.md
CHANGED
@@ -1,43 +1,68 @@
|
|
1
|
-
# ProgressPrinter
|
1
|
+
# ProgressPrinter [![Gem Version](https://badge.fury.io/rb/progress_printer.svg)](https://badge.fury.io/rb/progress_printer) [![Build Status](https://travis-ci.org/justincampbell/progress_printer.svg?branch=master)](https://travis-ci.org/justincampbell/progress_printer)
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
> Logs the progress of an operation, with estimated completion time.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
|
7
|
+
When using [Bundler](https://bundler.io), add this to your project's `Gemfile`:
|
10
8
|
|
11
9
|
```ruby
|
12
10
|
gem 'progress_printer'
|
13
11
|
```
|
14
12
|
|
15
|
-
|
16
|
-
|
17
|
-
$ bundle
|
13
|
+
Otherwise, install it with the `gem` command:
|
18
14
|
|
19
|
-
Or install it yourself as:
|
20
15
|
|
21
|
-
|
16
|
+
```shell
|
17
|
+
$ gem install progress_printer
|
18
|
+
```
|
22
19
|
|
23
20
|
## Usage
|
24
21
|
|
25
|
-
|
22
|
+
### Basic Usage
|
26
23
|
|
27
|
-
|
24
|
+
A `ProgressPrinter` must be created, started, and finished. Use `#increment` within your operation to increment the progress.
|
28
25
|
|
29
|
-
|
26
|
+
```rb
|
27
|
+
require 'progress_printer'
|
30
28
|
|
31
|
-
|
29
|
+
printer = ProgressPrinter.new(name: "Counting", total: 250)
|
30
|
+
printer.start
|
31
|
+
250.times { sleep 0.05; printer.increment }
|
32
|
+
printer.finish
|
33
|
+
```
|
32
34
|
|
33
|
-
|
35
|
+
Output:
|
34
36
|
|
35
|
-
|
37
|
+
```
|
38
|
+
Counting: 0/250 0% calculating...
|
39
|
+
Counting: 100/250 40% ~8s
|
40
|
+
Counting: 200/250 80% ~2s
|
41
|
+
Counting: 250/250 100% 14s total
|
42
|
+
```
|
36
43
|
|
37
|
-
|
44
|
+
You can also achieve the same results by using `.wrap` or `#wrap`:
|
38
45
|
|
39
|
-
|
46
|
+
```rb
|
47
|
+
ProgressPrinter.wrap(name: "Counting", total: 250) do |progress|
|
48
|
+
250.times { sleep 0.05; progress.increment }
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
```rb
|
53
|
+
printer = ProgressPrinter.new(name: "Counting", total: 250)
|
54
|
+
printer.wrap do |progress|
|
55
|
+
250.times { sleep 0.05; progress.increment }
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
### Arguments
|
40
60
|
|
41
|
-
|
61
|
+
* `total` - The total number of iterations expected. If this is omitted, estimated completion time will not be shown.
|
62
|
+
* `name` - A string to display next to each printed line. This helps identify the current operation, or the specific progress printer if using multiple.
|
63
|
+
* `every` (Default: `100`) - How many iterations should pass in between printing a line.
|
64
|
+
* `out` (Default: `$stdout`) - An object responding to `#puts` for printing the progress to.
|
42
65
|
|
43
|
-
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/justincampbell/progress_printer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
data/lib/progress_printer.rb
CHANGED
@@ -12,12 +12,24 @@
|
|
12
12
|
#
|
13
13
|
# Counting: 0/250 0% calculating...
|
14
14
|
# Counting: 100/250 40% ~1m30s
|
15
|
-
# Counting: 200/250 80% ~
|
16
|
-
# Counting: 250/250 100%
|
15
|
+
# Counting: 200/250 80% ~30s
|
16
|
+
# Counting: 250/250 100% 2m30s total
|
17
17
|
#
|
18
18
|
class ProgressPrinter
|
19
19
|
DEFAULT_EVERY = 100
|
20
20
|
|
21
|
+
class << self
|
22
|
+
attr_accessor :silent
|
23
|
+
|
24
|
+
def silence
|
25
|
+
self.silent = true
|
26
|
+
end
|
27
|
+
|
28
|
+
def wrap(*args, &block)
|
29
|
+
new(*args).wrap(&block)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
21
33
|
attr_reader :total, :name, :every, :out
|
22
34
|
attr_accessor :start_time
|
23
35
|
|
@@ -25,7 +37,19 @@ class ProgressPrinter
|
|
25
37
|
@total = total
|
26
38
|
@name = name
|
27
39
|
@every = every
|
28
|
-
|
40
|
+
|
41
|
+
if self.class.silent
|
42
|
+
@out = StringIO.new
|
43
|
+
else
|
44
|
+
@out = out
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def wrap
|
49
|
+
start
|
50
|
+
yield(self)
|
51
|
+
ensure
|
52
|
+
finish
|
29
53
|
end
|
30
54
|
|
31
55
|
def start
|
@@ -40,11 +64,7 @@ class ProgressPrinter
|
|
40
64
|
end
|
41
65
|
|
42
66
|
def finish
|
43
|
-
|
44
|
-
print_progress total
|
45
|
-
else
|
46
|
-
print_progress current unless at_milestone?
|
47
|
-
end
|
67
|
+
print_progress current, final: true
|
48
68
|
end
|
49
69
|
|
50
70
|
def percent_complete
|
@@ -75,8 +95,12 @@ class ProgressPrinter
|
|
75
95
|
return if percent_remaining == 1.0
|
76
96
|
return 0.0 if percent_remaining == 0.0
|
77
97
|
|
78
|
-
time_passed
|
79
|
-
|
98
|
+
time_passed(now) / percent_complete * (1.0 - percent_complete)
|
99
|
+
end
|
100
|
+
|
101
|
+
def time_passed(now = Time.now)
|
102
|
+
return unless start_time
|
103
|
+
now - start_time
|
80
104
|
end
|
81
105
|
|
82
106
|
def current
|
@@ -93,7 +117,7 @@ class ProgressPrinter
|
|
93
117
|
@enum ||= (1..Float::INFINITY).enum_for
|
94
118
|
end
|
95
119
|
|
96
|
-
def print_progress(n)
|
120
|
+
def print_progress(n, final: false)
|
97
121
|
buffer = StringIO.new
|
98
122
|
|
99
123
|
buffer.print "#{name}: " if name
|
@@ -102,11 +126,20 @@ class ProgressPrinter
|
|
102
126
|
buffer.print left_pad(n, total_length)
|
103
127
|
buffer.print "/#{total} "
|
104
128
|
buffer.print left_pad(percent_complete_string, 4)
|
105
|
-
buffer.print " #{estimated_time_remaining}"
|
106
129
|
else
|
107
130
|
buffer.print n
|
108
131
|
end
|
109
132
|
|
133
|
+
if final
|
134
|
+
if time_passed
|
135
|
+
buffer.print " #{self.class.format_duration(time_passed)} total"
|
136
|
+
end
|
137
|
+
else
|
138
|
+
if total
|
139
|
+
buffer.print " #{estimated_time_remaining}"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
110
143
|
out.puts buffer.string
|
111
144
|
end
|
112
145
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: progress_printer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Campbell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- ".gitignore"
|
63
63
|
- ".rspec"
|
64
64
|
- ".travis.yml"
|
65
|
+
- CHANGELOG.md
|
65
66
|
- CODE_OF_CONDUCT.md
|
66
67
|
- Gemfile
|
67
68
|
- LICENSE.txt
|
@@ -92,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
93
|
version: '0'
|
93
94
|
requirements: []
|
94
95
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.7.3
|
96
97
|
signing_key:
|
97
98
|
specification_version: 4
|
98
99
|
summary: Logs the progress of an operation, with estimated completion time.
|