Skip to main content

DetachDetector

Struct DetachDetector 

Source
pub struct DetachDetector { /* private fields */ }
Expand description

Deterministic detach-chord detector.

feed and tick accept caller-supplied timestamps so unit tests can drive every state transition without sleeping. The detector is purely a state machine: it never spawns threads, never reads from a terminal, and never owns a clock of its own.

§Contract

The detector’s behaviour is fully specified by the following rules:

  1. Strict code+modifier equality. A key matches the chord’s prefix (or detach) slot only when both KeyCode and the full KeyModifiers bitfield are byte-for-byte equal to the configured event. Ctrl+B does not match Ctrl+Shift+B.
  2. Prefix swallowing. While idle, observing the prefix transitions the detector to PrefixHeld and returns DetachOutcome::Armed; the prefix is consumed and is not forwarded to the pane until the timeout lapses or a mismatch is seen.
  3. Chord completion. While PrefixHeld, observing the detach follow-up returns DetachOutcome::DetachRequested and the detector returns to idle without forwarding anything.
  4. Mismatch forwarding. While PrefixHeld, observing any other event (including the prefix again) returns DetachOutcome::Forward(vec![prefix, event]) in that order, and the detector returns to idle.
  5. Timeout flushing. A feed or tick call where now.saturating_duration_since(prefix_arrival) >= timeout flushes the held prefix as Forward(vec![prefix]) and returns the detector to idle. For feed, the new event is then processed against the now-idle detector and any extra forwarded events are appended after the flushed prefix.
  6. Zero-timeout edge case. A Duration::ZERO timeout means any observation strictly after the prefix is treated as expired (>= is the comparison): the detector flushes the prefix and forwards the new event without ever firing the chord. Hosts that want chord behaviour must configure a non-zero timeout.
  7. Equal prefix/detach edge case. If a chord is configured with prefix == detach, the detach branch is checked first while PrefixHeld, so pressing the shared key twice quickly enough returns DetachRequested.
  8. Reusability. The detector is fully reusable after every terminal outcome: hosts may keep a single detector across sessions or runs. After DetachRequested, the detector is idle and a subsequent tick returns Forward(vec![]).

Implementations§

Source§

impl DetachDetector

Source

pub const DEFAULT_TIMEOUT: Duration

Default chord-completion window matching tmux’s interactive feel.

Source

pub const fn new(chord: DetachChord) -> Self

Constructs a detector for the given chord with Self::DEFAULT_TIMEOUT.

Source

pub const fn with_timeout(chord: DetachChord, timeout: Duration) -> Self

Constructs a detector with an explicit timeout window.

Source

pub const fn chord(&self) -> &DetachChord

Returns the chord this detector matches.

Source

pub const fn timeout(&self) -> Duration

Returns the configured chord-completion timeout.

Source

pub const fn is_prefix_armed(&self) -> bool

Returns true while the detector has consumed the prefix and is waiting for the follow-up key.

Source

pub fn reset(&mut self)

Resets the detector back to idle without forwarding anything.

Source

pub fn feed(&mut self, event: KeyEvent, now: Instant) -> DetachOutcome

Feeds an event into the detector and returns the outcome.

now is the timestamp at which the event is observed. Tests pass a deterministic Instant so timeout edges can be exercised precisely. The detector never blocks and never reads Instant::now() internally.

Source

pub fn tick(&mut self, now: Instant) -> DetachOutcome

Advances the detector’s clock without consuming an input event.

Hosts call this when they receive a non-key wakeup (poll loop tick, resize event, etc.) so the detector can release a held prefix once the timeout has lapsed. Returns Forward(vec![prefix]) when the timeout has elapsed; otherwise returns the current state.

Trait Implementations§

Source§

impl Clone for DetachDetector

Source§

fn clone(&self) -> DetachDetector

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DetachDetector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.