1010require 'ruby-progressbar/outputs/non_tty'
1111require 'ruby-progressbar/outputs/tty'
1212require 'ruby-progressbar/progress'
13+ require 'ruby-progressbar/projector'
1314require 'ruby-progressbar/timer'
1415
1516class ProgressBar
1617class Base
1718 extend Forwardable
1819
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+
1936 def_delegators :output ,
2037 :clear ,
2138 :log ,
@@ -26,15 +43,34 @@ class Base
2643 :total
2744
2845 def initialize ( options = { } ) # rubocop:disable Metrics/AbcSize
46+ options [ :projector ] ||= { }
47+
2948 self . autostart = options . fetch ( :autostart , true )
3049 self . autofinish = options . fetch ( :autofinish , true )
3150 self . finished = false
3251
3352 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 )
3469 self . progressable = Progress . new ( options )
3570
36- options = options . merge ( :progress => progressable ,
37- :timer => timer )
71+ options = options . merge ( :progress => progressable ,
72+ :projector => projector ,
73+ :timer => timer )
3874
3975 self . title_component = Components ::Title . new ( options )
4076 self . bar_component = Components ::Bar . new ( options )
@@ -79,6 +115,7 @@ def reset
79115 output . with_refresh do
80116 self . finished = false
81117 progressable . reset
118+ projector . reset
82119 timer . reset
83120 end
84121 end
@@ -174,6 +211,7 @@ def format=(other)
174211 protected
175212
176213 attr_accessor :output ,
214+ :projector ,
177215 :timer ,
178216 :progressable ,
179217 :title_component ,
@@ -188,6 +226,7 @@ def format=(other)
188226 def update_progress ( *args )
189227 output . with_refresh do
190228 progressable . __send__ ( *args )
229+ projector . __send__ ( *args )
191230 timer . stop if finished?
192231 end
193232 end
0 commit comments