Class: ProgressBar::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ruby-progressbar/base.rb

Constant Summary collapse

SMOOTHING_DEPRECATION_WARNING =

rubocop:disable Layout/HeredocIndentation

<<-HEREDOC.tr("\n", ' ')
WARNING: Passing the 'smoothing' option is deprecated  and will be removed
in version 2.0. Please pass { projector: { type: 'smoothing', strength: 0.x }}.
For more information on why this change is happening,  visit
https://github.com/jfelchner/ruby-progressbar/wiki/Upgrading
HEREDOC
RUNNING_AVERAGE_RATE_DEPRECATION_WARNING =
<<-HEREDOC.tr("\n", ' ')
WARNING: Passing the 'running_average_rate' option is deprecated  and will be removed
in version 2.0. Please pass { projector: { type: 'smoothing', strength: 0.x }}.
For more information on why this change is happening,  visit
https://github.com/jfelchner/ruby-progressbar/wiki/Upgrading
HEREDOC

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

rubocop:disable Metrics/AbcSize



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ruby-progressbar/base.rb', line 45

def initialize(options = {}) # rubocop:disable Metrics/AbcSize
  options[:projector] ||= {}

  self.autostart    = options.fetch(:autostart,  true)
  self.autofinish   = options.fetch(:autofinish, true)
  self.finished     = false

  self.timer        = Timer.new(options)
  projector_opts    = if options[:projector].any?
                        options[:projector]
                      elsif options[:smoothing]
                        warn SMOOTHING_DEPRECATION_WARNING

                        { :strength => options[:smoothing] }
                      elsif options[:running_average_rate]
                        warn RUNNING_AVERAGE_RATE_DEPRECATION_WARNING

                        { :strength => options[:smoothing] }
                      else
                        {}
                      end
  self.projector    = Projector.
                        from_type(options[:projector][:type]).
                        new(projector_opts)
  self.progressable = Progress.new(options)

  options = options.merge(:progress  => progressable,
                          :projector => projector,
                          :timer     => timer)

  self.title_component      = Components::Title.new(options)
  self.bar_component        = Components::Bar.new(options)
  self.percentage_component = Components::Percentage.new(options)
  self.rate_component       = Components::Rate.new(options)
  self.time_component       = Components::Time.new(options)

  self.output       = Output.detect(options.merge(:bar => self))
  @format           = Format::String.new(output.resolve_format(options[:format]))

  start :at => options[:starting_at] if autostart
end

Instance Method Details

#decrementObject



137
138
139
# File 'lib/ruby-progressbar/base.rb', line 137

def decrement
  update_progress(:decrement)
end

#finishObject



92
93
94
95
96
97
98
99
100
# File 'lib/ruby-progressbar/base.rb', line 92

def finish
  return if finished?

  output.with_refresh do
    self.finished = true
    progressable.finish
    timer.stop
  end
end

#finished?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/ruby-progressbar/base.rb', line 129

def finished?
  finished || (autofinish && progressable.finished?)
end

#format=(other) ⇒ Object Also known as: format



203
204
205
206
207
# File 'lib/ruby-progressbar/base.rb', line 203

def format=(other)
  output.refresh_with_format_change do
    @format = Format::String.new(other || output.default_format)
  end
end

#incrementObject



141
142
143
# File 'lib/ruby-progressbar/base.rb', line 141

def increment
  update_progress(:increment)
end

#inspectObject

rubocop:enable Metrics/AbcSize, Layout/LineLength



199
200
201
# File 'lib/ruby-progressbar/base.rb', line 199

def inspect
  "#<ProgressBar:#{progress}/#{total || 'unknown'}>"
end

#pauseObject



102
103
104
# File 'lib/ruby-progressbar/base.rb', line 102

def pause
  output.with_refresh { timer.pause } unless paused?
end

#progress=(new_progress) ⇒ Object



145
146
147
# File 'lib/ruby-progressbar/base.rb', line 145

def progress=(new_progress)
  update_progress(:progress=, new_progress)
end

#progress_mark=(mark) ⇒ Object



153
154
155
# File 'lib/ruby-progressbar/base.rb', line 153

def progress_mark=(mark)
  output.refresh_with_format_change { bar_component.progress_mark = mark }
end

#remainder_mark=(mark) ⇒ Object



157
158
159
# File 'lib/ruby-progressbar/base.rb', line 157

def remainder_mark=(mark)
  output.refresh_with_format_change { bar_component.remainder_mark = mark }
end

#resetObject



114
115
116
117
118
119
120
121
# File 'lib/ruby-progressbar/base.rb', line 114

def reset
  output.with_refresh do
    self.finished = false
    progressable.reset
    projector.reset
    timer.reset
  end
end

#resumeObject



110
111
112
# File 'lib/ruby-progressbar/base.rb', line 110

def resume
  output.with_refresh { timer.resume } if stopped?
end

#start(options = {}) ⇒ Object



87
88
89
90
# File 'lib/ruby-progressbar/base.rb', line 87

def start(options = {})
  timer.start
  update_progress(:start, options)
end

#started?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/ruby-progressbar/base.rb', line 133

def started?
  timer.started?
end

#stopObject



106
107
108
# File 'lib/ruby-progressbar/base.rb', line 106

def stop
  output.with_refresh { timer.stop } unless stopped?
end

#stopped?Boolean Also known as: paused?

Returns:

  • (Boolean)


123
124
125
# File 'lib/ruby-progressbar/base.rb', line 123

def stopped?
  timer.stopped? || finished?
end

#titleObject



161
162
163
# File 'lib/ruby-progressbar/base.rb', line 161

def title
  title_component.title
end

#title=(title) ⇒ Object



165
166
167
# File 'lib/ruby-progressbar/base.rb', line 165

def title=(title)
  output.refresh_with_format_change { title_component.title = title }
end

#to_hObject

rubocop:disable Metrics/AbcSize, Layout/LineLength



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ruby-progressbar/base.rb', line 176

def to_h
  {
    'output_stream'                       => output.__send__(:stream),
    'length'                              => output.length,
    'title'                               => title_component.title,
    'progress_mark'                       => bar_component.progress_mark,
    'remainder_mark'                      => bar_component.remainder_mark,
    'progress'                            => progressable.progress,
    'total'                               => progressable.total,
    'percentage'                          => progressable.percentage_completed_with_precision.to_f,
    'elapsed_time_in_seconds'             => time_component.__send__(:timer).elapsed_seconds,
    'estimated_time_remaining_in_seconds' => time_component.__send__(:estimated_seconds_remaining),
    'base_rate_of_change'                 => rate_component.__send__(:base_rate),
    'scaled_rate_of_change'               => rate_component.__send__(:scaled_rate),
    'unknown_progress_animation_steps'    => bar_component.upa_steps,
    'throttle_rate'                       => output.__send__(:throttle).rate,
    'started?'                            => started?,
    'stopped?'                            => stopped?,
    'finished?'                           => finished?
  }
end

#to_s(new_format = nil) ⇒ Object



169
170
171
172
173
# File 'lib/ruby-progressbar/base.rb', line 169

def to_s(new_format = nil)
  self.format = new_format if new_format

  Format::Formatter.process(@format, output.length, self)
end

#total=(new_total) ⇒ Object



149
150
151
# File 'lib/ruby-progressbar/base.rb', line 149

def total=(new_total)
  update_progress(:total=, new_total)
end