2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-10-13 09:31:59 -04:00
|
|
|
((window) => {
|
2020-11-02 19:15:29 -05:00
|
|
|
const { stdin } = window.__bootstrap.files;
|
2020-10-13 09:31:59 -04:00
|
|
|
const { isatty } = window.__bootstrap.tty;
|
|
|
|
const LF = "\n".charCodeAt(0);
|
2020-10-29 13:35:58 -04:00
|
|
|
const CR = "\r".charCodeAt(0);
|
2020-11-02 19:15:29 -05:00
|
|
|
const core = window.Deno.core;
|
2020-10-13 09:31:59 -04:00
|
|
|
|
|
|
|
function alert(message = "Alert") {
|
|
|
|
if (!isatty(stdin.rid)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-02 19:15:29 -05:00
|
|
|
core.print(`${message} [Enter] `, false);
|
2020-10-13 09:31:59 -04:00
|
|
|
|
|
|
|
readLineFromStdinSync();
|
|
|
|
}
|
|
|
|
|
|
|
|
function confirm(message = "Confirm") {
|
|
|
|
if (!isatty(stdin.rid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-02 19:15:29 -05:00
|
|
|
core.print(`${message} [y/N] `, false);
|
2020-10-13 09:31:59 -04:00
|
|
|
|
|
|
|
const answer = readLineFromStdinSync();
|
|
|
|
|
|
|
|
return answer === "Y" || answer === "y";
|
|
|
|
}
|
|
|
|
|
|
|
|
function prompt(message = "Prompt", defaultValue) {
|
|
|
|
defaultValue ??= null;
|
|
|
|
|
|
|
|
if (!isatty(stdin.rid)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-02 19:15:29 -05:00
|
|
|
core.print(`${message} `, false);
|
2020-10-13 09:31:59 -04:00
|
|
|
|
|
|
|
if (defaultValue) {
|
2020-11-02 19:15:29 -05:00
|
|
|
core.print(`[${defaultValue}] `, false);
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return readLineFromStdinSync() || defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
function readLineFromStdinSync() {
|
|
|
|
const c = new Uint8Array(1);
|
|
|
|
const buf = [];
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const n = stdin.readSync(c);
|
2020-10-29 13:35:58 -04:00
|
|
|
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) {
|
2020-10-13 09:31:59 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf.push(c[0]);
|
|
|
|
}
|
2021-06-05 17:10:07 -04:00
|
|
|
return core.decode(new Uint8Array(buf));
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.prompt = {
|
|
|
|
alert,
|
|
|
|
confirm,
|
|
|
|
prompt,
|
|
|
|
};
|
|
|
|
})(this);
|