diff options
Diffstat (limited to 'runtime/js/41_prompt.js')
-rw-r--r-- | runtime/js/41_prompt.js | 112 |
1 files changed, 53 insertions, 59 deletions
diff --git a/runtime/js/41_prompt.js b/runtime/js/41_prompt.js index 1d5acc028..441db9a2f 100644 --- a/runtime/js/41_prompt.js +++ b/runtime/js/41_prompt.js @@ -1,82 +1,76 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -"use strict"; -((window) => { - const { stdin } = window.__bootstrap.files; - const { ArrayPrototypePush, StringPrototypeCharCodeAt, Uint8Array } = - window.__bootstrap.primordials; - const { isatty } = window.__bootstrap.tty; - const LF = StringPrototypeCharCodeAt("\n", 0); - const CR = StringPrototypeCharCodeAt("\r", 0); - const core = window.Deno.core; +const core = globalThis.Deno.core; +const primordials = globalThis.__bootstrap.primordials; +import { isatty } from "internal:runtime/js/40_tty.js"; +import { stdin } from "internal:runtime/js/40_files.js"; +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(`${message} [Enter] `, false); - - readLineFromStdinSync(); +function alert(message = "Alert") { + if (!isatty(stdin.rid)) { + return; } - function confirm(message = "Confirm") { - if (!isatty(stdin.rid)) { - return false; - } - - core.print(`${message} [y/N] `, false); + core.print(`${message} [Enter] `, false); - const answer = readLineFromStdinSync(); + readLineFromStdinSync(); +} - return answer === "Y" || answer === "y"; +function confirm(message = "Confirm") { + if (!isatty(stdin.rid)) { + return false; } - function prompt(message = "Prompt", defaultValue) { - defaultValue ??= null; + core.print(`${message} [y/N] `, false); - if (!isatty(stdin.rid)) { - return null; - } + const answer = readLineFromStdinSync(); - core.print(`${message} `, false); + return answer === "Y" || answer === "y"; +} - if (defaultValue) { - core.print(`[${defaultValue}] `, false); - } +function prompt(message = "Prompt", defaultValue) { + defaultValue ??= null; + + if (!isatty(stdin.rid)) { + return null; + } - return readLineFromStdinSync() || defaultValue; + core.print(`${message} `, false); + + if (defaultValue) { + core.print(`[${defaultValue}] `, false); } - function readLineFromStdinSync() { - const c = new Uint8Array(1); - const buf = []; + return readLineFromStdinSync() || defaultValue; +} + +function readLineFromStdinSync() { + const c = new Uint8Array(1); + const buf = []; - while (true) { + while (true) { + const n = stdin.readSync(c); + if (n === null || n === 0) { + break; + } + if (c[0] === CR) { const n = stdin.readSync(c); - if (n === null || n === 0) { + if (c[0] === LF) { break; } - if (c[0] === CR) { - const n = stdin.readSync(c); - if (c[0] === LF) { - break; - } - ArrayPrototypePush(buf, CR); - if (n === null || n === 0) { - break; - } - } - if (c[0] === LF) { + ArrayPrototypePush(buf, CR); + if (n === null || n === 0) { break; } - ArrayPrototypePush(buf, c[0]); } - return core.decode(new Uint8Array(buf)); + if (c[0] === LF) { + break; + } + ArrayPrototypePush(buf, c[0]); } + return core.decode(new Uint8Array(buf)); +} - window.__bootstrap.prompt = { - alert, - confirm, - prompt, - }; -})(this); +export { alert, confirm, prompt }; |