Commit 43d81445 authored by AdminXVII's avatar AdminXVII

Add an init method to KeyMaps

parent 2240b579
Pipeline #4324 passed with stage
in 1 minute and 56 seconds
......@@ -110,7 +110,7 @@ impl Context {
let mut ed = Editor::new_with_init_buffer(stdout, prompt, f, self, buffer)?;
match self.key_bindings {
KeyBindings::Emacs => Self::handle_keys(keymap::Emacs::new(), ed, handler),
KeyBindings::Vi => Self::handle_keys(keymap::Vi::new(&mut ed), ed, handler),
KeyBindings::Vi => Self::handle_keys(keymap::Vi::new(), ed, handler),
}
};
......@@ -123,6 +123,7 @@ impl Context {
mut ed: Editor<'a, W>,
handler: &mut C,
) -> io::Result<String> {
keymap.init(&mut ed);
for c in stdin().keys() {
if keymap.handle_key(c.unwrap(), &mut ed, handler)? {
break;
......
......@@ -12,15 +12,14 @@ use KeyMap;
/// let mut context = Context::new();
/// context.key_bindings = KeyBindings::Emacs;
/// ```
#[derive(Default)]
pub struct Emacs {
last_arg_fetch_index: Option<usize>,
}
impl Emacs {
pub fn new() -> Self {
Emacs {
last_arg_fetch_index: None,
}
Self::default()
}
fn handle_ctrl_key<'a, W: Write>(&mut self, c: char, ed: &mut Editor<'a, W>) -> io::Result<()> {
......
......@@ -4,13 +4,15 @@ use std::io::{self, ErrorKind, Write};
use termion::event::Key;
use Editor;
pub trait KeyMap {
pub trait KeyMap: Default {
fn handle_key_core<'a, W: Write>(
&mut self,
key: Key,
editor: &mut Editor<'a, W>,
) -> io::Result<()>;
fn init<'a, W: Write>(&mut self, _editor: &mut Editor<'a, W>) {}
fn handle_key<'a, W: Write, C: Completer<W>>(
&mut self,
mut key: Key,
......@@ -84,6 +86,7 @@ mod tests {
use termion::event::Key::*;
use Context;
#[derive(Default)]
struct TestKeyMap;
impl KeyMap for TestKeyMap {
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment