Skip to main content

HashedEventRegister

Struct HashedEventRegister 

Source
pub struct HashedEventRegister<S>(/* private fields */);
Expand description

A hash store for events and it’s related callback

Each item is a key value pair, where the key is a event and it’s value is a callback. When a event occurs, it is matched inside and when the related match is found, it’s related callback is called.

Implementations§

Source§

impl HashedEventRegister<RandomState>

Source

pub fn with_default_hasher() -> Self

Create a new HashedEventRegister with the default hasher

Source§

impl<S> HashedEventRegister<S>
where S: BuildHasher,

Source

pub const fn new(s: S) -> Self

Create a new HashedEventRegister with the Hasher s

Source

pub fn format_help(&self) -> String

Format dynamic help table from all registered key bindings that have non-empty descriptions.

Source

pub fn insert_wild_event_matcher( &mut self, cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static, )

Adds a callback to handle all events that failed to match

Sometimes there are bunch of keys having equal importance that should have the same callback, for instance all the numbers on the keyboard. To handle these types of scenerios this is extremely useful. This callback is called when no event matches the incoming event, then we just match whether the event is a keyboard number and perform the required action.

This is also helpful when you need to do some action, like sending a message when the user presses wrong keyboard/mouse buttons.

Source

pub fn add_resize_event( &mut self, cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static, )

Adds a callback for handling resize events

§Example

These are from the original sources

use minus::input::{InputEvent, HashedEventRegister, crossterm_event::Event};

let mut input_register = HashedEventRegister::default();

input_register.add_resize_event(|ev, _| {
    let (cols, rows) = if let Event::Resize(cols, rows) = ev {
        (cols, rows)
    } else {
        unreachable!();
    };
    InputEvent::UpdateTermArea(cols as usize, rows as usize)
});
Source

pub fn remove_resize_event(&mut self)

Removes the currently active resize event callback

Source§

impl<S> HashedEventRegister<S>
where S: BuildHasher,

Source

pub fn add_key_events( &mut self, desc: &[&str], cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static, )

Add all elements of desc as key bindings that minus should respond to with the callback cb

You should prefer using the add_key_events_checked over this one.

§Example
use minus::input::{InputEvent, HashedEventRegister, crossterm_event};

let mut input_register = HashedEventRegister::default();

input_register.add_key_events(&["down"], |_, ps| {
    InputEvent::UpdateUpperMark(ps.upper_mark.saturating_sub(1))
});
Source

pub fn add_described_key_events( &mut self, keys: &[&str], desc: impl Into<Cow<'static, str>>, cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static, )

Add all elements of keys as key bindings with a description that minus should respond to with the callback cb.

Source

pub fn add_key_events_checked( &mut self, desc: &[&str], cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static, remap: bool, )

Add all elements of desc as key bindings that minus should respond to with the callback cb.

Prefer using this over add_key_events.

§Panics

This will panic if you the keybinding has been previously defined, unless the remap is set to true. This helps preventing accidental overrides of your keybindings.

§Example
use minus::input::{InputEvent, HashedEventRegister, crossterm_event};

let mut input_register = HashedEventRegister::default();

input_register.add_key_events_checked(&["down"], |_, ps| {
    InputEvent::UpdateUpperMark(ps.upper_mark.saturating_sub(1))
}, false);
Source

pub fn add_described_key_events_checked( &mut self, keys: &[&str], desc: impl Into<Cow<'static, str>>, cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static, remap: bool, )

Add all elements of keys as key bindings with a description that minus should respond to with the callback cb, with conflict checking.

§Panics

Panics if a key already exists and remap is false.

Source

pub fn remove_key_events(&mut self, desc: &[&str])

Removes the callback associated with the all the elements of desc.

use minus::input::{InputEvent, HashedEventRegister, crossterm_event};

let mut input_register = HashedEventRegister::default();

input_register.remove_key_events(&["down"])
Source

pub fn add_help_key(&mut self, desc: &[&str])

Add key binding(s) to show help in the pager prompt.

If desc is empty, defaults to &["m-h"].

§Example
use minus::input::HashedEventRegister;

let mut input_register = HashedEventRegister::default();
// Bind default Meta/Alt-h key to show help
input_register.add_help_key(&[]);
// Or specify custom keys
input_register.add_help_key(&["f1"]);
Source

pub fn add_help_key_checked(&mut self, desc: &[&str], remap: bool)

Add key binding(s) to show help in the pager prompt with conflict checking.

If desc is empty, defaults to &["m-h"].

§Panics

This will panic if any of the keybindings has been previously defined, unless remap is set to true.

Source§

impl<S> HashedEventRegister<S>
where S: BuildHasher,

Source

pub fn add_mouse_events( &mut self, desc: &[&str], cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static, )

Add all elemnts of desc as mouse bindings that minus should respond to with the callback cb

You should prefer using the add_mouse_events_checked over this one.

§Example
use minus::input::{InputEvent, HashedEventRegister};

let mut input_register = HashedEventRegister::default();

input_register.add_mouse_events(&["scroll:down"], |_, ps| {
    InputEvent::UpdateUpperMark(ps.upper_mark.saturating_sub(5))
});
Source

pub fn add_mouse_events_checked( &mut self, desc: &[&str], cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static, remap: bool, )

Add all elemnts of desc as mouse bindings that minus should respond to with the callback cb.

Prefer using this over add_mouse_events.

§Panics

This will panic if you the keybinding has been previously defined, unless the remap is set to true. This helps preventing accidental overrides of your keybindings.

§Example
use minus::input::{InputEvent, HashedEventRegister};

let mut input_register = HashedEventRegister::default();

input_register.add_mouse_events_checked(&["scroll:down"], |_, ps| {
    InputEvent::UpdateUpperMark(ps.upper_mark.saturating_sub(5))
}, false);
Source

pub fn remove_mouse_events(&mut self, mouse: &[&str])

Removes the callback associated with the all the elements of desc.

use minus::input::{InputEvent, HashedEventRegister, crossterm_event};

let mut input_register = HashedEventRegister::default();

input_register.remove_mouse_events(&["scroll:down"])

Trait Implementations§

Source§

impl Default for HashedEventRegister<RandomState>

Source§

fn default() -> Self

Create a new HashedEventRegister with the default hasher and insert the default bindings

Source§

impl<S> InputClassifier for HashedEventRegister<S>
where S: BuildHasher,

Source§

fn classify_input(&self, ev: Event, ps: &PagerState) -> Option<InputEvent>

👎Deprecated:

See #163.

Source§

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

👎Deprecated:

See #163.

Format dynamic help text from registered bindings, if supported.

Auto Trait Implementations§

§

impl<S> !RefUnwindSafe for HashedEventRegister<S>

§

impl<S> !UnwindSafe for HashedEventRegister<S>

§

impl<S> Freeze for HashedEventRegister<S>
where S: Freeze,

§

impl<S> Send for HashedEventRegister<S>
where S: Send,

§

impl<S> Sync for HashedEventRegister<S>
where S: Sync,

§

impl<S> Unpin for HashedEventRegister<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for HashedEventRegister<S>
where S: UnsafeUnpin,

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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, 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.