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
//! Provides mappings and actions to change the behavior of [`Prompt`] when parsing the user input.
//!
//! There is a built-in set of default [`Action`]s that will be executed upon user interaction.
//! These are meant to feel natural when coming from the default terminal, while also adding further
//! functionality and editing commands.
//!
//! However, bindings that override the default behavior can be given to [`Prompt`] to cause
//! a different [`Action`] to be taken.
//!
//! # Examples
//!
//! ```
//! use rucline::Prompt;
//! use rucline::actions::{Action, Event, KeyBindings};
//! use crossterm::event::KeyCode;
//!
//! let mut bindings = KeyBindings::new();
//! bindings.insert(Event::from(KeyCode::Tab), Action::Write('\t'));
//!
//! let prompt = Prompt::new().overrider(&bindings);
//! ```
//!
//! ```
//! use rucline::Prompt;
//! use rucline::actions::{Action, Context, Event, KeyBindings};
//! use crossterm::event::KeyCode;
//!
//! let prompt = Prompt::new().overrider(&|e, _: &dyn Context| if e == Event::from(KeyCode::Tab) {
//!     Some(Action::Write('\t'))
//! } else {
//!     None
//! });
//! ```
//!
//! # Overriding a default action
//!
//! The [`KeyBindings`] map provides a way to store the association
//! between [`Event`] and [`Action`] which will override the default behavior.
//!
//! ```
//! use rucline::actions::{Action, Event, KeyBindings};
//! use crossterm::event::KeyCode;
//!
//! let mut bindings = KeyBindings::new();
//! bindings.insert(Event::from(KeyCode::Tab), Action::Write('\t'));
//! ```
//!
//! # Disabling a default action
//!
//! To explicitly remove an [`Action`] from the default behavior, the [`Noop`] action can be
//! set as the override.
//!
//! ```
//! use rucline::actions::{Action, Event, KeyBindings};
//! use crossterm::event::KeyCode;
//!
//! let mut bindings = KeyBindings::new();
//! bindings.insert(Event::from(KeyCode::Tab), Action::Noop);
//! ```
//!
//! # Saving key binding configurations
//!
//! If the feature `serialize` is enabled, [`KeyBindings`] can be serialized, stored, and loaded
//! at runtime.
//!
//! # Default behavior
//!
//! In the absence of [`KeyBindings`] or an entry for a given [`Event`], the default behavior
//! will be as follows:
//!
//! ```no_run
//! # fn default_action(event: rucline::actions::Event) -> rucline::actions::Action {
//! # use crossterm::event::KeyCode;
//! # use rucline::actions::{Action::*, Direction::*, Range::*, Scope::* };
//! # match event.code {
//! KeyCode::Enter => Accept,
//! KeyCode::Esc => Cancel,
//! KeyCode::Tab => Suggest(Forward),
//! KeyCode::BackTab => Suggest(Backward),
//! KeyCode::Backspace => Delete(Relative(Single, Backward)),
//! KeyCode::Delete => Delete(Relative(Single, Forward)),
//! KeyCode::Right => Move(Single, Forward),
//! KeyCode::Left => Move(Single, Backward),
//! KeyCode::Home => Move(Line, Backward),
//! KeyCode::End => Move(Line, Forward),
//! KeyCode::Char(c) => {
//!     if event.modifiers == crossterm::event::KeyModifiers::CONTROL {
//!         match c {
//!             'm' | 'd' => Accept,
//!             'c' => Cancel,
//!
//!             'b' => Move(Single, Backward),
//!             'f' => Move(Single, Forward),
//!             'a' => Move(Line, Backward),
//!             'e' => Move(Line, Forward),
//!
//!             'j' => Delete(Relative(Word, Backward)),
//!             'k' => Delete(Relative(Word, Forward)),
//!             'h' => Delete(Relative(Line, Backward)),
//!             'l' => Delete(Relative(Line, Forward)),
//!             'w' => Delete(WholeWord),
//!             'u' => Delete(WholeLine),
//!             _ => Noop,
//!         }
//!     } else if event.modifiers == crossterm::event::KeyModifiers::ALT {
//!         match c {
//!             'b' => Move(Word, Backward),
//!             'f' => Move(Word, Forward),
//!             _ => Noop,
//!         }
//!     } else {
//!         Write(c)
//!     }
//! }
//! _ => Noop,
//! # }}
//! ```
//!
//! [`Prompt`]: ../prompt/struct.Prompt.html
//! [`KeyBindings`]: type.KeyBindings.html
//! [`Event`]: type.Event.html
//! [`Action`]: enum.Action.html
//! [`Noop`]: enum.Action.html#variant.Noop

pub use crate::Context;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Alias to `crossterm::event::KeyEvent` from [`crossterm`](https://docs.rs/crossterm/)
pub type Event = crossterm::event::KeyEvent;

/// Alias to [`HashMap<Event, Action>`](std::collections::HashMap)
pub type KeyBindings = std::collections::HashMap<Event, Action>;

/// An action that can be performed while reading a line
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Action {
    /// Write a single character where the cursor is
    Write(char),
    /// Delete a section based on the cursor, defined by [`Scope`](enum.Scope.html)
    Delete(Scope),
    /// Move the cursor for a [`Range`](enum.Range.html) in a [`Direction`](enum.Direction.html)
    Move(Range, Direction),
    /// Trigger the [`suggester`](../completion/trait.Suggester.html)
    Suggest(Direction),
    /// Accept [`Range`](enum.Range.html) from the current completion presented by
    /// [`completer`](../completion/trait.Completer.html), if any
    Complete(Range),
    /// Accept the current line
    Accept,
    /// Cancel the suggestions, if any. Else, discard the whole line
    Cancel,
    /// Do nothing and wait for the next [`Event`](type.Event.html)
    Noop,
}

/// The scope an [`Action`](enum.Action.html) should be applied on
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Scope {
    /// Represents a whole line
    WholeLine,
    /// Represents a whole word
    WholeWord,
    /// Represents a relative scope, with a [`Range`](enum.Range.html)
    /// and [`Direction`](enum.Direction.html)
    Relative(Range, Direction),
}

/// The range an [`Action`](enum.Action.html) should extend for
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Range {
    /// Represents the remainder of the line
    Line,
    /// Represents a single word
    Word,
    /// Represents a single character
    Single,
}

/// The direction an [`Action`](enum.Action.html) may take
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Direction {
    /// Represents a "left" or "down" direction
    Forward,
    /// Represents a "right" or "up" direction
    Backward,
}

/// Overrides the behavior for a given [`Event`].
///
/// This trait has a convenience implementation for [`KeyBindings`] and also a conversion
/// from lambdas.
///
/// # Example
///
/// ```
/// use rucline::Prompt;
/// use rucline::actions::{Action, Context, Event};
/// use crossterm::event::KeyCode;
///
/// let prompt = Prompt::new().overrider(&|e, _: &dyn Context| if e == Event::from(KeyCode::Tab) {
///     Some(Action::Write('\t'))
/// } else {
///     None
/// });
/// ```
///
/// [`Event`]: type.Event.html
/// [`KeyBindings`]: type.KeyBindings.html
pub trait Overrider {
    /// Overrides the behavior for the given [`Event`].
    ///
    /// [`Context`] will contain the current context of the prompt, in case the behavior should
    /// be contextual.
    ///
    /// # Arguments
    /// * [`event`] - The incoming event to be processed.
    /// * [`context`] - The current context in which this event is coming in.
    ///
    /// [`Event`]: type.Event.html
    /// [`Context`]: ../prompt/context/trait.Context.html
    fn override_for(&self, event: Event, context: &dyn Context) -> Option<Action>;
}

impl Overrider for KeyBindings {
    fn override_for(&self, event: Event, _: &dyn Context) -> Option<Action> {
        self.get(&event).copied()
    }
}

impl<F> Overrider for F
where
    F: Fn(Event, &dyn Context) -> Option<Action>,
{
    fn override_for(&self, event: Event, context: &dyn Context) -> Option<Action> {
        self(event, context)
    }
}

pub(super) fn action_for(
    overrides: Option<&dyn Overrider>,
    event: Event,
    context: &impl Context,
) -> Action {
    if let Some(action) = overrides
        .as_ref()
        .and_then(|b| b.override_for(event, context))
    {
        action
    } else {
        default_action(event, context)
    }
}

#[inline]
fn control_pressed(event: &Event) -> bool {
    event.modifiers == crossterm::event::KeyModifiers::CONTROL
}

#[inline]
fn alt_pressed(event: &Event) -> bool {
    event.modifiers == crossterm::event::KeyModifiers::ALT
}

#[inline]
fn complete_if_at_end_else_move(context: &impl Context, range: Range) -> Action {
    if context.cursor() == context.buffer().len() {
        if range == Range::Word {
            Action::Complete(Range::Word)
        } else {
            Action::Complete(Range::Line)
        }
    } else {
        Action::Move(range, Direction::Forward)
    }
}

// TODO: Investigate '\n' being parsed and 'ENTER'
fn default_action(event: Event, context: &impl Context) -> Action {
    use crossterm::event::KeyCode;
    use Action::{Accept, Cancel, Delete, Move, Noop, Suggest, Write};
    use Direction::{Backward, Forward};
    use Range::{Line, Single, Word};
    use Scope::{Relative, WholeLine, WholeWord};

    match event.code {
        KeyCode::Enter => Accept,
        KeyCode::Esc => Cancel,
        KeyCode::Tab => Suggest(Forward),
        KeyCode::BackTab => Suggest(Backward),
        KeyCode::Backspace => Delete(Relative(Single, Backward)),
        KeyCode::Delete => Delete(Relative(Single, Forward)),
        KeyCode::Right => complete_if_at_end_else_move(context, Single),
        KeyCode::Left => Move(Single, Backward),
        KeyCode::Home => Move(Line, Backward),
        KeyCode::End => complete_if_at_end_else_move(context, Line),
        KeyCode::Char(c) => {
            if control_pressed(&event) {
                match c {
                    'm' | 'd' => Accept,
                    'c' => Cancel,

                    'b' => Move(Single, Backward),
                    'f' => complete_if_at_end_else_move(context, Single),
                    'a' => Move(Line, Backward),
                    'e' => complete_if_at_end_else_move(context, Line),

                    'j' => Delete(Relative(Word, Backward)),
                    'k' => Delete(Relative(Word, Forward)),
                    'h' => Delete(Relative(Line, Backward)),
                    'l' => Delete(Relative(Line, Forward)),
                    'w' => Delete(WholeWord),
                    'u' => Delete(WholeLine),
                    _ => Noop,
                }
            } else if alt_pressed(&event) {
                match c {
                    'b' => Move(Word, Backward),
                    'f' => complete_if_at_end_else_move(context, Word),
                    _ => Noop,
                }
            } else {
                Write(c)
            }
        }
        _ => Noop,
    }
}

#[cfg(test)]
mod test {
    use super::{action_for, default_action, Action, Direction, Event, Range};
    use crate::test::mock::Context as Mock;

    #[test]
    fn should_complete_if_at_end() {
        use crossterm::event::KeyCode::{Char, End, Right};
        use crossterm::event::KeyModifiers;
        use Action::{Complete, Move};
        use Direction::Forward;
        use Range::{Line, Single, Word};

        let mut c = Mock::from("a");

        assert_eq!(default_action(Event::from(Right), &c), Complete(Line));
        assert_eq!(default_action(Event::from(End), &c), Complete(Line));
        assert_eq!(
            default_action(Event::new(Char('f'), KeyModifiers::CONTROL), &c),
            Complete(Line)
        );
        assert_eq!(
            default_action(Event::new(Char('f'), KeyModifiers::ALT), &c),
            Complete(Word)
        );

        c.cursor = 0;

        assert_eq!(
            default_action(Event::from(Right), &c),
            Move(Single, Forward)
        );
        assert_eq!(default_action(Event::from(End), &c), Move(Line, Forward));
        assert_eq!(
            default_action(Event::new(Char('f'), KeyModifiers::CONTROL), &c),
            Move(Single, Forward)
        );
        assert_eq!(
            default_action(Event::new(Char('f'), KeyModifiers::ALT), &c),
            Move(Word, Forward)
        );
    }

    #[test]
    fn should_default_if_no_mapping() {
        use crossterm::event::KeyCode::Tab;
        let action = action_for(None, Event::from(Tab), &Mock::empty());
        assert_eq!(action, Action::Suggest(Direction::Forward));
    }

    mod basic {
        use super::super::{action_for, Action, Direction, Event, KeyBindings};
        use super::Mock;
        use crossterm::event::KeyCode::Tab;

        #[test]
        fn should_default_if_event_missing_form_mapping() {
            let overrider = KeyBindings::new();
            let action = action_for(Some(&overrider), Event::from(Tab), &Mock::empty());
            assert_eq!(action, Action::Suggest(Direction::Forward));
        }

        #[test]
        fn should_override_if_defined() {
            let mut bindings = KeyBindings::new();
            bindings.insert(Event::from(Tab), Action::Write('\t'));
            let action = action_for(Some(&bindings), Event::from(Tab), &Mock::empty());
            assert_eq!(action, Action::Write('\t'));
        }
    }

    mod lambda {
        use super::super::{action_for, Action, Context, Direction, Event};
        use super::Mock;
        use crossterm::event::KeyCode::Tab;

        #[test]
        fn should_default_if_event_missing_form_mapping() {
            let overrider = |_, _: &dyn Context| None;
            let action = action_for(Some(&overrider), Event::from(Tab), &Mock::empty());
            assert_eq!(action, Action::Suggest(Direction::Forward));
        }

        #[test]
        fn should_override_if_defined() {
            let overrider = |e, _: &dyn Context| {
                if e == Event::from(Tab) {
                    Some(Action::Write('\t'))
                } else {
                    None
                }
            };
            let action = action_for(Some(&overrider), Event::from(Tab), &Mock::empty());
            assert_eq!(action, Action::Write('\t'));
        }
    }
}