Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add alert, confirm, and prompt #7507

Merged
merged 6 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: address feedbacks by @nayeemrmn
  • Loading branch information
kt3k committed Oct 12, 2020
commit ff444f15966917a043665f4fb5e699d6e019b015
36 changes: 11 additions & 25 deletions cli/rt/41_prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,36 @@
const encoder = new TextEncoder();
const decoder = new TextDecoder();

function alert(message) {
function alert(message = "Alert") {
if (!isatty(stdin.rid)) {
throw new Error(
"stdin is not interactive. 'alert' needs stdin to be interactive.",
);
return;
}

if (message) {
stdout.writeSync(encoder.encode(message));
}
stdout.writeSync(encoder.encode(`${message} [Enter] `));

readLineFromStdinSync();
}

function confirm(message) {
function confirm(message = "Confirm") {
if (!isatty(stdin.rid)) {
throw new Error(
"stdin is not interactive. 'confirm' needs stdin to be interactive.",
);
}

if (message) {
stdout.writeSync(encoder.encode(message));
return false;
}

stdout.writeSync(encoder.encode(" [y/N] "));
stdout.writeSync(encoder.encode(`${message} [y/N] `));

const answer = readLineFromStdinSync();

return answer === "Y" || answer === "y";
}

function prompt(message, defaultValue = "") {
if (!isatty(stdin.rid)) {
throw new Error(
"stdin is not interactive. 'prompt' needs stdin to be interactive.",
);
}
function prompt(message = "Prompt", defaultValue) {
defaultValue ??= null;

if (message) {
stdout.writeSync(encoder.encode(message));
if (!isatty(stdin.rid)) {
return defaultValue;
}

stdout.writeSync(encoder.encode(" "));
stdout.writeSync(encoder.encode(`${message} `));

if (defaultValue) {
stdout.writeSync(encoder.encode(`[${defaultValue}] `));
Expand Down
7 changes: 6 additions & 1 deletion cli/tests/066_prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ 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 default
console.log(`Your answer is ${answer2}`);
alert("Hi"); // Requires the enter key pressed
const answer3 = confirm(); // Answer with default
console.log(`Your answer is ${answer3}`);
alert("Hi");
alert();
console.log("The end of test");
4 changes: 3 additions & 1 deletion cli/tests/066_prompt.ts.out
Original file line number Diff line number Diff line change
@@ -1,6 +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
HiThe end of test
Confirm [y/N] Your answer is false
Hi [Enter] Alert [Enter] The end of test
2 changes: 1 addition & 1 deletion cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1923,7 +1923,7 @@ fn _066_prompt() {
let args = "run --unstable 066_prompt.ts";
let output = "066_prompt.ts.out";
// These are answers to prompt, confirm, and alert calls.
let input = b"John Doe\n\nY\nN\n\n\n";
let input = b"John Doe\n\nfoo\nY\nN\n\n\n\n\n";

util::test_pty(args, output, input);
}
Expand Down