Struct dialoguer::BasicHistory

source ·
pub struct BasicHistory { /* private fields */ }

Implementations§

source§

impl BasicHistory

source

pub fn new() -> Self

Creates a new basic history value which has no limit on the number of entries and allows for duplicates.

Example

A history with at most 8 entries and no duplicates:

let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);
Examples found in repository?
examples/history.rs (line 10)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    println!("Use 'exit' to quit the prompt");
    println!("In this example, history is limited to 8 entries and contains no duplicates");
    println!("Use the Up/Down arrows to scroll through history");
    println!();

    let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);

    loop {
        if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
            .with_prompt("dialoguer")
            .history_with(&mut history)
            .interact_text()
        {
            if cmd == "exit" {
                process::exit(0);
            }
            println!("Entered {}", cmd);
        }
    }
}
source

pub fn max_entries(self, max_entries: usize) -> Self

Limit the number of entries stored in the history.

Examples found in repository?
examples/history.rs (line 10)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    println!("Use 'exit' to quit the prompt");
    println!("In this example, history is limited to 8 entries and contains no duplicates");
    println!("Use the Up/Down arrows to scroll through history");
    println!();

    let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);

    loop {
        if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
            .with_prompt("dialoguer")
            .history_with(&mut history)
            .interact_text()
        {
            if cmd == "exit" {
                process::exit(0);
            }
            println!("Entered {}", cmd);
        }
    }
}
source

pub fn no_duplicates(self, no_duplicates: bool) -> Self

Prevent duplicates in the history. This means that any previous entries that are equal to a new entry are removed before the new entry is added.

Examples found in repository?
examples/history.rs (line 10)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    println!("Use 'exit' to quit the prompt");
    println!("In this example, history is limited to 8 entries and contains no duplicates");
    println!("Use the Up/Down arrows to scroll through history");
    println!();

    let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);

    loop {
        if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
            .with_prompt("dialoguer")
            .history_with(&mut history)
            .interact_text()
        {
            if cmd == "exit" {
                process::exit(0);
            }
            println!("Entered {}", cmd);
        }
    }
}

Trait Implementations§

source§

impl Default for BasicHistory

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T: ToString> History<T> for BasicHistory

source§

fn read(&self, pos: usize) -> Option<String>

This is called with the current position that should be read from history. The pos represents the number of times the Up/Down arrow key has been pressed. This would normally be used as an index to some sort of vector. If the pos does not have an entry, None should be returned.
source§

fn write(&mut self, val: &T)

This is called with the next value you should store in history at the first location. Normally history is implemented as a FIFO queue.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.