summaryrefslogtreecommitdiff
path: root/runtime/js/41_prompt.js
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2024-01-02 09:36:05 +0530
committerGitHub <noreply@github.com>2024-01-02 09:36:05 +0530
commitb21462355a61d69bedf15ae51304719f6014b8df (patch)
treef58b2e00551d9d2981882520d0f64f7e8cf73722 /runtime/js/41_prompt.js
parent96b581bdd2bedb31aa51c0a992686f27ed1a1f10 (diff)
Revert "fix(runtime): Make native modal keyboard interaction consistent with browsers" (#21739)
Reverts denoland/deno#18453 Fixes https://github.com/denoland/deno/issues/21602 https://github.com/denoland/deno/issues/21631 https://github.com/denoland/deno/issues/21641 Reasons for revert: - alert() and confirm() swallowed ^C with raw mode. - prompt() did not re-raise the interrupt signal from rustyline. - Default 'Y' on confirm() is a bad default and breaking change. cc @lionel-rowe
Diffstat (limited to 'runtime/js/41_prompt.js')
-rw-r--r--runtime/js/41_prompt.js107
1 files changed, 45 insertions, 62 deletions
diff --git a/runtime/js/41_prompt.js b/runtime/js/41_prompt.js
index ca2b82c21..787b9f9f7 100644
--- a/runtime/js/41_prompt.js
+++ b/runtime/js/41_prompt.js
@@ -1,95 +1,78 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { core, primordials } from "ext:core/mod.js";
-const ops = core.ops;
import { isatty } from "ext:runtime/40_tty.js";
import { stdin } from "ext:deno_io/12_io.js";
-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`;
-}
+const { ArrayPrototypePush, StringPrototypeCharCodeAt, Uint8Array } =
+ primordials;
+const LF = StringPrototypeCharCodeAt("\n", 0);
+const CR = StringPrototypeCharCodeAt("\r", 0);
function alert(message = "Alert") {
if (!isatty(stdin.rid)) {
return;
}
- core.print(
- `${yellow(bold(`${message}`))} [${italic("Press any key to continue")}] `,
- );
-
- try {
- stdin.setRaw(true);
- stdin.readSync(new Uint8Array(1024));
- } finally {
- stdin.setRaw(false);
- }
+ core.print(`${message} [Enter] `, false);
- core.print("\n");
+ readLineFromStdinSync();
}
-function prompt(message = "Prompt", defaultValue = "") {
+function confirm(message = "Confirm") {
if (!isatty(stdin.rid)) {
- return null;
+ return false;
}
- return ops.op_read_line_prompt(
- `${message} `,
- `${defaultValue}`,
- );
+ core.print(`${message} [y/N] `, false);
+
+ const answer = readLineFromStdinSync();
+
+ return answer === "Y" || answer === "y";
}
-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],
-]);
+function prompt(message = "Prompt", defaultValue) {
+ defaultValue ??= null;
-function confirm(message = "Confirm") {
if (!isatty(stdin.rid)) {
- return false;
+ return null;
}
- core.print(`${yellow(bold(`${message}`))} [${italic("Y/n")}] `);
+ if (defaultValue) {
+ message += ` [${defaultValue}]`;
+ }
- let val = false;
- try {
- stdin.setRaw(true);
+ message += " ";
- while (true) {
- const b = new Uint8Array(1024);
- stdin.readSync(b);
- let byteString = "";
+ // output in one shot to make the tests more reliable
+ core.print(message, false);
- let i = 0;
- while (b[i]) byteString += StringFromCodePoint(b[i++]);
+ return readLineFromStdinSync() || defaultValue;
+}
- if (inputMap.has(byteString)) {
- val = inputMap.get(byteString);
+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) {
break;
}
}
- } finally {
- stdin.setRaw(false);
+ if (c[0] === LF) {
+ break;
+ }
+ ArrayPrototypePush(buf, c[0]);
}
-
- core.print(`${val ? "y" : "n"}\n`);
- return val;
+ return core.decode(new Uint8Array(buf));
}
export { alert, confirm, prompt };