Class: ReadlineNonblock Private

Inherits:
Object show all
Defined in:
readline_nonblock.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

An IO wrapper class that allows performing non-blocking line reads on the provided instance. It is undefined behaviour to run this with other modifying IO operations, e.g. IO#read or IO#seek, on the same instance.

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • io (IO)


12
13
14
15
16
# File 'readline_nonblock.rb', line 12

def initialize(io)
  @io = io
  @buffer = T.let(+"", String)
  @line = T.let(+"", String)
end

Instance Method Details

#readString

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Reads and returns a line ending with "\n" or remaining text before EOF. Non-blocking reads should return similar output as IO#readline with "\n", while reads that would block raise IO::WaitReadable.

Note that this method does not support the global line separator $/. Also it does not modify $_.

Returns:

Raises:

  • (IO::WaitReadable)

    if read would block

  • (EOFError)

    on EOF



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'readline_nonblock.rb', line 29

def read
  begin
    loop do
      if (index = @buffer.index("\n"))
        @line.concat(@buffer.slice!(0..index).to_s)
        break
      end

      @line.concat(@buffer)
      @buffer.clear
      @io.read_nonblock(BUFFER_SIZE, @buffer)
    end
  rescue EOFError
    raise if @line.empty?
  end

  line = @line.freeze
  @line = +""
  line
end