Added default keybindings in separate module

This commit is contained in:
Ceferino Patino 2025-01-08 21:22:58 -06:00
commit 694c60d386
Signed by: c4patino
SSH key fingerprint: SHA256:9fQ9TsujGrdNNi76mnsu63v7dS5JOmHRZEqBOl49OR8
4 changed files with 30 additions and 70 deletions

View file

@ -1,19 +1,19 @@
use color_eyre::Report;
use crossterm::{
cursor,
event::{self, poll, read, Event, KeyCode, KeyEvent, KeyModifiers},
event::{poll, read, Event, KeyEvent},
execute, queue, style,
terminal::{self, ClearType},
};
use std::{
fs::File,
io::{self, BufRead, BufReader, Write},
sync::{atomic::AtomicBool, mpsc, Arc},
sync::mpsc,
time::{Duration, Instant},
};
use tokio::{runtime::Runtime, sync::oneshot};
use tokio::runtime::Runtime;
use super::Keymap;
use super::{default_keybinds, Keymap};
#[derive(Debug, Eq, PartialEq, Hash)]
pub enum Mode {
@ -23,28 +23,22 @@ pub enum Mode {
VISUAL,
}
#[derive(Debug)]
pub enum ControlCode {
CONTINUE,
QUIT,
}
#[derive(Debug)]
pub struct Editor {
buffer: Vec<String>,
dirty: bool,
stop: bool,
pub(crate) buffer: Vec<String>,
pub(crate) dirty: bool,
pub(crate) stop: bool,
mode: Mode,
pub(crate) mode: Mode,
size: (u16, u16),
cursor: (u16, u16),
offset: (u16, u16),
pub(crate) size: (u16, u16),
pub(crate) cursor: (u16, u16),
pub(crate) offset: (u16, u16),
keymap: Keymap,
last_key_time: Instant,
pub(crate) keymap: Keymap,
pub(crate) last_key_time: Instant,
out: io::Stdout,
pub(crate) out: io::Stdout,
}
impl Drop for Editor {
@ -56,25 +50,7 @@ impl Drop for Editor {
impl Editor {
pub fn new() -> Self {
let mut keymap = Keymap::new();
keymap.add_keybind(
vec![Mode::NORMAL],
vec![
KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL),
KeyEvent::new(KeyCode::Char('q'), KeyModifiers::CONTROL),
],
|editor| Ok(editor.stop = true),
);
keymap.add_keybind(
vec![Mode::NORMAL],
vec![
KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL),
KeyEvent::new(KeyCode::Char('q'), KeyModifiers::CONTROL),
KeyEvent::new(KeyCode::Char('q'), KeyModifiers::CONTROL),
],
|editor| Ok(editor.buffer.push("Double QUIT".to_string())),
);
default_keybinds(&mut keymap);
let mut editor = Self {
buffer: vec![String::new()],

13
src/editor/keybinds.rs Normal file
View file

@ -0,0 +1,13 @@
use super::{Keymap, Mode};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
pub fn default_keybinds(keymap: &mut Keymap) {
keymap.add_keybind(
vec![Mode::NORMAL],
vec![
KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL),
KeyEvent::new(KeyCode::Char('q'), KeyModifiers::CONTROL),
],
|editor| Ok(editor.stop = true),
);
}

View file

@ -107,34 +107,3 @@ impl Keymap {
self.current.is_none()
}
}
//impl<W: io::Write> Keymap<W> {
// pub fn new() -> Self {
// Self {
// root: KeyNode::new(),
// command: Vec::new(),
// }
// }
//
// pub fn add_keybind<F>(&mut self, modes: Vec<Mode>, sequence: Vec<KeyEvent>, action: F)
// where
// F: FnMut(&mut Editor<W>) -> io::Result<ControlCode> + 'static,
// {
// for mode in modes {
// self.root.insert(mode, sequence.clone(), action);
// }
// }
//
// pub fn clear(&mut self) {
// self.command.clear();
// }
//
// pub fn traverse(&mut self, mode: Mode, event: KeyEvent) -> Option<&mut KeyNode<W>> {
// let mut current_node = &mut self.root;
//
// if let Some(next_node) = current_node.children.get_mut(&(mode, event)) {
// self.current_path.push((mode, event));
// self.current
// }
// }
//}

View file

@ -1,7 +1,9 @@
mod editor;
mod keybinds;
mod keymap;
pub(crate) use self::editor::{ControlCode, Mode};
pub(crate) use self::keybinds::default_keybinds;
pub(crate) use self::keymap::Keymap;
pub use self::editor::Editor;