1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-26 16:09:27 -05:00

fix(cli): prompt works with windows eol and eof (#8149)

This commit is contained in:
Sander Hahn 2020-10-29 18:35:58 +01:00 committed by GitHub
parent 8d99adb6c4
commit 1854c6f73b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 2 deletions

View file

@ -3,6 +3,7 @@
const { stdin, stdout } = window.__bootstrap.files;
const { isatty } = window.__bootstrap.tty;
const LF = "\n".charCodeAt(0);
const CR = "\r".charCodeAt(0);
const encoder = new TextEncoder();
const decoder = new TextDecoder();
@ -50,7 +51,20 @@
while (true) {
const n = stdin.readSync(c);
if (n === 0 || c[0] === LF) {
if (n === null || n === 0) {
break;
}
if (c[0] === CR) {
const n = stdin.readSync(c);
if (c[0] === LF) {
break;
}
buf.push(CR);
if (n === null || n === 0) {
break;
}
}
if (c[0] === LF) {
break;
}
buf.push(c[0]);

View file

@ -12,6 +12,10 @@ 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}`);
const windows = prompt("What is Windows EOL?");
console.log(`Your answer is ${JSON.stringify(windows)}`);
alert("Hi");
alert();
console.log("The end of test");
const eof = prompt("What is EOF?");
console.log(`Your answer is ${JSON.stringify(eof)}`);

View file

@ -5,4 +5,6 @@ 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
What is Windows EOL? Your answer is "windows"
Hi [Enter] Alert [Enter] The end of test
What is EOF? Your answer is null

View file

@ -2185,7 +2185,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\nfoo\nY\nN\nyes\n\n\n\n";
let input = b"John Doe\n\nfoo\nY\nN\nyes\n\nwindows\r\n\n\n";
util::test_pty(args, output, input);
}