Crates.io | asking |
lib.rs | asking |
version | 0.0.2 |
source | src |
created_at | 2021-08-12 15:16:26.787374 |
updated_at | 2021-08-26 13:13:24.131838 |
description | Build async prompts. |
homepage | https://crates.io/crates/asking |
repository | https://github.com/saona-raimundo/asking |
max_upload_size | |
id | 435265 |
size | 101,431 |
Build async prompts.
Ever wanted non-blocking user input? Here you are!
Asynchronous I/O is a form of input/output processing that allows you to do something while waiting for an answer.
Asynchronous - You can work while the user inputs something and even timeout!
Common patterns - Built-in common question patterns including
yn
function).%Y-%m-%d
format (see date
function).inside
method).text
function).FromStr
).Cross-platform - Generic on writer and reader!
Help messages - Help the user to input a correct answer.
Test with feedback - Test the input and, optionally, give feedback upon errors.
Default values - Add a value for empty inputs.
Standardized error handling - You can manage errors!
Feedback - Display a final message depending on the accepted value.
Extensive documentation - If you do not think so, let me know!
Arc<dyn Fn>
. This allows both functions and closures, but it means that functions can not hold any mutable references (so no internal mutability).Let me know if you find anything else, I will be happy to add it!
Give only five seconds to the user to confirm something, and continue upon no input! (instead of keep waiting)
use asking::error::Processing;
use std::time::Duration;
let question = asking::yn()
.message("Shall I continue? (you have 5 seconds to answer)")
.default_value(true) // value upon empty input
.timeout(Duration::from_secs(5_u64))
.ask();
match async_std::task::block_on(question) { // we decide to just wait, at most five secs
Ok(true) => println!("Super!"),
Ok(false) => println!("Okay, shutting down..."),
Err(Processing::Timeout { .. }) => println!("I think you are not here, I will continue :)"), // Automatic decision!,
_ => eprintln!("Error with questionnaire, try again later"),
}
Check out more examples!
With cargo-edit installed, simply type
cargo add asking
and you are good to go!
There are several crates for handling user input, I recommend checking them all out!
If you've got a crate that would be a good fit, open an issue and let me know. I'd love to add it!
Some crates are good to use together!
Testing projects with user input can be challenging.
assert_cmd
crate. Check out
testing
.tests
of this repository.