popro 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 953431a63b91d3bb07e70cf638262c74dbbca5ced53020b5f6ba4b0b87b4a19f
4
- data.tar.gz: 9658ac180fa9259438c95ab264c19d0588da7c959187ddbf169ea130f639296d
3
+ metadata.gz: 28d2afab9d53f8fa8ebf80ec36ee2d0e1529d150a6ffd2993409ba845a3f4c79
4
+ data.tar.gz: 858dbf273be5fe62af6f0c74fbcfe51fd34f6491b344381f40fab71a053c50a1
5
5
  SHA512:
6
- metadata.gz: f04c243ad12db54726a87e17527d031adcea1f8229d8e815768c5e9416132c9c14b65e4ba324e0733bd15fb7a8773942499caa9aafa8566f97b7d39848f43091
7
- data.tar.gz: a6c71eec92cc09a69742cb4c9dba2fec469595804589a12d468d5093ae8b3a7d316aad639b0091ead3f0f5fa7bea694cf6dcb996b3ded7922e79d9762ef75916
6
+ metadata.gz: 337b2b031a41c3ba78e438b906fac942b98eaacff1e0c489d01e4811b5f676760cc8e0b9f2d6fbab27c1ec344c5f42d6c677546ce7fb43d52a17f89d0d38ac98
7
+ data.tar.gz: 5d3b42ff7fb6ee26873afc35217daeb43b9f431f05e251c59e3b112ab13f14481fc0f0d108d3f3cb53f3c458a531af5ca55c5821f058d5941949bc59a0eeae51
data/CHANGELOG CHANGED
@@ -1,2 +1,3 @@
1
+ 0.2.1 add support for estimation and re-use of default indicator with added concatted formatters
1
2
  0.2.0 various fixes and added unit tests
2
3
  0.0.1 initial version
@@ -7,7 +7,7 @@ module Popro
7
7
 
8
8
  require_relative 'popro/progress'
9
9
 
10
- def self.new(total=0, **options, &block)
10
+ def self.new(total = 0, **options, &block)
11
11
  raise ConfigError, 'using :total is not supported in new' if options.key?(:total) && (options[:total] != total)
12
12
 
13
13
  options[:total] = total
@@ -54,7 +54,7 @@ module Popro
54
54
  def did(yielded = nil, amount = nil)
55
55
  @info.start unless @info.running?
56
56
  amount = @step if amount.nil?
57
- raise TypeError('amount: expected an integer') unless amount.is_a? Integer
57
+ raise TypeError, "amount: expected an integer, got #{amount.class}" unless amount.is_a? Integer
58
58
 
59
59
  @info.current += amount unless amount.zero?
60
60
  @indicator.call(@info, yielded)
@@ -81,7 +81,7 @@ module Popro
81
81
 
82
82
  def _each(obj, total = nil, &block)
83
83
  total = obj.size if total.nil?
84
- raise TypeError('total: expected an integer') unless total.is_a?(Integer) || total.nil?
84
+ raise TypeError, "total: expected an integer got #{total.class}" unless total.is_a?(Integer) || total.nil?
85
85
 
86
86
  @info.total += total if total.positive?
87
87
  # block = proc { |d| d } unless block_given?
@@ -30,7 +30,7 @@ module Popro
30
30
  def call(info, *args)
31
31
  result = @formatter.call(info, *args)
32
32
  @longest = [@longest, result.size].max
33
- "\r" + result.ljust(@longest, ' ')
33
+ "\r#{result.ljust(@longest, ' ')}"
34
34
  end
35
35
  end
36
36
 
@@ -43,6 +43,53 @@ module Popro
43
43
  end
44
44
  end
45
45
 
46
+ class Estimate
47
+ # TODO: cleaner implementation/formatstring
48
+ attr_reader :info
49
+
50
+ def initialize
51
+ @start_time = current_time
52
+ @info = nil
53
+ end
54
+
55
+ def call(info, *_args)
56
+ @info = info
57
+
58
+ [
59
+ "estimated time left: #{format_duration(estimated_left)}",
60
+ "[#{format_duration(elapsed)}/#{format_duration(estimated_total)}]"
61
+ ].join(', ')
62
+ end
63
+
64
+ private
65
+
66
+ def elapsed
67
+ current_time - @start_time
68
+ end
69
+
70
+ def estimated_total
71
+ return nil if info.current.zero? || info.total.zero?
72
+
73
+ elapsed + (info.total / info.current) * elapsed
74
+ end
75
+
76
+ def estimated_left
77
+ return nil if info.current.zero? || info.total.zero?
78
+
79
+ (info.total / info.current) * elapsed
80
+ end
81
+
82
+ def format_duration(secs)
83
+ return '??d??h??m??s' if secs.nil?
84
+
85
+ format('%02dd%02dh%02dm%02ds', secs / 86_400, secs / 3600 % 24, secs / 60 % 60, secs % 60)
86
+ end
87
+
88
+ def current_time
89
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
90
+ end
91
+ end
92
+
46
93
  class Sprintf
47
94
  def initialize(format_string = nil)
48
95
  @format_string = format_string
@@ -27,7 +27,7 @@ module Popro
27
27
  formatter = self.class.default_formatter(formatter) if formatter.nil? || formatter.is_a?(String)
28
28
 
29
29
  @formatter = formatter
30
- @stream = stream || STDOUT
30
+ @stream = stream || $stdout
31
31
  end
32
32
 
33
33
  def call(*args)
@@ -45,11 +45,12 @@ module Popro
45
45
  end
46
46
  end
47
47
 
48
- def self.default_formatter
48
+ def self.default_formatter(*extra_formatters)
49
49
  ::Popro::Formatter::RewriteLine.new(
50
50
  ::Popro::Formatter::Concat.new(
51
51
  ::Popro::Formatter::Spinner.new(:dots, bounce: true),
52
52
  ::Popro::Formatter::Sprintf.new,
53
+ *extra_formatters,
53
54
  (proc do |_, yielded = nil|
54
55
  yielded if yielded.is_a?(String) || yielded.is_a?(Numeric)
55
56
  end),
@@ -58,8 +59,8 @@ module Popro
58
59
  )
59
60
  end
60
61
 
61
- def self.default
62
- Stream.new(formatter: default_formatter)
62
+ def self.default(*extra_formatters)
63
+ Stream.new(formatter: default_formatter(*extra_formatters))
63
64
  end
64
65
  end
65
66
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: popro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - MikeSmithEU
@@ -39,7 +39,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
39
39
  requirements:
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
- version: '0'
42
+ version: '2.7'
43
43
  required_rubygems_version: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="