[][src]Trait rucline::prompt::Builder

pub trait Builder: ChainedLineReader + Sized {
    fn buffer(self, buffer: Buffer) -> Self;
fn erase_after_read(self, erase_after_read: bool) -> Self;
fn overrider<O: Overrider>(self, overrider: O) -> WithOverrider<O, Self>;
fn overrider_fn<O>(self, overrider: O) -> WithOverrider<O, Self>
    where
        O: Fn(Event, &Buffer) -> Option<Action>
;
fn overrider_ref<O: Overrider>(
        self,
        overrider: &O
    ) -> WithRefOverrider<'_, O, Self>;
fn completer<C: Completer>(self, completer: C) -> WithCompleter<C, Self>;
fn completer_fn<'a, F, R>(
        self,
        closure: F
    ) -> WithCompleter<Closure<'a, F, Option<R>>, Self>
    where
        F: Fn(&Buffer) -> Option<R>,
        R: Into<Cow<'a, str>>
;
fn completer_ref<C: Completer>(
        self,
        completer: &C
    ) -> WithRefCompleter<'_, C, Self>;
fn suggester<S: Suggester>(self, suggester: S) -> WithSuggester<S, Self>;
fn suggester_fn<'a, F, R>(
        self,
        closure: F
    ) -> WithSuggester<Closure<'a, F, Vec<R>>, Self>
    where
        F: Fn(&Buffer) -> Vec<R>,
        R: Into<Cow<'a, str>>
;
fn suggester_ref<S: Suggester>(
        self,
        suggester: &S
    ) -> WithRefSuggester<'_, S, Self>;
fn read_line(self) -> Result<Outcome, Error>; }

Builder for a line reader, providing methods that allows chaining together parameters for customizing the behavior of the line reader.

It essentially is a helper for crafting an invocation of prompt::read_line.

Example

use rucline::prompt::{Builder, Prompt};

let outcome = Prompt::from("Delete file? ")
    .completer(vec!["yes", "no"])
    .erase_after_read(true)
    .read_line();

Re-using the prompt configuration

The builder is consumed on every method call, including the read_line method. To re-use a prompt, it is advisable to create a prompt provider. For instance:

use rucline::prompt::{Builder, Prompt};

let completions = vec!["some", "completions"];
let suggestions = vec!["some", "suggestions"];

let prompt = |text: &str| Prompt::from(text)
    .completer_ref(&completions)
    .suggester_ref(&suggestions);

let firstOutcome = prompt("First: ").read_line();
do_something(firstOutcome);

let secondOutcome = prompt("Second: ").read_line();
do_something(secondOutcome);

Adding multiple hooks of the same kind

Setting two hooks to the same event in the same chain will cause only the last hook to be called. For instance:

use rucline::prompt::{Builder, Prompt};

let some_completions = vec!["yes", "no"];
let some_other_completions = vec!["ja", "nei"];

let prompt = Prompt::from("Delete file? ")
    .completer(some_completions)         // Will be ignored
    .completer(some_other_completions);  // Superseeds the previous completer

Required methods

fn buffer(self, buffer: Buffer) -> Self

Prepopulates the prompt input with buffer.

Arguments

  • buffer - A buffer to be used when displaying the prompt.

fn erase_after_read(self, erase_after_read: bool) -> Self

Controls if the prompt shall be erased after user input.

If set to false (default), after user input, the terminal will receive a new line after the prompt text and the user input. Any drop-down completions will be removed, however.

If set to true, the whole prompt and input will be erased. The cursor returns to the original position as if nothing happened.

Arguments

  • erase_after_read - Whether the prompt shall be erased after user input.

fn overrider<O: Overrider>(self, overrider: O) -> WithOverrider<O, Self>

Modifies the behavior of the prompt by setting an Overrider.

The builder will take ownership of overrider. To pass in a reference, use overrider_ref.

Arguments

fn overrider_fn<O>(self, overrider: O) -> WithOverrider<O, Self> where
    O: Fn(Event, &Buffer) -> Option<Action>, 

Modifies the behavior of the prompt by setting an Overrider closure.

Arguments

fn overrider_ref<O: Overrider>(
    self,
    overrider: &O
) -> WithRefOverrider<'_, O, Self>

Modifies the behavior of the prompt by setting a Overrider reference.

Arguments

fn completer<C: Completer>(self, completer: C) -> WithCompleter<C, Self>

Sets the in-line completion provider.

The builder will take ownership of completer. To pass in a reference, use completer_ref.

Arguments

fn completer_fn<'a, F, R>(
    self,
    closure: F
) -> WithCompleter<Closure<'a, F, Option<R>>, Self> where
    F: Fn(&Buffer) -> Option<R>,
    R: Into<Cow<'a, str>>, 

Sets the in-line completion closure.

Arguments

  • completer - A closure that provides an optional completion.

fn completer_ref<C: Completer>(
    self,
    completer: &C
) -> WithRefCompleter<'_, C, Self>

Sets the in-line completion provider reference.

Arguments

fn suggester<S: Suggester>(self, suggester: S) -> WithSuggester<S, Self>

Sets the drop-down suggestion provider.

The builder will take ownership of suggester. To pass in a reference, use suggester_ref.

Arguments

fn suggester_fn<'a, F, R>(
    self,
    closure: F
) -> WithSuggester<Closure<'a, F, Vec<R>>, Self> where
    F: Fn(&Buffer) -> Vec<R>,
    R: Into<Cow<'a, str>>, 

Sets the drop-down suggestion closure.

Arguments

  • suggester - A closure that provides a list of suggestions.

fn suggester_ref<S: Suggester>(
    self,
    suggester: &S
) -> WithRefSuggester<'_, S, Self>

Sets the drop-down suggestion provider reference.

Arguments

fn read_line(self) -> Result<Outcome, Error>

Consumes this Builder to craft an invocation of prompt::read_line.

Errors

  • Error - If an error occurred while reading the user input.
Loading content...

Implementors

impl Builder for Prompt[src]

Loading content...