console-rs / dialoguer

Rust utility library for nice command line prompts and similar things
MIT License
1.31k stars 143 forks source link

Feature suggestion: prompt timeouts #266

Open leontoeides opened 1 year ago

leontoeides commented 1 year ago

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

Gordon01 commented 1 year ago

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?

Tudmotu commented 7 months ago

+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")
    }
};