summaryrefslogtreecommitdiff
path: root/runtime/permissions.rs
diff options
context:
space:
mode:
authorBartek Iwańczuk <biwanczuk@gmail.com>2022-09-22 14:16:06 +0200
committerGitHub <noreply@github.com>2022-09-22 14:16:06 +0200
commitb20431c5f995de47ccffba92c0c9f53f8eefa1fd (patch)
treea8417076b0a90cf0aa16b1e6a4f61e628201abc0 /runtime/permissions.rs
parent698a340ad7cbbea5d6782c4f410fb50a6d09dc4d (diff)
feat: Refresh interactive permission prompt (#15907)
Co-authored-by: David Sherret <dsherret@users.noreply.github.com>
Diffstat (limited to 'runtime/permissions.rs')
-rw-r--r--runtime/permissions.rs40
1 files changed, 31 insertions, 9 deletions
diff --git a/runtime/permissions.rs b/runtime/permissions.rs
index 44a658225..5c3eb2f5a 100644
--- a/runtime/permissions.rs
+++ b/runtime/permissions.rs
@@ -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);
}
};
}