Skip to main content

spawn_key_reader

Function spawn_key_reader 

Source
pub fn spawn_key_reader(tx: UnboundedSender<ReplEvent>) -> KeyReaderGuard
Expand description

Spawn the dedicated OS thread that polls for crossterm events and forwards translated events onto tx, returning a KeyReaderGuard that stops and joins it on drop.

Why: crossterm’s blocking read is happiest on its own OS thread rather than parking a tokio worker — matches crates/trusty-agents/src/repl/tui/run.rs’s key_thread. Polling (rather than that precedent’s blocking read()) is what makes shutdown bounded instead of tied to the next physical keystroke — see the module doc comment for the hang this replaces. What: loops crossterm::event::poll(KEY_POLL_INTERVAL) until the stop flag is set, poll/read errors (e.g. no input reader available, which is expected in a TTY-less sandbox and simply ends the loop), or tx.send fails (the receiver dropped). Each polled event is classified by [classify]; None results (filtered keys, unhandled mouse events) are silently dropped. Test: tests::key_reader_guard_drop_completes_promptly_without_a_keypress proves the returned guard’s Drop doesn’t block on a keystroke; the event-classification logic itself is covered directly via [classify]’s tests (no TTY needed for those).