@@ -8,6 +8,7 @@ pub struct Prompt {
88 pub cursor: usize,
99 pub completion: Option<Vec<String>>,
1010 pub should_close: bool,
11+ pub completion_selection_index: Option<usize>,
1112 completion_fn: Box<dyn FnMut(&str) -> Option<Vec<String>>>,
1213 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
1314}
@@ -17,13 +18,15 @@ impl Prompt {
1718 prompt: String,
1819 completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static,
1920 callback_fn: impl FnMut(&mut Editor, &str) + 'static,
21+ command_list: Vec<String>,
2022 ) -> Prompt {
2123 Prompt {
2224 prompt,
2325 line: String::new(),
2426 cursor: 0,
25− completion: None,
27+ completion: Some(command_list),
2628 should_close: false,
29+ completion_selection_index: None,
2730 completion_fn: Box::new(completion_fn),
2831 callback_fn: Box::new(callback_fn),
2932 }
@@ -32,6 +35,7 @@ impl Prompt {
3235 pub fn insert_char(&mut self, c: char) {
3336 self.line.insert(self.cursor, c);
3437 self.cursor += 1;
38+ self.completion = (self.completion_fn)(&self.line);
3539 }
3640
3741 pub fn move_char_left(&mut self) {
@@ -58,6 +62,15 @@ impl Prompt {
5862 if self.cursor > 0 {
5963 self.line.remove(self.cursor - 1);
6064 self.cursor -= 1;
65+ self.completion = (self.completion_fn)(&self.line);
66+ }
67+ }
68+
69+ 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)
6174 }
6275 }
6376
@@ -96,9 +109,7 @@ impl Prompt {
96109 } => (self.callback_fn)(editor, &self.line),
97110 KeyEvent {
98111 code: KeyCode::Tab, ..
99− } => {
100− self.completion = (self.completion_fn)(&self.line);
101− }
112+ } => self.change_completion_selection(),
102113 _ => (),
103114 }
104115 }