Skip to content

Commit d75d68c

Browse files
Add description option to choices (#905)
* Add long option to choices Adds a `long` option to a choice in a select prompt. If the option is set, the text in `long` for the currently selected option is shown as help at the bottom of the list window. * Rename long to description * revert README change Co-authored-by: Simon Boudrias <[email protected]>
1 parent 6642790 commit d75d68c

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ A question object is a `hash` containing question related values:
128128
- **message**: (String|Function) The question to print. If defined as a function, the first parameter will be the current inquirer session answers. Defaults to the value of `name` (followed by a colon).
129129
- **default**: (String|Number|Boolean|Array|Function) Default value(s) to use if nothing is entered, or a function that returns the default value(s). If defined as a function, the first parameter will be the current inquirer session answers.
130130
- **choices**: (Array|Function) Choices array or a function returning a choices array. If defined as a function, the first parameter will be the current inquirer session answers.
131-
Array values can be simple `numbers`, `strings`, or `objects` containing a `name` (to display in list), a `value` (to save in the answers hash) and a `short` (to display after selection) properties. The choices array can also contain [a `Separator`](#separator).
131+
Array values can be simple `numbers`, `strings`, or `objects` containing a `name` (to display in list), a `value` (to save in the answers hash), and a `short` (to display after selection) properties. The choices array can also contain [a `Separator`](#separator).
132132
- **validate**: (Function) Receive the user input and answers hash. Should return `true` if the value is valid, and an error message (`String`) otherwise. If `false` is returned, a default error message is provided.
133133
- **filter**: (Function) Receive the user input and answers hash. Returns the filtered value to be used inside the program. The value returned will be added to the _Answers_ hash.
134134
- **transformer**: (Function) Receive the user input, answers hash and option flags, and return a transformed value to display to the user. The transformation only impacts what is shown while editing. It does not modify the answers hash.

packages/select/demo.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ const select = require('.');
66
answer = await select({
77
message: 'Select a package manager',
88
choices: [
9-
{ name: 'npm', value: 'npm' },
10-
{ name: 'yarn', value: 'yarn' },
9+
{
10+
name: 'npm',
11+
value: 'npm',
12+
description: 'npm is the most popular package manager'
13+
},
14+
{ name: 'yarn', value: 'yarn', description: 'yarn is an awesome package manager' },
1115
{ name: 'jspm', value: 'jspm', disabled: true }
1216
]
1317
});
@@ -30,7 +34,7 @@ const select = require('.');
3034
{ value: 'L' },
3135
{ value: 'M' },
3236
{ value: 'N' },
33-
{ value: 'O' },
37+
{ value: 'O', description: 'Letter O, not number 0' },
3438
{ value: 'P' },
3539
{ value: 'Q' },
3640
{ value: 'R' },

packages/select/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,8 @@ module.exports = createPrompt((config, done) => {
6565
.join('\n');
6666
const windowedChoices = paginator.paginate(allChoices, cursorPosition, pageSize);
6767

68-
return `${prefix} ${message}\n${windowedChoices}${cursorHide}`;
68+
const choice = choices[cursorPosition];
69+
const choiceDescription = choice && choice.description ? `\n${choice.description}` : ``;
70+
71+
return `${prefix} ${message}\n${windowedChoices}${choiceDescription}${cursorHide}`;
6972
});

0 commit comments

Comments
 (0)