Class: ProgressBar::Base

Inherits:
Object
  • Object
show all
Includes:
Formatter, LengthCalculator
Defined in:
lib/ruby-progressbar/base.rb

Constant Summary collapse

DEFAULT_OUTPUT_STREAM =
$stdout

Constants included from Formatter

Formatter::DEFAULT_FORMAT_STRING, Formatter::DEFAULT_NON_TTY_FORMAT_STRING, Formatter::DEFAULT_TITLE

Instance Method Summary collapse

Methods included from Formatter

#format, #progress, #total

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby-progressbar/base.rb', line 8

def initialize(options = {})
  self.output       = options[:output] || DEFAULT_OUTPUT_STREAM
  autostart         = options.fetch(:autostart, true)

  super(options)

  @bar              = Components::Bar.new(options)
  @estimated_time   = Components::EstimatedTimer.new(options)
  @elapsed_time     = Components::ElapsedTimer.new
  @throttle         = Components::Throttle.new(options)

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

Instance Method Details

#clearObject

Output



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

def clear
  self.last_update_length = 0

  if output.tty?
    output.print clear_string
    output.print "\r"
  else
    output.print "\n"
  end
end

#decrementObject

Updating The Bar’s Progress



37
38
39
# File 'lib/ruby-progressbar/base.rb', line 37

def decrement
  update_progress(:decrement)
end

#finishObject

Stopping The Bar



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

def finish
  with_update { with_progressables(:finish); with_timers(:stop) } unless finished?
end

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  @estimated_time.finished? && @bar.finished?
end

#incrementObject



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

def increment
  update_progress(:increment)
end

#inspectObject



139
140
141
# File 'lib/ruby-progressbar/base.rb', line 139

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

#log(string) ⇒ Object



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

def log(string)
  clear
  output.puts string

  update(:force => true) unless stopped?
end

#pauseObject



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

def pause
  with_update { with_timers(:pause) } unless paused?
end

#progress=(new_progress) ⇒ Object



45
46
47
# File 'lib/ruby-progressbar/base.rb', line 45

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

#progress_mark=(mark) ⇒ Object

UI Updates



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

def progress_mark=(mark)
  with_update { @bar.progress_mark = mark }
end

#refreshObject



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

def refresh
  update
end

#remainder_mark=(mark) ⇒ Object



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

def remainder_mark=(mark)
  with_update { @bar.remainder_mark = mark }
end

#resetObject



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

def reset
  with_update do
    @bar.reset
    with_timers(:reset)
  end
end

#resumeObject



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

def resume
  with_update { with_timers(:resume) } if stopped?
end

#start(options = {}) ⇒ Object

Starting The Bar



25
26
27
28
29
30
31
32
# File 'lib/ruby-progressbar/base.rb', line 25

def start(options = {})
  clear

  with_update do
    with_progressables(:start, options)
    with_timers(:start)
  end
end

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  @estimated_time.started? && @elapsed_time.started? && @bar.started?
end

#stopObject



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

def stop
  with_update { with_timers(:stop) } unless stopped?
end

#stopped?Boolean Also known as: paused?

Returns:

  • (Boolean)


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

def stopped?
  (@estimated_time.stopped? && @elapsed_time.stopped?) || finished?
end

#title=(title) ⇒ Object



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

def title=(title)
  with_update { super }
end

#to_s(format_string = nil) ⇒ Object



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

def to_s(format_string = nil)
  format_string ||= @format_string

  format(format_string)
end

#total=(new_total) ⇒ Object



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

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