This commit is contained in:
Stephen Franceschelli 2019-07-30 13:41:05 -04:00
parent 596a6da241
commit c1a589c5b6
7078 changed files with 1882834 additions and 319 deletions

264
node_modules/prompts/lib/elements/autocomplete.js generated vendored Normal file
View file

@ -0,0 +1,264 @@
'use strict';
const color = require('kleur');
const Prompt = require('./prompt');
const { cursor } = require('sisteransi');
const { style, clear, figures, strip } = require('../util');
const getVal = (arr, i) => arr[i] && (arr[i].value || arr[i].title || arr[i]);
const getTitle = (arr, i) => arr[i] && (arr[i].title || arr[i].value || arr[i]);
const getIndex = (arr, valOrTitle) => {
const index = arr.findIndex(el => el.value === valOrTitle || el.title === valOrTitle);
return index > -1 ? index : undefined;
};
/**
* TextPrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {Array} opts.choices Array of auto-complete choices objects
* @param {Function} [opts.suggest] Filter function. Defaults to sort by title
* @param {Number} [opts.limit=10] Max number of results to show
* @param {Number} [opts.cursor=0] Cursor start position
* @param {String} [opts.style='default'] Render style
* @param {String} [opts.fallback] Fallback message - initial to default value
* @param {String} [opts.initial] Index of the default value
* @param {Stream} [opts.stdin] The Readable stream to listen to
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
* @param {String} [opts.noMatches] The no matches found label
*/
class AutocompletePrompt extends Prompt {
constructor(opts={}) {
super(opts);
this.msg = opts.message;
this.suggest = opts.suggest;
this.choices = opts.choices;
this.initial = typeof opts.initial === 'number'
? opts.initial
: getIndex(opts.choices, opts.initial);
this.select = this.initial || opts.cursor || 0;
this.fallback = opts.fallback || (
opts.initial !== undefined ?
`${figures.pointerSmall} ${getTitle(this.choices, this.initial)}` :
`${figures.pointerSmall} ${opts.noMatches || 'no matches found'}`
);
this.suggestions = [[]];
this.page = 0;
this.input = '';
this.limit = opts.limit || 10;
this.cursor = 0;
this.transform = style.render(opts.style);
this.scale = this.transform.scale;
this.render = this.render.bind(this);
this.complete = this.complete.bind(this);
this.clear = clear('');
this.complete(this.render);
this.render();
}
moveSelect(i) {
this.select = i;
if (this.suggestions[this.page].length > 0) {
this.value = getVal(this.suggestions[this.page], i);
} else {
this.value = this.initial !== undefined
? getVal(this.choices, this.initial)
: null;
}
this.fire();
}
async complete(cb) {
const p = (this.completing = this.suggest(this.input, this.choices));
const suggestions = await p;
if (this.completing !== p) return;
this.suggestions = suggestions
.map((s, i, arr) => ({title: getTitle(arr, i), value: getVal(arr, i)}))
.reduce((arr, sug) => {
if (arr[arr.length - 1].length < this.limit)
arr[arr.length - 1].push(sug);
else arr.push([sug]);
return arr;
}, [[]]);
this.isFallback = false;
this.completing = false;
if (!this.suggestions[this.page])
this.page = 0;
if (!this.suggestions.length && this.fallback) {
const index = getIndex(this.choices, this.fallback);
this.suggestions = [[]];
if (index !== undefined)
this.suggestions[0].push({ title: getTitle(this.choices, index), value: getVal(this.choices, index) });
this.isFallback = true;
}
const l = Math.max(suggestions.length - 1, 0);
this.moveSelect(Math.min(l, this.select));
cb && cb();
}
reset() {
this.input = '';
this.complete(() => {
this.moveSelect(this.initial !== void 0 ? this.initial : 0);
this.render();
});
this.render();
}
abort() {
this.done = this.aborted = true;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
submit() {
this.done = true;
this.aborted = false;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
_(c, key) { // TODO on ctrl+# go to page #
let s1 = this.input.slice(0, this.cursor);
let s2 = this.input.slice(this.cursor);
this.input = `${s1}${c}${s2}`;
this.cursor = s1.length+1;
this.complete(this.render);
this.render();
}
delete() {
if (this.cursor === 0) return this.bell();
let s1 = this.input.slice(0, this.cursor-1);
let s2 = this.input.slice(this.cursor);
this.input = `${s1}${s2}`;
this.complete(this.render);
this.cursor = this.cursor-1;
this.render();
}
deleteForward() {
if(this.cursor*this.scale >= this.rendered.length) return this.bell();
let s1 = this.input.slice(0, this.cursor);
let s2 = this.input.slice(this.cursor+1);
this.input = `${s1}${s2}`;
this.complete(this.render);
this.render();
}
first() {
this.moveSelect(0);
this.render();
}
last() {
this.moveSelect(this.suggestions[this.page].length - 1);
this.render();
}
up() {
if (this.select <= 0) return this.bell();
this.moveSelect(this.select - 1);
this.render();
}
down() {
if (this.select >= this.suggestions[this.page].length - 1) return this.bell();
this.moveSelect(this.select + 1);
this.render();
}
next() {
if (this.select === this.suggestions[this.page].length - 1) {
this.page = (this.page + 1) % this.suggestions.length;
this.moveSelect(0);
} else this.moveSelect(this.select + 1);
this.render();
}
nextPage() {
if (this.page >= this.suggestions.length - 1)
return this.bell();
this.page++;
this.moveSelect(0);
this.render();
}
prevPage() {
if (this.page <= 0)
return this.bell();
this.page--;
this.moveSelect(0);
this.render();
}
left() {
if (this.cursor <= 0) return this.bell();
this.cursor = this.cursor-1;
this.render();
}
right() {
if (this.cursor*this.scale >= this.rendered.length) return this.bell();
this.cursor = this.cursor+1;
this.render();
}
render() {
if (this.closed) return;
super.render();
if (this.lineCount) this.out.write(cursor.down(this.lineCount));
let prompt = color.bold(`${style.symbol(this.done, this.aborted)} ${this.msg} `)
+ `${style.delimiter(this.completing)} `;
let length = strip(prompt).length;
if (this.done && this.suggestions[this.page][this.select]) {
prompt += `${this.suggestions[this.page][this.select].title}`;
} else {
this.rendered = `${this.transform.render(this.input)}`;
length += this.rendered.length;
prompt += this.rendered;
}
if (!this.done) {
this.lineCount = this.suggestions[this.page].length;
let suggestions = this.suggestions[this.page].reduce((acc, item, i) =>
acc + `\n${i === this.select ? color.cyan(item.title) : item.title}`, '');
if (suggestions && !this.isFallback) {
prompt += suggestions;
if (this.suggestions.length > 1) {
this.lineCount++;
prompt += color.blue(`\nPage ${this.page+1}/${this.suggestions.length}`);
}
} else {
const fallbackIndex = getIndex(this.choices, this.fallback);
const fallbackTitle = fallbackIndex !== undefined
? getTitle(this.choices, fallbackIndex)
: this.fallback;
prompt += `\n${color.gray(fallbackTitle)}`;
this.lineCount++;
}
}
this.out.write(this.clear + prompt);
this.clear = clear(prompt);
if (this.lineCount && !this.done) {
let pos = cursor.up(this.lineCount);
pos += cursor.left+cursor.to(length);
pos += cursor.move(-this.rendered.length+this.cursor*this.scale);
this.out.write(pos);
}
}
}
module.exports = AutocompletePrompt;

View file

@ -0,0 +1,189 @@
'use strict';
const color = require('kleur');
const { cursor } = require('sisteransi');
const MultiselectPrompt = require('./multiselect');
const { clear, style, figures } = require('../util');
/**
* MultiselectPrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {Array} opts.choices Array of choice objects
* @param {String} [opts.hint] Hint to display
* @param {String} [opts.warn] Hint shown for disabled choices
* @param {Number} [opts.max] Max choices
* @param {Number} [opts.cursor=0] Cursor start position
* @param {Stream} [opts.stdin] The Readable stream to listen to
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
*/
class AutocompleteMultiselectPrompt extends MultiselectPrompt {
constructor(opts={}) {
opts.overrideRender = true;
super(opts);
this.inputValue = '';
this.clear = clear('');
this.filteredOptions = this.value;
this.render();
}
last() {
this.cursor = this.filteredOptions.length - 1;
this.render();
}
next() {
this.cursor = (this.cursor + 1) % this.filteredOptions.length;
this.render();
}
up() {
if (this.cursor === 0) {
this.cursor = this.filteredOptions.length - 1;
} else {
this.cursor--;
}
this.render();
}
down() {
if (this.cursor === this.filteredOptions.length - 1) {
this.cursor = 0;
} else {
this.cursor++;
}
this.render();
}
left() {
this.filteredOptions[this.cursor].selected = false;
this.render();
}
right() {
if (this.value.filter(e => e.selected).length >= this.maxChoices) return this.bell();
this.filteredOptions[this.cursor].selected = true;
this.render();
}
delete() {
if (this.inputValue.length) {
this.inputValue = this.inputValue.substr(0, this.inputValue.length - 1);
this.updateFilteredOptions();
}
}
updateFilteredOptions() {
const currentHighlight = this.filteredOptions[this.cursor];
this.filteredOptions = this.value
.filter(v => {
if (this.inputValue) {
if (typeof v.title === 'string') {
if (v.title.toLowerCase().includes(this.inputValue.toLowerCase())) {
return true;
}
}
if (typeof v.value === 'string') {
if (v.value.toLowerCase().includes(this.inputValue.toLowerCase())) {
return true;
}
}
return false;
}
return true;
});
const newHighlightIndex = this.filteredOptions.findIndex(v => v === currentHighlight)
this.cursor = newHighlightIndex < 0 ? 0 : newHighlightIndex;
this.render();
}
handleSpaceToggle() {
const v = this.filteredOptions[this.cursor];
if (v.selected) {
v.selected = false;
this.render();
} else if (v.disabled || this.value.filter(e => e.selected).length >= this.maxChoices) {
return this.bell();
} else {
v.selected = true;
this.render();
}
}
handleInputChange(c) {
this.inputValue = this.inputValue + c;
this.updateFilteredOptions();
}
_(c, key) {
if (c === ' ') {
this.handleSpaceToggle();
} else {
this.handleInputChange(c);
}
}
renderInstructions() {
return `
Instructions:
${figures.arrowUp}/${figures.arrowDown}: Highlight option
${figures.arrowLeft}/${figures.arrowRight}/[space]: Toggle selection
[a,b,c]/delete: Filter choices
enter/return: Complete answer
`
}
renderCurrentInput() {
return `
Filtered results for: ${this.inputValue ? this.inputValue : color.gray('Enter something to filter')}\n`;
}
renderOption(cursor, v, i) {
let title;
if (v.disabled) title = cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);
else title = cursor === i ? color.cyan().underline(v.title) : v.title;
return (v.selected ? color.green(figures.radioOn) : figures.radioOff) + ' ' + title
}
renderDoneOrInstructions() {
if (this.done) {
const selected = this.value
.filter(e => e.selected)
.map(v => v.title)
.join(', ');
return selected;
}
const output = [color.gray(this.hint), this.renderInstructions(), this.renderCurrentInput()];
if (this.filteredOptions.length && this.filteredOptions[this.cursor].disabled) {
output.push(color.yellow(this.warn));
}
return output.join(' ');
}
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
super.render();
// print prompt
let prompt = [
style.symbol(this.done, this.aborted),
color.bold(this.msg),
style.delimiter(false),
this.renderDoneOrInstructions()
].join(' ');
if (this.showMinError) {
prompt += color.red(`You must select a minimum of ${this.minSelected} choices.`);
this.showMinError = false;
}
prompt += this.renderOptions(this.filteredOptions);
this.out.write(this.clear + prompt);
this.clear = clear(prompt);
}
}
module.exports = AutocompleteMultiselectPrompt;

87
node_modules/prompts/lib/elements/confirm.js generated vendored Normal file
View file

@ -0,0 +1,87 @@
const color = require('kleur');
const Prompt = require('./prompt');
const { style } = require('../util');
const { erase, cursor } = require('sisteransi');
/**
* ConfirmPrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {Boolean} [opts.initial] Default value (true/false)
* @param {Stream} [opts.stdin] The Readable stream to listen to
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
* @param {String} [opts.yes] The "Yes" label
* @param {String} [opts.yesOption] The "Yes" option when choosing between yes/no
* @param {String} [opts.no] The "No" label
* @param {String} [opts.noOption] The "No" option when choosing between yes/no
*/
class ConfirmPrompt extends Prompt {
constructor(opts={}) {
super(opts);
this.msg = opts.message;
this.value = opts.initial;
this.initialValue = !!opts.initial;
this.yesMsg = opts.yes || 'yes';
this.yesOption = opts.yesOption || '(Y/n)';
this.noMsg = opts.no || 'no';
this.noOption = opts.noOption || '(y/N)';
this.render();
}
reset() {
this.value = this.initialValue;
this.fire();
this.render();
}
abort() {
this.done = this.aborted = true;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
submit() {
this.value = this.value || false;
this.done = true;
this.aborted = false;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
_(c, key) {
if (c.toLowerCase() === 'y') {
this.value = true;
return this.submit();
}
if (c.toLowerCase() === 'n') {
this.value = false;
return this.submit();
}
return this.bell();
}
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
super.render();
this.out.write(
erase.line +
cursor.to(0) +
[
style.symbol(this.done, this.aborted),
color.bold(this.msg),
style.delimiter(this.done),
this.done
? this.value ? this.yesMsg : this.noMsg
: color.gray(this.initialValue ? this.yesOption : this.noOption)
].join(' ')
);
}
}
module.exports = ConfirmPrompt;

213
node_modules/prompts/lib/elements/date.js generated vendored Normal file
View file

@ -0,0 +1,213 @@
'use strict';
const color = require('kleur');
const Prompt = require('./prompt');
const { style, clear, figures, strip } = require('../util');
const { erase, cursor } = require('sisteransi');
const { DatePart, Meridiem, Day, Hours, Milliseconds, Minutes, Month, Seconds, Year } = require('../dateparts');
const regex = /\\(.)|"((?:\\["\\]|[^"])+)"|(D[Do]?|d{3,4}|d)|(M{1,4})|(YY(?:YY)?)|([aA])|([Hh]{1,2})|(m{1,2})|(s{1,2})|(S{1,4})|./g;
const regexGroups = {
1: ({token}) => token.replace(/\\(.)/g, '$1'),
2: (opts) => new Day(opts), // Day // TODO
3: (opts) => new Month(opts), // Month
4: (opts) => new Year(opts), // Year
5: (opts) => new Meridiem(opts), // AM/PM // TODO (special)
6: (opts) => new Hours(opts), // Hours
7: (opts) => new Minutes(opts), // Minutes
8: (opts) => new Seconds(opts), // Seconds
9: (opts) => new Milliseconds(opts), // Fractional seconds
}
const dfltLocales = {
months: 'January,February,March,April,May,June,July,August,September,October,November,December'.split(','),
monthsShort: 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(','),
weekdays: 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'.split(','),
weekdaysShort: 'Sun,Mon,Tue,Wed,Thu,Fri,Sat'.split(',')
}
/**
* DatePrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {Number} [opts.initial] Index of default value
* @param {String} [opts.mask] The format mask
* @param {object} [opts.locales] The date locales
* @param {String} [opts.error] The error message shown on invalid value
* @param {Function} [opts.validate] Function to validate the submitted value
* @param {Stream} [opts.stdin] The Readable stream to listen to
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
*/
class DatePrompt extends Prompt {
constructor(opts={}) {
super(opts);
this.msg = opts.message;
this.cursor = 0;
this.typed = '';
this.locales = Object.assign(dfltLocales, opts.locales);
this._date = opts.initial || new Date();
this.errorMsg = opts.error || 'Please Enter A Valid Value';
this.validator = opts.validate || (() => true);
this.mask = opts.mask || 'YYYY-MM-DD HH:mm:ss';
this.clear = clear('');
this.render();
}
get value() {
return this.date
}
get date() {
return this._date;
}
set date(date) {
if (date) this._date.setTime(date.getTime());
}
set mask(mask) {
let result;
this.parts = [];
while(result = regex.exec(mask)) {
let match = result.shift();
let idx = result.findIndex(gr => gr != null);
this.parts.push(idx in regexGroups
? regexGroups[idx]({ token: result[idx] || match, date: this.date, parts: this.parts, locales: this.locales })
: result[idx] || match);
}
let parts = this.parts.reduce((arr, i) => {
if (typeof i === 'string' && typeof arr[arr.length - 1] === 'string')
arr[arr.length - 1] += i;
else arr.push(i);
return arr;
}, []);
this.parts.splice(0);
this.parts.push(...parts);
this.reset();
}
moveCursor(n) {
this.typed = '';
this.cursor = n;
this.fire();
}
reset() {
this.moveCursor(this.parts.findIndex(p => p instanceof DatePart));
this.fire();
this.render();
}
abort() {
this.done = this.aborted = true;
this.error = false;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
async validate() {
let valid = await this.validator(this.value);
if (typeof valid === 'string') {
this.errorMsg = valid;
valid = false;
}
this.error = !valid;
}
async submit() {
await this.validate();
if (this.error) {
this.color = 'red';
this.fire();
this.render();
return;
}
this.done = true;
this.aborted = false;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
up() {
this.typed = '';
this.parts[this.cursor].up();
this.render();
}
down() {
this.typed = '';
this.parts[this.cursor].down();
this.render();
}
left() {
let prev = this.parts[this.cursor].prev();
if (prev == null) return this.bell();
this.moveCursor(this.parts.indexOf(prev));
this.render();
}
right() {
let next = this.parts[this.cursor].next();
if (next == null) return this.bell();
this.moveCursor(this.parts.indexOf(next));
this.render();
}
next() {
let next = this.parts[this.cursor].next();
this.moveCursor(next
? this.parts.indexOf(next)
: this.parts.findIndex((part) => part instanceof DatePart));
this.render();
}
_(c) {
if (/\d/.test(c)) {
this.typed += c;
this.parts[this.cursor].setTo(this.typed);
this.render();
}
}
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(erase.lines(1));
super.render();
let clear = erase.line + (this.lines ? erase.down(this.lines) : '') + cursor.to(0);
this.lines = 0;
let error = '';
if (this.error) {
let lines = this.errorMsg.split('\n');
error = lines.reduce((a, l, i) => a + `\n${i ? ` ` : figures.pointerSmall} ${color.red().italic(l)}`, ``);
this.lines = lines.length;
}
// Print prompt
let prompt = [
style.symbol(this.done, this.aborted),
color.bold(this.msg),
style.delimiter(false),
this.parts.reduce((arr, p, idx) => arr.concat(idx === this.cursor && !this.done ? color.cyan().underline(p.toString()) : p), [])
.join(''),
].join(' ');
let position = '';
if (this.lines) {
position += cursor.up(this.lines);
position += cursor.left+cursor.to(strip(prompt).length);
}
this.out.write(clear+prompt+error+position);
}
}
module.exports = DatePrompt;

13
node_modules/prompts/lib/elements/index.js generated vendored Normal file
View file

@ -0,0 +1,13 @@
'use strict';
module.exports = {
TextPrompt: require('./text'),
SelectPrompt: require('./select'),
TogglePrompt: require('./toggle'),
DatePrompt: require('./date'),
NumberPrompt: require('./number'),
MultiselectPrompt: require('./multiselect'),
AutocompletePrompt: require('./autocomplete'),
AutocompleteMultiselectPrompt: require('./autocompleteMultiselect'),
ConfirmPrompt: require('./confirm')
};

238
node_modules/prompts/lib/elements/multiselect.js generated vendored Normal file
View file

@ -0,0 +1,238 @@
'use strict';
const color = require('kleur');
const { cursor } = require('sisteransi');
const Prompt = require('./prompt');
const { clear, figures, style } = require('../util');
/**
* MultiselectPrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {Array} opts.choices Array of choice objects
* @param {String} [opts.hint] Hint to display
* @param {String} [opts.warn] Hint shown for disabled choices
* @param {Number} [opts.max] Max choices
* @param {Number} [opts.cursor=0] Cursor start position
* @param {Stream} [opts.stdin] The Readable stream to listen to
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
*/
class MultiselectPrompt extends Prompt {
constructor(opts={}) {
super(opts);
this.msg = opts.message;
this.cursor = opts.cursor || 0;
this.scrollIndex = opts.cursor || 0;
this.hint = opts.hint || '';
this.warn = opts.warn || '- This option is disabled -';
this.minSelected = opts.min;
this.showMinError = false;
this.maxChoices = opts.max;
this.value = opts.choices.map((ch, idx) => {
if (typeof ch === 'string')
ch = {title: ch, value: idx};
return {
title: ch && (ch.title || ch.value || ch),
value: ch && (ch.value || idx),
selected: ch && ch.selected,
disabled: ch && ch.disabled
};
});
this.clear = clear('');
if (!opts.overrideRender) {
this.render();
}
}
reset() {
this.value.map(v => !v.selected);
this.cursor = 0;
this.fire();
this.render();
}
selected() {
return this.value.filter(v => v.selected);
}
abort() {
this.done = this.aborted = true;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
submit() {
const selected = this.value
.filter(e => e.selected);
if (this.minSelected && selected.length < this.minSelected) {
this.showMinError = true;
this.render();
} else {
this.done = true;
this.aborted = false;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
}
first() {
this.cursor = 0;
this.render();
}
last() {
this.cursor = this.value.length - 1;
this.render();
}
next() {
this.cursor = (this.cursor + 1) % this.value.length;
this.render();
}
up() {
if (this.cursor === 0) {
this.cursor = this.value.length - 1;
} else {
this.cursor--;
}
this.render();
}
down() {
if (this.cursor === this.value.length - 1) {
this.cursor = 0;
} else {
this.cursor++;
}
this.render();
}
left() {
this.value[this.cursor].selected = false;
this.render();
}
right() {
if (this.value.filter(e => e.selected).length >= this.maxChoices) return this.bell();
this.value[this.cursor].selected = true;
this.render();
}
handleSpaceToggle() {
const v = this.value[this.cursor];
if (v.selected) {
v.selected = false;
this.render();
} else if (v.disabled || this.value.filter(e => e.selected).length >= this.maxChoices) {
return this.bell();
} else {
v.selected = true;
this.render();
}
}
_(c, key) {
if (c === ' ') {
this.handleSpaceToggle();
} else {
return this.bell();
}
}
renderInstructions() {
return `
Instructions:
${figures.arrowUp}/${figures.arrowDown}: Highlight option
${figures.arrowLeft}/${figures.arrowRight}/[space]: Toggle selection
enter/return: Complete answer
`
}
renderOption(cursor, v, i) {
let title;
if (v.disabled) title = cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);
else title = cursor === i ? color.cyan().underline(v.title) : v.title;
return (v.selected ? color.green(figures.radioOn) : figures.radioOff) + ' ' + title
}
// shared with autocompleteMultiselect
paginateOptions(options) {
const c = this.cursor;
let styledOptions = options.map((v, i) => this.renderOption(c, v, i));
const numOfOptionsToRender = 10; // if needed, can add an option to change this.
let scopedOptions = styledOptions;
let hint = '';
if (styledOptions.length === 0) {
return color.red('No matches for this query.');
} else if (styledOptions.length > numOfOptionsToRender) {
let startIndex = c - (numOfOptionsToRender / 2);
let endIndex = c + (numOfOptionsToRender / 2);
if (startIndex < 0) {
startIndex = 0;
endIndex = numOfOptionsToRender;
} else if (endIndex > options.length) {
endIndex = options.length;
startIndex = endIndex - numOfOptionsToRender;
}
scopedOptions = styledOptions.slice(startIndex, endIndex);
hint = color.dim('(Move up and down to reveal more choices)');
}
return '\n' + scopedOptions.join('\n') + '\n' + hint;
}
// shared with autocomleteMultiselect
renderOptions(options) {
if (!this.done) {
return this.paginateOptions(options);
}
return '';
}
renderDoneOrInstructions() {
if (this.done) {
const selected = this.value
.filter(e => e.selected)
.map(v => v.title)
.join(', ');
return selected;
}
const output = [color.gray(this.hint), this.renderInstructions()];
if (this.value[this.cursor].disabled) {
output.push(color.yellow(this.warn));
}
return output.join(' ');
}
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
super.render();
// print prompt
let prompt = [
style.symbol(this.done, this.aborted),
color.bold(this.msg),
style.delimiter(false),
this.renderDoneOrInstructions()
].join(' ');
if (this.showMinError) {
prompt += color.red(`You must select a minimum of ${this.minSelected} choices.`);
this.showMinError = false;
}
prompt += this.renderOptions(this.value);
this.out.write(this.clear + prompt);
this.clear = clear(prompt);
}
}
module.exports = MultiselectPrompt;

202
node_modules/prompts/lib/elements/number.js generated vendored Normal file
View file

@ -0,0 +1,202 @@
const color = require('kleur');
const Prompt = require('./prompt');
const { cursor, erase } = require('sisteransi');
const { style, clear, figures, strip } = require('../util');
const isNumber = /[0-9]/;
const isDef = any => any !== undefined;
const round = (number, precision) => {
let factor = Math.pow(10, precision);
return Math.round(number * factor) / factor;
}
/**
* NumberPrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {String} [opts.style='default'] Render style
* @param {Number} [opts.initial] Default value
* @param {Number} [opts.max=+Infinity] Max value
* @param {Number} [opts.min=-Infinity] Min value
* @param {Boolean} [opts.float=false] Parse input as floats
* @param {Number} [opts.round=2] Round floats to x decimals
* @param {Number} [opts.increment=1] Number to increment by when using arrow-keys
* @param {Function} [opts.validate] Validate function
* @param {Stream} [opts.stdin] The Readable stream to listen to
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
* @param {String} [opts.error] The invalid error label
*/
class NumberPrompt extends Prompt {
constructor(opts={}) {
super(opts);
this.transform = style.render(opts.style);
this.msg = opts.message;
this.initial = isDef(opts.initial) ? opts.initial : '';
this.float = !!opts.float;
this.round = opts.round || 2;
this.inc = opts.increment || 1;
this.min = isDef(opts.min) ? opts.min : -Infinity;
this.max = isDef(opts.max) ? opts.max : Infinity;
this.errorMsg = opts.error || `Please Enter A Valid Value`;
this.validator = opts.validate || (() => true);
this.color = `cyan`;
this.value = ``;
this.typed = ``;
this.lastHit = 0;
this.render();
}
set value(v) {
if (!v && v !== 0) {
this.placeholder = true;
this.rendered = color.gray(this.transform.render(`${this.initial}`));
this._value = ``;
} else {
this.placeholder = false;
this.rendered = this.transform.render(`${round(v, this.round)}`);
this._value = round(v, this.round);
}
this.fire();
}
get value() {
return this._value;
}
parse(x) {
return this.float ? parseFloat(x) : parseInt(x);
}
valid(c) {
return c === `-` || c === `.` && this.float || isNumber.test(c)
}
reset() {
this.typed = ``;
this.value = ``;
this.fire();
this.render();
}
abort() {
let x = this.value;
this.value = x !== `` ? x : this.initial;
this.done = this.aborted = true;
this.error = false;
this.fire();
this.render();
this.out.write(`\n`);
this.close();
}
async validate() {
let valid = await this.validator(this.value);
if (typeof valid === `string`) {
this.errorMsg = valid;
valid = false;
}
this.error = !valid;
}
async submit() {
await this.validate();
if (this.error) {
this.color = `red`;
this.fire();
this.render();
return;
}
let x = this.value;
this.value = x !== `` ? x : this.initial;
this.done = true;
this.aborted = false;
this.error = false;
this.fire();
this.render();
this.out.write(`\n`);
this.close();
}
up() {
this.typed = ``;
if (this.value >= this.max) return this.bell();
this.value += this.inc;
this.color = `cyan`;
this.fire();
this.render();
}
down() {
this.typed = ``;
if (this.value <= this.min) return this.bell();
this.value -= this.inc;
this.color = `cyan`;
this.fire();
this.render();
}
delete() {
let val = this.value.toString();
if (val.length === 0) return this.bell();
this.value = this.parse((val = val.slice(0, -1))) || ``;
this.color = `cyan`;
this.fire();
this.render();
}
next() {
this.value = this.initial;
this.fire();
this.render();
}
_(c, key) {
if (!this.valid(c)) return this.bell();
const now = Date.now();
if (now - this.lastHit > 1000) this.typed = ``; // 1s elapsed
this.typed += c;
this.lastHit = now;
this.color = `cyan`;
if (c === `.`) return this.fire();
this.value = Math.min(this.parse(this.typed), this.max);
if (this.value > this.max) this.value = this.max;
if (this.value < this.min) this.value = this.min;
this.fire();
this.render();
}
render() {
if (this.closed) return;
super.render();
let clear = erase.line + (this.lines ? erase.down(this.lines) : ``) + cursor.to(0);
this.lines = 0;
let error = ``;
if (this.error) {
let lines = this.errorMsg.split(`\n`);
error += lines.reduce((a, l, i) => a + `\n${i ? ` ` : figures.pointerSmall} ${color.red().italic(l)}`, ``);
this.lines = lines.length;
}
let underline = !this.done || (!this.done && !this.placeholder);
let prompt = [
style.symbol(this.done, this.aborted),
color.bold(this.msg),
style.delimiter(this.done),
underline ? color[this.color]().underline(this.rendered) : this.rendered
].join(` `);
let position = ``;
if (this.lines) {
position += cursor.up(this.lines);
position += cursor.left+cursor.to(strip(prompt).length);
}
this.out.write(clear+prompt+error+position);
}
}
module.exports = NumberPrompt;

68
node_modules/prompts/lib/elements/prompt.js generated vendored Normal file
View file

@ -0,0 +1,68 @@
'use strict';
const readline = require('readline');
const { action } = require('../util');
const EventEmitter = require('events');
const { beep, cursor } = require('sisteransi');
const color = require('kleur');
/**
* Base prompt skeleton
* @param {Stream} [opts.stdin] The Readable stream to listen to
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
*/
class Prompt extends EventEmitter {
constructor(opts={}) {
super();
this.firstRender = true;
this.in = opts.in || process.stdin;
this.out = opts.out || process.stdout;
this.onRender = (opts.onRender || (() => void 0)).bind(this);
const rl = readline.createInterface(this.in);
readline.emitKeypressEvents(this.in, rl);
if (this.in.isTTY) this.in.setRawMode(true);
const keypress = (str, key) => {
let a = action(key);
if (a === false) {
this._ && this._(str, key);
} else if (typeof this[a] === 'function') {
this[a](key);
} else {
this.bell();
}
};
this.close = () => {
this.out.write(cursor.show);
this.in.removeListener('keypress', keypress);
if (this.in.isTTY) this.in.setRawMode(false);
rl.close();
this.emit(this.aborted ? 'abort' : 'submit', this.value);
this.closed = true;
};
this.in.on('keypress', keypress);
}
fire() {
this.emit('state', {
value: this.value,
aborted: !!this.aborted
});
}
bell() {
this.out.write(beep);
}
render() {
this.onRender(color);
if (this.firstRender) this.firstRender = false;
}
}
module.exports = Prompt;

144
node_modules/prompts/lib/elements/select.js generated vendored Normal file
View file

@ -0,0 +1,144 @@
'use strict';
const color = require('kleur');
const Prompt = require('./prompt');
const { style, clear, figures } = require('../util');
const { erase, cursor } = require('sisteransi');
/**
* SelectPrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {Array} opts.choices Array of choice objects
* @param {String} [opts.hint] Hint to display
* @param {Number} [opts.initial] Index of default value
* @param {Stream} [opts.stdin] The Readable stream to listen to
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
*/
class SelectPrompt extends Prompt {
constructor(opts={}) {
super(opts);
this.msg = opts.message;
this.hint = opts.hint || '- Use arrow-keys. Return to submit.';
this.warn = opts.warn || '- This option is disabled';
this.cursor = opts.initial || 0;
this.choices = opts.choices.map((ch, idx) => {
if (typeof ch === 'string')
ch = {title: ch, value: idx};
return {
title: ch && (ch.title || ch.value || ch),
value: ch && (ch.value || idx),
selected: ch && ch.selected,
disabled: ch && ch.disabled
};
});
this.value = (this.choices[this.cursor] || {}).value;
this.clear = clear('');
this.render();
}
moveCursor(n) {
this.cursor = n;
this.value = this.choices[n].value;
this.fire();
}
reset() {
this.moveCursor(0);
this.fire();
this.render();
}
abort() {
this.done = this.aborted = true;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
submit() {
if (!this.selection.disabled) {
this.done = true;
this.aborted = false;
this.fire();
this.render();
this.out.write('\n');
this.close();
} else
this.bell();
}
first() {
this.moveCursor(0);
this.render();
}
last() {
this.moveCursor(this.choices.length - 1);
this.render();
}
up() {
if (this.cursor === 0) return this.bell();
this.moveCursor(this.cursor - 1);
this.render();
}
down() {
if (this.cursor === this.choices.length - 1) return this.bell();
this.moveCursor(this.cursor + 1);
this.render();
}
next() {
this.moveCursor((this.cursor + 1) % this.choices.length);
this.render();
}
_(c, key) {
if (c === ' ') return this.submit();
}
get selection() {
return this.choices[this.cursor];
}
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(erase.lines(this.choices.length + 1));
super.render();
// Print prompt
this.out.write([
style.symbol(this.done, this.aborted),
color.bold(this.msg),
style.delimiter(false),
this.done ? this.selection.title : this.selection.disabled
? color.yellow(this.warn) : color.gray(this.hint)
].join(' '));
// Print choices
if (!this.done) {
this.out.write(
'\n' +
this.choices
.map((v, i) => {
let title, prefix;
if (v.disabled) {
title = this.cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);
prefix = this.cursor === i ? color.bold().gray(figures.pointer) + ' ' : ' ';
} else {
title = this.cursor === i ? color.cyan().underline(v.title) : v.title;
prefix = this.cursor === i ? color.cyan(figures.pointer) + ' ' : ' ';
}
return `${prefix} ${title}`;
})
.join('\n')
);
}
}
}
module.exports = SelectPrompt;

190
node_modules/prompts/lib/elements/text.js generated vendored Normal file
View file

@ -0,0 +1,190 @@
const color = require('kleur');
const Prompt = require('./prompt');
const { cursor } = require('sisteransi');
const { style, clear, strip, figures } = require('../util');
/**
* TextPrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {String} [opts.style='default'] Render style
* @param {String} [opts.initial] Default value
* @param {Function} [opts.validate] Validate function
* @param {Stream} [opts.stdin] The Readable stream to listen to
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
* @param {String} [opts.error] The invalid error label
*/
class TextPrompt extends Prompt {
constructor(opts={}) {
super(opts);
this.transform = style.render(opts.style);
this.scale = this.transform.scale;
this.msg = opts.message;
this.initial = opts.initial || ``;
this.validator = opts.validate || (() => true);
this.value = ``;
this.errorMsg = opts.error || `Please Enter A Valid Value`;
this.cursor = Number(!!this.initial);
this.clear = clear(``);
this.render();
}
set value(v) {
if (!v && this.initial) {
this.placeholder = true;
this.rendered = color.gray(this.transform.render(this.initial));
} else {
this.placeholder = false;
this.rendered = this.transform.render(v);
}
this._value = v;
this.fire();
}
get value() {
return this._value;
}
reset() {
this.value = ``;
this.cursor = Number(!!this.initial);
this.fire();
this.render();
}
abort() {
this.value = this.value || this.initial;
this.done = this.aborted = true;
this.error = false;
this.red = false;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
async validate() {
let valid = await this.validator(this.value);
if (typeof valid === `string`) {
this.errorMsg = valid;
valid = false;
}
this.error = !valid;
}
async submit() {
this.value = this.value || this.initial;
await this.validate();
if (this.error) {
this.red = true;
this.fire();
this.render();
return;
}
this.done = true;
this.aborted = false;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
next() {
if (!this.placeholder) return this.bell();
this.value = this.initial;
this.cursor = this.rendered.length;
this.fire();
this.render();
}
moveCursor(n) {
if (this.placeholder) return;
this.cursor = this.cursor+n;
}
_(c, key) {
let s1 = this.value.slice(0, this.cursor);
let s2 = this.value.slice(this.cursor);
this.value = `${s1}${c}${s2}`;
this.red = false;
this.cursor = this.placeholder ? 0 : s1.length+1;
this.render();
}
delete() {
if (this.cursor === 0) return this.bell();
let s1 = this.value.slice(0, this.cursor-1);
let s2 = this.value.slice(this.cursor);
this.value = `${s1}${s2}`;
this.red = false;
this.moveCursor(-1);
this.render();
}
deleteForward() {
if(this.cursor*this.scale >= this.rendered.length || this.placeholder) return this.bell();
let s1 = this.value.slice(0, this.cursor);
let s2 = this.value.slice(this.cursor+1);
this.value = `${s1}${s2}`;
this.red = false;
this.render();
}
first() {
this.cursor = 0;
this.render();
}
last() {
this.cursor = this.value.length;
this.render();
}
left() {
if (this.cursor <= 0 || this.placeholder) return this.bell();
this.moveCursor(-1);
this.render();
}
right() {
if (this.cursor*this.scale >= this.rendered.length || this.placeholder) return this.bell();
this.moveCursor(1);
this.render();
}
render() {
if (this.closed) return;
super.render();
let erase = (this.lines ? cursor.down(this.lines) : ``)+this.clear;
this.lines = 0;
let prompt = [
style.symbol(this.done, this.aborted),
color.bold(this.msg),
style.delimiter(this.done),
this.red ? color.red(this.rendered) : this.rendered
].join(` `);
let error = ``;
if (this.error) {
let lines = this.errorMsg.split(`\n`);
error += lines.reduce((a, l, i) => a += `\n${i ? ' ' : figures.pointerSmall} ${color.red().italic(l)}`, ``);
this.lines = lines.length;
}
let position = ``;
if (this.lines) {
position += cursor.up(this.lines);
position += cursor.left+cursor.to(strip(prompt).length);
}
position += cursor.move(this.placeholder ?
-this.initial.length*this.scale :
-this.rendered.length+this.cursor*this.scale
);
this.out.write(erase+prompt+error+position);
this.clear = clear(prompt+error);
}
}
module.exports = TextPrompt;

114
node_modules/prompts/lib/elements/toggle.js generated vendored Normal file
View file

@ -0,0 +1,114 @@
const color = require('kleur');
const Prompt = require('./prompt');
const { style, clear } = require('../util');
const { cursor, erase } = require('sisteransi');
/**
* TogglePrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {Boolean} [opts.initial=false] Default value
* @param {String} [opts.active='no'] Active label
* @param {String} [opts.inactive='off'] Inactive label
* @param {Stream} [opts.stdin] The Readable stream to listen to
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
*/
class TogglePrompt extends Prompt {
constructor(opts={}) {
super(opts);
this.msg = opts.message;
this.value = !!opts.initial;
this.active = opts.active || 'on';
this.inactive = opts.inactive || 'off';
this.initialValue = this.value;
this.render();
}
reset() {
this.value = this.initialValue;
this.fire();
this.render();
}
abort() {
this.done = this.aborted = true;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
submit() {
this.done = true;
this.aborted = false;
this.fire();
this.render();
this.out.write('\n');
this.close();
}
deactivate() {
if (this.value === false) return this.bell();
this.value = false;
this.render();
}
activate() {
if (this.value === true) return this.bell();
this.value = true;
this.render();
}
delete() {
this.deactivate();
}
left() {
this.deactivate();
}
right() {
this.activate();
}
down() {
this.deactivate();
}
up() {
this.activate();
}
next() {
this.value = !this.value;
this.fire();
this.render();
}
_(c, key) {
if (c === ' ') {
this.value = !this.value;
} else if (c === '1') {
this.value = true;
} else if (c === '0') {
this.value = false;
} else return this.bell();
this.render();
}
render() {
if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
super.render();
this.out.write(
erase.lines(this.first ? 1 : this.msg.split(/\n/g).length) +
cursor.to(0) + [
style.symbol(this.done, this.aborted),
color.bold(this.msg),
style.delimiter(this.done),
this.value ? this.inactive : color.cyan().underline(this.inactive),
color.gray('/'),
this.value ? color.cyan().underline(this.active) : this.active
].join(' ')
);
}
}
module.exports = TogglePrompt;