Expand description
Terminal events and event-stream decoding.
This module owns the core Event enum together with the
internal decoder that parses raw terminal bytes into events, the
platform-specific EventSource that drives the decoder from a
tty, and the key/mouse types that events carry.
§The decode pipeline
Input arrives as a raw byte stream. The source reads bytes (waking on a
self-pipe so another thread can interrupt a blocking read), feeds them to
the decoder, and hands back fully-formed Event values. Escape
sequences may straddle reads, so the decoder buffers partial input and a
short timeout disambiguates a lone Esc key from the start of a CSI/SS3
sequence.
tty input EventSource Decoder caller
───────── ─────────── ─────── ──────
bytes ───────▶ read + buffer ───────▶ scan sequences ─▶ Event
│ ▲ │
│ └── Esc-timeout ◀────────┘ (Esc key vs CSI/SS3?)
└── self-pipe wake ─┘ (interrupt a blocking read from another thread)Build an EventSource over a terminal’s input half and read typed
events in a loop. Keys parse from strings and compare by canonical
chord, so matching a shortcut is plain equality.
use uncurses::event::{Event, EventSource, Key};
use uncurses::terminal::Terminal;
let mut term = Terminal::stdio();
term.make_raw()?;
let mut events = EventSource::new(term.input())?;
let quit: Key = "ctrl+c".parse().unwrap();
loop {
match events.read()? {
Event::KeyPress(ref k) if *k == quit => break,
Event::KeyPress(k) => { let _ = k.code; }
Event::Resize(ws) => { let _ = (ws.col, ws.row); }
_ => {}
}
}
term.restore()§Queries
To ask the terminal a question (its background color, cell size,
device attributes, and so on), write the request bytes from the
ansi module to the output and read the matching reply
event back through the same source. The
Screen facade wraps this in request_* methods
whose replies surface as ordinary events, never swallowing the user’s
keystrokes in between.
§Async
With the async feature, EventStream reads the same events through a
futures_core::Stream, so the loop becomes while let Some(ev) = stream.next().await.
Structs§
- Event
Source - Wakeable event source backed by a platform readiness primitive.
- Event
Stream - A thread-backed [
futures_core::Stream] ofio::Result<Event>over a sharedEventSource. - Key
- A key event.
- KeyModifiers
- Keyboard modifier flags.
- Mouse
- Mouse-event payload with position, button, and modifier state.
- Waker
- Cloneable handle that interrupts an in-progress
EventSource::pollorEventSource::read.
Enums§
- Clipboard
Selection - Which system clipboard selection an OSC 52 event refers to.
- Color
Scheme - Reported terminal color scheme (DEC mode 2031).
- Event
- A terminal event.
- KeyCode
- Logical identity of a key, before modifiers are considered.
- Modify
Other Keys Mode - Decoded modifyOtherKeys mode (
CSI > 4 ; n m). - Mouse
Button - Mouse button or wheel direction associated with a
Mouseevent. - Parse
KeyError - Error produced when parsing a
KeyorKeyCodefrom a binding string.
Constants§
- DEFAULT_
ESC_ TIMEOUT - Default escape-sequence timeout.
- DEFAULT_
PASTE_ IDLE_ TIMEOUT - Default bracketed-paste idle timeout.
Traits§
- Input
- Platform-specific capabilities required from a Unix event input handle.
Functions§
- mouse_
pixel_ to_ cell - Convert an SGR-Pixel mouse payload to cell coordinates.