summaryrefslogtreecommitdiff
path: root/runtime/js/41_prompt.js
blob: ca2b82c219c7409a3f6facb216aaf3bb1ae53b6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 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`;
}

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("\n");
}

function prompt(message = "Prompt", defaultValue = "") {
  if (!isatty(stdin.rid)) {
    return null;
  }

  return ops.op_read_line_prompt(
    `${message} `,
    `${defaultValue}`,
  );
}

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 confirm(message = "Confirm") {
  if (!isatty(stdin.rid)) {
    return false;
  }

  core.print(`${yellow(bold(`${message}`))} [${italic("Y/n")}] `);

  let val = false;
  try {
    stdin.setRaw(true);

    while (true) {
      const b = new Uint8Array(1024);
      stdin.readSync(b);
      let byteString = "";

      let i = 0;
      while (b[i]) byteString += StringFromCodePoint(b[i++]);

      if (inputMap.has(byteString)) {
        val = inputMap.get(byteString);
        break;
      }
    }
  } finally {
    stdin.setRaw(false);
  }

  core.print(`${val ? "y" : "n"}\n`);
  return val;
}

export { alert, confirm, prompt };