on master help sign in
added prompt close
06502e5Jan Hrastnik
helix-term/src/application.rs+32−6
@@ -235,7 +235,14 @@ impl Renderer {
235 .set_string(1, self.size.1 - 2, mode, self.text_color);
235 .set_string(1, self.size.1 - 2, mode, self.text_color);
236 }
236 }
237
237
238− pub fn render_prompt(&mut self, prompt: &Prompt) {
238+ pub fn render_prompt(&mut self, view: &View, prompt: &Prompt) {
239+ // completion
240+ if prompt.completion.is_some() {
241+ self.surface.set_style(
242+ Rect::new(0, self.size.1 - 6, self.size.0, 4),
243+ view.theme.get("ui.statusline"),
244+ );
245+ }
239 // render buffer text
246 // render buffer text
240 self.surface
247 self.surface
241 .set_string(1, self.size.1 - 1, &prompt.prompt, self.text_color);
248 .set_string(1, self.size.1 - 1, &prompt.prompt, self.text_color);
@@ -309,10 +316,13 @@ impl Application {
309
316
310 if let Some(view) = &mut self.editor.view {
317 if let Some(view) = &mut self.editor.view {
311 self.terminal.render_view(view, viewport);
318 self.terminal.render_view(view, viewport);
312− }
313−
314− if let Some(prompt) = &self.prompt {
319+ if let Some(prompt) = &self.prompt {
320+ if prompt.should_close {
321+ self.prompt = None;
322+ } else {
315− self.terminal.render_prompt(prompt);
323+ self.terminal.render_prompt(view, prompt);
324+ }
325+ }
316 }
326 }
317
327
318 self.terminal.draw();
328 self.terminal.draw();
@@ -383,7 +393,23 @@ impl Application {
383 {
393 {
384 let prompt = Prompt::new(
394 let prompt = Prompt::new(
385 ":".to_owned(),
395 ":".to_owned(),
396+ |_input: &str| {
397+ let placeholder_list = vec![
398+ String::from("aaa"),
399+ String::from("bbb"),
400+ String::from("ccc"),
401+ ];
402+ let mut matches = vec![];
403+ for command in placeholder_list {
404+ if command.contains(_input) {
405+ matches.push(command);
406+ }
407+ }
408+ if matches.len() != 0 {
409+ return Some(matches);
410+ }
411+ None
386− |_input: &str| None, // completion
412+ }, // completion
387 |editor: &mut Editor, input: &str| match input {
413 |editor: &mut Editor, input: &str| match input {
388 "q" => editor.should_close = true,
414 "q" => editor.should_close = true,
389 _ => (),
415 _ => (),
helix-view/src/prompt.rs+8−5
@@ -6,20 +6,24 @@ pub struct Prompt {
6 pub prompt: String,
6 pub prompt: String,
7 pub line: String,
7 pub line: String,
8 pub cursor: usize,
8 pub cursor: usize,
9+ pub completion: Option<Vec<String>>,
10+ pub should_close: bool,
9− completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
11+ completion_fn: Box<dyn FnMut(&str) -> Option<Vec<String>>>,
10 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
12 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
11}
13}
12
14
13impl Prompt {
15impl Prompt {
14 pub fn new(
16 pub fn new(
15 prompt: String,
17 prompt: String,
16− completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
18+ completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static,
17 callback_fn: impl FnMut(&mut Editor, &str) + 'static,
19 callback_fn: impl FnMut(&mut Editor, &str) + 'static,
18 ) -> Prompt {
20 ) -> Prompt {
19 Prompt {
21 Prompt {
20 prompt,
22 prompt,
21 line: String::new(),
23 line: String::new(),
22 cursor: 0,
24 cursor: 0,
25+ completion: None,
26+ should_close: false,
23 completion_fn: Box::new(completion_fn),
27 completion_fn: Box::new(completion_fn),
24 callback_fn: Box::new(callback_fn),
28 callback_fn: Box::new(callback_fn),
25 }
29 }
@@ -65,7 +69,7 @@ impl Prompt {
65 } => self.insert_char(c),
69 } => self.insert_char(c),
66 KeyEvent {
70 KeyEvent {
67 code: KeyCode::Esc, ..
71 code: KeyCode::Esc, ..
68− } => unimplemented!("Exit prompt!"),
72+ } => self.should_close = true,
69 KeyEvent {
73 KeyEvent {
70 code: KeyCode::Right,
74 code: KeyCode::Right,
71 ..
75 ..
@@ -93,9 +97,8 @@ impl Prompt {
93 KeyEvent {
97 KeyEvent {
94 code: KeyCode::Tab, ..
98 code: KeyCode::Tab, ..
95 } => {
99 } => {
96− let _completion = (self.completion_fn)(&self.line);
100+ self.completion = (self.completion_fn)(&self.line);
97 }
101 }
98−
99 _ => (),
102 _ => (),
100 }
103 }
101 }
104 }