Skip to main content

EventSource

Struct EventSource 

Source
pub struct EventSource<I>
where I: Input,
{ /* private fields */ }
Expand description

Wakeable event source backed by a platform readiness primitive.

EventSource is the synchronous owner of terminal input. It stores pending bytes, a Decoder, an event queue, deadline state for ambiguous ESC prefixes, bracketed pastes, and clipboard replies, and the platform handles for wakeups and resize notifications.

Construct it with EventSource::new. Use EventSource::poll to perform I/O and wait for queued events, EventSource::try_read to pop an already queued event, or EventSource::read to block until one event is available. The type is generic over the platform Input handle.

Implementations§

Source§

impl<I> EventSource<I>
where I: Input,

Source

pub fn vt_input(&self) -> bool

Return whether VT input serialization is enabled.

Unix input is always VT bytes, so this returns true on Unix.

Source

pub fn set_vt_input(&mut self, enabled: bool)

Select how console key records are serialized.

On Windows, set enabled to match the console’s ENABLE_VIRTUAL_TERMINAL_INPUT bit. With false, native console keys go through the win32-input decoder. With true, character records supply VT input. EventSource::new reads the initial value from the console mode, and has_vt_input reads it from a saved State.

Update this setting after a successful console mode change and before the next read. This method changes only serialization, not console modes or decoder flags. A change discards incomplete UTF-16 pairs.

Unix input is always VT bytes, so this method has no effect on Unix. Portable code can call it on every platform.

Source

pub fn esc_timeout(&self) -> Duration

Return the configured escape-sequence timeout.

This is the duration used to disambiguate a physical Escape key from an Alt-prefixed key or a partial control sequence. Reading it has no side effects and never performs I/O.

Source

pub fn with_esc_timeout(self, timeout: Duration) -> Self

Set the escape-sequence timeout.

timeout is how long the source waits for a continuation byte before a buffered partial escape sequence is force-resolved. The default is DEFAULT_ESC_TIMEOUT. Fresh input resets the deadline for incomplete sequences. Ready continuation bytes take priority over their expiry. A lone Escape expires before the next read once its deadline passes.

A zero duration resolves a lone Escape on the next poll cycle and other ambiguous prefixes when a poll finds no continuation input. String bodies wait for their terminators regardless of this timeout.

This is a consuming builder intended to be chained after EventSource::new. It does not inspect or clear any already-buffered input and never panics.

Source

pub fn with_decoder_flags(self, flags: DecoderFlags) -> Self

Choose how the ambiguous legacy keys are read.

LF reads as Ctrl+J by default. Set DecoderFlags::LF_IS_ENTER to read it as Enter instead. See DecoderFlags for all mappings.

This is a consuming builder intended to be chained after EventSource::new. It never panics.

Source

pub fn set_decoder_flags(&mut self, flags: DecoderFlags)

Choose how the ambiguous legacy keys are read, on a source already in use.

The same choice with_decoder_flags makes, for a caller holding the source rather than building it. Buffered input is left alone: the flags decide how a byte is named, not how it is framed.

Source

pub fn decoder_flags(&self) -> DecoderFlags

How the ambiguous legacy keys are being read.

Source

pub fn backspace_mode(&self) -> bool

Return the decoder’s current Backarrow mode (DECBKM).

Defaults to reset (false).

Source

pub fn set_backspace_mode(&mut self, enabled: bool)

Select Backarrow mode for future legacy input without terminal I/O.

Set (true) selects BS (0x08) for Backspace; reset (false) selects DEL (0x7f). DecoderFlags choose the ambiguous interpretations. Mode reports are returned to the caller for handling. Already queued events keep their decoded values.

Use Program::set_backspace_mode to change both the terminal and decoder.

Source

pub fn with_paste_idle_timeout(self, timeout: Option<Duration>) -> Self

Set the idle timeout for an open bracketed paste.

If no further input arrives before timeout, the source flushes any held bytes as a final Event::PasteChunk, synthesizes Event::PasteEnd with complete: false, and leaves paste mode. Passing None disables this safety net and waits for a real terminator. The default is Some(DEFAULT_PASTE_IDLE_TIMEOUT).

Changing this setting during a paste restarts its idle wait.

Source

pub fn set_paste_idle_timeout(&mut self, timeout: Option<Duration>)

Set the paste idle timeout on an existing source.

This applies the same policy as with_paste_idle_timeout. Changing it during a paste restarts the idle wait; None disables it. Live async streams refresh their readiness waits when the deadline changes.

Source

pub fn with_clipboard_idle_timeout(self, timeout: Option<Duration>) -> Self

Set the idle timeout for an OSC 52 clipboard reply.

After its header, new input restarts the idle wait. Expiry flushes retained bytes as a final Event::ClipboardChunk, then emits Event::ClipboardEnd with complete: false and leaves clipboard mode. The default is Some(DEFAULT_CLIPBOARD_IDLE_TIMEOUT). None disables idle recovery.

This is a recovery heuristic: keys before expiry can become payload, and delayed payload after expiry can become keys.

Source

pub fn set_clipboard_idle_timeout(&mut self, timeout: Option<Duration>)

Change the clipboard idle timeout, restarting the wait in an active reply.

None disables idle recovery. Live async streams refresh their waits when the deadline changes. See Self::with_clipboard_idle_timeout.

Source

pub fn with_string_idle_timeout(self, timeout: Option<Duration>) -> Self

Set the idle timeout for a streamed control string.

After Event::StringStart, new input restarts the idle wait. Expiry flushes retained bytes as a final Event::StringChunk, then emits Event::StringEnd with complete: false and decodes later input normally. The default is Some(DEFAULT_STRING_IDLE_TIMEOUT). None disables idle recovery.

This is a recovery heuristic: keys before expiry can become string bytes, and delayed string bytes after expiry can become keys.

Source

pub fn set_string_idle_timeout(&mut self, timeout: Option<Duration>)

Change the string idle timeout, restarting the wait in an active string.

None disables idle recovery. Live async streams refresh their waits when the deadline changes. See Self::with_string_idle_timeout.

Source

pub fn waker(&self) -> Waker

Return a cloneable Waker bound to this source.

Use the returned handle from another thread to interrupt a blocking EventSource::poll or EventSource::read. Cloning the waker does not clone the source or its input handle.

Source

pub fn handle_resize(&self) -> bool

Return whether out-of-band resize handling is enabled.

On Unix, true means a readable SIGWINCH pipe can produce Event::Resize. On Windows, resize records are delivered in-band and this flag has no practical effect.

Source

pub fn set_handle_resize(&mut self, enable: bool)

Control whether the source delivers Event::Resize from the kernel’s out-of-band window-resize notification (SIGWINCH on Unix). Defaults to true.

Set this to false after enabling in-band resize reports (DEC mode 2048): the terminal then reports size changes in-band as CSI 48 t, which the decoder surfaces as Event::Resize, so leaving the SIGWINCH path on would deliver each resize twice. Restore it to true when in-band reporting is disabled again.

No effect on Windows, where resize is always delivered in-band through the decoder.

Source

pub fn poll(&mut self, timeout: Option<Duration>) -> Result<bool>

Wait up to timeout for at least one event to become available.

This method performs I/O, drains decoder output into the internal queue, handles resize notifications, and resolves any expired ESC or paste deadlines. None means block until an event or wake; Some(Duration::ZERO) means perform a non-blocking readiness pass. A caller timeout preserves an incomplete control string for the next poll.

A platform whose wait takes a whole number of milliseconds rounds a fractional millisecond up, so such a wait can return up to a millisecond after the deadline.

Returns:

  • Ok(true) when the queue has at least one event;
  • Ok(false) when the timeout elapsed or a paired Waker interrupted the wait without producing an event;
  • Err(_) for fatal input or platform readiness errors.

Ready and already queued events take priority and leave the wake pending. A later poll with no event clears the wake and returns Ok(false).

Source

pub fn try_read(&mut self) -> Option<Event>

Return the next queued event without performing I/O.

This takes one event from the internal queue, expanding one occurrence of a repeat run. Call EventSource::poll first when the queue may be empty but input could be ready. Returns None when no event is currently queued.

Source

pub fn read(&mut self) -> Result<Event>

Block until the next event is available, then return it.

This calls EventSource::poll with no caller timeout, then returns the next event. Ready events leave a pending wake for a later read.

Returns io::ErrorKind::Interrupted if a paired Waker fired while waiting, and propagates fatal input/readiness errors.

Source

pub fn unread(&mut self, event: Event)

Return an event to the front of the queue, so the next read / try_read yields it before anything already queued. Use to put back an event read while waiting for a specific reply (e.g. a cursor-position report), preserving it for normal delivery. Restore a batch in original order by unreading in reverse.

Source

pub fn end_paste(&mut self)

Force-exit bracketed paste mode.

If the decoder is currently inside a paste, this flushes any pending bytes as a Event::PasteChunk, queues Event::PasteEnd with complete: false, and clears paste state so later bytes parse as input. Use it when an embedding application enforces a paste size cap, user cancellation, or a custom watchdog. It has no effect outside paste mode and never panics. An active async stream wakes to deliver the queued events.

Source

pub fn end_clipboard(&mut self)

Force-exit an OSC 52 reply.

Flushes retained bytes as a final Event::ClipboardChunk, then queues Event::ClipboardEnd with complete: false. Later input is decoded normally. Use this for a size cap, cancellation, or custom recovery. This has no effect outside a reply. An active async stream wakes to deliver the queued events.

Source

pub fn end_string(&mut self)

Force-exit a streamed control string.

Flushes retained bytes as a final Event::StringChunk, then queues Event::StringEnd with complete: false. Later input is decoded normally. Use this for a size cap, cancellation, or custom recovery. This has no effect outside a streamed string. An active async stream wakes to deliver the queued events.

Source§

impl<I> EventSource<I>
where I: Input,

Source

pub fn new(input: I) -> Result<Self>

Build a new Unix event source for input.

The handle is used both for byte reads and as the TIOCGWINSZ target when SIGWINCH fires, so it should refer to the terminal whose size the caller cares about. Construction creates two non-blocking self-pipes, subscribes to the shared SIGWINCH fan-out, and registers the input, wake, and resize fds with the selected poll backend.

Timeouts start at DEFAULT_ESC_TIMEOUT, DEFAULT_PASTE_IDLE_TIMEOUT, DEFAULT_CLIPBOARD_IDLE_TIMEOUT, and DEFAULT_STRING_IDLE_TIMEOUT. Override them with EventSource::with_esc_timeout, EventSource::with_paste_idle_timeout, EventSource::with_clipboard_idle_timeout, and EventSource::with_string_idle_timeout.

Resize deduplication starts from the size input reports here, so a SIGWINCH that does not actually change the size produces no Event::Resize.

Returns any OS error from pipe creation, fd configuration, SIGWINCH subscription, or poller construction. It does not read from input.

Source§

impl<I> EventSource<I>
where I: Input + 'static,

Source

pub fn into_stream(self) -> EventStream<I>

Convert this source into a thread-backed EventStream.

The source is wrapped in Arc<Mutex<_>> owned solely by the stream. To keep reading the source synchronously alongside the stream, build the Arc<Mutex<_>> yourself and use EventStream::from_shared.

Auto Trait Implementations§

§

impl<I> !RefUnwindSafe for EventSource<I>

§

impl<I> !UnwindSafe for EventSource<I>

§

impl<I> Freeze for EventSource<I>
where I: Freeze,

§

impl<I> Send for EventSource<I>

§

impl<I> Sync for EventSource<I>
where I: Sync,

§

impl<I> Unpin for EventSource<I>
where I: Unpin,

§

impl<I> UnsafeUnpin for EventSource<I>
where I: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.