on master help sign in
added closures to prompt
fa55b1eJan Hrastnik
helix-term/src/editor.rs+17−10
@@ -36,7 +36,8 @@ pub struct Editor {
36 size: (u16, u16),
36 size: (u16, u16),
37 surface: Surface,
37 surface: Surface,
38 cache: Surface,
38 cache: Surface,
39− prompt: Prompt,
39+ prompt: Option<Prompt>,
40+ should_close: bool,
40}
41}
41
42
42impl Editor {
43impl Editor {
@@ -46,7 +47,6 @@ impl Editor {
46 let mut terminal = Terminal::new(backend)?;
47 let mut terminal = Terminal::new(backend)?;
47 let size = terminal::size().unwrap();
48 let size = terminal::size().unwrap();
48 let area = Rect::new(0, 0, size.0, size.1);
49 let area = Rect::new(0, 0, size.0, size.1);
49− let prompt = Prompt::new();
50
50
51 let mut editor = Editor {
51 let mut editor = Editor {
52 terminal,
52 terminal,
@@ -55,7 +55,8 @@ impl Editor {
55 surface: Surface::empty(area),
55 surface: Surface::empty(area),
56 cache: Surface::empty(area),
56 cache: Surface::empty(area),
57 // TODO; move to state
57 // TODO; move to state
58− prompt,
58+ prompt: None,
59+ should_close: false,
59 };
60 };
60
61
61 if let Some(file) = args.values_of_t::<PathBuf>("files").unwrap().pop() {
62 if let Some(file) = args.values_of_t::<PathBuf>("files").unwrap().pop() {
@@ -65,6 +66,15 @@ impl Editor {
65 Ok(editor)
66 Ok(editor)
66 }
67 }
67
68
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+
68 pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
78 pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
69 self.view = Some(View::open(path, self.size)?);
79 self.view = Some(View::open(path, self.size)?);
70 Ok(())
80 Ok(())
@@ -304,10 +314,14 @@ impl Editor {
304 let mut reader = EventStream::new();
314 let mut reader = EventStream::new();
305 let keymap = keymap::default();
315 let keymap = keymap::default();
306
316
317+ self.set_commands();
307 self.render();
318 self.render();
308
319
309 loop {
320 loop {
310 // Handle key events
321 // Handle key events
322+ if self.should_close {
323+ break;
324+ }
311 let mut event = reader.next().await;
325 let mut event = reader.next().await;
312 match event {
326 match event {
313 Some(Ok(Event::Resize(width, height))) => {
327 Some(Ok(Event::Resize(width, height))) => {
@@ -324,13 +338,6 @@ impl Editor {
324
338
325 self.render();
339 self.render();
326 }
340 }
327− Some(Ok(Event::Key(KeyEvent {
328− code: KeyCode::Char('q'),
329− ..
330− }))) => {
331− break;
332− }
333−
334 Some(Ok(Event::Key(event))) => {
341 Some(Ok(Event::Key(event))) => {
335 // TODO: sequences (`gg`)
342 // TODO: sequences (`gg`)
336 // TODO: handle count other than 1
343 // TODO: handle count other than 1
helix-view/src/prompt.rs+14−8
@@ -6,15 +6,21 @@ use std::string::String;
6pub struct Prompt {
6pub struct Prompt {
7 pub buffer: String,
7 pub buffer: String,
8 pub cursor_loc: usize,
8 pub cursor_loc: usize,
9+ completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
10+ callback_fn: Box<dyn Fn(&str)>,
9}
11}
10
12
11impl Prompt {
13impl Prompt {
12− pub fn new() -> Prompt {
14+ pub fn new(
15+ completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
16+ callback_fn: impl Fn(&str) + 'static,
17+ ) -> Prompt {
13− let prompt = Prompt {
18+ Prompt {
14 buffer: String::from(""),
19 buffer: String::from(""),
15 cursor_loc: 0,
20 cursor_loc: 0,
21+ completion_fn: Box::new(completion_fn),
22+ callback_fn: Box::new(callback_fn),
16− };
23+ }
17− prompt
18 }
24 }
19
25
20 pub fn insert_char(&mut self, c: char) {
26 pub fn insert_char(&mut self, c: char) {
@@ -49,10 +55,6 @@ impl Prompt {
49 }
55 }
50 }
56 }
51
57
52− pub fn success_fn() {
53− // TODO:
54− }
55−
56 pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
58 pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
57 match key_event {
59 match key_event {
58 KeyEvent {
60 KeyEvent {
@@ -82,6 +84,10 @@ impl Prompt {
82 code: KeyCode::Backspace,
84 code: KeyCode::Backspace,
83 ..
85 ..
84 } => self.delete_char_backwards(),
86 } => self.delete_char_backwards(),
87+ KeyEvent {
88+ code: KeyCode::Enter,
89+ ..
90+ } => (self.callback_fn)(&self.buffer),
85 _ => (),
91 _ => (),
86 }
92 }
87 }
93 }