Skip to main content

Autocomplete

Trait Autocomplete 

Source
pub trait Autocomplete: AutocompleteClone {
    // Required methods
    fn get_suggestions(
        &mut self,
        input: &str,
    ) -> Result<Vec<String>, Box<dyn Error>>;
    fn get_completion(
        &mut self,
        input: &str,
        highlighted_suggestion: Option<&str>,
    ) -> Result<Option<String>, Box<dyn Error>>;
}
Expand description

Trait for implementing autocompletion features for text inputs.

The Autocomplete trait has two provided methods: get_suggestions and get_completion.

  • get_suggestions is called whenever the user’s text input is modified, returning a Vec<String>. The Vec<String> is the list of suggestions that the prompt displays to the user according to their text input. The user can navigate through the list and if they submit while highlighting one of these suggestions, the suggestion is treated as the final answer.

  • get_completion is called whenever the user presses the autocompletion hotkey (tab by default), with the current text input and the text of the currently highlighted suggestion, if any, as parameters. This method should return whether any text replacement (an autocompletion) should be made.

§Example

use demand::{Input, Autocomplete};

#[derive(Clone)]
struct FileExtensionCompleter;

impl Autocomplete for FileExtensionCompleter {
    fn get_suggestions(&mut self, input: &str) -> Result<Vec<String>, Box<dyn std::error::Error>> {
        let extensions = vec![".rs", ".toml", ".md", ".txt"];
        Ok(extensions.iter()
            .filter(|ext| ext.starts_with(input) || input.is_empty())
            .map(|s| s.to_string())
            .collect())
    }

    fn get_completion(
        &mut self,
        input: &str,
        highlighted_suggestion: Option<&str>,
    ) -> Result<Option<String>, Box<dyn std::error::Error>> {
        if let Some(suggestion) = highlighted_suggestion {
            Ok(Some(suggestion.to_string()))
        } else {
            Ok(None)
        }
    }
}

let input = Input::new("File extension:")
    .autocomplete(FileExtensionCompleter)
    .run();

Required Methods§

Source

fn get_suggestions( &mut self, input: &str, ) -> Result<Vec<String>, Box<dyn Error>>

List of input suggestions to be displayed to the user upon typing the text input.

If the user presses the autocompletion hotkey (tab as default) with a suggestion highlighted, the user’s text input will be replaced by the content of the suggestion string.

Source

fn get_completion( &mut self, input: &str, highlighted_suggestion: Option<&str>, ) -> Result<Option<String>, Box<dyn Error>>

Standalone autocompletion that can be implemented based solely on the user’s input.

If the user presses the autocompletion hotkey (tab as default) and there are no suggestions highlighted, this function will be called in an attempt to autocomplete the user’s input.

If the returned value is of the Some variant, the text input will be replaced by the content of the string.

Trait Implementations§

Source§

impl Clone for Box<dyn Autocomplete>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Implementors§