Deno.stdin是类型File,因此您可以通过提供Uint8Arrayas 缓冲区并调用来读取它Deno.stdin.read(buf)
window.onload = async function main() {
const buf = new Uint8Array(1024);
/* Reading into `buf` from start.
* buf.subarray(0, n) is the read result.
* If n is instead Deno.EOF, then it means that stdin is closed.
*/
const n = await Deno.stdin.read(buf);
if (n == Deno.EOF) {
console.log("Standard input closed")
} else {
console.log("READ:", new TextDecoder().decode(buf.subarray(0, n)));
}
}
Run Code Online (Sandbox Code Playgroud)
我们可以在 deno 中使用 prompt。
const input = prompt('Please enter input');
Run Code Online (Sandbox Code Playgroud)
如果输入必须是数字。我们可以使用Number.parseInt(input);
一个简单的确认,可以用y或回答n:
import { readLines } from "https://deno.land/std@0.76.0/io/bufio.ts";
async function confirm(question) {
console.log(question);
for await (const line of readLines(Deno.stdin)) {
if (line === "y") {
return true;
} else if (line === "n") {
return false;
}
}
}
const answer = await confirm("Do you want to go on? [y/n]");
Run Code Online (Sandbox Code Playgroud)
或者,如果您想提示用户输入字符串:
import { readLines } from "https://deno.land/std@0.76.0/io/bufio.ts";
async function promptString(question) {
console.log(question);
for await (const line of readLines(Deno.stdin)) {
return line;
}
}
const userName = await promptString("Enter your name:");
Run Code Online (Sandbox Code Playgroud)