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
//! 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::hash_map::RandomState, collections::HashMap, hash::BuildHasher, hash::Hash,
    sync::Arc,
};

/// 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.
///
/// # Example
/// ```
/// use minus::input::{InputEvent, HashedEventRegister, crossterm_event::Event};
///
/// let mut input_register = HashedEventRegister::default();
///
/// input_register.add_key_events(&["down"], |_, ps| {
///     InputEvent::UpdateUpperMark(ps.upper_mark.saturating_sub(1))
/// });
///
/// input_register.add_mouse_events(&["scroll:up"], |_, ps| {
///     InputEvent::UpdateUpperMark(ps.upper_mark.saturating_sub(5))
/// });
///
/// 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)
/// });
/// ```
///
/// # Defining Keybindings
/// The general syntax for defining keybindings is `[MODIFIER]-[MODIFIER]-[MODIFIER]-{SINGLE KEY}`
///
/// `MODIFIER`s include or or more of the `Ctrl` `Alt` and `Shift` keys. They are writeen with the shorthands `c`, `m`
/// and `s` respectively.
///
/// `SINGLE CHAR` includes any key on the keyboard which is not a modifier like `a`, `z`, `1`, `F1` or `enter`
/// Each of these pieces are separated by a `-`.
///
/// Here are some examples
///
/// | Key Input    | Mean ing                                   |
/// |--------------|--------------------------------------------|
/// | `a`          | A literal `a`                              |
/// | `Z`          | A `Z`. Matched only when a caps lock is on |
/// | `c-q`        | `Ctrl+q`                                   |
/// | `enter`      | `ENTER` key                                |
/// | `c-m-pageup` | `Ctrl+Alt+PageUp`                          |
/// | `s-2`        | `Shift+2`                                  |
/// | `backspace`  | `Backspace` Key                            |
/// | `left`       | `Left Arrow` key                           |
///
/// # Defining Mouse Bindings
///
/// The general syntax for defining keybindings is `[MODIFIER]-[MODIFIER]-[MODIFIER]-{MOUSE ACTION}`
///
/// `MODIFIER`s include or or more of the `Ctrl` `Alt` and `Shift` keys which are pressed along with the mouse action.
/// They are writeen with the shorthands `c`, `m` and `s` respectively.
///
/// `MOUSE ACTION` includes actions like pressing down the left mouse button or taking up the right mouse button. It
/// also includes scrolling up/down or pressing the middle click.
///
/// Here are some examples
///
/// | Key Input    | Mean ing                                   |
/// |--------------|--------------------------------------------|
/// | `left:up`    | Releasing the left mouse button            |
/// | `right:down` | Pressing the right mouse button            |
/// | `c-mid:down  | Middle click in pressed along with Ctrl key|
/// | `m-scroll:up` | Scrolled down while pressing the Alt key   |

pub struct HashedEventRegister<S>(HashMap<EventWrapper, EventReturnType, S>);

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

#[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);
            }
        }
    }
}

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

    /// 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 nedd 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, Arc::new(cb));
    }

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

    /// 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)), v);
    }

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

// Key event Insertions functions
impl<S> HashedEventRegister<S>
where
    S: BuildHasher,
{
    /// Add a key binding that minus should respond to with the callback `cb`
    ///
    /// # 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,
        keys: &[&str],
        cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static,
    ) {
        let v = Arc::new(cb);
        for k in keys {
            self.0.insert(
                Event::Key(super::definitions::keydefs::parse_key_event(k)).into(),
                v.clone(),
            );
        }
    }

    /// Removes the callback associated for the given key bindings
    ///
    /// ```
    /// 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, keys: &[&str]) {
        for k in keys {
            self.0
                .remove(&Event::Key(super::definitions::keydefs::parse_key_event(k)).into());
        }
    }
}

// Mouse event insertions functions
impl<S> HashedEventRegister<S>
where
    S: BuildHasher,
{
    /// Add a mouse binding that minus should respond to with the callback `cb`
    ///
    /// # 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,
        keys: &[&str],
        cb: impl Fn(Event, &PagerState) -> InputEvent + Send + Sync + 'static,
    ) {
        let v = Arc::new(cb);
        for k in keys {
            self.0.insert(
                Event::Mouse(super::definitions::mousedefs::parse_mouse_event(k)).into(),
                v.clone(),
            );
        }
    }

    /// Removes the callback associated for the given mouse bindings
    ///
    /// ```
    /// 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, keys: &[&str]) {
        for k in keys {
            self.0
                .remove(&Event::Mouse(super::definitions::mousedefs::parse_mouse_event(k)).into());
        }
    }
}

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))
    }
}