Skip to main content

Module key_sequence

Module key_sequence 

Source
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

  1. Sequences are always non-empty when emitted.
  2. The timeout window is measured from the first key in a potential sequence.
  3. If no sequence is detected within the timeout, the buffered key(s) are emitted individually via KeySequenceAction::Emit.
  4. Non-blocking: KeySequenceAction::Pending signals 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 Emit actions (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§

KeySequenceConfig
Configuration for key sequence detection.
KeySequenceInterpreter
Stateful interpreter for multi-key sequences.

Enums§

KeySequenceAction
Action returned by the key sequence interpreter.
KeySequenceKind
Recognized key sequence patterns.