Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
portals::TerminalSession Class Reference

Move-only terminal mode and I/O owner; no I/O belongs in paint/measure. More...

#include <terminal.hpp>

Public Member Functions

 TerminalSession (const TerminalSession &)=delete
 Exclusive state cannot be copied.
TerminalSession & operator= (const TerminalSession &)=delete
 Exclusive state cannot be copied.
 TerminalSession (TerminalSession &&other) noexcept
 Transfer ownership; source becomes closed.
TerminalSession & operator= (TerminalSession &&other) noexcept
 Best-effort close old state, then transfer.
 ~TerminalSession () noexcept
 Best-effort restoration and descriptor cleanup; never flushes.
TerminalRead read (TerminalDeadline deadline, std::stop_token stop={})
 Read one bounded owning batch, changed size, deadline, stop, EOF or error. Stop takes precedence over an expired deadline. Resize is sampled on the owner, at most wait_cadence apart while waiting; changes can coalesce. No callbacks. Windows returns events only, using windows_input for VT/paste decoding. An empty progress batch yields after the native record work budget. Paste is emitted at its end delimiter; interleaved native events may precede that completed event.
std::expected< CellSize, TerminalError > size () const noexcept
 Query current columns/rows; returns closed/system error without mutation.
std::expected< void, TerminalError > submit (std::string bytes)
 Transfer one bounded owned byte batch; reject while output is pending. No I/O. Empty batch is allowed. Rejection leaves session state unchanged.
TerminalWrite flush (TerminalDeadline deadline, std::stop_token stop={})
 Write until batch completion, deadline, stop or error; retain unconfirmed bytes. Only successful native writes count as completed. Deadline/stop are checked between nonblocking syscalls on POSIX, preserving its retryable suffix. Windows uses one owning worker. Cancelling an issued write returns deadline or stopped with error=OUTPUT_UNCERTAIN; bytes remain owned, but cannot be retried. Further submit/flush fail until close. A pre-expired/pre-stopped call that has issued no write preserves retryability and has no uncertainty error. Successful writes do not prove terminal interpretation or rendered-frame state.
std::size_t pendingBytes () const noexcept
 Number of pending owned bytes; zero for a closed/moved-from session.
std::expected< void, TerminalError > close () noexcept
 Discard pending bytes and restore all changed modes/flags independently. Always closes owned duplicate descriptors, even on restoration error. Returns first native restoration/close error. Idempotent; destructor cannot report errors. Windows first requests cancellation until the worker actually completes, joins, then restores modes/code page. Console-host progress is required: restoration can wait for a paused host to resume output drain. No hard deadline is promised for close/destruction or arbitrary stalled drivers/hosts; nothing is detached. Incomplete/failed output leaves renderer belief uncertain, owned by the caller.

Static Public Member Functions

static std::expected< TerminalSession, TerminalError > open (NativeTerminalHandles handles, TerminalOptions options={})
 Save both original states before mutation and enter raw nonblocking mode. Both POSIX descriptors must be TTYs; both Windows handles must be console handles. Windows enables VT input/output and UTF-8 output, preserving the input code page. On failure, independently restore everything already changed; restoration_code reports rollback failure. Allocation can throw.

Friends

class detail::NativeTerminal

Detailed Description

Move-only terminal mode and I/O owner; no I/O belongs in paint/measure.

One executor owns all calls including destruction; concurrent calls are unsupported. Transfer between executors requires external synchronization. POSIX and Windows open duplicate both native handles and own the duplicates; caller handles stay open. All aliases of their terminals/open-file descriptions must be exclusively used by this owner until close, because termios and file status flags are shared. Windows console modes and output code page are console-wide, including all aliases. Input/output values own storage. Allocation exceptions propagate with RAII cleanup; no caller buffer/view is retained. This backend installs no signal handlers. No guarantee is made for abrupt process death or externally closed/modified handles.

Constructor & Destructor Documentation

◆ TerminalSession()

portals::TerminalSession::TerminalSession ( TerminalSession && other)
noexcept

Transfer ownership; source becomes closed.

Parameters
otherSession whose owned descriptors and mode-restoration responsibility are transferred.

Member Function Documentation

◆ close()

std::expected< void, TerminalError > portals::TerminalSession::close ( )
nodiscardnoexcept

Discard pending bytes and restore all changed modes/flags independently. Always closes owned duplicate descriptors, even on restoration error. Returns first native restoration/close error. Idempotent; destructor cannot report errors. Windows first requests cancellation until the worker actually completes, joins, then restores modes/code page. Console-host progress is required: restoration can wait for a paused host to resume output drain. No hard deadline is promised for close/destruction or arbitrary stalled drivers/hosts; nothing is detached. Incomplete/failed output leaves renderer belief uncertain, owned by the caller.

Returns
Success, including repeated close, or the first native restoration/cleanup error.

◆ flush()

TerminalWrite portals::TerminalSession::flush ( TerminalDeadline deadline,
std::stop_token stop = {} )
nodiscard

Write until batch completion, deadline, stop or error; retain unconfirmed bytes. Only successful native writes count as completed. Deadline/stop are checked between nonblocking syscalls on POSIX, preserving its retryable suffix. Windows uses one owning worker. Cancelling an issued write returns deadline or stopped with error=OUTPUT_UNCERTAIN; bytes remain owned, but cannot be retried. Further submit/flush fail until close. A pre-expired/pre-stopped call that has issued no write preserves retryability and has no uncertainty error. Successful writes do not prove terminal interpretation or rendered-frame state.

Parameters
deadlineAbsolute steady-clock deadline for completing the current byte batch.
stopCooperative cancellation; cancellation of issued Windows I/O can make progress uncertain.
Returns
Bytes completed in this call, remaining owned bytes, completion status and optional error.

◆ open()

std::expected< TerminalSession, TerminalError > portals::TerminalSession::open ( NativeTerminalHandles handles,
TerminalOptions options = {} )
staticnodiscard

Save both original states before mutation and enter raw nonblocking mode. Both POSIX descriptors must be TTYs; both Windows handles must be console handles. Windows enables VT input/output and UTF-8 output, preserving the input code page. On failure, independently restore everything already changed; restoration_code reports rollback failure. Allocation can throw.

Parameters
handlesBorrowed native input/output handles to duplicate; both must identify supported terminals.
optionsPositive storage/work caps and polling cadence, copied into the session.
Returns
Exclusive session ownership, or an error including any failed rollback in restoration_code.
Exceptions
std::bad_allocIf session or bounded working storage cannot be allocated.

◆ operator=()

TerminalSession & portals::TerminalSession::operator= ( TerminalSession && other)
noexcept

Best-effort close old state, then transfer.

Parameters
otherSession to consume; assignment first releases this session's previous native state.
Returns
This session; a distinct source becomes closed. Restoration errors during assignment cannot be reported.

◆ pendingBytes()

std::size_t portals::TerminalSession::pendingBytes ( ) const
nodiscardnoexcept

Number of pending owned bytes; zero for a closed/moved-from session.

Returns
Bytes whose successful transmission has not been confirmed; uncertain bytes must not be replayed.

◆ read()

TerminalRead portals::TerminalSession::read ( TerminalDeadline deadline,
std::stop_token stop = {} )
nodiscard

Read one bounded owning batch, changed size, deadline, stop, EOF or error. Stop takes precedence over an expired deadline. Resize is sampled on the owner, at most wait_cadence apart while waiting; changes can coalesce. No callbacks. Windows returns events only, using windows_input for VT/paste decoding. An empty progress batch yields after the native record work budget. Paste is emitted at its end delimiter; interleaved native events may precede that completed event.

Parameters
deadlineAbsolute steady-clock deadline bounding this read operation.
stopCooperative cancellation request, checked before the deadline.
Returns
An owning input/resize batch, or deadline, stopped, eof or an explicit terminal error.
Exceptions
std::bad_allocIf owning batch storage cannot be allocated.

◆ size()

std::expected< CellSize, TerminalError > portals::TerminalSession::size ( ) const
nodiscardnoexcept

Query current columns/rows; returns closed/system error without mutation.

Returns
Current viewport columns/rows, or closed, unsupported or system failure as applicable.

◆ submit()

std::expected< void, TerminalError > portals::TerminalSession::submit ( std::string bytes)
nodiscard

Transfer one bounded owned byte batch; reject while output is pending. No I/O. Empty batch is allowed. Rejection leaves session state unchanged.

Parameters
bytesComplete byte batch, moved into session storage only on acceptance.
Returns
Success, or closed/OUTPUT_PENDING/OUTPUT_LIMIT/OUTPUT_UNCERTAIN without replacing pending output.

The documentation for this class was generated from the following file: