class
Termisu::Reader
- Termisu::Reader
- Reference
- Object
Overview
Input reader abstraction for terminal I/O.
Provides buffered, non-blocking input operations with support for peeking, timeouts, and availability checking.
EINTR Handling: All system calls (select, read) are wrapped with retry logic to handle interrupted system calls (EINTR). This ensures reliable operation when signals are delivered during I/O operations.
Example:
terminal = Termisu::Terminal.new
reader = Termisu::Reader.new(terminal.infd)
if byte = reader.read_byte
puts "Read: #{byte.chr}"
end
reader.close
Defined in:
termisu/reader.crConstant Summary
-
Log =
Termisu::Logs::Reader -
MAX_EINTR_RETRIES =
100 -
Maximum retry attempts for EINTR before giving up. This prevents infinite loops in pathological signal storms.
Constructors
-
.new(fd : Int32, buffer_size : Int32 = 128)
Creates a new reader for the given file descriptor.
Instance Method Summary
-
#available? : Bool
Checks if data is available for reading.
-
#clear_buffer
Clears any buffered data.
-
#close
Closes the reader (does not close the file descriptor).
-
#peek_byte : UInt8 | Nil
Peeks at the next byte without consuming it.
-
#read_byte : UInt8 | Nil
Reads a single byte from the input.
-
#read_bytes(count : Int32) : Bytes | Nil
Reads exactly
countbytes from the input. -
#wait_for_data(timeout_ms : Int32) : Bool
Waits for data with a timeout.
Constructor Detail
Creates a new reader for the given file descriptor.
fd- File descriptor to read frombuffer_size- Internal buffer size (default: 128 bytes)
Instance Method Detail
Checks if data is available for reading.
Uses select(2) with zero timeout for non-blocking check.
Peeks at the next byte without consuming it.
Returns nil if no data is available.
Reads a single byte from the input.
Returns nil if no data is available or on EOF.
This is a non-blocking operation when the terminal is in raw mode.
Reads exactly count bytes from the input.
Returns nil if fewer than count bytes are available.
Blocks until all bytes are read or timeout occurs.
Waits for data with a timeout.
timeout_ms- Timeout in milliseconds
Returns true if data becomes available, false on timeout.