-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathlib.rs
74 lines (65 loc) · 2.18 KB
/
lib.rs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use console::{style, Key, Term};
/// Prompt the user for confirmation in the given [`Term`].
///
/// This is a slimmed-down version of `dialoguer::Confirm`, with the post-confirmation report
/// enabled.
pub fn confirm(message: &str, term: &Term, default: bool) -> std::io::Result<bool> {
// Set the Ctrl-C handler to exit the process.
let result = ctrlc::set_handler(move || {
let term = Term::stderr();
term.show_cursor().ok();
term.flush().ok();
#[allow(clippy::exit, clippy::cast_possible_wrap)]
std::process::exit(if cfg!(windows) {
0xC000_013A_u32 as i32
} else {
130
});
});
match result {
Ok(()) => {}
Err(ctrlc::Error::MultipleHandlers) => {
// If multiple handlers were set, we assume that the existing handler is our
// confirmation handler, and continue.
}
Err(err) => return Err(std::io::Error::new(std::io::ErrorKind::Other, err)),
}
let prompt = format!(
"{} {} {} {} {}",
style("?".to_string()).for_stderr().yellow(),
style(message).for_stderr().bold(),
style("[y/n]").for_stderr().black().bright(),
style("›").for_stderr().black().bright(),
style(if default { "yes" } else { "no" })
.for_stderr()
.cyan(),
);
term.write_str(&prompt)?;
term.hide_cursor()?;
term.flush()?;
// Match continuously on every keystroke, and do not wait for user to hit the
// `Enter` key.
let response = loop {
let input = term.read_key()?;
match input {
Key::Char('y' | 'Y') => break true,
Key::Char('n' | 'N') => break false,
Key::Enter => break default,
_ => {}
};
};
let report = format!(
"{} {} {} {}",
style("✔".to_string()).for_stderr().green(),
style(message).for_stderr().bold(),
style("·").for_stderr().black().bright(),
style(if response { "yes" } else { "no" })
.for_stderr()
.cyan(),
);
term.clear_line()?;
term.write_line(&report)?;
term.show_cursor()?;
term.flush()?;
Ok(response)
}