ComposedKeyHandler

Struct ComposedKeyHandler 

Source
pub struct ComposedKeyHandler<H: KeyHandler> { /* private fields */ }
Expand description

A composable key handler wrapper with pre-processing hooks.

ComposedKeyHandler wraps an inner handler and allows adding hooks that run before the inner handler processes keys. This enables:

  • Intercepting specific keys before they reach the inner handler
  • Adding logging or debugging behavior
  • Implementing layered key handling (e.g., modal modes on top of base handler)

§Example

let base_handler = DefaultKeyHandler::new(KeyBindings::emacs());
let handler = ComposedKeyHandler::new(base_handler)
    .with_pre_hook(|key, _ctx| {
        // Intercept F1 for help
        if key.code == KeyCode::F(1) {
            return Some(AppKeyResult::Action(AppKeyAction::custom(ShowHelp)));
        }
        None // Let inner handler process
    });

Implementations§

Source§

impl<H: KeyHandler> ComposedKeyHandler<H>

Source

pub fn new(inner: H) -> Self

Create a new composed handler wrapping the given inner handler.

Source

pub fn with_pre_hook<F>(self, hook: F) -> Self
where F: Fn(&KeyEvent, &KeyContext) -> Option<AppKeyResult> + Send + 'static,

Add a hook that runs before the inner handler.

If the hook returns Some(result), that result is used and the inner handler is skipped. If the hook returns None, processing continues to the next hook or the inner handler.

Hooks are called in the order they were added.

§Arguments
  • hook - A function that receives the key event and context, returning Some(AppKeyResult) to handle the key or None to pass through
§Example
let handler = ComposedKeyHandler::new(DefaultKeyHandler::default())
    .with_pre_hook(|key, ctx| {
        // Log all key presses
        eprintln!("Key: {:?}", key);
        None // Don't consume, let inner handler process
    })
    .with_pre_hook(|key, _ctx| {
        // Intercept Ctrl+H for custom help
        if key.code == KeyCode::Char('h') && key.modifiers == KeyModifiers::CONTROL {
            return Some(AppKeyResult::Action(AppKeyAction::custom(MyHelp)));
        }
        None
    });
Source

pub fn inner(&self) -> &H

Get a reference to the inner handler.

Source

pub fn inner_mut(&mut self) -> &mut H

Get a mutable reference to the inner handler.

Trait Implementations§

Source§

impl<H: KeyHandler> KeyHandler for ComposedKeyHandler<H>

Source§

fn handle_key(&mut self, key: KeyEvent, context: &KeyContext) -> AppKeyResult

Handle a key event. Read more
Source§

fn status_hint(&self) -> Option<String>

Get a status hint to display in the status bar. Read more
Source§

fn bindings(&self) -> &KeyBindings

Get a reference to the key bindings. Read more

Auto Trait Implementations§

§

impl<H> Freeze for ComposedKeyHandler<H>
where H: Freeze,

§

impl<H> !RefUnwindSafe for ComposedKeyHandler<H>

§

impl<H> Send for ComposedKeyHandler<H>

§

impl<H> !Sync for ComposedKeyHandler<H>

§

impl<H> Unpin for ComposedKeyHandler<H>
where H: Unpin,

§

impl<H> !UnwindSafe for ComposedKeyHandler<H>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more