on master help sign in
added closures to prompt
commitfa55b1e51c2310cf73689da37c5c57441d7e89c3 authorJan Hrastnik <jan.hrastnik2@gmail.com> dateThu 15 Oct 2020, 12:08 (6 years ago) parent3c0f187
2 files changed+31−18
helix-term/src/editor.rs+17−10 helix-view/src/prompt.rs+14−8
helix-term/src/editor.rs+17−10
@@ -36,7 +36,8 @@ pub struct Editor {
3636 size: (u16, u16),
3737 surface: Surface,
3838 cache: Surface,
39− prompt: Prompt,
39+ prompt: Option<Prompt>,
40+ should_close: bool,
4041}
4142
4243impl Editor {
@@ -46,7 +47,6 @@ impl Editor {
4647 let mut terminal = Terminal::new(backend)?;
4748 let size = terminal::size().unwrap();
4849 let area = Rect::new(0, 0, size.0, size.1);
49− let prompt = Prompt::new();
5050
5151 let mut editor = Editor {
5252 terminal,
@@ -55,7 +55,8 @@ impl Editor {
5555 surface: Surface::empty(area),
5656 cache: Surface::empty(area),
5757 // TODO; move to state
58− prompt,
58+ prompt: None,
59+ should_close: false,
5960 };
6061
6162 if let Some(file) = args.values_of_t::<PathBuf>("files").unwrap().pop() {
@@ -65,6 +66,15 @@ impl Editor {
6566 Ok(editor)
6667 }
6768
69+ pub fn set_commands(self) {
70+ let commands = |input: &str| match input {
71+ "q" => self.should_close = true,
72+ _ => (),
73+ };
74+ let prompt = Prompt::new(|input| None, commands);
75+ self.prompt = Some(prompt);
76+ }
77+
6878 pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
6979 self.view = Some(View::open(path, self.size)?);
7080 Ok(())
@@ -304,10 +314,14 @@ impl Editor {
304314 let mut reader = EventStream::new();
305315 let keymap = keymap::default();
306316
317+ self.set_commands();
307318 self.render();
308319
309320 loop {
310321 // Handle key events
322+ if self.should_close {
323+ break;
324+ }
311325 let mut event = reader.next().await;
312326 match event {
313327 Some(Ok(Event::Resize(width, height))) => {
@@ -324,13 +338,6 @@ impl Editor {
324338
325339 self.render();
326340 }
327− Some(Ok(Event::Key(KeyEvent {
328− code: KeyCode::Char('q'),
329− ..
330− }))) => {
331− break;
332− }
333−
334341 Some(Ok(Event::Key(event))) => {
335342 // TODO: sequences (`gg`)
336343 // TODO: handle count other than 1
helix-view/src/prompt.rs+14−8
@@ -6,15 +6,21 @@ use std::string::String;
66pub struct Prompt {
77 pub buffer: String,
88 pub cursor_loc: usize,
9+ completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
10+ callback_fn: Box<dyn Fn(&str)>,
911}
1012
1113impl Prompt {
12− pub fn new() -> Prompt {
13− let prompt = Prompt {
14+ pub fn new(
15+ completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
16+ callback_fn: impl Fn(&str) + 'static,
17+ ) -> Prompt {
18+ Prompt {
1419 buffer: String::from(""),
1520 cursor_loc: 0,
16− };
17− prompt
21+ completion_fn: Box::new(completion_fn),
22+ callback_fn: Box::new(callback_fn),
23+ }
1824 }
1925
2026 pub fn insert_char(&mut self, c: char) {
@@ -49,10 +55,6 @@ impl Prompt {
4955 }
5056 }
5157
52− pub fn success_fn() {
53− // TODO:
54− }
55−
5658 pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
5759 match key_event {
5860 KeyEvent {
@@ -82,6 +84,10 @@ impl Prompt {
8284 code: KeyCode::Backspace,
8385 ..
8486 } => self.delete_char_backwards(),
87+ KeyEvent {
88+ code: KeyCode::Enter,
89+ ..
90+ } => (self.callback_fn)(&self.buffer),
8591 _ => (),
8692 }
8793 }