Sha256: c26736f7b89581838e861154f04d3ef80f85d96c3d2e0e3727a7e9a85049b44f

Contents?: true

Size: 986 Bytes

Versions: 6

Compression:

Stored size: 986 Bytes

Contents

require 'stringio'

module Vendorificator
  class IOProxy < StringIO
    attr_reader :shell

    def initialize(shell, verbosity = :default)
      @shell = shell
      @verbosity = verbosity

      super()
      capture_stdout
    end

    def puts(value, verb_level = :default)
      @orig_stdout.puts value if should_speak?(verb_level)
      super
    end

    def say_status(verb_level, *args)
      write args[0..1].join('  ' * @shell.padding)
      @shell.say_status(*args) if @shell && should_speak?(verb_level)
    end

    def say(verb_level, *args)
      write args[0]
      @shell.say(*args) if @shell && should_speak?(verb_level)
    end

    private

    def capture_stdout
      @orig_stdout = $stdout

      $stdout = self
    end

    def should_speak?(level)
      levels = {:quiet => 1, :default => 2, :chatty => 3, :debug => 9}
      raise "Unknown verbosity level: #{level.inspect}" if levels[level].nil?

      levels[level] <= levels[@verbosity]
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
vendorificator-0.5.3 lib/vendorificator/io_proxy.rb
vendorificator-0.5.2 lib/vendorificator/io_proxy.rb
vendorificator-0.5.1 lib/vendorificator/io_proxy.rb
vendorificator-0.5.0 lib/vendorificator/io_proxy.rb
vendorificator-0.5.git.v0.4.0.63.g8e9d54d lib/vendorificator/io_proxy.rb
vendorificator-0.5.git.v0.4.0.60.g9c35209 lib/vendorificator/io_proxy.rb