several fixes

pull/4/head
Jan Hrastnik 4 years ago
parent 8f37c26f35
commit a123cf37a0

@ -237,8 +237,7 @@ impl Renderer {
pub fn render_prompt(&mut self, view: &View, prompt: &Prompt) { pub fn render_prompt(&mut self, view: &View, prompt: &Prompt) {
// completion // completion
if prompt.completion.is_some() { if let Some(completion) = &prompt.completion {
let completion = prompt.completion.clone().unwrap();
// TODO: find out better way of clearing individual lines of the screen // TODO: find out better way of clearing individual lines of the screen
for i in (3..7) { for i in (3..7) {
self.surface.set_string( self.surface.set_string(
@ -253,23 +252,16 @@ impl Renderer {
view.theme.get("ui.statusline"), view.theme.get("ui.statusline"),
); );
for i in (0..completion.len()) { for i in (0..completion.len()) {
let color;
if prompt.completion_selection_index.is_some() if prompt.completion_selection_index.is_some()
&& i == prompt.completion_selection_index.unwrap() && i == prompt.completion_selection_index.unwrap()
{ {
self.surface.set_string( color = Style::default().bg(Color::Rgb(104, 060, 232));
1,
self.size.1 - 6 + i as u16,
&completion[i],
Style::default().bg(Color::Rgb(104, 060, 232)),
);
} else { } else {
self.surface.set_string( color = self.text_color;
1,
self.size.1 - 6 + i as u16,
&completion[i],
self.text_color,
);
} }
self.surface
.set_string(1, self.size.1 - 6 + i as u16, &completion[i], color);
} }
} }
// render buffer text // render buffer text
@ -420,25 +412,18 @@ impl Application {
.. ..
}] = keys.as_slice() }] = keys.as_slice()
{ {
let command_list = vec![
String::from("q"),
String::from("aaa"),
String::from("bbb"),
String::from("ccc"),
];
let prompt = Prompt::new( let prompt = Prompt::new(
":".to_owned(), ":".to_owned(),
|_input: &str| { |_input: &str| {
let mut matches = vec![]; let mut matches = vec![];
// TODO: i need this duplicate list right now to avoid borrow checker issues // TODO: i need this duplicate list right now to avoid borrow checker issues
let placeholder_list = vec![ let command_list = vec![
String::from("q"), String::from("q"),
String::from("aaa"), String::from("aaa"),
String::from("bbb"), String::from("bbb"),
String::from("ccc"), String::from("ccc"),
]; ];
for command in placeholder_list { for command in command_list {
if command.contains(_input) { if command.contains(_input) {
matches.push(command); matches.push(command);
} }
@ -452,7 +437,6 @@ impl Application {
"q" => editor.should_close = true, "q" => editor.should_close = true,
_ => (), _ => (),
}, },
command_list,
); );
self.prompt = Some(prompt); self.prompt = Some(prompt);

@ -16,15 +16,14 @@ pub struct Prompt {
impl Prompt { impl Prompt {
pub fn new( pub fn new(
prompt: String, prompt: String,
completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static, mut 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,
command_list: Vec<String>,
) -> Prompt { ) -> Prompt {
Prompt { Prompt {
prompt, prompt,
line: String::new(), line: String::new(),
cursor: 0, cursor: 0,
completion: Some(command_list), completion: completion_fn(""),
should_close: false, should_close: false,
completion_selection_index: None, completion_selection_index: None,
completion_fn: Box::new(completion_fn), completion_fn: Box::new(completion_fn),
@ -67,11 +66,16 @@ impl Prompt {
} }
pub fn change_completion_selection(&mut self) { pub fn change_completion_selection(&mut self) {
if self.completion_selection_index.is_none() { self.completion_selection_index = self
self.completion_selection_index = Some(0) .completion_selection_index
.map(|i| {
if i == self.completion.as_ref().unwrap().len() - 1 {
0
} else { } else {
self.completion_selection_index = Some(self.completion_selection_index.unwrap() + 1) i + 1
} }
})
.or(Some(0))
} }
pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) { pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {

Loading…
Cancel
Save