class

Phosphor::AnsiBackend

Inherits Phosphor::Backend < Reference < Object

The real-terminal Backend — drives a live ANSI terminal via raw mode, the alternate screen buffer, and standard escape sequences.

This is the extraction of what Terminal did directly before Phase 27: same LibC calls, same escape sequences, same event parsing — moved here so Terminal can become a thin layer over it (see Terminal's own doc comment) and so a future non-terminal Backend (e.g. TestBackend) has a working example to model itself on.

RenderMode (fullscreen vs. inline) is deliberately not a concept this class knows about — that's Terminal-level policy layered on top. AnsiBackend#enter/#exit implement the plain "take over a terminal with the alternate screen buffer" sequence; Terminal in Inline mode never calls these two methods at all (it holds a Backend rather than inheriting from one, and reimplements the small raw-mode/mouse/paste subset of this behavior itself — see Terminal's own doc comment) since inline sessions must never touch the alternate screen.

See TASKS.md § "Phase 27: Pluggable backends" for the full rationale.

Constructors

new(tty : IO = File.open("/dev/tty", "w"), stdin : IO = STDIN)

tty overrides the terminal device #enter/#exit/#write/#size act on — for tests only; production code should never pass this. stdin overrides the IO #read_event reads from, passed through to the lazily-constructed StdinPort — same rationale, tests only.

Source

Instance methods

enter(mouse : Bool = false) : Void

Enters raw mode and switches to the alternate screen buffer, hides the cursor, clears it, and enables bracketed paste — plus mouse tracking if mouse is true.

Source
exit

Disables mouse tracking and bracketed paste (if enabled), leaves the alternate screen buffer, shows the cursor, and restores the termios settings #enter saved — the exact inverse of #enter.

Source
mouse_enabled=(mouse_enabled : Bool)

Whether mouse tracking is currently on — set by #enter's mouse argument, read by #exit's write_mouse_and_paste_disable to decide whether the disable sequence is needed. Also settable directly for tests that never call #enter at all.

Source
mouse_enabled?

Whether mouse tracking is currently on — set by #enter's mouse argument, read by #exit's write_mouse_and_paste_disable to decide whether the disable sequence is needed. Also settable directly for tests that never call #enter at all.

Source
read_event

Non-blocking read of the next input event, via a lazily-constructed StdinPort (see @stdin_port's own doc comment for why lazy). Delegates entirely to StdinPort#poll — same paste/mouse/key parsing, not a reimplementation of it.

Source
size

Returns the current terminal dimensions as {cols, rows}.

Queries the kernel via ioctl(TIOCGWINSZ) on every call — no caching. Queries against @tty, not STDOUTTIOCGWINSZ only works on a fd connected to a real tty, and fails with ENOTTY on a redirected STDOUT.

Source
write(diff : Array(Diff)) : Void

Translates diff into ANSI escape sequences and writes them to @tty — the diff-to-ANSI generation that used to live in Renderer#draw, plus the actual write (Terminal#write's old job).

Cell::CONTINUATION slots are skipped — the wide character's primary cluster already occupies two terminal columns when written. No-op (no cursor-hide/show pair written either) when diff is empty, matching Renderer#draw's existing unless diffs.empty? guard.

Source