Class: ProgressBar::Base

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

rubocop:disable Metrics/AbcSize



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby-progressbar/base.rb', line 28

def initialize(options = {}) # rubocop:disable Metrics/AbcSize
  self.autostart    = options.fetch(:autostart,  true)
  self.autofinish   = options.fetch(:autofinish, true)
  self.finished     = false

  self.timer        = Timer.new(options)
  self.progressable = Progress.new(options)

  options = options.merge(:progress => progressable,
                          :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



100
101
102
# File 'lib/ruby-progressbar/base.rb', line 100

def decrement
  update_progress(:decrement)
end

#finishObject



56
57
58
59
60
61
62
63
64
# File 'lib/ruby-progressbar/base.rb', line 56

def finish
  return if finished?

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

#finished?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/ruby-progressbar/base.rb', line 92

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

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



166
167
168
169
170
# File 'lib/ruby-progressbar/base.rb', line 166

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

#incrementObject



104
105
106
# File 'lib/ruby-progressbar/base.rb', line 104

def increment
  update_progress(:increment)
end

#inspectObject

rubocop:enable Metrics/AbcSize, Layout/LineLength



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

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

#pauseObject



66
67
68
# File 'lib/ruby-progressbar/base.rb', line 66

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

#progress=(new_progress) ⇒ Object



108
109
110
# File 'lib/ruby-progressbar/base.rb', line 108

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

#progress_mark=(mark) ⇒ Object



116
117
118
# File 'lib/ruby-progressbar/base.rb', line 116

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

#remainder_mark=(mark) ⇒ Object



120
121
122
# File 'lib/ruby-progressbar/base.rb', line 120

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

#resetObject



78
79
80
81
82
83
84
# File 'lib/ruby-progressbar/base.rb', line 78

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

#resumeObject



74
75
76
# File 'lib/ruby-progressbar/base.rb', line 74

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

#start(options = {}) ⇒ Object



51
52
53
54
# File 'lib/ruby-progressbar/base.rb', line 51

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

#started?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/ruby-progressbar/base.rb', line 96

def started?
  timer.started?
end

#stopObject



70
71
72
# File 'lib/ruby-progressbar/base.rb', line 70

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

#stopped?Boolean Also known as: paused?

Returns:

  • (Boolean)


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

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

#titleObject



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

def title
  title_component.title
end

#title=(title) ⇒ Object



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

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

#to_hObject

rubocop:disable Metrics/AbcSize, Layout/LineLength



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ruby-progressbar/base.rb', line 139

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



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

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



112
113
114
# File 'lib/ruby-progressbar/base.rb', line 112

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