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.



7
8
9
10
11
12
13
14
15
# File 'lib/ruby-progressbar/output.rb', line 7

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.



5
6
7
# File 'lib/ruby-progressbar/output.rb', line 5

def stream
  @stream
end

Class Method Details

.detect(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/ruby-progressbar/output.rb', line 17

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



34
35
36
# File 'lib/ruby-progressbar/output.rb', line 34

def clear_string
  ' ' * length_calculator.length
end

#lengthObject



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

def length
  length_calculator.length
end

#log(string) ⇒ Object



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

def log(string)
  clear
  stream.puts string

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

#refresh(options = {}) ⇒ Object



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

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



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

def with_refresh
  yield
  refresh
end