blob: 91d91a700313f7432a21d4174b2d8896a16f8e9b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
use tokio::io::{self, AsyncBufReadExt, AsyncWriteExt, BufReader};
/// Confirm the current operation
/// Waits for user input of 'y' or 'n'
pub async fn confirm_hint(text: impl Into<String>) -> bool {
let prompt = text.into().trim().to_string();
let mut stdout = io::stdout();
let mut stdin = BufReader::new(io::stdin());
stdout
.write_all(prompt.as_bytes())
.await
.expect("Failed to write prompt");
stdout.flush().await.expect("Failed to flush stdout");
let mut input = String::new();
stdin
.read_line(&mut input)
.await
.expect("Failed to read input");
input.trim().eq_ignore_ascii_case("y")
}
/// Confirm the current operation, or execute a closure if rejected
/// Waits for user input of 'y' or 'n'
/// If 'n' is entered, executes the provided closure and returns false
pub async fn confirm_hint_or<F>(text: impl Into<String>, on_reject: F) -> bool
where
F: FnOnce(),
{
let confirmed = confirm_hint(text).await;
if !confirmed {
on_reject();
}
confirmed
}
/// Confirm the current operation, and execute a closure if confirmed
/// Waits for user input of 'y' or 'n'
/// If 'y' is entered, executes the provided closure and returns true
pub async fn confirm_hint_then<F>(text: impl Into<String>, on_confirm: F) -> bool
where
F: FnOnce(),
{
let confirmed = confirm_hint(text).await;
if confirmed {
on_confirm();
}
confirmed
}
|