diff options
author | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
---|---|---|
committer | Valentin Popov <valentin@popov.link> | 2024-01-08 00:21:28 +0300 |
commit | 1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch) | |
tree | 7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/dialoguer/examples/completion.rs | |
parent | 5ecd8cf2cba827454317368b68571df0d13d7842 (diff) | |
download | fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip |
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/dialoguer/examples/completion.rs')
-rw-r--r-- | vendor/dialoguer/examples/completion.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/vendor/dialoguer/examples/completion.rs b/vendor/dialoguer/examples/completion.rs new file mode 100644 index 0000000..76d790b --- /dev/null +++ b/vendor/dialoguer/examples/completion.rs @@ -0,0 +1,44 @@ +use dialoguer::{theme::ColorfulTheme, Completion, Input}; + +fn main() -> Result<(), std::io::Error> { + println!("Use the Right arrow or Tab to complete your command"); + let completion = MyCompletion::default(); + Input::<String>::with_theme(&ColorfulTheme::default()) + .with_prompt("dialoguer") + .completion_with(&completion) + .interact_text()?; + Ok(()) +} + +struct MyCompletion { + options: Vec<String>, +} + +impl Default for MyCompletion { + fn default() -> Self { + MyCompletion { + options: vec![ + "orange".to_string(), + "apple".to_string(), + "banana".to_string(), + ], + } + } +} + +impl Completion for MyCompletion { + /// Simple completion implementation based on substring + fn get(&self, input: &str) -> Option<String> { + let matches = self + .options + .iter() + .filter(|option| option.starts_with(input)) + .collect::<Vec<_>>(); + + if matches.len() == 1 { + Some(matches[0].to_string()) + } else { + None + } + } +} |