Skip to main content

Crate termwright_protocol

Crate termwright_protocol 

Source
Expand description

Semantic side-channel client for the termwright terminal test driver.

An instrumented TUI publishes its widget tree over a unix socket and commits each render with a signed OSC marker, so tests can assert on roles and names instead of screen-scraping cells. This crate is the protocol side of that contract: framing, the marker, message and snapshot validation, and a blocking socket client. It ships no framework adapter — wire it into whatever draws your screen.

Dormant rule. Without TERMWRIGHT_ENDPOINT and TERMWRIGHT_TOKEN in the environment, Client::from_env returns None and nothing happens at all: no socket, no marker, no change to what the terminal receives.

use termwright_protocol::{Client, Node, Options, Role, Snapshot};

let mut client = match Client::from_env(Options::new("my-tui", "1.0.0")) {
    Some(client) => client,
    None => return, // not instrumented: render normally and stop here
};
client.connect(termwright_protocol::DIAL_TIMEOUT).expect("handshake");

let mut snapshot = Snapshot::new(80, 24);
snapshot.push(Node::new("root", Role::Dialog, "Permission"));
snapshot.push(Node::new("ok", Role::Button, "Approve").with_parent("root"));

if let Some(marker) = client.publish(&mut snapshot).expect("publish") {
    // Only after the render's last byte has been written.
    print!("{marker}");
}

The normative implementation is the TypeScript package @termwright/protocol; this crate is verified against the shared vectors in clients/test-vectors.

Re-exports§

pub use client::Client;
pub use client::Options;
pub use client::DIAL_TIMEOUT;
pub use client::ENV_ENDPOINT;
pub use client::ENV_TOKEN;
pub use debug::debug_path;
pub use debug::Category;
pub use debug::DebugLog;
pub use debug::ENV_DEBUG;
pub use debug::ENV_DEBUG_FILE;
pub use error::Error;
pub use error::ParseError;
pub use error::ValidationError;
pub use error::Violation;
pub use framing::encode_frame;
pub use framing::project_dto;
pub use framing::Frame;
pub use framing::FrameDecoder;
pub use framing::FRAME_HEADER_BYTES;
pub use limits::Limits;
pub use limits::ABSOLUTE_LIMITS;
pub use limits::DEFAULT_LIMITS;
pub use limits::DEFAULT_NEGOTIATION_MS;
pub use logs::validate_log_record;
pub use logs::AttrValue;
pub use logs::LogLevel;
pub use logs::LogRecord;
pub use logs::LOG_LEVELS;
pub use logs::MAX_LOG_ATTRS;
pub use marker::compute_mac;
pub use marker::encode_marker;
pub use marker::verify_marker_payload;
pub use marker::RenderMarker;
pub use marker::MARKER_MAC_BYTES;
pub use marker::MARKER_OSC_CODE;
pub use marker::MARKER_OSC_PREFIX;
pub use messages::parse_adapter_message;
pub use messages::parse_driver_message;
pub use messages::DegradedSessionCapability;
pub use messages::EvidenceProviderRegistration;
pub use messages::ProbeIdentityKind;
pub use messages::ProbeInfo;
pub use messages::ProbeInjectionTier;
pub use messages::ProbeInstrumentation;
pub use messages::ProbeSemanticClass;
pub use messages::PROTOCOL_ID;
pub use messages::PROTOCOL_VERSION;
pub use publication_queue::PublicationQueue;
pub use roles::Action;
pub use roles::Capability;
pub use roles::Role;
pub use tree::Cursor;
pub use tree::CursorShape;
pub use tree::EvidenceMethod;
pub use tree::EvidenceProvenance;
pub use tree::EvidenceSource;
pub use tree::EvidenceStrength;
pub use tree::Node;
pub use tree::NodeGeometryObservations;
pub use tree::Observation;
pub use tree::Orientation;
pub use tree::PhysicalInputRecipe;
pub use tree::PhysicalInputRecipeAction;
pub use tree::PhysicalInputRecipeStep;
pub use tree::PointerHitGrid;
pub use tree::PointerHitRegion;
pub use tree::Provenance;
pub use tree::ProviderActionRecipes;
pub use tree::ProviderFocusState;
pub use tree::ProviderPaintedRegion;
pub use tree::ProviderPointerRegion;
pub use tree::ProviderPointerSpan;
pub use tree::ProviderRevisionEvidence;
pub use tree::ProviderScrollState;
pub use tree::ProviderTerminalInputModes;
pub use tree::Rect;
pub use tree::ScrollState;
pub use tree::SemanticPaintedRegion;
pub use tree::SemanticValueObservation;
pub use tree::SemanticValueSensitivity;
pub use tree::Snapshot;
pub use tree::State;
pub use tree::TextRange;

Modules§

client
Bounded local-socket client for the semantic side-channel.
debug
Opt-in diagnostic log for the adapter side, written to a file.
error
Error types shared by the protocol modules.
evidence
Session-scoped application evidence providers.
framing
Wire framing: a 4-byte big-endian length prefix and a UTF-8 JSON body.
limits
Protocol limits. Callers may tighten the defaults, never widen the maxima.
logs
Application log records carried over the semantic channel.
marker
Render-commit marker.
messages
Wire messages: typed builders for what an adapter sends, checked parsers for what it receives.
publication_queue
Ordered, bounded semantic publication off a framework render thread.
roles
Closed vocabularies. Unknown members are rejected, never passed through.
schema_keys
The fields a node and a state may carry, as this client knows them.
tree
Semantic tree DTOs.

Functions§

validate_snapshot
Checks unique ids, existing and acyclic parents, the closed role, action and state vocabularies, bounded strings and counts, and rects that intersect the viewport unless the node is hidden.