Refactor command calling.

pull/4/head
Blaž Hrastnik 4 years ago
parent 49cc6c1924
commit 49b4cdb566

@ -37,7 +37,6 @@ static EX: smol::Executor = smol::Executor::new();
pub struct Application { pub struct Application {
editor: Editor, editor: Editor,
prompt: Option<Prompt>, prompt: Option<Prompt>,
should_close: bool,
terminal: Renderer, terminal: Renderer,
} }
@ -299,19 +298,18 @@ impl Application {
terminal, terminal,
// TODO; move to state // TODO; move to state
prompt: None, prompt: None,
should_close: false,
}; };
Ok(app) Ok(app)
} }
pub fn set_prompt(&mut self) { pub fn set_prompt(&mut self) {
// let commands = |input| match input { let commands = |editor: &mut Editor, input: &str| match input {
// "q" => self.should_close = true, "q" => editor.should_close = true,
// _ => (), _ => (),
// }; };
// let prompt = Prompt::new(|input| None, commands); let prompt = Prompt::new(|input| None, commands);
// self.prompt = Some(prompt); self.prompt = Some(prompt);
} }
fn render(&mut self) { fn render(&mut self) {
@ -341,12 +339,12 @@ impl Application {
self.render(); self.render();
loop { loop {
// Handle key events if self.editor.should_close {
if self.should_close {
break; break;
} }
let mut event = reader.next().await;
match event { // Handle key events
match reader.next().await {
Some(Ok(Event::Resize(width, height))) => { Some(Ok(Event::Resize(width, height))) => {
self.terminal.resize(width, height); self.terminal.resize(width, height);
@ -359,10 +357,16 @@ impl Application {
self.render(); self.render();
} }
Some(Ok(Event::Key(event))) => { Some(Ok(Event::Key(event))) => {
// TODO: sequences (`gg`) // if there's a prompt, it takes priority
// TODO: handle count other than 1 if let Some(prompt) = &mut self.prompt {
if let Some(view) = &mut self.editor.view { self.prompt
.as_mut()
.unwrap()
.handle_input(event, &mut self.editor);
} else if let Some(view) = &mut self.editor.view {
let keys = vec![event]; let keys = vec![event];
// TODO: sequences (`gg`)
// TODO: handle count other than 1
match view.state.mode() { match view.state.mode() {
Mode::Insert => { Mode::Insert => {
if let Some(command) = keymap[&Mode::Insert].get(&keys) { if let Some(command) = keymap[&Mode::Insert].get(&keys) {
@ -376,9 +380,7 @@ impl Application {
} }
view.ensure_cursor_in_view(); view.ensure_cursor_in_view();
} }
Mode::Command => { Mode::Command => unreachable!(),
self.prompt.as_mut().unwrap().handle_input(event, view);
}
mode => { mode => {
if let Some(command) = keymap[&mode].get(&keys) { if let Some(command) = keymap[&mode].get(&keys) {
command(view, 1); command(view, 1);
@ -391,9 +393,7 @@ impl Application {
self.render(); self.render();
} }
} }
Some(Ok(_)) => { Some(Ok(Event::Mouse(_))) => (), // unhandled
// unhandled event
}
Some(Err(x)) => panic!(x), Some(Err(x)) => panic!(x),
None => break, None => break,
} }

@ -6,11 +6,15 @@ use anyhow::Error;
pub struct Editor { pub struct Editor {
pub view: Option<View>, pub view: Option<View>,
pub should_close: bool,
} }
impl Editor { impl Editor {
pub fn new() -> Self { pub fn new() -> Self {
Self { view: None } Self {
view: None,
should_close: false,
}
} }
pub fn open(&mut self, path: PathBuf, size: (u16, u16)) -> Result<(), Error> { pub fn open(&mut self, path: PathBuf, size: (u16, u16)) -> Result<(), Error> {

@ -1,5 +1,5 @@
use crate::commands; use crate::commands;
use crate::View; use crate::{Editor, View};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::string::String; use std::string::String;
@ -7,13 +7,13 @@ pub struct Prompt {
pub buffer: String, pub buffer: String,
pub cursor_loc: usize, pub cursor_loc: usize,
completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>, completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
callback_fn: Box<dyn FnMut(&str)>, callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
} }
impl Prompt { impl Prompt {
pub fn new( pub fn new(
completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static, completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
callback_fn: impl FnMut(&str) + 'static, callback_fn: impl FnMut(&mut Editor, &str) + 'static,
) -> Prompt { ) -> Prompt {
Prompt { Prompt {
buffer: String::from(""), buffer: String::from(""),
@ -55,7 +55,7 @@ impl Prompt {
} }
} }
pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) { pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {
match key_event { match key_event {
KeyEvent { KeyEvent {
code: KeyCode::Char(c), code: KeyCode::Char(c),
@ -63,7 +63,7 @@ impl Prompt {
} => self.insert_char(c), } => self.insert_char(c),
KeyEvent { KeyEvent {
code: KeyCode::Esc, .. code: KeyCode::Esc, ..
} => commands::normal_mode(view, 1), } => unimplemented!("Exit prompt!"),
KeyEvent { KeyEvent {
code: KeyCode::Right, code: KeyCode::Right,
.. ..
@ -87,7 +87,7 @@ impl Prompt {
KeyEvent { KeyEvent {
code: KeyCode::Enter, code: KeyCode::Enter,
.. ..
} => (self.callback_fn)(&self.buffer), } => (self.callback_fn)(editor, &self.buffer),
_ => (), _ => (),
} }
} }

Loading…
Cancel
Save