added prompt close

pull/4/head
Jan Hrastnik 4 years ago
parent ae8ff9623e
commit 06502e5a2e

@ -235,7 +235,14 @@ impl Renderer {
.set_string(1, self.size.1 - 2, mode, self.text_color); .set_string(1, self.size.1 - 2, mode, self.text_color);
} }
pub fn render_prompt(&mut self, prompt: &Prompt) { pub fn render_prompt(&mut self, view: &View, prompt: &Prompt) {
// completion
if prompt.completion.is_some() {
self.surface.set_style(
Rect::new(0, self.size.1 - 6, self.size.0, 4),
view.theme.get("ui.statusline"),
);
}
// render buffer text // render buffer text
self.surface self.surface
.set_string(1, self.size.1 - 1, &prompt.prompt, self.text_color); .set_string(1, self.size.1 - 1, &prompt.prompt, self.text_color);
@ -309,10 +316,13 @@ impl Application {
if let Some(view) = &mut self.editor.view { if let Some(view) = &mut self.editor.view {
self.terminal.render_view(view, viewport); self.terminal.render_view(view, viewport);
} if let Some(prompt) = &self.prompt {
if prompt.should_close {
if let Some(prompt) = &self.prompt { self.prompt = None;
self.terminal.render_prompt(prompt); } else {
self.terminal.render_prompt(view, prompt);
}
}
} }
self.terminal.draw(); self.terminal.draw();
@ -383,7 +393,23 @@ impl Application {
{ {
let prompt = Prompt::new( let prompt = Prompt::new(
":".to_owned(), ":".to_owned(),
|_input: &str| None, // completion |_input: &str| {
let placeholder_list = vec![
String::from("aaa"),
String::from("bbb"),
String::from("ccc"),
];
let mut matches = vec![];
for command in placeholder_list {
if command.contains(_input) {
matches.push(command);
}
}
if matches.len() != 0 {
return Some(matches);
}
None
}, // completion
|editor: &mut Editor, input: &str| match input { |editor: &mut Editor, input: &str| match input {
"q" => editor.should_close = true, "q" => editor.should_close = true,
_ => (), _ => (),

@ -6,20 +6,24 @@ pub struct Prompt {
pub prompt: String, pub prompt: String,
pub line: String, pub line: String,
pub cursor: usize, pub cursor: usize,
completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>, pub completion: Option<Vec<String>>,
pub should_close: bool,
completion_fn: Box<dyn FnMut(&str) -> Option<Vec<String>>>,
callback_fn: Box<dyn FnMut(&mut Editor, &str)>, callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
} }
impl Prompt { impl Prompt {
pub fn new( pub fn new(
prompt: String, prompt: String,
completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static, completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static,
callback_fn: impl FnMut(&mut Editor, &str) + 'static, callback_fn: impl FnMut(&mut Editor, &str) + 'static,
) -> Prompt { ) -> Prompt {
Prompt { Prompt {
prompt, prompt,
line: String::new(), line: String::new(),
cursor: 0, cursor: 0,
completion: None,
should_close: false,
completion_fn: Box::new(completion_fn), completion_fn: Box::new(completion_fn),
callback_fn: Box::new(callback_fn), callback_fn: Box::new(callback_fn),
} }
@ -65,7 +69,7 @@ impl Prompt {
} => self.insert_char(c), } => self.insert_char(c),
KeyEvent { KeyEvent {
code: KeyCode::Esc, .. code: KeyCode::Esc, ..
} => unimplemented!("Exit prompt!"), } => self.should_close = true,
KeyEvent { KeyEvent {
code: KeyCode::Right, code: KeyCode::Right,
.. ..
@ -93,9 +97,8 @@ impl Prompt {
KeyEvent { KeyEvent {
code: KeyCode::Tab, .. code: KeyCode::Tab, ..
} => { } => {
let _completion = (self.completion_fn)(&self.line); self.completion = (self.completion_fn)(&self.line);
} }
_ => (), _ => (),
} }
} }

Loading…
Cancel
Save