2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2023-03-08 06:44:54 -05:00
|
|
|
import { isatty } from "ext:runtime/40_tty.js";
|
|
|
|
import { stdin } from "ext:deno_io/12_io.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
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
|
|
|
if (defaultValue) {
|
2023-03-28 17:49:00 -04:00
|
|
|
message += ` [${defaultValue}]`;
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2023-03-28 17:49:00 -04:00
|
|
|
message += " ";
|
|
|
|
|
|
|
|
// output in one shot to make the tests more reliable
|
|
|
|
core.print(message, false);
|
|
|
|
|
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 };
|