2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
import { isatty } from "internal:runtime/js/40_tty.js";
|
|
|
|
import { stdin } from "internal:runtime/js/40_files.js";
|
|
|
|
const { ArrayPrototypePush, StringPrototypeCharCodeAt, Uint8Array } =
|
|
|
|
primordials;
|
|
|
|
const LF = StringPrototypeCharCodeAt("\n", 0);
|
|
|
|
const CR = StringPrototypeCharCodeAt("\r", 0);
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function alert(message = "Alert") {
|
|
|
|
if (!isatty(stdin.rid)) {
|
|
|
|
return;
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
core.print(`${message} [Enter] `, false);
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
readLineFromStdinSync();
|
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function confirm(message = "Confirm") {
|
|
|
|
if (!isatty(stdin.rid)) {
|
|
|
|
return false;
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
core.print(`${message} [y/N] `, false);
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const answer = readLineFromStdinSync();
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return answer === "Y" || answer === "y";
|
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function prompt(message = "Prompt", defaultValue) {
|
|
|
|
defaultValue ??= null;
|
|
|
|
|
|
|
|
if (!isatty(stdin.rid)) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
core.print(`${message} `, false);
|
|
|
|
|
|
|
|
if (defaultValue) {
|
|
|
|
core.print(`[${defaultValue}] `, false);
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return readLineFromStdinSync() || defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
function readLineFromStdinSync() {
|
|
|
|
const c = new Uint8Array(1);
|
|
|
|
const buf = [];
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
while (true) {
|
|
|
|
const n = stdin.readSync(c);
|
|
|
|
if (n === null || n === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c[0] === CR) {
|
2020-10-13 09:31:59 -04:00
|
|
|
const n = stdin.readSync(c);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (c[0] === LF) {
|
2020-10-29 13:35:58 -04:00
|
|
|
break;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypePush(buf, CR);
|
|
|
|
if (n === null || n === 0) {
|
2020-10-13 09:31:59 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (c[0] === LF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ArrayPrototypePush(buf, c[0]);
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return core.decode(new Uint8Array(buf));
|
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { alert, confirm, prompt };
|