|
| 1 | +use crate::completion::FileOnlyShCompleter; |
| 2 | +use crate::completion::ShCompleter; |
| 3 | +use crate::completion::{Completer, CompletionResult}; |
| 4 | +use crossterm::event::Event; |
| 5 | +use tui_input::backend::crossterm::to_input_request; |
| 6 | +use tui_input::{Input, InputRequest, InputResponse, StateChanged}; |
| 7 | + |
| 8 | +pub struct CompletableInput { |
| 9 | + input: Input, |
| 10 | + completions: Option<(CompletionResult, usize)>, |
| 11 | + completer: Box<dyn Completer>, |
| 12 | +} |
| 13 | + |
| 14 | +impl From<String> for CompletableInput { |
| 15 | + fn from(value: String) -> Self { |
| 16 | + Self { |
| 17 | + input: Input::new(value), |
| 18 | + completions: None, |
| 19 | + completer: Box::new(ShCompleter {}), |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl From<&str> for CompletableInput { |
| 25 | + fn from(value: &str) -> Self { |
| 26 | + Self { |
| 27 | + input: Input::new(value.to_string()), |
| 28 | + completions: None, |
| 29 | + completer: Box::new(ShCompleter {}), |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl CompletableInput { |
| 35 | + pub fn file_only(str: &str) -> Self { |
| 36 | + Self { |
| 37 | + input: Input::new(str.to_string()), |
| 38 | + completions: None, |
| 39 | + completer: Box::new(FileOnlyShCompleter {}), |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + pub fn cursor(&self) -> usize { |
| 44 | + self.input.cursor() |
| 45 | + } |
| 46 | + |
| 47 | + pub fn handle(&mut self, req: InputRequest) -> InputResponse { |
| 48 | + self.input.handle(req) |
| 49 | + } |
| 50 | + |
| 51 | + pub fn handle_event(&mut self, evt: &Event) -> Option<StateChanged> { |
| 52 | + self.completions = None; |
| 53 | + to_input_request(evt).and_then(|req| self.input.handle(req)) |
| 54 | + } |
| 55 | + |
| 56 | + pub fn value(&self) -> &str { |
| 57 | + self.input.value() |
| 58 | + } |
| 59 | + |
| 60 | + pub fn visual_cursor(&self) -> usize { |
| 61 | + self.input.visual_cursor() |
| 62 | + } |
| 63 | + |
| 64 | + pub fn clear_completions(&mut self) { |
| 65 | + self.completions = None; |
| 66 | + } |
| 67 | + |
| 68 | + pub fn complete(&mut self, next: bool) { |
| 69 | + let current_value = self.input.value().to_string(); |
| 70 | + let cursor_pos = self.input.visual_cursor(); |
| 71 | + |
| 72 | + if let Some((res, index)) = self.completions.as_mut() { |
| 73 | + if next { |
| 74 | + *index = (*index + 1) % res.completions.len(); |
| 75 | + } else { |
| 76 | + *index = if *index == 0 { |
| 77 | + res.completions.len() - 1 |
| 78 | + } else { |
| 79 | + *index - 1 |
| 80 | + }; |
| 81 | + } |
| 82 | + let completion = &res.completions[*index]; |
| 83 | + let new_value = format!( |
| 84 | + "{}{}{}", |
| 85 | + ¤t_value[..res.word_start], |
| 86 | + completion, |
| 87 | + ¤t_value[cursor_pos..] |
| 88 | + ); |
| 89 | + self.input = Input::from(new_value); |
| 90 | + self.input |
| 91 | + .handle(InputRequest::SetCursor(res.word_start + completion.len())); |
| 92 | + } else if let Some(res) = self.completer.completions(¤t_value, cursor_pos) { |
| 93 | + let index = if next { 0 } else { res.completions.len() - 1 }; |
| 94 | + let word_start = res.word_start; |
| 95 | + let completion = res.completions[index].clone(); |
| 96 | + let new_value = format!( |
| 97 | + "{}{}{}", |
| 98 | + ¤t_value[..word_start], |
| 99 | + completion, |
| 100 | + ¤t_value[cursor_pos..] |
| 101 | + ); |
| 102 | + self.completions = Some((res, index)); |
| 103 | + self.input = Input::from(new_value); |
| 104 | + self.input |
| 105 | + .handle(InputRequest::SetCursor(word_start + completion.len())); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#[cfg(test)] |
| 111 | +mod tests { |
| 112 | + use super::*; |
| 113 | + use crossterm::event::KeyCode::Char; |
| 114 | + use crossterm::event::{Event, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers}; |
| 115 | + use tui_input::Input; |
| 116 | + |
| 117 | + struct TestCompleter; |
| 118 | + |
| 119 | + impl Completer for TestCompleter { |
| 120 | + fn completions(&self, _input: &str, _cursor_pos: usize) -> Option<CompletionResult> { |
| 121 | + Some(CompletionResult { |
| 122 | + completions: vec!["command".to_string(), "command_other".to_string()], |
| 123 | + word_start: 0, |
| 124 | + }) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + impl Default for CompletableInput { |
| 129 | + fn default() -> Self { |
| 130 | + CompletableInput { |
| 131 | + input: Input::from(""), |
| 132 | + completions: None, |
| 133 | + completer: Box::new(TestCompleter {}), |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn completer() { |
| 140 | + let mut input = CompletableInput::default(); |
| 141 | + |
| 142 | + input_text(&mut input, "co"); |
| 143 | + |
| 144 | + input.complete(true); |
| 145 | + assert_eq!(input.value(), "command"); |
| 146 | + |
| 147 | + input.complete(true); |
| 148 | + assert_eq!(input.value(), "command_other"); |
| 149 | + |
| 150 | + input.complete(false); |
| 151 | + assert_eq!(input.value(), "command"); |
| 152 | + } |
| 153 | + |
| 154 | + fn input_text(app: &mut CompletableInput, text: &str) { |
| 155 | + for c in text.chars() { |
| 156 | + app.handle_event(&Event::Key(KeyEvent { |
| 157 | + code: Char(c), |
| 158 | + modifiers: KeyModifiers::NONE, |
| 159 | + kind: KeyEventKind::Press, |
| 160 | + state: KeyEventState::NONE, |
| 161 | + })); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments