1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | function _fs() {
|
9 | const data = _interopRequireDefault(require("fs"));
|
10 |
|
11 | _fs = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | const term = 13;
|
31 |
|
32 | function 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 |
|
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 |
|
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;
|
89 | }
|
90 |
|
91 |
|
92 | character = buf[read - 1];
|
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 | }
|
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 |
|
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 |
|
161 | var _default = create;
|
162 | exports.default = _default;
|
163 |
|
164 |
|
\ | No newline at end of file |