Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
diff.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <memory>
6#include <portals/diagnostics.hpp>
7
8#include <portals/cell_buffer.hpp>
9#include <portals/terminal.hpp>
10
11#include <expected>
12#include <optional>
13#include <string>
14
15namespace portals {
21
27
47[[nodiscard]] std::expected<std::string, DiffError> encodeDiff(const CellBuffer *previous, const CellBuffer &next,
48 std::size_t max_bytes = 1024 * 1024,
50
59public:
60 virtual ~FrameOutput() = default;
65 [[nodiscard]] virtual std::expected<void, TerminalError> submit(std::string bytes) = 0;
71 [[nodiscard]] virtual TerminalWrite flush(TerminalDeadline deadline, std::stop_token stop) = 0;
72};
73
78class TerminalFrameOutput final : public FrameOutput {
79public:
83 explicit TerminalFrameOutput(TerminalSession &session) noexcept;
88 [[nodiscard]] std::expected<void, TerminalError> submit(std::string bytes) override;
94 [[nodiscard]] TerminalWrite flush(TerminalDeadline deadline, std::stop_token stop) override;
95
96private:
97 TerminalSession &session_;
98};
99
102 std::size_t max_output_bytes{1024 * 1024};
103 std::size_t max_frame_storage_bytes{64 * 1024 * 1024};
105};
106
114
129public:
135 explicit FrameWriter(FrameOutput &output, FrameWriterLimits limits = {},
136 std::shared_ptr<Diagnostics> diagnostics = {}) noexcept;
137 FrameWriter(const FrameWriter &) = delete;
138 FrameWriter &operator=(const FrameWriter &) = delete;
147 [[nodiscard]] std::expected<void, FrameError> submit(CellBuffer &&frame, DiagnosticContext context = {});
157 [[nodiscard]] TerminalWrite flush(TerminalDeadline deadline, std::stop_token stop = {});
163 [[nodiscard]] const CellBuffer *committedFrame() const noexcept;
167 [[nodiscard]] bool pending() const noexcept;
171 [[nodiscard]] bool poisoned() const noexcept;
172
173private:
174 void poison() noexcept;
175 void trace(DiagnosticEvent event, DiagnosticReason reason) noexcept;
176 std::shared_ptr<Diagnostics> diagnostics_;
177 DiagnosticContext diagnostic_{};
178 bool diagnostic_submission_{};
179 FrameOutput &output_;
180 FrameWriterLimits limits_;
181 std::optional<CellBuffer> committed_;
182 std::optional<CellBuffer> pending_;
183 std::string bytes_;
184 std::size_t remaining_{};
185 bool submitted_{};
186 bool poisoned_{};
187};
188} // namespace portals
Owning bounded cell framebuffer and immutable-width-policy intern pools.
Definition cell_buffer.hpp:203
Explicit, thread-safe metadata recorder. Disabled by default; no clocks or I/O. enable/disable/snapsh...
Definition diagnostics.hpp:141
Exclusive transport boundary, borrowed by FrameWriter for its lifetime. Implementations own submitted...
Definition diff.hpp:58
virtual TerminalWrite flush(TerminalDeadline deadline, std::stop_token stop)=0
Flush the owned suffix; report bytes for this call and total pending.
virtual std::expected< void, TerminalError > submit(std::string bytes)=0
Take an owning batch, or reject without output; allocation may throw.
virtual ~FrameOutput()=default
Destroy the adapter, without an implicit flush.
bool poisoned() const noexcept
Whether unknown side effects permanently prohibit further output.
FrameWriter(const FrameWriter &)=delete
Exclusive output cannot be shared by copying.
FrameWriter(FrameOutput &output, FrameWriterLimits limits={}, std::shared_ptr< Diagnostics > diagnostics={}) noexcept
Borrow exclusive output.
const CellBuffer * committedFrame() const noexcept
Borrow last fully transmitted frame; null initially/after error. This is historical during partial ou...
TerminalWrite flush(TerminalDeadline deadline, std::stop_token stop={})
Perform transport I/O; commit only after complete, consistent progress. Must run outside paint/measur...
std::expected< void, FrameError > submit(CellBuffer &&frame, DiagnosticContext context={})
Encode and take frame only on success; reject busy/poisoned/limits without I/O. Semantically unchange...
FrameWriter & operator=(const FrameWriter &)=delete
Exclusive output cannot be reassigned.
bool pending() const noexcept
Whether a transaction still requires completion; no allocation/errors.
std::expected< void, TerminalError > submit(std::string bytes) override
Forward ownership.
TerminalWrite flush(TerminalDeadline deadline, std::stop_token stop) override
Forward I/O.
TerminalFrameOutput(TerminalSession &session) noexcept
Borrow a stable session.
Move-only terminal mode and I/O owner; no I/O belongs in paint/measure.
Definition terminal.hpp:130
Public Portals API.
Definition action.hpp:8
FrameError
Submission failure; the supplied frame remains owned by the caller.
Definition diff.hpp:107
@ FRAME_LIMIT
Frame allocation accounting exceeds the configured cap.
Definition diff.hpp:110
@ POISONED
Unknown output state; close/reestablish terminal before creating a new writer.
Definition diff.hpp:109
@ PENDING
Complete or abandon the owning session before submitting another frame.
Definition diff.hpp:108
DiagnosticReason
Enumerated evidence, never an exception message or inferred model value.
Definition diagnostics.hpp:39
std::chrono::steady_clock::time_point TerminalDeadline
Absolute monotonic time used by terminal waits.
Definition terminal.hpp:22
ColorMode
Color encoding selected explicitly by the terminal owner.
Definition diff.hpp:17
@ ANSI16
Nearest standard RGB palette entry encoded as SGR 30-37/90-97 and 40-47/100-107.
Definition diff.hpp:19
@ TRUE_COLOR
RGB SGR 38;2/48;2, preserving the existing default.
Definition diff.hpp:18
DiagnosticEvent
Metadata stages; no stage contains application text or terminal bytes.
Definition diagnostics.hpp:16
std::expected< std::string, DiffError > encodeDiff(const CellBuffer *previous, const CellBuffer &next, std::size_t max_bytes=1024 *1024, ColorMode colorMode=ColorMode::TRUE_COLOR)
Encode changed complete rows into one owning VT transaction.
DiffError
Bounded encoding failure; input buffers are unchanged.
Definition diff.hpp:23
@ OUTPUT_LIMIT
The complete transaction cannot fit the configured byte cap.
Definition diff.hpp:24
@ ALLOCATION_FAILURE
Storage allocation failed; the original buffer remains unchanged.
Definition cell_buffer.hpp:160
Fixed-size causal token; zero fields mean unobserved, never a reused identity.
Definition diagnostics.hpp:92
Owning writer resource caps. At most two capped frames coexist.
Definition diff.hpp:101
ColorMode colorMode
Fixed output palette for this writer's complete lifetime.
Definition diff.hpp:104
std::size_t max_frame_storage_bytes
Per-frame CellBuffer::memory().total_bytes cap.
Definition diff.hpp:103
std::size_t max_output_bytes
Complete VT transaction cap.
Definition diff.hpp:102
Owning output progress for this flush call, not a rendered-frame commit.
Definition terminal.hpp:107