completion/
completion.rs

1use dialoguer::{theme::ColorfulTheme, Completion, Input};
2
3fn main() {
4    println!("Use the Right arrow or Tab to complete your command");
5
6    let completion = MyCompletion::default();
7
8    Input::<String>::with_theme(&ColorfulTheme::default())
9        .with_prompt("dialoguer")
10        .completion_with(&completion)
11        .interact_text()
12        .unwrap();
13}
14
15struct MyCompletion {
16    options: Vec<String>,
17}
18
19impl Default for MyCompletion {
20    fn default() -> Self {
21        MyCompletion {
22            options: vec![
23                "orange".to_string(),
24                "apple".to_string(),
25                "banana".to_string(),
26            ],
27        }
28    }
29}
30
31impl Completion for MyCompletion {
32    /// Simple completion implementation based on substring
33    fn get(&self, input: &str) -> Option<String> {
34        let matches = self
35            .options
36            .iter()
37            .filter(|option| option.starts_with(input))
38            .collect::<Vec<_>>();
39
40        if matches.len() == 1 {
41            Some(matches[0].to_string())
42        } else {
43            None
44        }
45    }
46}