Skip to main content

resolve_passthrough

Function resolve_passthrough 

Source
pub fn resolve_passthrough<'a, A, L>(
    layers: L,
    input: &KeyInput,
    raw: RawInput<'a>,
) -> Resolution<'a, A>
where L: IntoIterator<Item = &'a Keymap<A>>, A: 'a,
Expand description

Resolves input against an ordered list of layers for a caller that forwards unbound keys to a terminal sink (a PTY), carrying the original raw bytes so a miss can be forwarded verbatim.

This is the raw-byte-carrying sibling of resolve_layered: it runs the same first-hit-wins resolution, returning Resolution::Action on a hit and Resolution::Passthrough (wrapping raw) on a miss in every layer. It never returns Resolution::Consume — see the module docs: a modal grab is caller mode state, so the caller maps a Passthrough to Consume when it is grabbing.

use keymap_core::{
    resolve_passthrough, Key, KeyInput, Keymap, Modifiers, RawInput, Resolution,
};

#[derive(PartialEq, Debug)]
enum Action { Quit }

let ctrl_q = KeyInput::new(Key::Char('q'), Modifiers::CTRL);
let mut base = Keymap::new();
base.bind(ctrl_q.clone(), Action::Quit);

// A bound key resolves to its action.
let raw = RawInput::from_bytes(&[0x11]); // ctrl+q as a C0 byte
assert_eq!(
    resolve_passthrough([&base], &ctrl_q, raw),
    Resolution::Action(&Action::Quit),
);

// An unbound key carries its bytes out for verbatim forwarding.
let letter = KeyInput::new(Key::Char('a'), Modifiers::NONE);
let raw = RawInput::from_bytes(b"a");
match resolve_passthrough([&base], &letter, raw) {
    Resolution::Passthrough(bytes) => assert_eq!(bytes.as_bytes(), b"a"),
    other => panic!("expected passthrough, got {other:?}"),
}