6 个版本
0.3.0 | 2022年11月11日 |
---|---|
0.2.0 | 2022年11月9日 |
0.1.3 | 2022年11月4日 |
#28 in #down
每月 22 次下载
68KB
264 代码行
eprompt 是一个易于使用的 Rust 提示库。
没有复杂的结构体或特质。只有简单直接的函数。
使用示例
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();
lib.rs
:
这是一个使用枚举的酷例子!
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(())
}
依赖
~1–12MB
~73K SLoC