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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d36d6773874ac74b24ec824a6b9255fb9a6015df
4
- data.tar.gz: 83bca370e9059c05fb447ebe9999d4e4ab61996d
2
+ SHA256:
3
+ metadata.gz: 66bac07a17f8eafa553b1384da0c1cfb8c353fac09720d5b6cbb20bc53504370
4
+ data.tar.gz: 9a510a4a9e62f3f6a81e5d1849df04d2838c1a13543cf9a6b544e105c995b22e
5
5
  SHA512:
6
- metadata.gz: cd946f471c49b66bd5a591abf166cfe5636954bc6415ea3d0b811fb7a2a46b64cf623a95071dcbdb1f0e5a696bdf3895f8f2abdb461a7ba894cd84332fd06455
7
- data.tar.gz: c9b07528d3ff8ea35809cfe7856a24b89f77bc9ae0d654495e07349f84c68a0265dd518dc388f7124f2d7c321a3b7aba8d1048f9dacdc28839b10a3b5dc536c2
6
+ metadata.gz: cddf35442374d0b86a579ca5411af75af86ce30f4e81f33fd26fd3e5ee13c14309567664c2bf1686c747bf78b2e80aa3b4f0289fecc4abf85cc86e8fa9ac9fc1
7
+ data.tar.gz: 6f3268e6e4d93141acf90cf8c33eeabc56030576957b00f11fa92310bb5496195337980e1a88e92cac6f714a03742a138cd4fdede7b5c4033c9a7bb256cd0a5c
data/.travis.yml CHANGED
@@ -1,5 +1,13 @@
1
1
  sudo: false
2
+
2
3
  language: ruby
4
+
3
5
  rvm:
4
- - 2.4.2
5
- before_install: gem install bundler -v 1.15.4
6
+ - 2.2
7
+ - 2.3
8
+ - 2.4
9
+ - ruby-head
10
+
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: ruby-head
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
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/progress_printer`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- Add this line to your application's Gemfile:
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
- And then execute:
16
-
17
- $ bundle
13
+ Otherwise, install it with the `gem` command:
18
14
 
19
- Or install it yourself as:
20
15
 
21
- $ gem install progress_printer
16
+ ```shell
17
+ $ gem install progress_printer
18
+ ```
22
19
 
23
20
  ## Usage
24
21
 
25
- TODO: Write usage instructions here
22
+ ### Basic Usage
26
23
 
27
- ## Development
24
+ A `ProgressPrinter` must be created, started, and finished. Use `#increment` within your operation to increment the progress.
28
25
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+ ```rb
27
+ require 'progress_printer'
30
28
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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
- ## Contributing
35
+ Output:
34
36
 
35
- 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.
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
- ## License
44
+ You can also achieve the same results by using `.wrap` or `#wrap`:
38
45
 
39
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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
- ## Code of Conduct
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
- Everyone interacting in the ProgressPrinter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/justincampbell/progress_printer/blob/master/CODE_OF_CONDUCT.md).
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.
@@ -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% ~45s
16
- # Counting: 250/250 100% ~0s
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
- @out = out
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
- if total
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 = now - start_time
79
- time_passed / percent_complete * (1.0 - percent_complete)
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
 
@@ -1,3 +1,3 @@
1
1
  class ProgressPrinter
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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: 2017-10-13 00:00:00.000000000 Z
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.6.14
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.