Sha256: 3085f65a4a8a26b6d35dc9eb9cb20897236e503fbbc575549aa1fc8753175107

Contents?: true

Size: 1.16 KB

Versions: 10

Compression:

Stored size: 1.16 KB

Contents

class   ProgressBar
class   Output
  DEFAULT_OUTPUT_STREAM = $stdout

  attr_accessor :stream

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

  def self.detect(options = {})
    if (options[:output] || DEFAULT_OUTPUT_STREAM).tty?
      Outputs::Tty.new(options)
    else
      Outputs::NonTty.new(options)
    end
  end

  def log(string)
    clear
    stream.puts string

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

  def clear_string
    ' ' * length_calculator.length
  end

  def length
    length_calculator.length
  end

  def with_refresh
    yield
    refresh
  end

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

      print_and_flush
    end
  end

  def print_and_flush
    stream.print bar_update_string + eol
    stream.flush
  end

  protected

  attr_accessor :length_calculator,
                :throttle,
                :bar
end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
ruby-progressbar-1.8.3 lib/ruby-progressbar/output.rb
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/ruby-progressbar-1.8.1/lib/ruby-progressbar/output.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/ruby-progressbar-1.8.1/lib/ruby-progressbar/output.rb
progressbar-1.8.2 lib/ruby-progressbar/output.rb
progressbar-1.8.1 lib/ruby-progressbar/output.rb
ruby-progressbar-1.8.1 lib/ruby-progressbar/output.rb
ruby-progressbar-1.8.0 lib/ruby-progressbar/output.rb
ruby-progressbar-1.7.5 lib/ruby-progressbar/output.rb
ruby-progressbar-1.7.1 lib/ruby-progressbar/output.rb
ruby-progressbar-1.7.0 lib/ruby-progressbar/output.rb