2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 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";
|
2024-01-01 23:06:05 -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
|
|
|
}
|
|
|
|
|
2024-01-01 23:06:05 -05:00
|
|
|
core.print(`${message} [Enter] `, false);
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2024-01-01 23:06:05 -05:00
|
|
|
readLineFromStdinSync();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2024-01-01 23:06:05 -05:00
|
|
|
function confirm(message = "Confirm") {
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!isatty(stdin.rid)) {
|
2024-01-01 23:06:05 -05:00
|
|
|
return false;
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2024-01-01 23:06:05 -05:00
|
|
|
core.print(`${message} [y/N] `, false);
|
|
|
|
|
|
|
|
const answer = readLineFromStdinSync();
|
|
|
|
|
|
|
|
return answer === "Y" || answer === "y";
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2024-01-01 23:06:05 -05:00
|
|
|
function prompt(message = "Prompt", defaultValue) {
|
|
|
|
defaultValue ??= null;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
if (!isatty(stdin.rid)) {
|
2024-01-01 23:06:05 -05:00
|
|
|
return null;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2024-01-01 23:06:05 -05:00
|
|
|
if (defaultValue) {
|
|
|
|
message += ` [${defaultValue}]`;
|
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2024-01-01 23:06:05 -05:00
|
|
|
message += " ";
|
2023-03-28 17:49:00 -04:00
|
|
|
|
2024-01-01 23:06:05 -05:00
|
|
|
// output in one shot to make the tests more reliable
|
|
|
|
core.print(message, false);
|
2023-03-28 17:49:00 -04:00
|
|
|
|
2024-01-01 23:06:05 -05:00
|
|
|
return readLineFromStdinSync() || defaultValue;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2024-01-01 23:06:05 -05:00
|
|
|
function readLineFromStdinSync() {
|
|
|
|
const c = new Uint8Array(1);
|
|
|
|
const buf = [];
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const n = stdin.readSync(c);
|
|
|
|
if (n === null || n === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c[0] === CR) {
|
|
|
|
const n = stdin.readSync(c);
|
|
|
|
if (c[0] === LF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ArrayPrototypePush(buf, CR);
|
|
|
|
if (n === null || n === 0) {
|
2020-10-13 09:31:59 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-01-01 23:06:05 -05:00
|
|
|
if (c[0] === LF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ArrayPrototypePush(buf, c[0]);
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
2024-01-01 23:06:05 -05:00
|
|
|
return core.decode(new Uint8Array(buf));
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { alert, confirm, prompt };
|