Skip to content
docsv0.3.0

StreamTerminal

Terminal backed by PHP input/output streams. The PHP analog of pi-tui's `ProcessTerminal`. Defaults to `STDIN` / `STDOUT` and uses `stty` to toggle raw mode, so it works on any POSIX terminal the way the existing `RetainedTuiLoop::run()` does — but now behind a contract the loop can swap out. A `StreamTerminal` owns no state beyond the streams it was given and the saved `stty` settings; it is safe to construct one per loop session and discard it on {@see stop()}.

StreamTerminal::__construct()

public function __construct(?string $title = null, ?mixed $input = null, ?mixed $output = null):

Parameters

Parameters of __construct()
NameTypeDescription
$title?string
$input(resource | null)Stream the terminal reads input bytes from.
$output(resource | null)Stream the terminal writes ANSI output to.

StreamTerminal::start()

public function start(callable $onInput, callable $onResize): void

Puts the terminal in raw mode and begins delivering input and resize events. Saves the previous terminal settings so {@see self::stop()} can restore them.

Parameters

Parameters of start()
NameTypeDescription
$onInputcallable
$onResizecallable

StreamTerminal::stop()

public function stop(): void

Restores the terminal to how it was found: previous settings, visible cursor, default resize handling.

StreamTerminal::write()

public function write(string $data): void

Writes raw bytes to the output stream, unmodified.

Parameters

Parameters of write()
NameTypeDescription
$datastring

StreamTerminal::columns()

public function columns(): int

The terminal's current width in columns.

StreamTerminal::rows()

public function rows(): int

The terminal's current height in rows.

StreamTerminal::moveBy()

public function moveBy(int $lines): void

Moves the cursor by the given number of lines, negative being upwards.

Parameters

Parameters of moveBy()
NameTypeDescription
$linesint

StreamTerminal::hideCursor()

public function hideCursor(): void

Hides the hardware cursor.

StreamTerminal::showCursor()

public function showCursor(): void

Shows the hardware cursor.

StreamTerminal::clearLine()

public function clearLine(): void

Clears the line the cursor is on.

StreamTerminal::clearFromCursor()

public function clearFromCursor(): void

Clears from the cursor to the end of the screen.

StreamTerminal::clearScreen()

public function clearScreen(): void

Clears the whole screen.

StreamTerminal::setTitle()

public function setTitle(string $title): void

Sets the terminal window title.

Parameters

Parameters of setTitle()
NameTypeDescription
$titlestring

StreamTerminal::pollInput()

public function pollInput(): string

Returns the current input byte chunk waiting on the input stream, or `''` when nothing is pending. The loop polls this on each tick; the method is intentionally non-blocking so a busy loop stays responsive.

StreamTerminal::atEndOfInput()

public function atEndOfInput(): bool

True once the input stream is drained. A real terminal never reaches this: `feof()` on a TTY stays false for as long as the device is open.

StreamTerminal::dispatchInput()

public function dispatchInput(string $bytes): void

Feeds raw input bytes to the registered handler, used to drive the terminal from a test or another source instead of the real stream.

Parameters

Parameters of dispatchInput()
NameTypeDescription
$bytesstring