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-12-13 05:31:12 -05:00
|
|
|
const ops = core.ops;
|
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-12-13 05:31:12 -05:00
|
|
|
import { getNoColor } from "ext:deno_console/01_console.js";
|
|
|
|
const { Uint8Array, StringFromCodePoint } = primordials;
|
|
|
|
|
|
|
|
const ESC = "\x1b";
|
|
|
|
const CTRL_C = "\x03";
|
|
|
|
const CTRL_D = "\x04";
|
|
|
|
|
|
|
|
const bold = ansi(1, 22);
|
|
|
|
const italic = ansi(3, 23);
|
|
|
|
const yellow = ansi(33, 0);
|
|
|
|
function ansi(start, end) {
|
|
|
|
return (str) => getNoColor() ? str : `\x1b[${start}m${str}\x1b[${end}m`;
|
|
|
|
}
|
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-12-13 05:31:12 -05:00
|
|
|
core.print(
|
|
|
|
`${yellow(bold(`${message}`))} [${italic("Press any key to continue")}] `,
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
stdin.setRaw(true);
|
|
|
|
stdin.readSync(new Uint8Array(1024));
|
|
|
|
} finally {
|
|
|
|
stdin.setRaw(false);
|
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-12-13 05:31:12 -05:00
|
|
|
core.print("\n");
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-12-13 05:31:12 -05:00
|
|
|
function prompt(message = "Prompt", defaultValue = "") {
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!isatty(stdin.rid)) {
|
2023-12-13 05:31:12 -05:00
|
|
|
return null;
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2023-12-13 05:31:12 -05:00
|
|
|
return ops.op_read_line_prompt(
|
|
|
|
`${message} `,
|
|
|
|
`${defaultValue}`,
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-12-13 05:31:12 -05:00
|
|
|
const inputMap = new primordials.Map([
|
|
|
|
["Y", true],
|
|
|
|
["y", true],
|
|
|
|
["\r", true],
|
|
|
|
["\n", true],
|
|
|
|
["\r\n", true],
|
|
|
|
["N", false],
|
|
|
|
["n", false],
|
|
|
|
[ESC, false],
|
|
|
|
[CTRL_C, false],
|
|
|
|
[CTRL_D, false],
|
|
|
|
]);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-12-13 05:31:12 -05:00
|
|
|
function confirm(message = "Confirm") {
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!isatty(stdin.rid)) {
|
2023-12-13 05:31:12 -05:00
|
|
|
return false;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-12-13 05:31:12 -05:00
|
|
|
core.print(`${yellow(bold(`${message}`))} [${italic("Y/n")}] `);
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-12-13 05:31:12 -05:00
|
|
|
let val = false;
|
|
|
|
try {
|
|
|
|
stdin.setRaw(true);
|
2023-03-28 17:49:00 -04:00
|
|
|
|
2023-12-13 05:31:12 -05:00
|
|
|
while (true) {
|
|
|
|
const b = new Uint8Array(1024);
|
|
|
|
stdin.readSync(b);
|
|
|
|
let byteString = "";
|
2023-03-28 17:49:00 -04:00
|
|
|
|
2023-12-13 05:31:12 -05:00
|
|
|
let i = 0;
|
|
|
|
while (b[i]) byteString += StringFromCodePoint(b[i++]);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-12-13 05:31:12 -05:00
|
|
|
if (inputMap.has(byteString)) {
|
|
|
|
val = inputMap.get(byteString);
|
2020-10-13 09:31:59 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-12-13 05:31:12 -05:00
|
|
|
} finally {
|
|
|
|
stdin.setRaw(false);
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
2023-12-13 05:31:12 -05:00
|
|
|
|
|
|
|
core.print(`${val ? "y" : "n"}\n`);
|
|
|
|
return val;
|
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 };
|