progress_printer 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/test.yml +32 -0
- data/CHANGELOG.md +13 -0
- data/README.md +50 -19
- data/lib/progress_printer/version.rb +1 -1
- data/lib/progress_printer.rb +45 -12
- data/progress_printer.gemspec +2 -3
- metadata +12 -26
- data/.travis.yml +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5d3d46c39de72ccdae5d17e0b344766ba6531f3b1e0850b1d35a7f8c31f7e499
|
4
|
+
data.tar.gz: 4cdfccdd8ea9c360e006f8006770b6b5dc3fad34b68f427b9a4fe020a610fcfc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2991c9ee3edbf0299680ab3736fcde1b7963d56b99f40d18398da883304966229c4304d4a497e04de602e337ae03266775a9dc787aa22ca61762e5a380fdf52
|
7
|
+
data.tar.gz: d3679db624d9eabd39314239d1ef558e7cf56b6521438d1830ffb067c8c15ebf5e0ec84b9f0d7ce43f09437351ab347a9ba20f120f0f94b1b8ae145d262bbe78
|
@@ -0,0 +1,32 @@
|
|
1
|
+
name: Test
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
pull_request:
|
8
|
+
|
9
|
+
permissions:
|
10
|
+
contents: read
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
rspec:
|
14
|
+
runs-on: ubuntu-latest
|
15
|
+
strategy:
|
16
|
+
matrix:
|
17
|
+
ruby-version:
|
18
|
+
- "2.7"
|
19
|
+
- "3.0"
|
20
|
+
- "3.1"
|
21
|
+
- "3.2"
|
22
|
+
steps:
|
23
|
+
- uses: actions/checkout@v3
|
24
|
+
|
25
|
+
- name: Set up Ruby
|
26
|
+
uses: ruby/setup-ruby@v1
|
27
|
+
with:
|
28
|
+
ruby-version: ${{ matrix.ruby-version }}
|
29
|
+
bundler-cache: true
|
30
|
+
|
31
|
+
- name: Run tests
|
32
|
+
run: bundle exec rake spec
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
## 0.2.1
|
2
|
+
|
3
|
+
* Fix ProgressPrinter.wrap error on newer Ruby versions [[50d763f](https://github.com/justincampbell/progress_printer/commit/50d763f)]
|
4
|
+
|
5
|
+
## 0.2.0
|
6
|
+
|
7
|
+
* Print the total runtime on #finish [[PR 6](https://github.com/justincampbell/progress_printer/pull/6/files)]
|
8
|
+
* Added .wrap/#wrap methods which take blocks [[PR 4](https://github.com/justincampbell/progress_printer/pull/4/files)]
|
9
|
+
|
10
|
+
## 0.1.0
|
11
|
+
|
12
|
+
* Initial release
|
13
|
+
|
data/README.md
CHANGED
@@ -1,43 +1,74 @@
|
|
1
|
-
# ProgressPrinter
|
1
|
+
# ProgressPrinter [![Gem Version](https://badge.fury.io/rb/progress_printer.svg)](https://badge.fury.io/rb/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
|
-
|
13
|
+
Otherwise, install it with the `gem` command:
|
14
|
+
|
15
|
+
|
16
|
+
```shell
|
17
|
+
$ gem install progress_printer
|
18
|
+
```
|
16
19
|
|
17
|
-
|
20
|
+
### Already in a console?
|
18
21
|
|
19
|
-
|
22
|
+
ProgressPrinter is in a single file with no dependencies, so you're able to copy/paste the entire file into an `irb` session, and then immediately use `ProgressPrinter.new` (see below). The raw source to copy is available here:
|
20
23
|
|
21
|
-
|
24
|
+
[https://raw.githubusercontent.com/justincampbell/progress_printer/master/lib/progress_printer.rb](https://raw.githubusercontent.com/justincampbell/progress_printer/master/lib/progress_printer.rb)
|
22
25
|
|
23
26
|
## Usage
|
24
27
|
|
25
|
-
|
28
|
+
### Basic Usage
|
26
29
|
|
27
|
-
|
30
|
+
A `ProgressPrinter` must be created, started, and finished. Use `#increment` within your operation to increment the progress.
|
28
31
|
|
29
|
-
|
32
|
+
```rb
|
33
|
+
require 'progress_printer'
|
30
34
|
|
31
|
-
|
35
|
+
printer = ProgressPrinter.new(name: "Counting", total: 250)
|
36
|
+
printer.start
|
37
|
+
250.times { sleep 0.05; printer.increment }
|
38
|
+
printer.finish
|
39
|
+
```
|
32
40
|
|
33
|
-
|
41
|
+
Output:
|
34
42
|
|
35
|
-
|
43
|
+
```
|
44
|
+
Counting: 0/250 0% calculating...
|
45
|
+
Counting: 100/250 40% ~8s
|
46
|
+
Counting: 200/250 80% ~2s
|
47
|
+
Counting: 250/250 100% 14s total
|
48
|
+
```
|
49
|
+
|
50
|
+
You can also achieve the same results by using `.wrap` or `#wrap`:
|
51
|
+
|
52
|
+
```rb
|
53
|
+
ProgressPrinter.wrap(name: "Counting", total: 250) do |progress|
|
54
|
+
250.times { sleep 0.05; progress.increment }
|
55
|
+
end
|
56
|
+
```
|
36
57
|
|
37
|
-
|
58
|
+
```rb
|
59
|
+
printer = ProgressPrinter.new(name: "Counting", total: 250)
|
60
|
+
printer.wrap do |progress|
|
61
|
+
250.times { sleep 0.05; progress.increment }
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
### Arguments
|
38
66
|
|
39
|
-
The
|
67
|
+
* `total` - The total number of iterations expected. If this is omitted, estimated completion time will not be shown.
|
68
|
+
* `name` - A string to display next to each printed line. This helps identify the current operation, or the specific progress printer if using multiple.
|
69
|
+
* `every` (Default: `100`) - How many iterations should pass in between printing a line.
|
70
|
+
* `out` (Default: `$stdout`) - An object responding to `#puts` for printing the progress to.
|
40
71
|
|
41
|
-
##
|
72
|
+
## Contributing
|
42
73
|
|
43
|
-
|
74
|
+
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
|
|
data/progress_printer.gemspec
CHANGED
@@ -19,7 +19,6 @@ Gem::Specification.new do |spec|
|
|
19
19
|
end
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_development_dependency "
|
23
|
-
spec.add_development_dependency "
|
24
|
-
spec.add_development_dependency "rspec", "~> 3.0"
|
22
|
+
spec.add_development_dependency "rake", "~> 13.0.6"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.12.0"
|
25
24
|
end
|
metadata
CHANGED
@@ -1,57 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: progress_printer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Campbell
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.15'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.15'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rake
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
30
16
|
requirements:
|
31
17
|
- - "~>"
|
32
18
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
19
|
+
version: 13.0.6
|
34
20
|
type: :development
|
35
21
|
prerelease: false
|
36
22
|
version_requirements: !ruby/object:Gem::Requirement
|
37
23
|
requirements:
|
38
24
|
- - "~>"
|
39
25
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
26
|
+
version: 13.0.6
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: rspec
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
44
30
|
requirements:
|
45
31
|
- - "~>"
|
46
32
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
33
|
+
version: 3.12.0
|
48
34
|
type: :development
|
49
35
|
prerelease: false
|
50
36
|
version_requirements: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
38
|
- - "~>"
|
53
39
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
40
|
+
version: 3.12.0
|
55
41
|
description: Logs the progress of an operation, with estimated completion time.
|
56
42
|
email:
|
57
43
|
- justin@justincampbell.me
|
@@ -59,9 +45,10 @@ executables: []
|
|
59
45
|
extensions: []
|
60
46
|
extra_rdoc_files: []
|
61
47
|
files:
|
48
|
+
- ".github/workflows/test.yml"
|
62
49
|
- ".gitignore"
|
63
50
|
- ".rspec"
|
64
|
-
-
|
51
|
+
- CHANGELOG.md
|
65
52
|
- CODE_OF_CONDUCT.md
|
66
53
|
- Gemfile
|
67
54
|
- LICENSE.txt
|
@@ -76,7 +63,7 @@ homepage: https://github.com/justincampbell/progress_printer
|
|
76
63
|
licenses:
|
77
64
|
- MIT
|
78
65
|
metadata: {}
|
79
|
-
post_install_message:
|
66
|
+
post_install_message:
|
80
67
|
rdoc_options: []
|
81
68
|
require_paths:
|
82
69
|
- lib
|
@@ -91,9 +78,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
78
|
- !ruby/object:Gem::Version
|
92
79
|
version: '0'
|
93
80
|
requirements: []
|
94
|
-
|
95
|
-
|
96
|
-
signing_key:
|
81
|
+
rubygems_version: 3.3.7
|
82
|
+
signing_key:
|
97
83
|
specification_version: 4
|
98
84
|
summary: Logs the progress of an operation, with estimated completion time.
|
99
85
|
test_files: []
|