#prompt #down #up #structs #interactive #traits #input

eprompt

Easy to use interactive prompt library. No complex structs or traits, just a few simple to use functions.

6 releases

0.3.0 Nov 11, 2022
0.2.0 Nov 9, 2022
0.1.3 Nov 4, 2022

#23 in #down

Beerware

68KB
264 lines

eprompt is a easy to use prompt library for rust.

No complicated structs, or traits. Just simple functions that do what they say.

Usage example:

use eprompt::*;

// user can use j, k, up or down to change selection. Space to check a box, enter to finalize.
let tasks: Vec<_> = multi_select("What would you like to do today?", &["Eat a cake", "Go to work", "Go on a hike"]).unwrap();

// user can use j, k, up or down to change selection, space or enter to finalize.
let selection: &f32 = select("Choose an option", &[6.9, 3.14])).unwrap();

// This will automatically parse the input to the desired type. If the input is invalid it will have the user try again.
let age: i32 = input("How old are you?").unwrap();

screenshot


lib.rs:

Here is a cool example with enums!

 use eprompt::*;
 use std::fmt::Display;

 #[derive(Debug)]
 enum Choices {
     Opt1(i32),
     Opt2(&'static str),
     Opt3,
 }

 impl Display for Choices {
     fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
         match *self {
             Self::Opt1(x) => write!(fmt, "{} of Option 1", x),
             Self::Opt2(x) => write!(fmt, "Option 2 is {}", x),
             Self::Opt3 => write!(fmt, "Mystery option 3"),
         }
     }
 }

 fn main() -> Result<(), std::io::Error> {
     for choice in multi_select("Make a selection",
         &[Choices::Opt1(69), Choices::Opt2("A Brand new pony"), Choices::Opt3]
     )?.map(|x| x.1) {
         match choice {
             Choices::Opt1(x) => todo!("Do something with option 1"),
             Choices::Opt2(x) => todo!("Do something with option 2"),
     }
     
     Ok(())
 }

Dependencies

~1–11MB
~83K SLoC