UNPKG

3.87 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8function _fs() {
9 const data = _interopRequireDefault(require("fs"));
10
11 _fs = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20/**
21 * Copyright (c) Facebook, Inc. and its affiliates.
22 *
23 * This source code is licensed under the MIT license found in the
24 * LICENSE file in the root directory of this source tree.
25 *
26 * @format
27 */
28// Simplified version of:
29// https://github.com/0x00A/prompt-sync/blob/master/index.js
30const term = 13; // carriage return
31
32function create() {
33 return prompt;
34
35 function prompt(ask, value, opts) {
36 let insert = 0;
37 opts = opts || {};
38
39 if (typeof ask === 'object') {
40 opts = ask;
41 ask = opts.ask;
42 } else if (typeof value === 'object') {
43 opts = value;
44 value = opts.value;
45 }
46
47 ask = ask || '';
48 const echo = opts.echo;
49 const masked = ('echo' in opts);
50 let fd;
51
52 if (process.platform === 'win32') {
53 // @ts-ignore
54 fd = process.stdin.fd;
55 } else {
56 fd = _fs().default.openSync('/dev/tty', 'rs');
57 }
58
59 const wasRaw = process.stdin.isRaw;
60
61 if (!wasRaw && process.stdin.setRawMode) {
62 process.stdin.setRawMode(true);
63 }
64
65 let buf = Buffer.alloc(3);
66 let str = '';
67 let character;
68 let read;
69
70 if (ask) {
71 process.stdout.write(ask);
72 }
73
74 while (true) {
75 read = _fs().default.readSync(fd, buf, 0, 3, null);
76
77 if (read > 1) {
78 // received a control sequence
79 if (buf.toString()) {
80 str += buf.toString();
81 str = str.replace(/\0/g, '');
82 insert = str.length;
83 process.stdout.write(`\u001b[2K\u001b[0G${ask}${str}`);
84 process.stdout.write(`\u001b[${insert + ask.length + 1}G`);
85 buf = Buffer.alloc(3);
86 }
87
88 continue; // any other 3 character sequence is ignored
89 } // if it is not a control character seq, assume only one character is read
90
91
92 character = buf[read - 1]; // catch a ^C and return null
93
94 if (character === 3) {
95 process.stdout.write('^C\n');
96
97 _fs().default.closeSync(fd);
98
99 process.exit(130);
100
101 if (process.stdin.setRawMode) {
102 process.stdin.setRawMode(!!wasRaw);
103 }
104
105 return null;
106 } // catch the terminating character
107
108
109 if (character === term) {
110 _fs().default.closeSync(fd);
111
112 break;
113 }
114
115 if (character === 127 || process.platform === 'win32' && character === 8) {
116 // backspace
117 if (!insert) {
118 continue;
119 }
120
121 str = str.slice(0, insert - 1) + str.slice(insert);
122 insert--;
123 process.stdout.write('\u001b[2D');
124 } else {
125 if (character < 32 || character > 126) {
126 continue;
127 }
128
129 str = str.slice(0, insert) + String.fromCharCode(character) + str.slice(insert);
130 insert++;
131 }
132
133 if (masked) {
134 process.stdout.write(`\u001b[2K\u001b[0G${ask}${Array(str.length + 1).join(echo)}`);
135 } else {
136 process.stdout.write('\u001b[s');
137
138 if (insert === str.length) {
139 process.stdout.write(`\u001b[2K\u001b[0G${ask}${str}`);
140 } else if (ask) {
141 process.stdout.write(`\u001b[2K\u001b[0G${ask}${str}`);
142 } else {
143 process.stdout.write(`\u001b[2K\u001b[0G${str}\u001b[${str.length - insert}D`);
144 }
145
146 process.stdout.write('\u001b[u');
147 process.stdout.write('\u001b[1C');
148 }
149 }
150
151 process.stdout.write('\n');
152
153 if (process.stdin.setRawMode) {
154 process.stdin.setRawMode(!!wasRaw);
155 }
156
157 return str || value || '';
158 }
159}
160
161var _default = create;
162exports.default = _default;
163
164//# sourceMappingURL=promptSync.js.map
\No newline at end of file