Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add choices support function #425

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/elements/autocomplete.js
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ const getIndex = (arr, valOrTitle) => {
* TextPrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {Array} opts.choices Array of auto-complete choices objects
* @param {Array | (()=>Array) } opts.choices Array of choice objects or a function returning an array
* @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
@@ -33,7 +33,7 @@ class AutocompletePrompt extends Prompt {
super(opts);
this.msg = opts.message;
this.suggest = opts.suggest;
this.choices = opts.choices;
this.choices = typeof(opts.choices) === 'function' ? opts.choices.call(this) : opts.choices;;
this.initial = typeof opts.initial === 'number'
? opts.initial
: getIndex(opts.choices, opts.initial);
@@ -102,7 +102,7 @@ class AutocompletePrompt extends Prompt {
if (this.clearFirst && this.input.length > 0) {
this.reset();
} else {
this.done = this.exited = true;
this.done = this.exited = true;
this.aborted = false;
this.fire();
this.render();
2 changes: 1 addition & 1 deletion lib/elements/autocompleteMultiselect.js
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ 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 {Array | (()=>Array) } opts.choices Array of choice objects or a function returning an array
* @param {String} [opts.hint] Hint to display
* @param {String} [opts.warn] Hint shown for disabled choices
* @param {Number} [opts.max] Max choices
25 changes: 13 additions & 12 deletions lib/elements/multiselect.js
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ const { clear, figures, style, wrap, entriesToDisplay } = require('../util');
* MultiselectPrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {Array} opts.choices Array of choice objects
* @param {Array | (()=>Array) } opts.choices Array of choice objects or a function returning an array
* @param {String} [opts.hint] Hint to display
* @param {String} [opts.warn] Hint shown for disabled choices
* @param {Number} [opts.max] Max choices
@@ -31,17 +31,18 @@ class MultiselectPrompt extends Prompt {
this.maxChoices = opts.max;
this.instructions = opts.instructions;
this.optionsPerPage = opts.optionsPerPage || 10;
this.value = opts.choices.map((ch, idx) => {
if (typeof ch === 'string')
ch = {title: ch, value: idx};
return {
title: ch && (ch.title || ch.value || ch),
description: ch && ch.description,
value: ch && (ch.value === undefined ? idx : ch.value),
selected: ch && ch.selected,
disabled: ch && ch.disabled
};
});
const choices = typeof(opts.choices) === 'function' ? opts.choices.call(this) : opts.choices;
this.value = choices.map((ch, idx) => {
if (typeof ch === 'string')
ch = {title: ch, value: idx};
return {
title: ch && (ch.title || ch.value || ch),
description: ch && ch.description,
value: ch && (ch.value === undefined ? idx : ch.value),
selected: ch && ch.selected,
disabled: ch && ch.disabled
};
});
this.clear = clear('', this.out.columns);
if (!opts.overrideRender) {
this.render();
5 changes: 3 additions & 2 deletions lib/elements/select.js
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ const { cursor } = require('sisteransi');
* SelectPrompt Base Element
* @param {Object} opts Options
* @param {String} opts.message Message
* @param {Array} opts.choices Array of choice objects
* @param {Array | (()=>Array) } opts.choices Array of choice objects or a function returning an array
* @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
@@ -23,7 +23,8 @@ class SelectPrompt extends Prompt {
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) => {
const choices = typeof(opts.choices) === 'function' ? opts.choices.call(this) : opts.choices;
this.choices = choices.map((ch, idx) => {
if (typeof ch === 'string')
ch = {title: ch, value: idx};
return {