2020-07-13 08:03:56 -04:00
|
|
|
/* istanbul ignore file */
|
2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
|
|
|
function repeat(chr, count) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let result = "";
|
2019-05-14 18:48:48 -04:00
|
|
|
while (result.length < count) {
|
|
|
|
result += chr;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
function _getPrompt(prompt, options, callback) {
|
|
|
|
process.stdout.write(prompt);
|
2019-08-25 02:39:20 -04:00
|
|
|
let stdin = process.stdin;
|
2019-05-14 18:48:48 -04:00
|
|
|
stdin.resume();
|
|
|
|
stdin.setRawMode(true);
|
|
|
|
stdin.resume();
|
|
|
|
stdin.setEncoding('utf8');
|
2019-08-25 02:39:20 -04:00
|
|
|
let message = '';
|
|
|
|
let respond = (ctrlC, message) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
process.stdout.write('\n');
|
|
|
|
stdin.setRawMode(false);
|
|
|
|
stdin.pause();
|
|
|
|
stdin.removeListener('data', handler);
|
|
|
|
callback(ctrlC, message);
|
|
|
|
};
|
|
|
|
function handler(chr) {
|
|
|
|
chr = String(chr);
|
|
|
|
switch (chr) {
|
|
|
|
// Enter (ish)
|
|
|
|
case "\n":
|
|
|
|
case "\r":
|
|
|
|
case "\u0004":
|
|
|
|
if (options.choice) {
|
|
|
|
if (options.defaultChoice) {
|
|
|
|
respond(null, options.defaultChoice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
respond(null, message);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Backspace
|
|
|
|
case "\u007f":
|
|
|
|
if (message.length > 0 && options.choice == null) {
|
|
|
|
message = message.substring(0, message.length - 1);
|
|
|
|
(process.stdout).clearLine();
|
|
|
|
(process.stdout).cursorTo(0);
|
|
|
|
if (options.mask) {
|
|
|
|
process.stdout.write(prompt + repeat(options.mask, message.length));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
process.stdout.write(prompt + message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Ctrl-C
|
|
|
|
case "\u0003":
|
|
|
|
process.stdout.write('\n[ CTRL-C ]');
|
|
|
|
respond(true, null);
|
|
|
|
break;
|
|
|
|
// Any other character
|
|
|
|
default:
|
|
|
|
if (options.choice) {
|
|
|
|
if (options.choice.indexOf(chr) >= 0) {
|
|
|
|
process.stdout.write(chr);
|
|
|
|
respond(null, chr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// More passsword characters
|
|
|
|
if (options.mask) {
|
|
|
|
process.stdout.write('*');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
process.stdout.write(chr);
|
|
|
|
}
|
|
|
|
message += chr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stdin.on('data', handler);
|
|
|
|
}
|
|
|
|
function getPrompt(prompt, options) {
|
2019-08-25 02:39:20 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
_getPrompt(prompt, options, (ctrlC, password) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (ctrlC) {
|
|
|
|
return reject(new Error("cancelled"));
|
|
|
|
}
|
|
|
|
resolve(password);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function getProgressBar(action) {
|
|
|
|
let lastProgress = -1;
|
2019-05-14 18:48:48 -04:00
|
|
|
return function (percent) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let progress = Math.trunc(percent * 100);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (progress == lastProgress) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastProgress = progress;
|
|
|
|
process.stdin.setRawMode(false);
|
|
|
|
process.stdin.pause();
|
|
|
|
(process.stdout).clearLine();
|
|
|
|
(process.stdout).cursorTo(0);
|
|
|
|
process.stdout.write(action + "... " + progress + "%");
|
|
|
|
if (percent === 1) {
|
|
|
|
process.stdout.write('\n');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function getPassword(prompt) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return getPrompt(prompt, { mask: "*" });
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function getMessage(prompt) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return getPrompt(prompt, {});
|
|
|
|
}
|
|
|
|
// @TODO: Allow choices to be an array, [ "Yes", "No", "All" ] => "(y)es/ (N)o/ (a)ll"
|
2019-08-25 02:39:20 -04:00
|
|
|
export function getChoice(prompt, choices, defaultChoice) {
|
|
|
|
let choice = choices.toLowerCase().split("");
|
2019-05-14 18:48:48 -04:00
|
|
|
if (defaultChoice) {
|
|
|
|
defaultChoice = defaultChoice.toLowerCase();
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let options = { choice: choice, defaultChoice: defaultChoice };
|
|
|
|
let hint = choice.map((c) => ((c === defaultChoice) ? c.toUpperCase() : c)).join("/");
|
2019-05-14 18:48:48 -04:00
|
|
|
return getPrompt((prompt + " (" + hint + ") "), options);
|
|
|
|
}
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=prompt.js.map
|