Sha256: ac3116b31c3106e18b01d739cc05ce67d04345e4b203c9627483354713587305

Contents?: true

Size: 958 Bytes

Versions: 2

Compression:

Stored size: 958 Bytes

Contents

module ConfCtl
  # Pair of line buffers for standard output/error
  class StdLineBuffer
    # @yieldparam out [String, nil]
    # @yieldparam err [String, nil]
    def initialize(&block)
      @out_buffer = LineBuffer.new
      @err_buffer = LineBuffer.new
      @block = block
      @mutex = Mutex.new
    end

    # Get a block which can be called to feed data to the buffer
    # @return [Proc]
    def feed_block
      proc do |stdout, stderr|
        @mutex.synchronize do
          out_buffer << stdout if stdout
          err_buffer << stderr if stderr

          loop do
            out_line = out_buffer.read_line
            err_line = err_buffer.read_line
            break if out_line.nil? && err_line.nil?

            block.call(out_line, err_line)
          end
        end
      end
    end

    def flush
      block.call(out_buffer.flush, err_buffer.flush)
    end

    protected

    attr_reader :out_buffer, :err_buffer, :block
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
confctl-2.0.0 lib/confctl/std_line_buffer.rb
confctl-1.0.0 lib/confctl/std_line_buffer.rb