Crate promptuity

Source
Expand description

§Promptuity

Promptuity is a library that provides interactive prompts. It is highly extensible, allowing you to build your original prompts from scratch. It brings ingenuity to various projects.

§Concept

  • Not easy, But simple
    • Avoids APIs with implicit behavior, aiming to provide as transparent APIs as possible.
    • The amount of code required to start a prompt may be more compared to other libraries.
  • Extensible
    • You can customize built-in prompts or build your prompts from scratch.
    • The built-in prompts are minimal, assuming that prompt requirements vary by project.
  • Beautiful
    • Offers two types of built-in Themes.
    • Themes can also be fully customized to fit your ideal.

§Quick Start

The basic usage is as follows.

use promptuity::prompts::{Confirm, Input, Select, SelectOption};
use promptuity::themes::FancyTheme;
use promptuity::{Error, Promptuity, Term};

fn main() -> Result<(), Error> {
    let mut term = Term::default();
    let mut theme = FancyTheme::default();
    let mut p = Promptuity::new(&mut term, &mut theme);

    p.term().clear()?;

    p.with_intro("Survey").begin()?;

    let name = p.prompt(Input::new("Please enter your username").with_placeholder("username"))?;

    let _ = p.prompt(Confirm::new("Are you a full-time software developer?").with_default(true))?;

    let _ = p.prompt(
        Select::new(
            "Select your primary programming language",
            vec![
                SelectOption::new("Rust", "rust"),
                SelectOption::new("Go", "go"),
                SelectOption::new("C++", "cpp"),
                SelectOption::new("C", "c"),
                SelectOption::new("TypeScript", "typescript"),
                SelectOption::new("JavaScript", "javascript"),
                SelectOption::new("Deno", "deno"),
                SelectOption::new("Python", "python"),
                SelectOption::new("Java", "java"),
                SelectOption::new("Dart", "dart"),
                SelectOption::new("Other", "other"),
            ],
        )
        .with_hint("Submit with Space or Enter."),
    )?;

    p.with_outro(format!("Thank you for your response, {}!", name))
        .finish()?;

    Ok(())
}

§Error Handling

All errors are consolidated into promptuity::Error.

In many cases, prompt interruptions will need to be handled individually. Interruptions occur during user input reception, typically through inputs like Ctrl + C or ESC.

use promptuity::prompts::Input;
use promptuity::themes::MinimalTheme;
use promptuity::{Error, Promptuity, Term};

fn ask() -> Result<String, Error> {
    let mut term = Term::default();
    let mut theme = MinimalTheme::default();
    let mut p = Promptuity::new(&mut term, &mut theme);

    p.begin()?;
    let name = p.prompt(Input::new("Please enter your username").with_placeholder("username"))?;
    p.finish()?;

    Ok(name)
}

fn main() {
    match ask() {
        Ok(name) => println!("Hello, {}!", name),
        Err(Error::Cancel) => {}
        Err(e) => eprintln!("Error: {}", e),
    }
}

Prompt interruptions can be handled as Error::Cancel. In the above examples, no message is displayed in the event of an interruption.

Modules§

event
A module to read events.
pagination
A module that assists with pagination functionality.
prompts
A module that provides built-in prompts.
style
A module to apply attributes and colors on your text.
themes
A module that provides built-in themes.

Structs§

CursorPosition
A struct to represent the cursor position.
InputCursor
A struct to represent the input cursor.
Promptuity
The core struct of promptuity.
RenderPayload
A struct representing the payload required for prompt rendering.
RenderSnapshot
A struct aggregating the content for rendering.
Term
A struct to represent a terminal.
TermSize
A struct to represent the terminal size.

Enums§

Error
The error type for promptuity.
PromptBody
A struct representing the body of the prompt.
PromptInput
A struct representing the input of the prompt.
PromptState
A struct representing the state of the prompt.

Traits§

Prompt
A trait representing the behavior of a prompt.
Terminal
A trait to represent a terminal.
Theme
A trait for the Theme that determines what Promptuity renders.
Validator
A trait for performing prompt validation.