on master help sign in
several fixes
commita123cf37a0d8a60146c82b898ab8da9f56276b17 authorJan Hrastnik <jan.hrastnik2@gmail.com> dateSat 24 Oct 2020, 13:36 (6 years ago) parent8f37c26
2 files changed+20−32
helix-term/src/application.rs+8−24 helix-view/src/prompt.rs+12−8
helix-term/src/application.rs+8−24
@@ -237,8 +237,7 @@ impl Renderer {
237237
238238 pub fn render_prompt(&mut self, view: &View, prompt: &Prompt) {
239239 // completion
240− if prompt.completion.is_some() {
241− let completion = prompt.completion.clone().unwrap();
240+ if let Some(completion) = &prompt.completion {
242241 // TODO: find out better way of clearing individual lines of the screen
243242 for i in (3..7) {
244243 self.surface.set_string(
@@ -253,23 +252,16 @@ impl Renderer {
253252 view.theme.get("ui.statusline"),
254253 );
255254 for i in (0..completion.len()) {
255+ let color;
256256 if prompt.completion_selection_index.is_some()
257257 && i == prompt.completion_selection_index.unwrap()
258258 {
259− self.surface.set_string(
260− 1,
261− self.size.1 - 6 + i as u16,
262− &completion[i],
263− Style::default().bg(Color::Rgb(104, 060, 232)),
264− );
259+ color = Style::default().bg(Color::Rgb(104, 060, 232));
265260 } else {
266− self.surface.set_string(
267− 1,
268− self.size.1 - 6 + i as u16,
269− &completion[i],
270− self.text_color,
271− );
261+ color = self.text_color;
272262 }
263+ self.surface
264+ .set_string(1, self.size.1 - 6 + i as u16, &completion[i], color);
273265 }
274266 }
275267 // render buffer text
@@ -420,25 +412,18 @@ impl Application {
420412 ..
421413 }] = keys.as_slice()
422414 {
423− let command_list = vec![
424− String::from("q"),
425− String::from("aaa"),
426− String::from("bbb"),
427− String::from("ccc"),
428− ];
429−
430415 let prompt = Prompt::new(
431416 ":".to_owned(),
432417 |_input: &str| {
433418 let mut matches = vec![];
434419 // TODO: i need this duplicate list right now to avoid borrow checker issues
435− let placeholder_list = vec![
420+ let command_list = vec![
436421 String::from("q"),
437422 String::from("aaa"),
438423 String::from("bbb"),
439424 String::from("ccc"),
440425 ];
441− for command in placeholder_list {
426+ for command in command_list {
442427 if command.contains(_input) {
443428 matches.push(command);
444429 }
@@ -452,7 +437,6 @@ impl Application {
452437 "q" => editor.should_close = true,
453438 _ => (),
454439 },
455− command_list,
456440 );
457441
458442 self.prompt = Some(prompt);
helix-view/src/prompt.rs+12−8
@@ -16,15 +16,14 @@ pub struct Prompt {
1616impl Prompt {
1717 pub fn new(
1818 prompt: String,
19− completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static,
19+ mut completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static,
2020 callback_fn: impl FnMut(&mut Editor, &str) + 'static,
21− command_list: Vec<String>,
2221 ) -> Prompt {
2322 Prompt {
2423 prompt,
2524 line: String::new(),
2625 cursor: 0,
27− completion: Some(command_list),
26+ completion: completion_fn(""),
2827 should_close: false,
2928 completion_selection_index: None,
3029 completion_fn: Box::new(completion_fn),
@@ -67,11 +66,16 @@ impl Prompt {
6766 }
6867
6968 pub fn change_completion_selection(&mut self) {
70− if self.completion_selection_index.is_none() {
71− self.completion_selection_index = Some(0)
72− } else {
73− self.completion_selection_index = Some(self.completion_selection_index.unwrap() + 1)
74− }
69+ self.completion_selection_index = self
70+ .completion_selection_index
71+ .map(|i| {
72+ if i == self.completion.as_ref().unwrap().len() - 1 {
73+ 0
74+ } else {
75+ i + 1
76+ }
77+ })
78+ .or(Some(0))
7579 }
7680
7781 pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {