Expand description
Key sequence interpreter for multi-key sequences (bd-2vne.2).
This module provides a stateful interpreter for detecting key sequences like
Esc Esc, independent of the low-level input parsing. It operates on the
KeyEvent stream and uses a configurable timeout window to detect sequences.
§Design
§Invariants
- Sequences are always non-empty when emitted.
- The timeout window is measured from the first key in a potential sequence.
- If no sequence is detected within the timeout, the buffered key(s) are
emitted individually via
KeySequenceAction::Emit. - Non-blocking:
KeySequenceAction::Pendingsignals that more input is needed, but the caller can continue with other work.
§Failure Modes
- If the timeout expires mid-sequence, buffered keys are flushed as individual
Emitactions (graceful degradation). - Unknown keys that don’t match any sequence pattern are passed through immediately.
§Example
use ftui_core::key_sequence::{KeySequenceInterpreter, KeySequenceConfig, KeySequenceAction};
use ftui_core::event::{KeyEvent, KeyCode, KeyEventKind, Modifiers};
use std::time::{Duration, Instant};
let config = KeySequenceConfig::default();
let mut interp = KeySequenceInterpreter::new(config);
let esc = KeyEvent {
code: KeyCode::Escape,
modifiers: Modifiers::NONE,
kind: KeyEventKind::Press,
};
let now = Instant::now();
// First Esc: pending (waiting for potential second Esc)
let action = interp.feed(&esc, now);
assert!(matches!(action, KeySequenceAction::Pending));
// Second Esc within timeout: emit sequence
let action = interp.feed(&esc, now + Duration::from_millis(100));
assert!(matches!(action, KeySequenceAction::EmitSequence { .. }));Structs§
- KeySequence
Config - Configuration for key sequence detection.
- KeySequence
Interpreter - Stateful interpreter for multi-key sequences.
Enums§
- KeySequence
Action - Action returned by the key sequence interpreter.
- KeySequence
Kind - Recognized key sequence patterns.