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";
|
2024-01-26 17:46:46 -05:00
|
|
|
import { op_read_line_prompt } from "ext:core/ops";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
ArrayPrototypePush,
|
|
|
|
StringPrototypeCharCodeAt,
|
|
|
|
Uint8Array,
|
|
|
|
} = primordials;
|
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
import { stdin } from "ext:deno_io/12_io.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
|
2024-01-01 23:06:05 -05:00
|
|
|
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") {
|
2024-01-23 18:01:56 -05:00
|
|
|
if (!stdin.isTerminal()) {
|
2023-02-07 14:22:46 -05:00
|
|
|
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") {
|
2024-01-23 18:01:56 -05:00
|
|
|
if (!stdin.isTerminal()) {
|
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) {
|
2024-01-11 16:05:55 -05:00
|
|
|
defaultValue ??= "";
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2024-01-23 18:01:56 -05:00
|
|
|
if (!stdin.isTerminal()) {
|
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-23 07:04:53 -05:00
|
|
|
return op_read_line_prompt(
|
2024-01-11 16:05:55 -05:00
|
|
|
`${message} `,
|
|
|
|
`${defaultValue}`,
|
|
|
|
);
|
2024-01-01 23:06:05 -05:00
|
|
|
}
|
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 };
|