|
1 | 1 | use std::{fmt::Debug, io, iter, str::FromStr};
|
2 | 2 |
|
| 3 | +#[cfg(feature = "completion")] |
| 4 | +use crate::completion::Completion; |
3 | 5 | #[cfg(feature = "history")]
|
4 | 6 | use crate::history::History;
|
5 | 7 | use crate::{
|
@@ -45,6 +47,8 @@ pub struct Input<'a, T> {
|
45 | 47 | validator: Option<Box<dyn FnMut(&T) -> Option<String> + 'a>>,
|
46 | 48 | #[cfg(feature = "history")]
|
47 | 49 | history: Option<&'a mut dyn History<T>>,
|
| 50 | + #[cfg(feature = "completion")] |
| 51 | + completion: Option<&'a dyn Completion>, |
48 | 52 | }
|
49 | 53 |
|
50 | 54 | impl<T> Default for Input<'static, T> {
|
@@ -114,6 +118,8 @@ impl<'a, T> Input<'a, T> {
|
114 | 118 | validator: None,
|
115 | 119 | #[cfg(feature = "history")]
|
116 | 120 | history: None,
|
| 121 | + #[cfg(feature = "completion")] |
| 122 | + completion: None, |
117 | 123 | }
|
118 | 124 | }
|
119 | 125 |
|
@@ -166,6 +172,16 @@ impl<'a, T> Input<'a, T> {
|
166 | 172 | self.history = Some(history);
|
167 | 173 | self
|
168 | 174 | }
|
| 175 | + |
| 176 | + /// Enable completion |
| 177 | + #[cfg(feature = "completion")] |
| 178 | + pub fn completion_with<C>(&mut self, completion: &'a C) -> &mut Self |
| 179 | + where |
| 180 | + C: Completion, |
| 181 | + { |
| 182 | + self.completion = Some(completion); |
| 183 | + self |
| 184 | + } |
169 | 185 | }
|
170 | 186 |
|
171 | 187 | impl<'a, T> Input<'a, T>
|
@@ -297,6 +313,23 @@ where
|
297 | 313 | position += 1;
|
298 | 314 | term.flush()?;
|
299 | 315 | }
|
| 316 | + #[cfg(feature = "completion")] |
| 317 | + Key::ArrowRight | Key::Tab => { |
| 318 | + if let Some(completion) = &self.completion { |
| 319 | + let input: String = chars.clone().into_iter().collect(); |
| 320 | + if let Some(x) = completion.get(&input) { |
| 321 | + term.clear_chars(chars.len())?; |
| 322 | + chars.clear(); |
| 323 | + position = 0; |
| 324 | + for ch in x.chars() { |
| 325 | + chars.insert(position, ch); |
| 326 | + position += 1; |
| 327 | + } |
| 328 | + term.write_str(&x)?; |
| 329 | + term.flush()?; |
| 330 | + } |
| 331 | + } |
| 332 | + } |
300 | 333 | #[cfg(feature = "history")]
|
301 | 334 | Key::ArrowUp => {
|
302 | 335 | if let Some(history) = &self.history {
|
|
0 commit comments