Class: ProgressBar::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-progressbar/output.rb

Constant Summary collapse

DEFAULT_OUTPUT_STREAM =
$stdout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Output

Returns a new instance of Output.



10
11
12
13
14
15
16
17
18
# File 'lib/ruby-progressbar/output.rb', line 10

def initialize(options = {})
  self.bar               = options[:bar]
  self.stream            = options[:output] || DEFAULT_OUTPUT_STREAM
  self.throttle          = Throttle.new(options)
  self.length_calculator = Calculators::Length.new(
                             :length => options[:length],
                             :output => stream
                           )
end

Instance Attribute Details

#streamObject

Returns the value of attribute stream.



8
9
10
# File 'lib/ruby-progressbar/output.rb', line 8

def stream
  @stream
end

Class Method Details

.detect(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/ruby-progressbar/output.rb', line 20

def self.detect(options = {})
  if options[:output].is_a?(Class) && options[:output] <= ProgressBar::Output
    options[:output].new(options)
  elsif (options[:output] || DEFAULT_OUTPUT_STREAM).tty?
    Outputs::Tty.new(options)
  else
    Outputs::NonTty.new(options)
  end
end

Instance Method Details

#clear_stringObject



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

def clear_string
  ' ' * length_calculator.length
end

#lengthObject



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

def length
  length_calculator.length
end

#log(string) ⇒ Object



30
31
32
33
34
35
# File 'lib/ruby-progressbar/output.rb', line 30

def log(string)
  clear
  stream.puts string

  refresh(:force => true) unless bar.stopped?
end

#refresh(options = {}) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/ruby-progressbar/output.rb', line 50

def refresh(options = {})
  throttle.choke(:force_update_if => (bar.stopped? || options[:force])) do
    clear if length_calculator.length_changed?

    print_and_flush
  end
end

#with_refreshObject



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

def with_refresh
  yield
  refresh
end