progressbar 1.11.0 → 1.13.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5bc49bb14e8635e80a71dd8aae8e6e80d42b1eedda7342c4b3ff975487bd119
4
- data.tar.gz: af44f894c1ab7d25ae2ce4a4f150bc565e68669aa8f24220cb91e59eb6cb98ae
3
+ metadata.gz: f418592d256e13252084abad3577d8000cdaacfdf2714c930cef99bb02690dcb
4
+ data.tar.gz: 05fe75f498f6dc53fc26ee79c5cb229359e7f4fc158e3aed861ae84b02700a4a
5
5
  SHA512:
6
- metadata.gz: 231ae03f6609373a4dbe57ab1fe4866b03afadc376b6766e00c60c3fde434acc39a69df57f68496d4927285d69437f5573f1a8be608fd2e997e4f9eaa9ee1890
7
- data.tar.gz: 4efaeb90bd8c1d420a39fc73df3c17d1d3745ccf3f0987528eb9f6750b8fe0b06633ae19a3dd9ec634a55883a1f6271710935e7d10744c409fc0c4eb64f414ed
6
+ metadata.gz: c56aeb64b49c3e7bc9fffcc5e79c23bd2d5a96048a39b89398506397f317a891796eedd64f8924afb0a671d37b7df0bd3528d95a58218089c4e4d1ad464e9920
7
+ data.tar.gz: b20205abe7ce6979211102f81b7157ccbd3eec58f2ed9076626dcce4eba4aa1c6763708dca35bb046b5923e3f2803f44e2955113a8a1380ac0fab03a85ae6717
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/progressbar.rb CHANGED
@@ -1,15 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ruby-progressbar/output'
4
- require 'ruby-progressbar/outputs/tty'
5
- require 'ruby-progressbar/outputs/non_tty'
6
- require 'ruby-progressbar/timer'
7
- require 'ruby-progressbar/progress'
8
- require 'ruby-progressbar/throttle'
9
- require 'ruby-progressbar/calculators/length'
10
- require 'ruby-progressbar/calculators/running_average'
11
- require 'ruby-progressbar/components'
12
- require 'ruby-progressbar/format'
13
3
  require 'ruby-progressbar/base'
14
4
  require 'ruby-progressbar/refinements' if Module.
15
5
  private_instance_methods.
@@ -1,9 +1,38 @@
1
1
  require 'forwardable'
2
2
 
3
+ require 'ruby-progressbar/components/bar'
4
+ require 'ruby-progressbar/components/percentage'
5
+ require 'ruby-progressbar/components/rate'
6
+ require 'ruby-progressbar/components/time'
7
+ require 'ruby-progressbar/components/title'
8
+ require 'ruby-progressbar/format/formatter'
9
+ require 'ruby-progressbar/format/string'
10
+ require 'ruby-progressbar/outputs/non_tty'
11
+ require 'ruby-progressbar/outputs/tty'
12
+ require 'ruby-progressbar/progress'
13
+ require 'ruby-progressbar/projector'
14
+ require 'ruby-progressbar/timer'
15
+
3
16
  class ProgressBar
4
17
  class Base
5
18
  extend Forwardable
6
19
 
20
+ # rubocop:disable Layout/HeredocIndentation
21
+ SMOOTHING_DEPRECATION_WARNING = <<-HEREDOC.tr("\n", ' ')
22
+ WARNING: Passing the 'smoothing' option is deprecated and will be removed
23
+ in version 2.0. Please pass { projector: { type: 'smoothing', strength: 0.x }}.
24
+ For more information on why this change is happening, visit
25
+ https://github.com/jfelchner/ruby-progressbar/wiki/Upgrading
26
+ HEREDOC
27
+
28
+ RUNNING_AVERAGE_RATE_DEPRECATION_WARNING = <<-HEREDOC.tr("\n", ' ')
29
+ WARNING: Passing the 'running_average_rate' option is deprecated and will be removed
30
+ in version 2.0. Please pass { projector: { type: 'smoothing', strength: 0.x }}.
31
+ For more information on why this change is happening, visit
32
+ https://github.com/jfelchner/ruby-progressbar/wiki/Upgrading
33
+ HEREDOC
34
+ # rubocop:enable Layout/HeredocIndentation
35
+
7
36
  def_delegators :output,
8
37
  :clear,
9
38
  :log,
@@ -14,21 +43,40 @@ class Base
14
43
  :total
15
44
 
16
45
  def initialize(options = {}) # rubocop:disable Metrics/AbcSize
46
+ options[:projector] ||= {}
47
+
17
48
  self.autostart = options.fetch(:autostart, true)
18
49
  self.autofinish = options.fetch(:autofinish, true)
19
50
  self.finished = false
20
51
 
21
52
  self.timer = Timer.new(options)
53
+ projector_opts = if options[:projector].any?
54
+ options[:projector]
55
+ elsif options[:smoothing]
56
+ warn SMOOTHING_DEPRECATION_WARNING
57
+
58
+ { :strength => options[:smoothing] }
59
+ elsif options[:running_average_rate]
60
+ warn RUNNING_AVERAGE_RATE_DEPRECATION_WARNING
61
+
62
+ { :strength => options[:smoothing] }
63
+ else
64
+ {}
65
+ end
66
+ self.projector = Projector.
67
+ from_type(options[:projector][:type]).
68
+ new(projector_opts)
22
69
  self.progressable = Progress.new(options)
23
70
 
24
- options = options.merge(:timer => timer,
25
- :progress => progressable)
71
+ options = options.merge(:progress => progressable,
72
+ :projector => projector,
73
+ :timer => timer)
26
74
 
27
- self.title_comp = Components::Title.new(options)
28
- self.bar = Components::Bar.new(options)
29
- self.percentage = Components::Percentage.new(options)
30
- self.rate = Components::Rate.new(options)
31
- self.time = Components::Time.new(options)
75
+ self.title_component = Components::Title.new(options)
76
+ self.bar_component = Components::Bar.new(options)
77
+ self.percentage_component = Components::Percentage.new(options)
78
+ self.rate_component = Components::Rate.new(options)
79
+ self.time_component = Components::Time.new(options)
32
80
 
33
81
  self.output = Output.detect(options.merge(:bar => self))
34
82
  @format = Format::String.new(output.resolve_format(options[:format]))
@@ -67,6 +115,7 @@ class Base
67
115
  output.with_refresh do
68
116
  self.finished = false
69
117
  progressable.reset
118
+ projector.reset
70
119
  timer.reset
71
120
  end
72
121
  end
@@ -102,19 +151,19 @@ class Base
102
151
  end
103
152
 
104
153
  def progress_mark=(mark)
105
- output.refresh_with_format_change { bar.progress_mark = mark }
154
+ output.refresh_with_format_change { bar_component.progress_mark = mark }
106
155
  end
107
156
 
108
157
  def remainder_mark=(mark)
109
- output.refresh_with_format_change { bar.remainder_mark = mark }
158
+ output.refresh_with_format_change { bar_component.remainder_mark = mark }
110
159
  end
111
160
 
112
161
  def title
113
- title_comp.title
162
+ title_component.title
114
163
  end
115
164
 
116
165
  def title=(title)
117
- output.refresh_with_format_change { title_comp.title = title }
166
+ output.refresh_with_format_change { title_component.title = title }
118
167
  end
119
168
 
120
169
  def to_s(new_format = nil)
@@ -128,17 +177,17 @@ class Base
128
177
  {
129
178
  'output_stream' => output.__send__(:stream),
130
179
  'length' => output.length,
131
- 'title' => title_comp.title,
132
- 'progress_mark' => bar.progress_mark,
133
- 'remainder_mark' => bar.remainder_mark,
180
+ 'title' => title_component.title,
181
+ 'progress_mark' => bar_component.progress_mark,
182
+ 'remainder_mark' => bar_component.remainder_mark,
134
183
  'progress' => progressable.progress,
135
184
  'total' => progressable.total,
136
185
  'percentage' => progressable.percentage_completed_with_precision.to_f,
137
- 'elapsed_time_in_seconds' => time.__send__(:timer).elapsed_seconds,
138
- 'estimated_time_remaining_in_seconds' => time.__send__(:estimated_seconds_remaining),
139
- 'base_rate_of_change' => rate.__send__(:base_rate),
140
- 'scaled_rate_of_change' => rate.__send__(:scaled_rate),
141
- 'unknown_progress_animation_steps' => bar.upa_steps,
186
+ 'elapsed_time_in_seconds' => time_component.__send__(:timer).elapsed_seconds,
187
+ 'estimated_time_remaining_in_seconds' => time_component.__send__(:estimated_seconds_remaining),
188
+ 'base_rate_of_change' => rate_component.__send__(:base_rate),
189
+ 'scaled_rate_of_change' => rate_component.__send__(:scaled_rate),
190
+ 'unknown_progress_animation_steps' => bar_component.upa_steps,
142
191
  'throttle_rate' => output.__send__(:throttle).rate,
143
192
  'started?' => started?,
144
193
  'stopped?' => stopped?,
@@ -162,13 +211,14 @@ class Base
162
211
  protected
163
212
 
164
213
  attr_accessor :output,
214
+ :projector,
165
215
  :timer,
166
216
  :progressable,
167
- :title_comp,
168
- :bar,
169
- :percentage,
170
- :rate,
171
- :time,
217
+ :title_component,
218
+ :bar_component,
219
+ :percentage_component,
220
+ :rate_component,
221
+ :time_component,
172
222
  :autostart,
173
223
  :autofinish,
174
224
  :finished
@@ -176,6 +226,7 @@ class Base
176
226
  def update_progress(*args)
177
227
  output.with_refresh do
178
228
  progressable.__send__(*args)
229
+ projector.__send__(*args)
179
230
  timer.stop if finished?
180
231
  end
181
232
  end
@@ -50,7 +50,6 @@ class Length
50
50
  end
51
51
  # rubocop:enable Style/RescueStandardError
52
52
 
53
- # rubocop:disable Lint/DuplicateMethods
54
53
  begin
55
54
  require 'io/console'
56
55
 
@@ -68,7 +67,6 @@ class Length
68
67
  dynamic_width_via_system_calls
69
68
  end
70
69
  end
71
- # rubocop:enable Lint/DuplicateMethods
72
70
 
73
71
  def dynamic_width_via_output_stream_object
74
72
  _rows, columns = output.winsize
@@ -32,22 +32,6 @@ class Bar
32
32
  end
33
33
  end
34
34
 
35
- private
36
-
37
- def integrated_percentage_complete_string
38
- return standard_complete_string if completed_length < 5
39
-
40
- " #{progress.percentage_completed} ".to_s.center(completed_length, progress_mark)
41
- end
42
-
43
- def standard_complete_string
44
- progress_mark * completed_length
45
- end
46
-
47
- def incomplete_string
48
- remainder_mark * (length - completed_length)
49
- end
50
-
51
35
  def bar(length)
52
36
  self.length = length
53
37
 
@@ -66,12 +50,6 @@ class Bar
66
50
  to_s(:format => :integrated_percentage)
67
51
  end
68
52
 
69
- def unknown_string
70
- unknown_frame_string = unknown_progress_frame * ((length / upa_steps.size) + 2)
71
-
72
- unknown_frame_string[0, length]
73
- end
74
-
75
53
  def incomplete_space(length)
76
54
  self.length = length
77
55
 
@@ -88,6 +66,28 @@ class Bar
88
66
  integrated_percentage_complete_string
89
67
  end
90
68
 
69
+ private
70
+
71
+ def integrated_percentage_complete_string
72
+ return standard_complete_string if completed_length < 5
73
+
74
+ " #{progress.percentage_completed} ".to_s.center(completed_length, progress_mark)
75
+ end
76
+
77
+ def standard_complete_string
78
+ progress_mark * completed_length
79
+ end
80
+
81
+ def incomplete_string
82
+ remainder_mark * (length - completed_length)
83
+ end
84
+
85
+ def unknown_string
86
+ unknown_frame_string = unknown_progress_frame * ((length / upa_steps.size) + 2)
87
+
88
+ unknown_frame_string[0, length]
89
+ end
90
+
91
91
  def completed_length
92
92
  (length * progress.percentage_completed / 100).floor
93
93
  end
@@ -7,10 +7,8 @@ class Percentage
7
7
  self.progress = options[:progress]
8
8
  end
9
9
 
10
- private
11
-
12
10
  def percentage
13
- progress.percentage_completed
11
+ progress.percentage_completed.to_s
14
12
  end
15
13
 
16
14
  def justified_percentage
@@ -2,23 +2,17 @@ class ProgressBar
2
2
  module Components
3
3
  class Rate
4
4
  attr_accessor :rate_scale,
5
- :started_at,
6
- :stopped_at,
7
5
  :timer,
8
6
  :progress
9
7
 
10
8
  def initialize(options = {})
11
9
  self.rate_scale = options[:rate_scale] || lambda { |x| x }
12
- self.started_at = nil
13
- self.stopped_at = nil
14
10
  self.timer = options[:timer]
15
11
  self.progress = options[:progress]
16
12
  end
17
13
 
18
- private
19
-
20
14
  def rate_of_change(format_string = '%i')
21
- return 0 unless elapsed_seconds > 0
15
+ return '0' if elapsed_seconds <= 0
22
16
 
23
17
  format_string % scaled_rate
24
18
  end
@@ -27,6 +21,8 @@ class Rate
27
21
  rate_of_change('%.2f')
28
22
  end
29
23
 
24
+ private
25
+
30
26
  def scaled_rate
31
27
  rate_scale.call(base_rate)
32
28
  end
@@ -12,60 +12,58 @@ class Time
12
12
  NO_TIME_ELAPSED_TEXT = '--:--:--'.freeze
13
13
  ESTIMATED_LABEL = ' ETA'.freeze
14
14
  ELAPSED_LABEL = 'Time'.freeze
15
+ WALL_CLOCK_FORMAT = '%H:%M:%S'.freeze
15
16
  OOB_TEXT_TO_FORMAT = {
16
17
  :unknown => OOB_UNKNOWN_TIME_TEXT,
17
18
  :friendly => OOB_FRIENDLY_TIME_TEXT
18
19
  }.freeze
19
20
 
20
21
  def initialize(options = {})
21
- self.out_of_bounds_time_format = options[:out_of_bounds_time_format]
22
- self.timer = options[:timer]
23
- self.progress = options[:progress]
22
+ self.timer = options[:timer]
23
+ self.progress = options[:progress]
24
+ self.projector = options[:projector]
24
25
  end
25
26
 
26
- def estimated_with_label
27
- "#{ESTIMATED_LABEL}: #{estimated}"
27
+ def estimated_with_label(out_of_bounds_time_format = nil)
28
+ "#{ESTIMATED_LABEL}: #{estimated(out_of_bounds_time_format)}"
28
29
  end
29
30
 
30
31
  def elapsed_with_label
31
32
  "#{ELAPSED_LABEL}: #{elapsed}"
32
33
  end
33
34
 
34
- protected
35
-
36
35
  def estimated_with_no_oob
37
- self.out_of_bounds_time_format = nil
38
-
39
- estimated_with_elapsed_fallback
36
+ estimated_with_elapsed_fallback(nil)
40
37
  end
41
38
 
42
39
  def estimated_with_unknown_oob
43
- self.out_of_bounds_time_format = :unknown
44
-
45
- estimated_with_elapsed_fallback
40
+ estimated_with_elapsed_fallback(:unknown)
46
41
  end
47
42
 
48
43
  def estimated_with_friendly_oob
49
- self.out_of_bounds_time_format = :friendly
50
-
51
- estimated_with_elapsed_fallback
44
+ estimated_with_elapsed_fallback(:friendly)
52
45
  end
53
46
 
54
- attr_reader :out_of_bounds_time_format
55
- attr_accessor :timer,
56
- :progress
47
+ def estimated_wall_clock
48
+ return timer.stopped_at.strftime(WALL_CLOCK_FORMAT) if progress.finished?
49
+ return NO_TIME_ELAPSED_TEXT unless timer.started?
57
50
 
58
- def out_of_bounds_time_format=(format)
59
- unless OOB_TIME_FORMATS.include? format
60
- fail StandardError, "Invalid Out Of Bounds time format. Valid formats are #{OOB_TIME_FORMATS.inspect}"
61
- end
51
+ memo_estimated_seconds_remaining = estimated_seconds_remaining
52
+ return NO_TIME_ELAPSED_TEXT unless memo_estimated_seconds_remaining
62
53
 
63
- @out_of_bounds_time_format = format
54
+ (timer.now + memo_estimated_seconds_remaining).
55
+ strftime(WALL_CLOCK_FORMAT)
64
56
  end
65
57
 
58
+ protected
59
+
60
+ attr_accessor :timer,
61
+ :progress,
62
+ :projector
63
+
66
64
  private
67
65
 
68
- def estimated
66
+ def estimated(out_of_bounds_time_format)
69
67
  memo_estimated_seconds_remaining = estimated_seconds_remaining
70
68
 
71
69
  return OOB_UNKNOWN_TIME_TEXT unless memo_estimated_seconds_remaining
@@ -73,7 +71,7 @@ class Time
73
71
  hours, minutes, seconds = timer.divide_seconds(memo_estimated_seconds_remaining)
74
72
 
75
73
  if hours > OOB_LIMIT_IN_HOURS && out_of_bounds_time_format
76
- OOB_TEXT_TO_FORMAT[out_of_bounds_time_format]
74
+ OOB_TEXT_TO_FORMAT.fetch(out_of_bounds_time_format)
77
75
  else
78
76
  TIME_FORMAT % [hours, minutes, seconds]
79
77
  end
@@ -87,14 +85,16 @@ class Time
87
85
  TIME_FORMAT % [hours, minutes, seconds]
88
86
  end
89
87
 
90
- def estimated_with_elapsed_fallback
91
- progress.finished? ? elapsed_with_label : estimated_with_label
88
+ def estimated_with_elapsed_fallback(out_of_bounds_time_format)
89
+ return elapsed_with_label if progress.finished?
90
+
91
+ estimated_with_label(out_of_bounds_time_format)
92
92
  end
93
93
 
94
94
  def estimated_seconds_remaining
95
- return if progress.unknown? || progress.none? || timer.stopped? || timer.reset?
95
+ return if progress.unknown? || projector.none? || progress.none? || timer.stopped? || timer.reset?
96
96
 
97
- (timer.elapsed_seconds * (progress.total / progress.running_average - 1)).round
97
+ (timer.elapsed_seconds * ((progress.total / projector.projection) - 1)).round
98
98
  end
99
99
  end
100
100
  end
@@ -13,7 +13,7 @@ class Formatter
13
13
  bar_length = max_length -
14
14
  processed_string.displayable_length +
15
15
  format_string.bar_molecule_placeholder_length
16
- bar_length = (bar_length < 0) ? 0 : bar_length
16
+ bar_length = 0 if bar_length < 0
17
17
 
18
18
  format_string.bar_molecules.each do |molecule|
19
19
  processed_string.gsub!(molecule.full_key,
@@ -2,26 +2,27 @@ class ProgressBar
2
2
  module Format
3
3
  class Molecule
4
4
  MOLECULES = {
5
- :t => [:title_comp, :title],
6
- :T => [:title_comp, :title],
7
- :c => [:progressable, :progress],
8
- :C => [:progressable, :total],
9
- :u => [:progressable, :total_with_unknown_indicator],
10
- :p => [:percentage, :percentage],
11
- :P => [:percentage, :percentage_with_precision],
12
- :j => [:percentage, :justified_percentage],
13
- :J => [:percentage, :justified_percentage_with_precision],
14
- :a => [:time, :elapsed_with_label],
15
- :e => [:time, :estimated_with_unknown_oob],
16
- :E => [:time, :estimated_with_friendly_oob],
17
- :f => [:time, :estimated_with_no_oob],
18
- :B => [:bar, :complete_bar],
19
- :b => [:bar, :bar],
20
- :W => [:bar, :complete_bar_with_percentage],
21
- :w => [:bar, :bar_with_percentage],
22
- :i => [:bar, :incomplete_space],
23
- :r => [:rate, :rate_of_change],
24
- :R => [:rate, :rate_of_change_with_precision]
5
+ :t => [:title_component, :title],
6
+ :T => [:title_component, :title],
7
+ :c => [:progressable, :progress],
8
+ :C => [:progressable, :total],
9
+ :u => [:progressable, :total_with_unknown_indicator],
10
+ :p => [:percentage_component, :percentage],
11
+ :P => [:percentage_component, :percentage_with_precision],
12
+ :j => [:percentage_component, :justified_percentage],
13
+ :J => [:percentage_component, :justified_percentage_with_precision],
14
+ :a => [:time_component, :elapsed_with_label],
15
+ :e => [:time_component, :estimated_with_unknown_oob],
16
+ :E => [:time_component, :estimated_with_friendly_oob],
17
+ :f => [:time_component, :estimated_with_no_oob],
18
+ :l => [:time_component, :estimated_wall_clock],
19
+ :B => [:bar_component, :complete_bar],
20
+ :b => [:bar_component, :bar],
21
+ :W => [:bar_component, :complete_bar_with_percentage],
22
+ :w => [:bar_component, :bar_with_percentage],
23
+ :i => [:bar_component, :incomplete_space],
24
+ :r => [:rate_component, :rate_of_change],
25
+ :R => [:rate_component, :rate_of_change_with_precision]
25
26
  }.freeze
26
27
 
27
28
  BAR_MOLECULES = %w{W w B b i}.freeze
@@ -1,3 +1,5 @@
1
+ require 'ruby-progressbar/format/molecule'
2
+
1
3
  class ProgressBar
2
4
  module Format
3
5
  class String < ::String
@@ -1,3 +1,6 @@
1
+ require 'ruby-progressbar/calculators/length'
2
+ require 'ruby-progressbar/throttle'
3
+
1
4
  class ProgressBar
2
5
  class Output
3
6
  DEFAULT_OUTPUT_STREAM = $stdout
@@ -4,25 +4,19 @@ class ProgressBar
4
4
  class Progress
5
5
  DEFAULT_TOTAL = 100
6
6
  DEFAULT_BEGINNING_POSITION = 0
7
- DEFAULT_SMOOTHING = 0.1
8
7
 
9
8
  attr_reader :total,
10
9
  :progress
11
-
12
- attr_accessor :starting_position,
13
- :running_average,
14
- :smoothing
10
+ attr_accessor :starting_position
15
11
 
16
12
  def initialize(options = {})
17
- self.total = options.fetch(:total, DEFAULT_TOTAL)
18
- self.smoothing = options[:smoothing] || DEFAULT_SMOOTHING
13
+ self.total = options.fetch(:total, DEFAULT_TOTAL)
19
14
 
20
- start :at => DEFAULT_BEGINNING_POSITION
15
+ start(:at => DEFAULT_BEGINNING_POSITION)
21
16
  end
22
17
 
23
18
  def start(options = {})
24
- self.running_average = 0
25
- self.progress = \
19
+ self.progress = \
26
20
  self.starting_position = options[:at] || progress
27
21
  end
28
22
 
@@ -55,7 +49,7 @@ class Progress
55
49
  end
56
50
 
57
51
  def reset
58
- start :at => starting_position
52
+ start(:at => starting_position)
59
53
  end
60
54
 
61
55
  def progress=(new_progress)
@@ -65,10 +59,6 @@ class Progress
65
59
  end
66
60
 
67
61
  @progress = new_progress
68
-
69
- self.running_average = Calculators::RunningAverage.calculate(running_average,
70
- absolute,
71
- smoothing)
72
62
  end
73
63
 
74
64
  def total=(new_total)
@@ -93,7 +83,7 @@ class Progress
93
83
  end
94
84
 
95
85
  def none?
96
- running_average.zero? || progress.zero?
86
+ progress.zero?
97
87
  end
98
88
 
99
89
  def unknown?
@@ -0,0 +1,14 @@
1
+ require 'ruby-progressbar/projectors/smoothed_average'
2
+
3
+ class ProgressBar
4
+ class Projector
5
+ DEFAULT_PROJECTOR = ProgressBar::Projectors::SmoothedAverage
6
+ NAME_TO_PROJECTOR_MAP = {
7
+ 'smoothed' => ProgressBar::Projectors::SmoothedAverage
8
+ }.freeze
9
+
10
+ def self.from_type(name)
11
+ NAME_TO_PROJECTOR_MAP.fetch(name, DEFAULT_PROJECTOR)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,71 @@
1
+ class ProgressBar
2
+ module Projectors
3
+ class SmoothedAverage
4
+ DEFAULT_STRENGTH = 0.1
5
+ DEFAULT_BEGINNING_POSITION = 0
6
+
7
+ attr_accessor :samples,
8
+ :strength
9
+ attr_reader :projection
10
+
11
+ def initialize(options = {})
12
+ self.samples = []
13
+ self.projection = 0.0
14
+ self.strength = options[:strength] || DEFAULT_STRENGTH
15
+
16
+ start(:at => DEFAULT_BEGINNING_POSITION)
17
+ end
18
+
19
+ def start(options = {})
20
+ self.projection = 0.0
21
+ self.progress = samples[0] = (options[:at] || progress)
22
+ end
23
+
24
+ def decrement
25
+ self.progress -= 1
26
+ end
27
+
28
+ def increment
29
+ self.progress += 1
30
+ end
31
+
32
+ def progress
33
+ samples[1]
34
+ end
35
+
36
+ def total=(_new_total); end
37
+
38
+ def reset
39
+ start(:at => samples[0])
40
+ end
41
+
42
+ def progress=(new_progress)
43
+ samples[1] = new_progress
44
+ self.projection = \
45
+ self.class.calculate(
46
+ @projection,
47
+ absolute,
48
+ strength
49
+ )
50
+ end
51
+
52
+ def none?
53
+ projection.zero?
54
+ end
55
+
56
+ def self.calculate(current_projection, new_value, rate)
57
+ (new_value * (1.0 - rate)) + (current_projection * rate)
58
+ end
59
+
60
+ protected
61
+
62
+ attr_writer :projection
63
+
64
+ private
65
+
66
+ def absolute
67
+ samples[1] - samples[0]
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,28 @@
1
+ class ProgressBar
2
+ module Refinements
3
+ module Enumerator
4
+ ARITY_ERROR_MESSAGE = 'Only two arguments allowed to be passed to ' \
5
+ 'with_progressbar (item, progress_bar)'.freeze
6
+
7
+ refine ::Enumerator do
8
+ def with_progressbar(options = {}, &block)
9
+ progress_bar = ProgressBar.create(options.merge(:starting_at => 0, :total => size))
10
+
11
+ each do |item|
12
+ progress_bar.increment
13
+
14
+ next unless block
15
+
16
+ yielded_args = []
17
+ yielded_args << item if block.arity > 0
18
+ yielded_args << progress_bar if block.arity > 1
19
+
20
+ fail ::ArgumentError, ARITY_ERROR_MESSAGE if block.arity > 2
21
+
22
+ yield(*yielded_args)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1 +1 @@
1
- require 'ruby-progressbar/refinements/enumerator'
1
+ require 'ruby-progressbar/refinements/progress_enumerator'
@@ -17,11 +17,9 @@ class Time
17
17
  end
18
18
 
19
19
  def unmocked_time_method
20
- @unmocked_time_method ||= begin
21
- TIME_MOCKING_LIBRARY_METHODS.find do |method|
22
- time.respond_to? method
23
- end
24
- end
20
+ @unmocked_time_method ||= TIME_MOCKING_LIBRARY_METHODS.find do |method|
21
+ time.respond_to? method
22
+ end
25
23
  end
26
24
 
27
25
  protected
@@ -28,6 +28,10 @@ class Timer
28
28
  start
29
29
  end
30
30
 
31
+ def now
32
+ time.now
33
+ end
34
+
31
35
  def started?
32
36
  started_at
33
37
  end
@@ -51,6 +55,8 @@ class Timer
51
55
  end
52
56
 
53
57
  def elapsed_seconds
58
+ return 0 unless started?
59
+
54
60
  ((stopped_at || time.now) - started_at)
55
61
  end
56
62
 
@@ -1,3 +1,3 @@
1
1
  class ProgressBar
2
- VERSION = '1.11.0'.freeze
2
+ VERSION = '1.13.0'.freeze
3
3
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: progressbar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.0
4
+ version: 1.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thekompanee
8
8
  - jfelchner
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain:
12
12
  - |
13
13
  -----BEGIN CERTIFICATE-----
14
- MIIEGDCCAoCgAwIBAgIBAjANBgkqhkiG9w0BAQsFADAyMTAwLgYDVQQDDCdhY2Nv
15
- dW50c19ydWJ5Z2Vtcy9EQz10aGVrb21wYW5lZS9EQz1jb20wHhcNMjAxMjI2MjIz
16
- MTE2WhcNMjExMjI2MjIzMTE2WjAyMTAwLgYD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WW6L9MSZzKkjWVJXD43B3tWBjIDthQVTzS4j90PUkUXgBXjS7Jxj/
36
38
  -----END CERTIFICATE-----
37
- date: 2020-12-31 00:00:00.000000000 Z
39
+ date: 2023-03-04 00:00:00.000000000 Z
38
40
  dependencies:
39
41
  - !ruby/object:Gem::Dependency
40
42
  name: rspec
@@ -82,16 +84,16 @@ dependencies:
82
84
  name: timecop
83
85
  requirement: !ruby/object:Gem::Requirement
84
86
  requirements:
85
- - - '='
87
+ - - "~>"
86
88
  - !ruby/object:Gem::Version
87
- version: 0.6.0
89
+ version: '0.9'
88
90
  type: :development
89
91
  prerelease: false
90
92
  version_requirements: !ruby/object:Gem::Requirement
91
93
  requirements:
92
- - - '='
94
+ - - "~>"
93
95
  - !ruby/object:Gem::Version
94
- version: 0.6.0
96
+ version: '0.9'
95
97
  description: 'Ruby/ProgressBar is an extremely flexible text progress bar library
96
98
  for Ruby. The output can be customized with a flexible formatting system including:
97
99
  percentage, bars of various formats, elapsed time and estimated time remaining.'
@@ -107,15 +109,12 @@ files:
107
109
  - lib/progressbar.rb
108
110
  - lib/ruby-progressbar/base.rb
109
111
  - lib/ruby-progressbar/calculators/length.rb
110
- - lib/ruby-progressbar/calculators/running_average.rb
111
- - lib/ruby-progressbar/components.rb
112
112
  - lib/ruby-progressbar/components/bar.rb
113
113
  - lib/ruby-progressbar/components/percentage.rb
114
114
  - lib/ruby-progressbar/components/rate.rb
115
115
  - lib/ruby-progressbar/components/time.rb
116
116
  - lib/ruby-progressbar/components/title.rb
117
117
  - lib/ruby-progressbar/errors/invalid_progress_error.rb
118
- - lib/ruby-progressbar/format.rb
119
118
  - lib/ruby-progressbar/format/formatter.rb
120
119
  - lib/ruby-progressbar/format/molecule.rb
121
120
  - lib/ruby-progressbar/format/string.rb
@@ -124,8 +123,10 @@ files:
124
123
  - lib/ruby-progressbar/outputs/null.rb
125
124
  - lib/ruby-progressbar/outputs/tty.rb
126
125
  - lib/ruby-progressbar/progress.rb
126
+ - lib/ruby-progressbar/projector.rb
127
+ - lib/ruby-progressbar/projectors/smoothed_average.rb
127
128
  - lib/ruby-progressbar/refinements.rb
128
- - lib/ruby-progressbar/refinements/enumerator.rb
129
+ - lib/ruby-progressbar/refinements/progress_enumerator.rb
129
130
  - lib/ruby-progressbar/throttle.rb
130
131
  - lib/ruby-progressbar/time.rb
131
132
  - lib/ruby-progressbar/timer.rb
@@ -136,11 +137,11 @@ licenses:
136
137
  metadata:
137
138
  bug_tracker_uri: https://github.com/jfelchner/ruby-progressbar/issues
138
139
  changelog_uri: https://github.com/jfelchner/ruby-progressbar/blob/master/CHANGELOG.md
139
- documentation_uri: https://github.com/jfelchner/ruby-progressbar/tree/releases/v1.11.0
140
+ documentation_uri: https://github.com/jfelchner/ruby-progressbar/tree/releases/v1.13.0
140
141
  homepage_uri: https://github.com/jfelchner/ruby-progressbar
141
142
  source_code_uri: https://github.com/jfelchner/ruby-progressbar
142
143
  wiki_uri: https://github.com/jfelchner/ruby-progressbar/wiki
143
- post_install_message:
144
+ post_install_message:
144
145
  rdoc_options: []
145
146
  require_paths:
146
147
  - lib
@@ -155,8 +156,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
156
  - !ruby/object:Gem::Version
156
157
  version: '0'
157
158
  requirements: []
158
- rubygems_version: 3.2.3
159
- signing_key:
159
+ rubygems_version: 3.1.6
160
+ signing_key:
160
161
  specification_version: 4
161
162
  summary: Ruby/ProgressBar is a flexible text progress bar library for Ruby.
162
163
  test_files: []
metadata.gz.sig CHANGED
Binary file
@@ -1,9 +0,0 @@
1
- class ProgressBar
2
- module Calculators
3
- class RunningAverage
4
- def self.calculate(current_average, new_value_to_average, smoothing_factor)
5
- new_value_to_average * (1.0 - smoothing_factor) + current_average * smoothing_factor
6
- end
7
- end
8
- end
9
- end
@@ -1,5 +0,0 @@
1
- require 'ruby-progressbar/components/bar'
2
- require 'ruby-progressbar/components/percentage'
3
- require 'ruby-progressbar/components/rate'
4
- require 'ruby-progressbar/components/time'
5
- require 'ruby-progressbar/components/title'
@@ -1,3 +0,0 @@
1
- require 'ruby-progressbar/format/molecule'
2
- require 'ruby-progressbar/format/formatter'
3
- require 'ruby-progressbar/format/string'
@@ -1,23 +0,0 @@
1
- class ProgressBar
2
- module Refinements
3
- module Enumerator
4
- refine ::Enumerator do
5
- def with_progressbar(options = {}, &block)
6
- chain = ::Enumerator.new do |yielder|
7
- progress_bar = ProgressBar.create(options.merge(:starting_at => 0, :total => size))
8
-
9
- each do |*args|
10
- yielder.yield(*args).tap do
11
- progress_bar.increment
12
- end
13
- end
14
- end
15
-
16
- return chain unless block
17
-
18
- chain.each(&block)
19
- end
20
- end
21
- end
22
- end
23
- end