on master help sign in
Refactor command calling.
commit49b4cdb5669b1c572b2b26af4353cb16cb1260f7 authorBlaž Hrastnik <blaz@mxxn.io> dateFri 16 Oct 2020, 14:37 (6 years ago) parent49cc6c1
3 files changed+32−28
helix-term/src/application.rs+21−21 helix-view/src/editor.rs+5−1 helix-view/src/prompt.rs+6−6
helix-term/src/application.rs+21−21
@@ -37,7 +37,6 @@ static EX: smol::Executor = smol::Executor::new();
3737pub struct Application {
3838 editor: Editor,
3939 prompt: Option<Prompt>,
40− should_close: bool,
4140 terminal: Renderer,
4241}
4342
@@ -299,19 +298,18 @@ impl Application {
299298 terminal,
300299 // TODO; move to state
301300 prompt: None,
302− should_close: false,
303301 };
304302
305303 Ok(app)
306304 }
307305
308306 pub fn set_prompt(&mut self) {
309− // let commands = |input| match input {
310− // "q" => self.should_close = true,
311− // _ => (),
312− // };
313− // let prompt = Prompt::new(|input| None, commands);
314− // self.prompt = Some(prompt);
307+ let commands = |editor: &mut Editor, input: &str| match input {
308+ "q" => editor.should_close = true,
309+ _ => (),
310+ };
311+ let prompt = Prompt::new(|input| None, commands);
312+ self.prompt = Some(prompt);
315313 }
316314
317315 fn render(&mut self) {
@@ -341,12 +339,12 @@ impl Application {
341339 self.render();
342340
343341 loop {
344− // Handle key events
345− if self.should_close {
342+ if self.editor.should_close {
346343 break;
347344 }
348− let mut event = reader.next().await;
349− match event {
345+
346+ // Handle key events
347+ match reader.next().await {
350348 Some(Ok(Event::Resize(width, height))) => {
351349 self.terminal.resize(width, height);
352350
@@ -359,10 +357,16 @@ impl Application {
359357 self.render();
360358 }
361359 Some(Ok(Event::Key(event))) => {
362− // TODO: sequences (`gg`)
363− // TODO: handle count other than 1
364− if let Some(view) = &mut self.editor.view {
360+ // if there's a prompt, it takes priority
361+ if let Some(prompt) = &mut self.prompt {
362+ self.prompt
363+ .as_mut()
364+ .unwrap()
365+ .handle_input(event, &mut self.editor);
366+ } else if let Some(view) = &mut self.editor.view {
365367 let keys = vec![event];
368+ // TODO: sequences (`gg`)
369+ // TODO: handle count other than 1
366370 match view.state.mode() {
367371 Mode::Insert => {
368372 if let Some(command) = keymap[&Mode::Insert].get(&keys) {
@@ -376,9 +380,7 @@ impl Application {
376380 }
377381 view.ensure_cursor_in_view();
378382 }
379− Mode::Command => {
380− self.prompt.as_mut().unwrap().handle_input(event, view);
381− }
383+ Mode::Command => unreachable!(),
382384 mode => {
383385 if let Some(command) = keymap[&mode].get(&keys) {
384386 command(view, 1);
@@ -391,9 +393,7 @@ impl Application {
391393 self.render();
392394 }
393395 }
394− Some(Ok(_)) => {
395− // unhandled event
396− }
396+ Some(Ok(Event::Mouse(_))) => (), // unhandled
397397 Some(Err(x)) => panic!(x),
398398 None => break,
399399 }
helix-view/src/editor.rs+5−1
@@ -6,11 +6,15 @@ use anyhow::Error;
66
77pub struct Editor {
88 pub view: Option<View>,
9+ pub should_close: bool,
910}
1011
1112impl Editor {
1213 pub fn new() -> Self {
13− Self { view: None }
14+ Self {
15+ view: None,
16+ should_close: false,
17+ }
1418 }
1519
1620 pub fn open(&mut self, path: PathBuf, size: (u16, u16)) -> Result<(), Error> {
helix-view/src/prompt.rs+6−6
@@ -1,5 +1,5 @@
11use crate::commands;
2−use crate::View;
2+use crate::{Editor, View};
33use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
44use std::string::String;
55
@@ -7,13 +7,13 @@ pub struct Prompt {
77 pub buffer: String,
88 pub cursor_loc: usize,
99 completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
10− callback_fn: Box<dyn FnMut(&str)>,
10+ callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
1111}
1212
1313impl Prompt {
1414 pub fn new(
1515 completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
16− callback_fn: impl FnMut(&str) + 'static,
16+ callback_fn: impl FnMut(&mut Editor, &str) + 'static,
1717 ) -> Prompt {
1818 Prompt {
1919 buffer: String::from(""),
@@ -55,7 +55,7 @@ impl Prompt {
5555 }
5656 }
5757
58− pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
58+ pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {
5959 match key_event {
6060 KeyEvent {
6161 code: KeyCode::Char(c),
@@ -63,7 +63,7 @@ impl Prompt {
6363 } => self.insert_char(c),
6464 KeyEvent {
6565 code: KeyCode::Esc, ..
66− } => commands::normal_mode(view, 1),
66+ } => unimplemented!("Exit prompt!"),
6767 KeyEvent {
6868 code: KeyCode::Right,
6969 ..
@@ -87,7 +87,7 @@ impl Prompt {
8787 KeyEvent {
8888 code: KeyCode::Enter,
8989 ..
90− } => (self.callback_fn)(&self.buffer),
90+ } => (self.callback_fn)(editor, &self.buffer),
9191 _ => (),
9292 }
9393 }