1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-08 15:19:40 -05:00

feat: Refresh interactive permission prompt (#15907)

Co-authored-by: David Sherret <dsherret@users.noreply.github.com>
This commit is contained in:
Bartek Iwańczuk 2022-09-22 14:16:06 +02:00 committed by GitHub
parent 698a340ad7
commit b20431c5f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2066,6 +2066,11 @@ fn permission_prompt(message: &str, name: &str) -> bool {
}
}
// Clear n-lines in terminal and move cursor to the beginning of the line.
fn clear_n_lines(n: usize) {
eprint!("\x1B[{}A\x1B[0J", n);
}
// For security reasons we must consume everything in stdin so that previously
// buffered data cannot effect the prompt.
if let Err(err) = clear_stdin() {
@ -2073,13 +2078,19 @@ fn permission_prompt(message: &str, name: &str) -> bool {
return false; // don't grant permission if this fails
}
let opts = "[y/n (y = yes allow, n = no deny)] ";
// print to stderr so that if stdout is piped this is still displayed.
const OPTS: &str = "[y/n] (y = yes, allow; n = no, deny)";
eprint!("{}", PERMISSION_EMOJI);
eprint!("{}", colors::bold("Deno requests "));
eprint!("{}", colors::bold(message));
eprintln!("{}", colors::bold("."));
let msg = format!(
"{} Deno requests {}. Run again with --allow-{} to bypass this prompt.\n Allow? {} ",
PERMISSION_EMOJI, message, name, opts
" ├ Run again with --allow-{} to bypass this prompt.",
name
);
// print to stderr so that if deno is > to a file this is still displayed.
eprint!("{}", colors::bold(&msg));
eprintln!("{}", colors::italic(&msg));
eprint!("{}", colors::bold("Allow?"));
eprint!(" {} > ", OPTS);
loop {
let mut input = String::new();
let stdin = std::io::stdin();
@ -2092,12 +2103,23 @@ fn permission_prompt(message: &str, name: &str) -> bool {
Some(v) => v,
};
match ch.to_ascii_lowercase() {
'y' => return true,
'n' => return false,
'y' => {
clear_n_lines(4);
let msg = format!("Granted {}.", message);
eprintln!("{}", colors::bold(&msg));
return true;
}
'n' => {
clear_n_lines(4);
let msg = format!("Denied {}.", message);
eprintln!("{}", colors::bold(&msg));
return false;
}
_ => {
// If we don't get a recognized option try again.
let msg_again = format!("Unrecognized option '{}' {}", ch, opts);
eprint!("{}", colors::bold(&msg_again));
clear_n_lines(1);
eprint!("{}", colors::bold("Unrecognized option. Allow?"));
eprint!(" {} > ", OPTS);
}
};
}