Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
terminal.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3
4#pragma once
5
6#include <portals/geometry.hpp>
7#include <portals/input.hpp>
8
9#include <chrono>
10#include <cstddef>
11#include <cstdint>
12#include <expected>
13#include <memory>
14#include <optional>
15#include <stop_token>
16#include <string>
17#include <vector>
18
19namespace portals {
20
22using TerminalDeadline = std::chrono::steady_clock::time_point;
23
26 std::intptr_t input{};
27 std::intptr_t output{};
28};
29
32 AUTO,
33 OFF
34};
35
41
48
51 std::size_t max_input_bytes{4096};
52 std::size_t max_input_events{256};
53 std::size_t max_output_bytes{1024 * 1024};
54 std::chrono::milliseconds wait_cadence{10};
56};
57
71
77
85
94
98 std::string bytes;
99 std::vector<InputEvent> events;
100 std::optional<CellSize> resized;
101 std::optional<TerminalError> error;
102 std::optional<InputCoordinateMode>
104};
105
109 std::size_t completed{};
110 std::size_t pending{};
111 std::optional<TerminalError> error;
112};
113
114namespace detail {
115class NativeTerminal;
116}
117
131public:
142 [[nodiscard]] static std::expected<TerminalSession, TerminalError> open(NativeTerminalHandles handles,
143 TerminalOptions options = {});
155 ~TerminalSession() noexcept;
156
168 [[nodiscard]] TerminalRead read(TerminalDeadline deadline, std::stop_token stop = {});
172 [[nodiscard]] std::expected<CellSize, TerminalError> size() const noexcept;
178 [[nodiscard]] std::expected<void, TerminalError> submit(std::string bytes);
191 [[nodiscard]] TerminalWrite flush(TerminalDeadline deadline, std::stop_token stop = {});
195 [[nodiscard]] std::size_t pendingBytes() const noexcept;
206 [[nodiscard]] std::expected<void, TerminalError> close() noexcept;
207
208private:
209 friend class detail::NativeTerminal;
213 [[nodiscard]] std::expected<void, TerminalError> saveCursorVisibility() noexcept;
214
222 [[nodiscard]] std::expected<void, TerminalError> joinOutputWriter() noexcept;
223
231 [[nodiscard]] std::expected<void, TerminalError> closeWithOutput(std::string bytes,
232 TerminalDeadline deadline) noexcept;
233 struct Impl;
234 explicit TerminalSession(std::unique_ptr<Impl> impl) noexcept;
235 std::unique_ptr<Impl> impl_;
236};
237
238} // namespace portals
TerminalSession(TerminalSession &&other) noexcept
Transfer ownership; source becomes closed.
TerminalSession(const TerminalSession &)=delete
Exclusive state cannot be copied.
TerminalRead read(TerminalDeadline deadline, std::stop_token stop={})
Read one bounded owning batch, changed size, deadline, stop, EOF or error. Stop takes precedence over...
~TerminalSession() noexcept
Best-effort restoration and descriptor cleanup; never flushes.
std::expected< void, TerminalError > close() noexcept
Discard pending bytes and restore all changed modes/flags independently. Always closes owned duplicat...
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...
TerminalSession & operator=(const TerminalSession &)=delete
Exclusive state cannot be copied.
std::size_t pendingBytes() const noexcept
Number of pending owned bytes; zero for a closed/moved-from session.
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...
TerminalSession & operator=(TerminalSession &&other) noexcept
Best-effort close old state, then transfer.
TerminalWrite flush(TerminalDeadline deadline, std::stop_token stop={})
Write until batch completion, deadline, stop or error; retain unconfirmed bytes. Only successful nati...
Public Portals API.
Definition action.hpp:8
TerminalErrorKind
Terminal failure classification; native_code provides platform detail.
Definition terminal.hpp:59
@ INPUT_DECODE
Windows input decoder failed; close and reopen the session.
Definition terminal.hpp:66
@ OUTPUT_UNCERTAIN
Windows output was cancelled/failed; close before further submission.
Definition terminal.hpp:67
@ INPUT_PROTOCOL
A correlated wire-coordinate transition failed; close restores owned modes.
Definition terminal.hpp:69
@ SYSTEM
A native system operation failed.
Definition terminal.hpp:62
@ FAILED
The coroutine returned an error or threw.
Definition async.hpp:46
NativeInputOrder
Host declaration; a successful DECRPM alone cannot establish this guarantee.
Definition terminal.hpp:37
@ UNCONFIRMED
Decline precision without changing ordinary startup.
Definition terminal.hpp:38
@ FIFO
Complete input packets precede replies to subsequently processed queries, without interleaving.
Definition terminal.hpp:39
@ OUTPUT_PENDING
Transport retained an unfinished suffix.
Definition diagnostics.hpp:57
@ INVALID_OPTIONS
Runner options are invalid, or a native run was requested with an injected clock.
Definition application.hpp:31
@ CLOSED
Owner, node scope, scheduler or completion endpoint is closed.
Definition async.hpp:28
TerminalProtocolIssue
Concrete protocol context failures, independent of native errno values.
Definition terminal.hpp:73
@ INITIAL_PIXEL_CONTEXT_UNKNOWN
Input predates managed cell setup while 1016 is already active.
Definition terminal.hpp:75
std::chrono::steady_clock::time_point TerminalDeadline
Absolute monotonic time used by terminal waits.
Definition terminal.hpp:22
@ UNSUPPORTED
The application explicitly excludes this annotated phase.
Definition key_binding.hpp:44
@ NONE
Caption only, including a single-glyph action button.
Definition controls.hpp:157
NativePointerPolicy
Optional native pointer enhancement; ordinary cell input remains the fallback.
Definition terminal.hpp:31
@ AUTO
Negotiate precision only when the host ordering contract is declared and optional probes succeed.
Definition terminal.hpp:32
TerminalStatus
Why an input or output operation returned.
Definition terminal.hpp:87
@ END_OF_INPUT
Input ended after previously readable bytes were delivered.
Definition terminal.hpp:91
@ DEADLINE
Absolute deadline reached; inspect error for Windows output uncertainty.
Definition terminal.hpp:89
@ PROGRESS
Input/resize available, or the entire pending output batch completed.
Definition terminal.hpp:88
@ INPUT_LIMIT
Input bytes exceed the selected budget.
Definition pss_data.hpp:155
@ STOPPED
Stop was requested; no further publications are accepted.
Definition scheduler.hpp:49
@ OUTPUT_LIMIT
The complete transaction cannot fit the configured byte cap.
Definition diff.hpp:24
Explicit resource and timing policy, independent of terminal capabilities.
Definition input.hpp:437
Advanced native pointer policy; no physical host qualification is inferred.
Definition terminal.hpp:43
NativeInputOrder ordering
Explicit supported-host ordering declaration.
Definition terminal.hpp:45
NativePointerPolicy policy
Optional enhancement policy.
Definition terminal.hpp:44
std::chrono::milliseconds probeTimeout
Positive optional probe cap; at most one quarter of setup budget.
Definition terminal.hpp:46
Borrowed native descriptors supplied to open(); no Windows headers required.
Definition terminal.hpp:25
std::intptr_t input
POSIX fd or borrowed Windows HANDLE converted to intptr_t.
Definition terminal.hpp:26
std::intptr_t output
POSIX fd or borrowed Windows HANDLE converted to intptr_t.
Definition terminal.hpp:27
Owning error value, independent of session lifetime.
Definition terminal.hpp:79
int native_code
errno on POSIX, GetLastError on Windows; zero for contract errors.
Definition terminal.hpp:81
TerminalProtocolIssue protocol_issue
Owning protocol classification; NONE preserves legacy error aggregates.
Definition terminal.hpp:83
int restoration_code
First rollback native error on failed open; zero on success.
Definition terminal.hpp:82
TerminalErrorKind kind
Stable failure category.
Definition terminal.hpp:80
Session resource bounds and cancellation/resize sampling cadence.
Definition terminal.hpp:50
std::size_t max_input_bytes
Positive maximum POSIX raw bytes per read; unused on Windows.
Definition terminal.hpp:51
std::size_t max_input_events
Positive Windows event and native record work bound per read.
Definition terminal.hpp:52
std::chrono::milliseconds wait_cadence
Positive maximum individual poll interval.
Definition terminal.hpp:54
std::size_t max_output_bytes
Positive maximum size of one owned output batch.
Definition terminal.hpp:53
InputOptions windows_input
Windows VT decoder limits/timeouts; unused on POSIX.
Definition terminal.hpp:55
Bounded owning transport result; earlier results are never invalidated.
Definition terminal.hpp:96
std::optional< CellSize > resized
Newly observed dimensions, if changed.
Definition terminal.hpp:100
std::optional< TerminalError > error
Present exactly when status is error.
Definition terminal.hpp:101
std::vector< InputEvent > events
Native semantic events; empty on POSIX.
Definition terminal.hpp:99
std::optional< InputCoordinateMode > coordinate_mode
Proven units before this batch; preserve partial decoder state.
Definition terminal.hpp:103
TerminalStatus status
Completion reason.
Definition terminal.hpp:97
std::string bytes
POSIX raw bytes; decode with InputDecoder.
Definition terminal.hpp:98
Owning output progress for this flush call, not a rendered-frame commit.
Definition terminal.hpp:107
std::size_t pending
Unconfirmed bytes still owned; Windows cancellation forbids replay.
Definition terminal.hpp:110
std::optional< TerminalError > error
Native error, or OUTPUT_UNCERTAIN with Windows deadline/stopped.
Definition terminal.hpp:111
std::size_t completed
Bytes successfully written in this call only.
Definition terminal.hpp:109
TerminalStatus status
Completion reason.
Definition terminal.hpp:108