Sha256: 16a4ed6b062bb502ebdbf5de52efd873b984b6ad9b193fe882b11eb12775c5f1

Contents?: true

Size: 1.5 KB

Versions: 14

Compression:

Stored size: 1.5 KB

Contents

module Lucid
  module Formatter
    module Interceptor
      class Pipe
        attr_reader :pipe, :buffer
        def initialize(pipe)
          @pipe = pipe
          @buffer = []
          @wrapped = true
        end

        def write(str)
          @buffer << str if @wrapped
          return @pipe.write(str)
        end

        def unwrap!
          @wrapped = false
          @pipe
        end

        def method_missing(method, *args, &blk)
          @pipe.send(method, *args, &blk)
        end

        def respond_to?(method, include_private=false)
          super || @pipe.respond_to?(method, include_private)
        end

        def self.validate_pipe(pipe)
          unless [:stdout, :stderr].include? pipe
            raise ArgumentError, '#wrap only accepts :stderr or :stdout'
          end
        end

        def self.unwrap!(pipe)
          validate_pipe pipe
          wrapped = nil
          case pipe
          when :stdout
            wrapped = $stdout
            $stdout = wrapped.unwrap! if $stdout.respond_to?(:unwrap!)
          when :stderr
            wrapped = $stderr
            $stderr = wrapped.unwrap! if $stderr.respond_to?(:unwrap!)
          end
          wrapped
        end

        def self.wrap(pipe)
          validate_pipe pipe

          case pipe
          when :stderr
            $stderr = self.new($stderr)
            return $stderr
          when :stdout
            $stdout = self.new($stdout)
            return $stdout
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
lucid-0.5.1 lib/lucid/formatter/interceptor.rb
lucid-0.4.1 lib/lucid/formatter/interceptor.rb
lucid-0.4.0 lib/lucid/formatter/interceptor.rb
lucid-0.3.3 lib/lucid/formatter/interceptor.rb
lucid-0.3.0 lib/lucid/formatter/interceptor.rb
lucid-0.2.1 lib/lucid/formatter/interceptor.rb
lucid-0.2.0 lib/lucid/formatter/interceptor.rb
lucid-0.1.1 lib/lucid/formatter/interceptor.rb
lucid-0.1.0 lib/lucid/formatter/interceptor.rb
lucid-0.0.9 lib/lucid/formatter/interceptor.rb
lucid-0.0.8 lib/lucid/formatter/interceptor.rb
lucid-0.0.7 lib/lucid/formatter/interceptor.rb
lucid-0.0.6 lib/lucid/formatter/interceptor.rb
lucid-0.0.5 lib/lucid/formatter/interceptor.rb