console-rs / dialoguer

Rust utility library for nice command line prompts and similar things

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature suggestion: prompt timeouts

leontoeides opened this issue · comments

Thank you for this useful crate. Very nice attention to detail! I see there are lots of pending requests I hope this doesn't overwhelm you.

Just an idea: a timeout for the prompts. If the user does not respond within 1 minute, 10 minutes, 30 minutes, etc. the prompt will exit and return a None. This is useful for situations where someone may not be available to key something in, but the system has alternative (but less desirable) sources of information. For an automated process that can be guided by a user, if they're available

This would require a cancellable https://docs.rs/console/latest/console/struct.Term.html#method.read_line, which, I believe is not trivial to implement. Do you have ideas on how this can be implemented?

+1 for the feature request.

Personally I managed to solve this for Input using a thread and a mpsc::channel.

But when I try this with Password it seems Dialoguer is unable to pick up the inputs, and it just fails after the timeout.

@Gordon01 do you have any idea if that's solvable?

Here's an approximation of the code I have:

let (send, recv) = mpsc::channel();

thread::spawn(move || {
    let input = Input::new()
        .allow_empty(true)
        .with_prompt("Name")
        .interact_text()
        .unwrap();
    send.send(input).unwrap();
});

let res = match recv.recv_timeout(Duration::from_secs(60)) {
    Ok(s) => Ok(s),
    Err(_error) => {
        std::println!("");
        Err("Prompt timed out")
    }
};