-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add alert, confirm, and prompt (#7507)
This commit adds "alert", "confirm" and "prompt" functions from web standards.
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. | ||
((window) => { | ||
const { stdin, stdout } = window.__bootstrap.files; | ||
const { isatty } = window.__bootstrap.tty; | ||
const LF = "\n".charCodeAt(0); | ||
const encoder = new TextEncoder(); | ||
const decoder = new TextDecoder(); | ||
|
||
function alert(message = "Alert") { | ||
if (!isatty(stdin.rid)) { | ||
return; | ||
} | ||
|
||
stdout.writeSync(encoder.encode(`${message} [Enter] `)); | ||
|
||
readLineFromStdinSync(); | ||
} | ||
|
||
function confirm(message = "Confirm") { | ||
if (!isatty(stdin.rid)) { | ||
return false; | ||
} | ||
|
||
stdout.writeSync(encoder.encode(`${message} [y/N] `)); | ||
|
||
const answer = readLineFromStdinSync(); | ||
|
||
return answer === "Y" || answer === "y"; | ||
} | ||
|
||
function prompt(message = "Prompt", defaultValue) { | ||
defaultValue ??= null; | ||
|
||
if (!isatty(stdin.rid)) { | ||
return null; | ||
} | ||
|
||
stdout.writeSync(encoder.encode(`${message} `)); | ||
|
||
if (defaultValue) { | ||
stdout.writeSync(encoder.encode(`[${defaultValue}] `)); | ||
} | ||
|
||
return readLineFromStdinSync() || defaultValue; | ||
} | ||
|
||
function readLineFromStdinSync() { | ||
const c = new Uint8Array(1); | ||
const buf = []; | ||
|
||
while (true) { | ||
const n = stdin.readSync(c); | ||
if (n === 0 || c[0] === LF) { | ||
break; | ||
} | ||
buf.push(c[0]); | ||
} | ||
return decoder.decode(new Uint8Array(buf)); | ||
} | ||
|
||
window.__bootstrap.prompt = { | ||
alert, | ||
confirm, | ||
prompt, | ||
}; | ||
})(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const name0 = prompt("What is your name?", "Jane Doe"); // Answer John Doe | ||
console.log(`Your name is ${name0}.`); | ||
const name1 = prompt("What is your name?", "Jane Doe"); // Answer with default | ||
console.log(`Your name is ${name1}.`); | ||
const input = prompt(); // Answer foo | ||
console.log(`Your input is ${input}.`); | ||
const answer0 = confirm("Question 0"); // Answer y | ||
console.log(`Your answer is ${answer0}`); | ||
const answer1 = confirm("Question 1"); // Answer n | ||
console.log(`Your answer is ${answer1}`); | ||
const answer2 = confirm("Question 2"); // Answer with yes (returns false) | ||
console.log(`Your answer is ${answer2}`); | ||
const answer3 = confirm(); // Answer with default | ||
console.log(`Your answer is ${answer3}`); | ||
alert("Hi"); | ||
alert(); | ||
console.log("The end of test"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[WILDCARD]What is your name? [Jane Doe] Your name is John Doe. | ||
What is your name? [Jane Doe] Your name is Jane Doe. | ||
Prompt Your input is foo. | ||
Question 0 [y/N] Your answer is true | ||
Question 1 [y/N] Your answer is false | ||
Question 2 [y/N] Your answer is false | ||
Confirm [y/N] Your answer is false | ||
Hi [Enter] Alert [Enter] The end of test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters