Struct promkit::Prompt

source ·
pub struct Prompt<T: Renderer> {
    pub renderer: T,
}
Expand description

Represents a customizable prompt that can handle user input and produce a result.

This struct encapsulates the rendering logic, event handling, and result production for a prompt.

Fields§

§renderer: T

Implementations§

source§

impl<T: Renderer> Prompt<T>

source

pub fn run(&mut self) -> Result<T::Return>

Runs the prompt, handling events and producing a result.

This method initializes the terminal, and enters a loop to handle events until a quit signal is received. After exiting the loop, it produces and returns the result.

§Returns

Returns a Result containing the produced result or an error.

Examples found in repository?
examples/confirm.rs (line 5)
3
4
5
6
7
fn main() -> anyhow::Result<()> {
    let mut p = Confirm::new("Do you have a pet?").prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
More examples
Hide additional examples
examples/listbox.rs (line 7)
3
4
5
6
7
8
9
fn main() -> anyhow::Result<()> {
    let mut p = Listbox::new(0..100)
        .title("What number do you like?")
        .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
examples/tree.rs (line 8)
3
4
5
6
7
8
9
10
fn main() -> anyhow::Result<()> {
    let mut p = Tree::new(Node::try_from(&std::env::current_dir()?.join("src"))?)
        .title("Select a directory or file")
        .tree_lines(10)
        .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
examples/password.rs (line 11)
3
4
5
6
7
8
9
10
11
12
13
fn main() -> anyhow::Result<()> {
    let mut p = Password::default()
        .title("Put your password")
        .validator(
            |text| 4 < text.len() && text.len() < 10,
            |text| format!("Length must be over 4 and within 10 but got {}", text.len()),
        )
        .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
examples/checkbox.rs (line 19)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() -> anyhow::Result<()> {
    let mut p = Checkbox::new(vec![
        "Apple",
        "Banana",
        "Orange",
        "Mango",
        "Strawberry",
        "Pineapple",
        "Grape",
        "Watermelon",
        "Kiwi",
        "Pear",
    ])
    .title("What are your favorite fruits?")
    .checkbox_lines(5)
    .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}
examples/readline.rs (line 17)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() -> anyhow::Result<()> {
    let mut p = Readline::default()
        .title("Hi!")
        .enable_suggest(Suggest::from_iter([
            "apple",
            "applet",
            "application",
            "banana",
        ]))
        .validator(
            |text| text.len() > 10,
            |text| format!("Length must be over 10 but got {}", text.len()),
        )
        .prompt()?;
    println!("result: {:?}", p.run()?);
    Ok(())
}

Trait Implementations§

source§

impl<T: Renderer> Drop for Prompt<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Prompt<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Prompt<T>
where T: RefUnwindSafe,

§

impl<T> Send for Prompt<T>
where T: Send,

§

impl<T> Sync for Prompt<T>
where T: Sync,

§

impl<T> Unpin for Prompt<T>
where T: Unpin,

§

impl<T> UnwindSafe for Prompt<T>
where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where 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 T
where 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.