Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

prompts

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prompts - npm Package Compare versions

Comparing version 2.1.0 to 2.2.0

dist/util/lines.js

115

dist/elements/autocomplete.js

@@ -12,2 +12,3 @@ 'use strict';

const _require = require('sisteransi'),
erase = _require.erase,
cursor = _require.cursor;

@@ -18,4 +19,3 @@

clear = _require2.clear,
figures = _require2.figures,
strip = _require2.strip;
wrap = _require2.wrap;

@@ -55,3 +55,6 @@ const getVal = (arr, i) => arr[i] && (arr[i].value || arr[i].title || arr[i]);

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.i18n = {
noMatches: opts.noMatches || 'no matches found'
};
this.fallback = opts.fallback || this.initial;
this.suggestions = [[]];

@@ -71,11 +74,19 @@ this.page = 0;

set fallback(fb) {
this._fb = Number.isSafeInteger(parseInt(fb)) ? parseInt(fb) : fb;
}
get fallback() {
let choice;
if (typeof this._fb === 'number') choice = this.choices[this._fb];else if (typeof this._fb === 'string') choice = {
title: this._fb
};
return choice || this._fb || {
title: this.i18n.noMatches
};
}
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;
}
if (this.suggestions[this.page].length > 0) this.value = getVal(this.suggestions[this.page], i);else this.value = this.fallback.value;
this.fire();

@@ -94,3 +105,4 @@ }

title: getTitle(arr, i),
value: getVal(arr, i)
value: getVal(arr, i),
description: s.description
})).reduce((arr, sug) => {

@@ -100,16 +112,11 @@ if (arr[arr.length - 1].length < _this.limit) arr[arr.length - 1].push(sug);else arr.push([sug]);

}, [[]]);
_this.isFallback = false;
_this.completing = false;
if (!_this.suggestions[_this.page]) _this.page = 0;
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;
// }
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);

@@ -235,45 +242,39 @@

renderOption(v, hovered) {
let desc,
title = v.title;
if (hovered) {
title = color.cyan(v.title);
if (v.description) {
desc = ` - ${v.description}`;
if (title.length + desc.length >= this.out.columns || v.description.split(/\r?\n/).length > 1) {
desc = '\n' + wrap(v.description, {
width: this.out.columns
});
}
}
}
return title + color.gray(desc || '');
}
render() {
if (this.closed) return;
if (!this.firstRender) this.out.write(clear(this.outputText));
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;
this.outputText = [color.bold(style.symbol(this.done, this.aborted)), color.bold(this.msg), style.delimiter(this.completing), this.done && this.suggestions[this.page][this.select] ? this.suggestions[this.page][this.select].title : this.rendered = this.transform.render(this.input)].join(' ');
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}`, '');
const suggestions = this.suggestions[this.page].map((item, i) => this.renderOption(item, this.select === i)).join('\n');
this.outputText += `\n` + (suggestions || color.gray(this.fallback.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++;
if (this.suggestions[this.page].length > 1) {
this.outputText += color.blue(`\nPage ${this.page + 1}/${this.suggestions.length}`);
}
}
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);
}
this.out.write(erase.line + cursor.to(0) + this.outputText);
}

@@ -280,0 +281,0 @@

@@ -162,4 +162,3 @@ 'use strict';

if (this.done) {
const selected = this.value.filter(e => e.selected).map(v => v.title).join(', ');
return selected;
return this.value.filter(e => e.selected).map(v => v.title).join(', ');
}

@@ -166,0 +165,0 @@

@@ -8,3 +8,4 @@ "use strict";

const _require = require('../util'),
style = _require.style;
style = _require.style,
clear = _require.clear;

@@ -81,5 +82,6 @@ const _require2 = require('sisteransi'),

if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
if (this.firstRender) this.out.write(cursor.hide);else this.out.write(clear(this.outputText));
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(' '));
this.outputText = [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(' ');
this.out.write(erase.line + cursor.to(0) + this.outputText);
}

@@ -86,0 +88,0 @@

@@ -14,4 +14,3 @@ 'use strict';

clear = _require.clear,
figures = _require.figures,
strip = _require.strip;
figures = _require.figures;

@@ -235,24 +234,12 @@ const _require2 = require('sisteransi'),

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.firstRender) this.out.write(cursor.hide);else this.out.write(clear(this.outputText));
super.render(); // Print prompt
this.outputText = [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(' '); // Print 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.outputText += this.errorMsg.split('\n').reduce((a, l, i) => a + `\n${i ? ` ` : figures.pointerSmall} ${color.red().italic(l)}`, ``);
}
this.out.write(clear + prompt + error + position);
this.out.write(erase.line + cursor.to(0) + this.outputText);
}

@@ -259,0 +246,0 @@

@@ -13,3 +13,5 @@ 'use strict';

figures = _require2.figures,
style = _require2.style;
style = _require2.style,
wrap = _require2.wrap,
strip = _require2.strip;
/**

@@ -40,2 +42,3 @@ * MultiselectPrompt Base Element

this.maxChoices = opts.max;
this.instructions = opts.instructions;
this.value = opts.choices.map((ch, idx) => {

@@ -48,2 +51,3 @@ if (typeof ch === 'string') ch = {

title: ch && (ch.title || ch.value || ch),
description: ch && ch.description,
value: ch && (ch.value || idx),

@@ -156,5 +160,17 @@ selected: ch && ch.selected,

toggleAll() {
if (this.maxChoices !== undefined || this.value[this.cursor].disabled) {
return this.bell();
}
const newSelected = !this.value[this.cursor].selected;
this.value.filter(v => !v.disabled).forEach(v => v.selected = newSelected);
this.render();
}
_(c, key) {
if (c === ' ') {
this.handleSpaceToggle();
} else if (c === 'a') {
this.toggleAll();
} else {

@@ -166,14 +182,35 @@ return this.bell();

renderInstructions() {
return `
Instructions:
${figures.arrowUp}/${figures.arrowDown}: Highlight option
${figures.arrowLeft}/${figures.arrowRight}/[space]: Toggle selection
enter/return: Complete answer
`;
if (this.instructions === undefined || this.instructions) {
if (typeof this.instructions === 'string') {
return this.instructions;
}
return '\nInstructions:\n' + ` ${figures.arrowUp}/${figures.arrowDown}: Highlight option\n` + ` ${figures.arrowLeft}/${figures.arrowRight}/[space]: Toggle selection\n` + (this.maxChoices === undefined ? ` a: Toggle all\n` : '') + ` enter/return: Complete answer`;
}
return '';
}
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;
const prefix = (v.selected ? color.green(figures.radioOn) : figures.radioOff) + ' ';
let title, desc;
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;
if (cursor === i && v.description) {
desc = ` - ${v.description}`;
if (prefix.length + title.length + desc.length >= this.out.columns || v.description.split(/\r?\n/).length > 1) {
desc = '\n' + wrap(v.description, {
margin: prefix.length,
width: this.out.columns
});
}
}
}
return prefix + title + color.gray(desc || '');
} // shared with autocompleteMultiselect

@@ -180,0 +217,0 @@

@@ -17,5 +17,5 @@ "use strict";

style = _require2.style,
figures = _require2.figures,
clear = _require2.clear,
figures = _require2.figures,
strip = _require2.strip;
lines = _require2.lines;

@@ -212,23 +212,18 @@ const isNumber = /[0-9]/;

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;
if (!this.firstRender) {
if (this.outputError) this.out.write(cursor.down(lines(this.outputError) - 1) + clear(this.outputError));
this.out.write(clear(this.outputText));
}
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 = ``;
super.render();
this.outputError = ''; // Print prompt
if (this.lines) {
position += cursor.up(this.lines);
position += cursor.left + cursor.to(strip(prompt).length);
this.outputText = [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(this.done), !this.done || !this.done && !this.placeholder ? color[this.color]().underline(this.rendered) : this.rendered].join(` `); // Print error
if (this.error) {
this.outputError += this.errorMsg.split(`\n`).reduce((a, l, i) => a + `\n${i ? ` ` : figures.pointerSmall} ${color.red().italic(l)}`, ``);
}
this.out.write(clear + prompt + error + position);
this.out.write(erase.line + cursor.to(0) + this.outputText + cursor.save + this.outputError + cursor.restore);
}

@@ -235,0 +230,0 @@

@@ -34,5 +34,6 @@ 'use strict';

if (this.in.isTTY) this.in.setRawMode(true);
const isSelect = ['SelectPrompt', 'MultiselectPrompt'].indexOf(this.constructor.name) > -1;
const keypress = (str, key) => {
let a = action(key);
let a = action(key, isSelect);

@@ -39,0 +40,0 @@ if (a === false) {

@@ -10,6 +10,6 @@ 'use strict';

clear = _require.clear,
figures = _require.figures;
figures = _require.figures,
wrap = _require.wrap;
const _require2 = require('sisteransi'),
erase = _require2.erase,
cursor = _require2.cursor;

@@ -43,2 +43,3 @@ /**

value: ch && (ch.value || idx),
description: ch && ch.description,
selected: ch && ch.selected,

@@ -121,10 +122,12 @@ disabled: ch && ch.disabled

if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);else this.out.write(erase.lines(this.choices.length + 1));
if (this.firstRender) this.out.write(cursor.hide);else this.out.write(clear(this.outputText));
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
this.outputText = [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;
this.outputText += '\n' + this.choices.map((v, i) => {
let title,
prefix,
desc = '';

@@ -137,7 +140,20 @@ if (v.disabled) {

prefix = this.cursor === i ? color.cyan(figures.pointer) + ' ' : ' ';
if (v.description && this.cursor === i) {
desc = ` - ${v.description}`;
if (prefix.length + title.length + desc.length >= this.out.columns || v.description.split(/\r?\n/).length > 1) {
desc = '\n' + wrap(v.description, {
margin: 3,
width: this.out.columns
});
}
}
}
return `${prefix} ${title}`;
}).join('\n'));
return `${prefix} ${title}${color.gray(desc)}`;
}).join('\n');
}
this.out.write(this.outputText);
}

@@ -144,0 +160,0 @@

@@ -12,2 +12,3 @@ "use strict";

const _require = require('sisteransi'),
erase = _require.erase,
cursor = _require.cursor;

@@ -18,3 +19,3 @@

clear = _require2.clear,
strip = _require2.strip,
lines = _require2.lines,
figures = _require2.figures;

@@ -196,24 +197,17 @@ /**

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;
if (!this.firstRender) {
if (this.outputError) this.out.write(cursor.down(lines(this.outputError) - 1) + clear(this.outputError));
this.out.write(clear(this.outputText));
}
let position = ``;
super.render();
this.outputError = '';
this.outputText = [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(this.done), this.red ? color.red(this.rendered) : this.rendered].join(` `);
if (this.lines) {
position += cursor.up(this.lines);
position += cursor.left + cursor.to(strip(prompt).length);
if (this.error) {
this.outputError += this.errorMsg.split(`\n`).reduce((a, l, i) => a + `\n${i ? ' ' : figures.pointerSmall} ${color.red().italic(l)}`, ``);
}
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);
this.out.write(erase.line + cursor.to(0) + this.outputText + cursor.save + this.outputError + cursor.restore);
}

@@ -220,0 +214,0 @@

@@ -112,5 +112,6 @@ "use strict";

if (this.closed) return;
if (this.firstRender) this.out.write(cursor.hide);
if (this.firstRender) this.out.write(cursor.hide);else this.out.write(clear(this.outputText));
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(' '));
this.outputText = [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(' ');
this.out.write(erase.line + cursor.to(0) + this.outputText);
}

@@ -117,0 +118,0 @@

'use strict';
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

@@ -6,0 +8,0 @@

'use strict';
module.exports = key => {
module.exports = (key, isSelect) => {
if (key.meta) return;
if (key.ctrl) {

@@ -12,2 +14,7 @@ if (key.name === 'a') return 'first';

if (isSelect) {
if (key.name === 'j') return 'down';
if (key.name === 'k') return 'up';
}
if (key.name === 'return') return 'submit';

@@ -14,0 +21,0 @@ if (key.name === 'enter') return 'submit'; // ctrl + J

@@ -8,3 +8,5 @@ 'use strict';

strip: require('./strip'),
figures: require('./figures')
figures: require('./figures'),
lines: require('./lines'),
wrap: require('./wrap')
};

@@ -5,4 +5,4 @@ 'use strict';

const Prompt = require('./prompt');
const { cursor } = require('sisteransi');
const { style, clear, figures, strip } = require('../util');
const { erase, cursor } = require('sisteransi');
const { style, clear, wrap } = require('../util');

@@ -41,7 +41,4 @@ const getVal = (arr, i) => arr[i] && (arr[i].value || arr[i].title || arr[i]);

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.i18n = { noMatches: opts.noMatches || 'no matches found' };
this.fallback = opts.fallback || this.initial;
this.suggestions = [[]];

@@ -61,11 +58,20 @@ this.page = 0;

set fallback(fb) {
this._fb = Number.isSafeInteger(parseInt(fb)) ? parseInt(fb) : fb;
}
get fallback() {
let choice;
if (typeof this._fb === 'number')
choice = this.choices[this._fb];
else if (typeof this._fb === 'string')
choice = { title: this._fb };
return choice || this._fb || { title: this.i18n.noMatches };
}
moveSelect(i) {
this.select = i;
if (this.suggestions[this.page].length > 0) {
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;
}
else this.value = this.fallback.value;
this.fire();

@@ -80,3 +86,3 @@ }

this.suggestions = suggestions
.map((s, i, arr) => ({title: getTitle(arr, i), value: getVal(arr, i)}))
.map((s, i, arr) => ({ title: getTitle(arr, i), value: getVal(arr, i), description: s.description }))
.reduce((arr, sug) => {

@@ -88,3 +94,2 @@ if (arr[arr.length - 1].length < this.limit)

}, [[]]);
this.isFallback = false;
this.completing = false;

@@ -94,9 +99,9 @@ if (!this.suggestions[this.page])

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;
}
// 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;
// }

@@ -221,48 +226,43 @@ const l = Math.max(suggestions.length - 1, 0);

renderOption(v, hovered) {
let desc, title = v.title;
if (hovered) {
title = color.cyan(v.title);
if (v.description) {
desc = ` - ${v.description}`;
if (title.length + desc.length >= this.out.columns
|| v.description.split(/\r?\n/).length > 1) {
desc = '\n' + wrap(v.description, { width: this.out.columns })
}
}
}
return title + color.gray(desc || '');
}
render() {
if (this.closed) return;
if (!this.firstRender) this.out.write(clear(this.outputText));
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;
this.outputText = [
color.bold(style.symbol(this.done, this.aborted)),
color.bold(this.msg),
style.delimiter(this.completing),
this.done && this.suggestions[this.page][this.select]
? this.suggestions[this.page][this.select].title
: this.rendered = this.transform.render(this.input)
].join(' ');
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) {
const suggestions = this.suggestions[this.page]
.map((item, i) => this.renderOption(item, this.select === i))
.join('\n');
this.outputText += `\n` + (suggestions || color.gray(this.fallback.title));
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++;
if (this.suggestions[this.page].length > 1) {
this.outputText += color.blue(`\nPage ${this.page+1}/${this.suggestions.length}`);
}
}
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);
}
this.out.write(erase.line + cursor.to(0) + this.outputText);
}

@@ -269,0 +269,0 @@ }

@@ -122,3 +122,3 @@ 'use strict';

this.handleInputChange(c);
}
}
}

@@ -150,9 +150,8 @@

if (this.done) {
const selected = this.value
return this.value
.filter(e => e.selected)
.map(v => v.title)
.join(', ');
return selected;
}
const output = [color.gray(this.hint), this.renderInstructions(), this.renderCurrentInput()];

@@ -172,3 +171,3 @@

// print prompt
let prompt = [

@@ -180,3 +179,3 @@ style.symbol(this.done, this.aborted),

].join(' ');
if (this.showMinError) {

@@ -183,0 +182,0 @@ prompt += color.red(`You must select a minimum of ${this.minSelected} choices.`);

const color = require('kleur');
const Prompt = require('./prompt');
const { style } = require('../util');
const { style, clear } = require('../util');
const { erase, cursor } = require('sisteransi');

@@ -70,16 +70,14 @@

if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(clear(this.outputText));
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(' ')
);
this.outputText = [
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(' ');
this.out.write(erase.line + cursor.to(0) + this.outputText);
}

@@ -86,0 +84,0 @@ }

@@ -5,3 +5,3 @@ 'use strict';

const Prompt = require('./prompt');
const { style, clear, figures, strip } = require('../util');
const { style, clear, figures } = require('../util');
const { erase, cursor } = require('sisteransi');

@@ -30,2 +30,3 @@ const { DatePart, Meridiem, Day, Hours, Milliseconds, Minutes, Month, Seconds, Year } = require('../dateparts');

/**

@@ -113,3 +114,3 @@ * DatePrompt Base Element

}
async validate() {

@@ -185,16 +186,7 @@ let valid = await this.validator(this.value);

if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(erase.lines(1));
else this.out.write(clear(this.outputText));
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 = [
this.outputText = [
style.symbol(this.done, this.aborted),

@@ -204,12 +196,12 @@ color.bold(this.msg),

this.parts.reduce((arr, p, idx) => arr.concat(idx === this.cursor && !this.done ? color.cyan().underline(p.toString()) : p), [])
.join(''),
.join('')
].join(' ');
let position = '';
if (this.lines) {
position += cursor.up(this.lines);
position += cursor.left+cursor.to(strip(prompt).length);
// Print error
if (this.error) {
this.outputText += this.errorMsg.split('\n').reduce(
(a, l, i) => a + `\n${i ? ` ` : figures.pointerSmall} ${color.red().italic(l)}`, ``);
}
this.out.write(clear+prompt+error+position);
this.out.write(erase.line + cursor.to(0) + this.outputText);
}

@@ -216,0 +208,0 @@ }

@@ -6,3 +6,3 @@ 'use strict';

const Prompt = require('./prompt');
const { clear, figures, style } = require('../util');
const { clear, figures, style, wrap, strip } = require('../util');

@@ -32,3 +32,4 @@ /**

this.maxChoices = opts.max;
this.value = opts.choices.map((ch, idx) => {
this.instructions = opts.instructions;
this.value = opts.choices.map((ch, idx) => {
if (typeof ch === 'string')

@@ -38,2 +39,3 @@ ch = {title: ch, value: idx};

title: ch && (ch.title || ch.value || ch),
description: ch && ch.description,
value: ch && (ch.value || idx),

@@ -142,24 +144,54 @@ selected: ch && ch.selected,

toggleAll() {
if (this.maxChoices !== undefined || this.value[this.cursor].disabled) {
return this.bell();
}
const newSelected = !this.value[this.cursor].selected;
this.value.filter(v => !v.disabled).forEach(v => v.selected = newSelected);
this.render();
}
_(c, key) {
if (c === ' ') {
this.handleSpaceToggle();
} else if (c === 'a') {
this.toggleAll();
} else {
return this.bell();
}
}
}
renderInstructions() {
return `
Instructions:
${figures.arrowUp}/${figures.arrowDown}: Highlight option
${figures.arrowLeft}/${figures.arrowRight}/[space]: Toggle selection
enter/return: Complete answer
`
if (this.instructions === undefined || this.instructions) {
if (typeof this.instructions === 'string') {
return this.instructions;
}
return '\nInstructions:\n'
+ ` ${figures.arrowUp}/${figures.arrowDown}: Highlight option\n`
+ ` ${figures.arrowLeft}/${figures.arrowRight}/[space]: Toggle selection\n`
+ (this.maxChoices === undefined ? ` a: Toggle all\n` : '')
+ ` enter/return: Complete answer`;
}
return '';
}
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
const prefix = (v.selected ? color.green(figures.radioOn) : figures.radioOff) + ' ';
let title, desc;
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;
if (cursor === i && v.description) {
desc = ` - ${v.description}`;
if (prefix.length + title.length + desc.length >= this.out.columns
|| v.description.split(/\r?\n/).length > 1) {
desc = '\n' + wrap(v.description, { margin: prefix.length, width: this.out.columns });
}
}
}
return prefix + title + color.gray(desc || '');
}

@@ -209,3 +241,3 @@

}
const output = [color.gray(this.hint), this.renderInstructions()];

@@ -225,3 +257,2 @@

// print prompt
let prompt = [

@@ -228,0 +259,0 @@ style.symbol(this.done, this.aborted),

const color = require('kleur');
const Prompt = require('./prompt');
const { cursor, erase } = require('sisteransi');
const { style, clear, figures, strip } = require('../util');
const { style, figures, clear, lines } = require('../util');

@@ -173,28 +173,26 @@ const isNumber = /[0-9]/;

if (this.closed) return;
if (!this.firstRender) {
if (this.outputError)
this.out.write(cursor.down(lines(this.outputError) - 1) + clear(this.outputError));
this.out.write(clear(this.outputText));
}
super.render();
let clear = erase.line + (this.lines ? erase.down(this.lines) : ``) + cursor.to(0);
this.lines = 0;
this.outputError = '';
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 = [
// Print prompt
this.outputText = [
style.symbol(this.done, this.aborted),
color.bold(this.msg),
style.delimiter(this.done),
underline ? color[this.color]().underline(this.rendered) : this.rendered
!this.done || (!this.done && !this.placeholder)
? 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);
// Print error
if (this.error) {
this.outputError += this.errorMsg.split(`\n`)
.reduce((a, l, i) => a + `\n${i ? ` ` : figures.pointerSmall} ${color.red().italic(l)}`, ``);
}
this.out.write(clear+prompt+error+position);
this.out.write(erase.line + cursor.to(0) + this.outputText + cursor.save + this.outputError + cursor.restore);
}

@@ -201,0 +199,0 @@ }

@@ -22,3 +22,2 @@ 'use strict';

this.onRender = (opts.onRender || (() => void 0)).bind(this);
const rl = readline.createInterface(this.in);

@@ -28,5 +27,5 @@ readline.emitKeypressEvents(this.in, rl);

if (this.in.isTTY) this.in.setRawMode(true);
const isSelect = [ 'SelectPrompt', 'MultiselectPrompt' ].indexOf(this.constructor.name) > -1;
const keypress = (str, key) => {
let a = action(key);
let a = action(key, isSelect);
if (a === false) {

@@ -33,0 +32,0 @@ this._ && this._(str, key);

@@ -5,4 +5,4 @@ 'use strict';

const Prompt = require('./prompt');
const { style, clear, figures } = require('../util');
const { erase, cursor } = require('sisteransi');
const { style, clear, figures, wrap } = require('../util');
const { cursor } = require('sisteransi');

@@ -26,3 +26,3 @@ /**

this.cursor = opts.initial || 0;
this.choices = opts.choices.map((ch, idx) => {
this.choices = opts.choices.map((ch, idx) => {
if (typeof ch === 'string')

@@ -33,2 +33,3 @@ ch = {title: ch, value: idx};

value: ch && (ch.value || idx),
description: ch && ch.description,
selected: ch && ch.selected,

@@ -113,21 +114,20 @@ disabled: ch && ch.disabled

if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(erase.lines(this.choices.length + 1));
else this.out.write(clear(this.outputText));
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(' '));
this.outputText = [
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.outputText += '\n' +
this.choices
.map((v, i) => {
let title, prefix;
let title, prefix, desc = '';
if (v.disabled) {

@@ -139,8 +139,16 @@ title = this.cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);

prefix = this.cursor === i ? color.cyan(figures.pointer) + ' ' : ' ';
if (v.description && this.cursor === i) {
desc = ` - ${v.description}`;
if (prefix.length + title.length + desc.length >= this.out.columns
|| v.description.split(/\r?\n/).length > 1) {
desc = '\n' + wrap(v.description, { margin: 3, width: this.out.columns });
}
}
}
return `${prefix} ${title}`;
return `${prefix} ${title}${color.gray(desc)}`;
})
.join('\n')
);
.join('\n');
}
this.out.write(this.outputText);
}

@@ -147,0 +155,0 @@ }

const color = require('kleur');
const Prompt = require('./prompt');
const { cursor } = require('sisteransi');
const { style, clear, strip, figures } = require('../util');
const { erase, cursor } = require('sisteransi');
const { style, clear, lines, figures } = require('../util');

@@ -157,7 +157,11 @@ /**

if (this.closed) return;
if (!this.firstRender) {
if (this.outputError)
this.out.write(cursor.down(lines(this.outputError) - 1) + clear(this.outputError));
this.out.write(clear(this.outputText));
}
super.render();
let erase = (this.lines ? cursor.down(this.lines) : ``)+this.clear;
this.lines = 0;
this.outputError = '';
let prompt = [
this.outputText = [
style.symbol(this.done, this.aborted),

@@ -169,21 +173,8 @@ color.bold(this.msg),

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;
this.outputError += this.errorMsg.split(`\n`)
.reduce((a, l, i) => a + `\n${i ? ' ' : figures.pointerSmall} ${color.red().italic(l)}`, ``);
}
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);
this.out.write(erase.line + cursor.to(0) + this.outputText + cursor.save + this.outputError + cursor.restore);
}

@@ -190,0 +181,0 @@ }

@@ -98,15 +98,15 @@ const color = require('kleur');

if (this.firstRender) this.out.write(cursor.hide);
else this.out.write(clear(this.outputText));
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(' ')
);
this.outputText = [
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(' ');
this.out.write(erase.line + cursor.to(0) + this.outputText);
}

@@ -113,0 +113,0 @@ }

'use strict';
module.exports = key => {
module.exports = (key, isSelect) => {
if (key.meta) return;
if (key.ctrl) {

@@ -11,2 +13,7 @@ if (key.name === 'a') return 'first';

}
if (isSelect) {
if (key.name === 'j') return 'down';
if (key.name === 'k') return 'up';
}

@@ -13,0 +20,0 @@ if (key.name === 'return') return 'submit';

@@ -8,3 +8,5 @@ 'use strict';

strip: require('./strip'),
figures: require('./figures')
figures: require('./figures'),
lines: require('./lines'),
wrap: require('./wrap')
};
{
"name": "prompts",
"version": "2.1.0",
"version": "2.2.0",
"description": "Lightweight, beautiful and user-friendly prompts",

@@ -39,12 +39,12 @@ "license": "MIT",

"dependencies": {
"kleur": "^3.0.2",
"sisteransi": "^1.0.0"
"kleur": "^3.0.3",
"sisteransi": "^1.0.3"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.3",
"@babel/plugin-proposal-object-rest-spread": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"tap-spec": "^5.0.0",
"tape": "^4.10.1"
"tape": "^4.11.0"
},

@@ -51,0 +51,0 @@ "engines": {

@@ -645,3 +645,3 @@ <p align="center">

choices: [
{ title: 'Red', value: '#ff0000' },
{ title: 'Red', description: 'This option has a description', value: '#ff0000' },
{ title: 'Green', value: '#00ff00', disabled: true },

@@ -662,3 +662,3 @@ { title: 'Blue', value: '#0000ff' }

| warn | `string` | Message to display when selecting a disabled option |
| choices | `Array` | Array of strings or choices objects `[{ title, value, disabled }, ...]`. The choice's index in the array will be used as its value if it is not specified. |
| choices | `Array` | Array of strings or choices objects `[{ title, description, value, disabled }, ...]`. The choice's index in the array will be used as its value if it is not specified. |
| onRender | `function` | On render callback. Keyword `this` refers to the current prompt |

@@ -698,2 +698,3 @@ | onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |

| format | `function` | Receive user input. The returned value will be added to the response object |
| instructions | `string` or `boolean` | Prompt instructions to display |
| choices | `Array` | Array of strings or choices objects `[{ title, value, disabled }, ...]`. The choice's index in the array will be used as its value if it is not specified. |

@@ -748,3 +749,3 @@ | min | `number` | Min select - will display error |

| initial | `string \| number` | Default initial value |
| fallback | `function` | Fallback message when no match is found. Defaults to `initial` value if provided |
| fallback | `string` | Fallback message when no match is found. Defaults to `initial` value if provided |
| onRender | `function` | On render callback. Keyword `this` refers to the current prompt |

@@ -751,0 +752,0 @@ | onState | `function` | On state change callback. Function signature is an `object` with two propetires: `value` and `aborted` |

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc