matchmaker-minus 5.7.201

An asynchronous data feedable terminal paging library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Provides the [`HashedEventRegister`] and related items
//!
//! This module holds the [`HashedEventRegister`] which is a [`HashMap`] that stores events and their associated
//! callbacks. When the user does an action on the terminal, the event is scanned and matched against this register.
//! If their is a match related to that event, the associated callback is called

use super::{InputClassifier, InputEvent};
use crate::PagerState;
use crossterm::event::{Event, MouseEvent};
use std::{
    collections::HashMap, collections::hash_map::RandomState, hash::BuildHasher, hash::Hash,
    sync::Arc,
};

use std::borrow::Cow;

/// A convenient type for the return type of [`HashedEventRegister::get`]
type EventReturnType = Arc<dyn Fn(Event, &PagerState) -> InputEvent + Send + Sync>;

#[derive(Clone)]
struct EventCallback {
    cb: EventReturnType,
    desc: Cow<'static, str>,
}

// //////////////////////////////
// EVENTWRAPPER TYPE
// //////////////////////////////

#[derive(Clone, Eq)]
enum EventWrapper {
    ExactMatchEvent(Event),
    WildEvent,
}

impl From<Event> for EventWrapper {
    fn from(e: Event) -> Self {
        Self::ExactMatchEvent(e)
    }
}

impl From<&Event> for EventWrapper {
    fn from(e: &Event) -> Self {
        Self::ExactMatchEvent(e.clone())
    }
}

impl PartialEq for EventWrapper {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (
                Self::ExactMatchEvent(Event::Mouse(MouseEvent {
                    kind, modifiers, ..
                })),
                Self::ExactMatchEvent(Event::Mouse(MouseEvent {
                    kind: o_kind,
                    modifiers: o_modifiers,
                    ..
                })),
            ) => kind == o_kind && modifiers == o_modifiers,
            (
                Self::ExactMatchEvent(Event::Resize(..)),
                Self::ExactMatchEvent(Event::Resize(..)),
            )
            | (Self::WildEvent, Self::WildEvent) => true,
            (Self::ExactMatchEvent(ev), Self::ExactMatchEvent(o_ev)) => ev == o_ev,
            _ => false,
        }
    }
}

impl Hash for EventWrapper {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        let tag = std::mem::discriminant(self);
        tag.hash(state);
        match self {
            Self::ExactMatchEvent(Event::Mouse(MouseEvent {
                kind, modifiers, ..
            })) => {
                kind.hash(state);
                modifiers.hash(state);
            }
            Self::WildEvent | Self::ExactMatchEvent(Event::Resize(..)) => {}
            Self::ExactMatchEvent(v) => {
                v.hash(state);
            }
        }
    }
}

// /////////////////////////////////////////////////
// HASHED EVENT REGISTER TYPE AND ITS APIs
// ////////////////////////////////////////////////

/// 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.
pub struct HashedEventRegister<S>(HashMap<EventWrapper, EventCallback, S>);

impl HashedEventRegister<RandomState> {
    /// Create a new [`HashedEventRegister`] with the default hasher
    #[must_use]
    pub fn with_default_hasher() -> Self {
        Self::new(RandomState::new())
    }
}

impl Default for HashedEventRegister<RandomState> {
    /// Create a new [`HashedEventRegister`] with the default hasher and insert the default bindings
    fn default() -> Self {
        let mut event_register = Self::new(RandomState::new());
        super::generate_default_bindings(&mut event_register);
        event_register
    }
}

impl<S> InputClassifier for HashedEventRegister<S>
where
    S: BuildHasher,
{
    fn classify_input(&self, ev: Event, ps: &crate::PagerState) -> Option<InputEvent> {
        self.get(&ev).map(|c| c(ev, ps))
    }

    fn format_help(&self) -> Option<String> {
        let h = self.format_help();
        if h.is_empty() {
            None
        } else {
            Some(h)
        }
    }
}

// ####################
// GENERAL FUNCTIONS
// ####################
impl<S> HashedEventRegister<S>
where
    S: BuildHasher,
{
    /// Create a new `HashedEventRegister` with the Hasher `s`
    pub const fn new(s: S) -> Self {
        Self(HashMap::with_hasher(s))
    }

    /// Format dynamic help table from all registered key bindings that have non-empty descriptions.
    #[must_use]
    pub fn format_help(&self) -> String {
        let entries = self.0.iter().filter_map(|(k, v)| match k {
            EventWrapper::ExactMatchEvent(Event::Key(ke)) => Some((ke, v.desc.as_ref())),
            _ => None,
        });
        crate::help::format_help_table_from_entries(entries)
    }

    /// 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.
    pub fn insert_wild_event_matcher(
        &mut self,
        cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static,
    ) {
        self.0.insert(
            EventWrapper::WildEvent,
            EventCallback {
                cb: Arc::new(cb),
                desc: Cow::Borrowed(""),
            },
        );
    }

    fn get(&self, k: &Event) -> Option<&EventReturnType> {
        self.0
            .get(&k.into())
            .map_or_else(|| self.0.get(&EventWrapper::WildEvent), Some)
            .map(|entry| &entry.cb)
    }

    /// 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)
    /// });
    /// ```
    pub fn add_resize_event(
        &mut self,
        cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static,
    ) {
        let v = Arc::new(cb);
        // The 0, 0 are present just to ensure everything compiles and they can be anything.
        // These values are never hashed or stored into the HashedEventRegister
        self.0.insert(
            EventWrapper::ExactMatchEvent(Event::Resize(0, 0)),
            EventCallback {
                cb: v,
                desc: Cow::Borrowed(""),
            },
        );
    }

    /// Removes the currently active resize event callback
    pub fn remove_resize_event(&mut self) {
        self.0
            .remove(&EventWrapper::ExactMatchEvent(Event::Resize(0, 0)));
    }
}

// ###############################
// KEYBOARD SPECIFIC FUNCTIONS
// ###############################
impl<S> HashedEventRegister<S>
where
    S: BuildHasher,
{
    /// 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`](HashedEventRegister::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))
    /// });
    /// ```
    pub fn add_key_events(
        &mut self,
        desc: &[&str],
        cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static,
    ) {
        self.add_described_key_events(desc, "", cb);
    }

    /// Add all elements of `keys` as key bindings with a description that minus should respond to with the callback `cb`.
    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,
    ) {
        let v = Arc::new(cb);
        let d = desc.into();
        for k in keys {
            self.0.insert(
                Event::Key(super::definitions::keydefs::parse_key_event(k)).into(),
                EventCallback {
                    cb: v.clone(),
                    desc: d.clone(),
                },
            );
        }
    }

    /// Add all elements of `desc` as key bindings that minus should respond to with the callback `cb`.
    ///
    /// Prefer using this over [`add_key_events`](HashedEventRegister::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
    /// ```should_panic
    /// 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);
    /// ```
    pub fn add_key_events_checked(
        &mut self,
        desc: &[&str],
        cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static,
        remap: bool,
    ) {
        self.add_described_key_events_checked(desc, "", cb, remap);
    }

    /// 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`.
    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,
    ) {
        let v = Arc::new(cb);
        let d = desc.into();
        for k in keys {
            let def: EventWrapper =
                Event::Key(super::definitions::keydefs::parse_key_event(k)).into();
            assert!(self.0.contains_key(&def) && remap, "");
            self.0.insert(
                def,
                EventCallback {
                    cb: v.clone(),
                    desc: d.clone(),
                },
            );
        }
    }

    /// 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"])
    /// ```
    pub fn remove_key_events(&mut self, desc: &[&str]) {
        for k in desc {
            self.0
                .remove(&Event::Key(super::definitions::keydefs::parse_key_event(k)).into());
        }
    }

    /// 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"]);
    /// ```
    pub fn add_help_key(&mut self, desc: &[&str]) {
        let keys = if desc.is_empty() { &["m-h"][..] } else { desc };
        self.add_described_key_events(keys, "help", |_, _| InputEvent::ShowHelp);
    }

    /// 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.
    pub fn add_help_key_checked(&mut self, desc: &[&str], remap: bool) {
        let keys = if desc.is_empty() { &["m-h"][..] } else { desc };
        self.add_described_key_events_checked(keys, "help", |_, _| InputEvent::ShowHelp, remap);
    }
}

// ###############################
// MOUSE SPECIFIC FUNCTIONS
// ###############################
impl<S> HashedEventRegister<S>
where
    S: BuildHasher,
{
    /// 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`](HashedEventRegister::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))
    /// });
    /// ```
    pub fn add_mouse_events(
        &mut self,
        desc: &[&str],
        cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static,
    ) {
        let v = Arc::new(cb);
        for k in desc {
            self.0.insert(
                Event::Mouse(super::definitions::mousedefs::parse_mouse_event(k)).into(),
                EventCallback {
                    cb: v.clone(),
                    desc: Cow::Borrowed(""),
                },
            );
        }
    }

    /// Add all elemnts of `desc` as mouse bindings that minus should respond to with the callback `cb`.
    ///
    /// Prefer using this over [`add_mouse_events`](HashedEventRegister::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
    /// ```should_panic
    /// 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);
    /// ```
    pub fn add_mouse_events_checked(
        &mut self,
        desc: &[&str],
        cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static,
        remap: bool,
    ) {
        let v = Arc::new(cb);
        for k in desc {
            let def: EventWrapper =
                Event::Mouse(super::definitions::mousedefs::parse_mouse_event(k)).into();
            assert!(self.0.contains_key(&def) && remap, "");
            self.0.insert(
                def,
                EventCallback {
                    cb: v.clone(),
                    desc: Cow::Borrowed(""),
                },
            );
        }
    }

    /// 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"])
    /// ```
    pub fn remove_mouse_events(&mut self, mouse: &[&str]) {
        for k in mouse {
            self.0
                .remove(&Event::Mouse(super::definitions::mousedefs::parse_mouse_event(k)).into());
        }
    }
}