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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby-progressbar/base.rb', line 17

def initialize(options = {})
  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(:timer    => timer,
                                    :progress => progressable)

  self.title_comp   = Components::Title.new(options)
  self.bar          = Components::Bar.new(options)
  self.percentage   = Components::Percentage.new(options)
  self.rate         = Components::Rate.new(options)
  self.time         = 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



90
91
92
# File 'lib/ruby-progressbar/base.rb', line 90

def decrement
  update_progress(:decrement)
end

#finishObject



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

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

#finished?Boolean

Returns:

  • (Boolean)


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

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

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



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

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

#incrementObject



94
95
96
# File 'lib/ruby-progressbar/base.rb', line 94

def increment
  update_progress(:increment)
end

#inspectObject



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

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

#pauseObject



56
57
58
# File 'lib/ruby-progressbar/base.rb', line 56

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

#progress=(new_progress) ⇒ Object



98
99
100
# File 'lib/ruby-progressbar/base.rb', line 98

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

#progress_mark=(mark) ⇒ Object



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

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

#remainder_mark=(mark) ⇒ Object



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

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

#resetObject



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

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

#resumeObject



64
65
66
# File 'lib/ruby-progressbar/base.rb', line 64

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

#start(options = {}) ⇒ Object

rubocop:enable Metrics/AbcSize



41
42
43
44
45
46
# File 'lib/ruby-progressbar/base.rb', line 41

def start(options = {})
  clear

  timer.start
  update_progress(:start, options)
end

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  timer.started?
end

#stopObject



60
61
62
# File 'lib/ruby-progressbar/base.rb', line 60

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

#stopped?Boolean Also known as: paused?

Returns:

  • (Boolean)


76
77
78
# File 'lib/ruby-progressbar/base.rb', line 76

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

#titleObject



114
115
116
# File 'lib/ruby-progressbar/base.rb', line 114

def title
  title_comp.title
end

#title=(title) ⇒ Object



118
119
120
# File 'lib/ruby-progressbar/base.rb', line 118

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

#to_s(new_format = nil) ⇒ Object



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

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



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

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