@inquirer/select
Advanced tools
| "use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.Separator = void 0; | ||
| const core_1 = require("@inquirer/core"); | ||
| Object.defineProperty(exports, "Separator", { enumerable: true, get: function () { return core_1.Separator; } }); | ||
| const chalk_1 = __importDefault(require("chalk")); | ||
| const figures_1 = __importDefault(require("figures")); | ||
| const ansi_escapes_1 = __importDefault(require("ansi-escapes")); | ||
| function isSelectableChoice(choice) { | ||
| return choice != null && choice.type !== 'separator' && !choice.disabled; | ||
| } | ||
| exports.default = (0, core_1.createPrompt)((config, done) => { | ||
| const { choices } = config; | ||
| const paginator = (0, core_1.useRef)(new core_1.Paginator()).current; | ||
| const firstRender = (0, core_1.useRef)(true); | ||
| const prefix = (0, core_1.usePrefix)(); | ||
| const [status, setStatus] = (0, core_1.useState)('pending'); | ||
| const [cursorPosition, setCursorPos] = (0, core_1.useState)(() => { | ||
| const startIndex = choices.findIndex(isSelectableChoice); | ||
| if (startIndex < 0) { | ||
| throw new Error('[select prompt] No selectable choices. All choices are disabled.'); | ||
| } | ||
| return startIndex; | ||
| }); | ||
| // Safe to assume the cursor position always point to a Choice. | ||
| const choice = choices[cursorPosition]; | ||
| (0, core_1.useKeypress)((key) => { | ||
| if ((0, core_1.isEnterKey)(key)) { | ||
| setStatus('done'); | ||
| done(choice.value); | ||
| } | ||
| else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) { | ||
| let newCursorPosition = cursorPosition; | ||
| const offset = (0, core_1.isUpKey)(key) ? -1 : 1; | ||
| let selectedOption; | ||
| while (!isSelectableChoice(selectedOption)) { | ||
| newCursorPosition = | ||
| (newCursorPosition + offset + choices.length) % choices.length; | ||
| selectedOption = choices[newCursorPosition]; | ||
| } | ||
| setCursorPos(newCursorPosition); | ||
| } | ||
| else if ((0, core_1.isNumberKey)(key)) { | ||
| // Adjust index to start at 1 | ||
| const newCursorPosition = Number(key.name) - 1; | ||
| // Abort if the choice doesn't exists or if disabled | ||
| if (!isSelectableChoice(choices[newCursorPosition])) { | ||
| return; | ||
| } | ||
| setCursorPos(newCursorPosition); | ||
| } | ||
| }); | ||
| let message = chalk_1.default.bold(config.message); | ||
| if (firstRender.current) { | ||
| message += chalk_1.default.dim(' (Use arrow keys)'); | ||
| firstRender.current = false; | ||
| } | ||
| if (status === 'done') { | ||
| return `${prefix} ${message} ${chalk_1.default.cyan(choice.name || choice.value)}`; | ||
| } | ||
| const allChoices = choices | ||
| .map((choice, index) => { | ||
| if (choice.type === 'separator') { | ||
| return ` ${choice.separator}`; | ||
| } | ||
| const line = choice.name || choice.value; | ||
| if (choice.disabled) { | ||
| const disabledLabel = typeof choice.disabled === 'string' ? choice.disabled : '(disabled)'; | ||
| return chalk_1.default.dim(`- ${line} ${disabledLabel}`); | ||
| } | ||
| if (index === cursorPosition) { | ||
| return chalk_1.default.cyan(`${figures_1.default.pointer} ${line}`); | ||
| } | ||
| return ` ${line}`; | ||
| }) | ||
| .join('\n'); | ||
| const windowedChoices = paginator.paginate(allChoices, cursorPosition, config.pageSize); | ||
| const choiceDescription = choice.description ? `\n${choice.description}` : ``; | ||
| return `${prefix} ${message}\n${windowedChoices}${choiceDescription}${ansi_escapes_1.default.cursorHide}`; | ||
| }); |
@@ -1,9 +0,11 @@ | ||
| import { AsyncPromptConfig } from '@inquirer/core'; | ||
| import { Separator, AsyncPromptConfig, SeparatorType } from '@inquirer/core'; | ||
| type Choice = { | ||
| type?: undefined; | ||
| value: string; | ||
| name?: string; | ||
| description?: string; | ||
| disabled?: boolean | string; | ||
| }; | ||
| type SelectConfig = AsyncPromptConfig & { | ||
| choices: { | ||
| value: string; | ||
| name?: string; | ||
| description?: string; | ||
| disabled?: boolean | string; | ||
| }[]; | ||
| choices: Array<SeparatorType | Choice>; | ||
| pageSize?: number; | ||
@@ -13,1 +15,2 @@ }; | ||
| export default _default; | ||
| export { Separator }; |
+26
-13
@@ -1,8 +0,10 @@ | ||
| import { createPrompt, useState, useKeypress, useRef, usePrefix, isEnterKey, isUpKey, isDownKey, isNumberKey, Paginator, } from '@inquirer/core'; | ||
| import { createPrompt, useState, useKeypress, useRef, usePrefix, isEnterKey, isUpKey, isDownKey, isNumberKey, Paginator, Separator, } from '@inquirer/core'; | ||
| import chalk from 'chalk'; | ||
| import figures from 'figures'; | ||
| import ansiEscapes from 'ansi-escapes'; | ||
| function isSelectableChoice(choice) { | ||
| return choice != null && choice.type !== 'separator' && !choice.disabled; | ||
| } | ||
| export default createPrompt((config, done) => { | ||
| const { choices } = config; | ||
| const startIndex = Math.max(choices.findIndex(({ disabled }) => !disabled), 0); | ||
| const paginator = useRef(new Paginator()).current; | ||
@@ -12,7 +14,15 @@ const firstRender = useRef(true); | ||
| const [status, setStatus] = useState('pending'); | ||
| const [cursorPosition, setCursorPos] = useState(startIndex); | ||
| const [cursorPosition, setCursorPos] = useState(() => { | ||
| const startIndex = choices.findIndex(isSelectableChoice); | ||
| if (startIndex < 0) { | ||
| throw new Error('[select prompt] No selectable choices. All choices are disabled.'); | ||
| } | ||
| return startIndex; | ||
| }); | ||
| // Safe to assume the cursor position always point to a Choice. | ||
| const choice = choices[cursorPosition]; | ||
| useKeypress((key) => { | ||
| if (isEnterKey(key)) { | ||
| setStatus('done'); | ||
| done(choices[cursorPosition].value); | ||
| done(choice.value); | ||
| } | ||
@@ -23,3 +33,3 @@ else if (isUpKey(key) || isDownKey(key)) { | ||
| let selectedOption; | ||
| while (!selectedOption || selectedOption.disabled) { | ||
| while (!isSelectableChoice(selectedOption)) { | ||
| newCursorPosition = | ||
@@ -35,3 +45,3 @@ (newCursorPosition + offset + choices.length) % choices.length; | ||
| // Abort if the choice doesn't exists or if disabled | ||
| if (!choices[newCursorPosition] || choices[newCursorPosition].disabled) { | ||
| if (!isSelectableChoice(choices[newCursorPosition])) { | ||
| return; | ||
@@ -48,11 +58,14 @@ } | ||
| if (status === 'done') { | ||
| const choice = choices[cursorPosition]; | ||
| return `${prefix} ${message} ${chalk.cyan(choice.name || choice.value)}`; | ||
| } | ||
| const allChoices = choices | ||
| .map(({ name, value, disabled }, index) => { | ||
| const line = name || value; | ||
| if (disabled) { | ||
| return chalk.dim(`- ${line} ${typeof disabled === 'string' ? disabled : '(disabled)'}`); | ||
| .map((choice, index) => { | ||
| if (choice.type === 'separator') { | ||
| return ` ${choice.separator}`; | ||
| } | ||
| const line = choice.name || choice.value; | ||
| if (choice.disabled) { | ||
| const disabledLabel = typeof choice.disabled === 'string' ? choice.disabled : '(disabled)'; | ||
| return chalk.dim(`- ${line} ${disabledLabel}`); | ||
| } | ||
| if (index === cursorPosition) { | ||
@@ -65,5 +78,5 @@ return chalk.cyan(`${figures.pointer} ${line}`); | ||
| const windowedChoices = paginator.paginate(allChoices, cursorPosition, config.pageSize); | ||
| const choice = choices[cursorPosition]; | ||
| const choiceDescription = choice && choice.description ? `\n${choice.description}` : ``; | ||
| const choiceDescription = choice.description ? `\n${choice.description}` : ``; | ||
| return `${prefix} ${message}\n${windowedChoices}${choiceDescription}${ansiEscapes.cursorHide}`; | ||
| }); | ||
| export { Separator }; |
@@ -1,9 +0,11 @@ | ||
| import { AsyncPromptConfig } from '@inquirer/core'; | ||
| import { Separator, AsyncPromptConfig, SeparatorType } from '@inquirer/core'; | ||
| type Choice = { | ||
| type?: undefined; | ||
| value: string; | ||
| name?: string; | ||
| description?: string; | ||
| disabled?: boolean | string; | ||
| }; | ||
| type SelectConfig = AsyncPromptConfig & { | ||
| choices: { | ||
| value: string; | ||
| name?: string; | ||
| description?: string; | ||
| disabled?: boolean | string; | ||
| }[]; | ||
| choices: Array<SeparatorType | Choice>; | ||
| pageSize?: number; | ||
@@ -13,1 +15,2 @@ }; | ||
| export default _default; | ||
| export { Separator }; |
+3
-3
| { | ||
| "name": "@inquirer/select", | ||
| "version": "1.0.3", | ||
| "version": "1.1.0", | ||
| "description": "Inquirer select/list prompt", | ||
@@ -57,3 +57,3 @@ "main": "./dist/cjs/index.js", | ||
| "dependencies": { | ||
| "@inquirer/core": "^1.0.4", | ||
| "@inquirer/core": "^1.1.0", | ||
| "@inquirer/type": "^1.0.3", | ||
@@ -89,3 +89,3 @@ "ansi-escapes": "^4.3.2", | ||
| }, | ||
| "gitHead": "12fecef5292da253564c845ef5be67b37441e381" | ||
| "gitHead": "09f738e0ab2b52c0b33a3973f37d5256bca9a6ab" | ||
| } |
+9
-6
@@ -18,3 +18,3 @@ # `@inquirer/select` | ||
| ```js | ||
| import select from '@inquirer/select'; | ||
| import select, { Separator } from '@inquirer/select'; | ||
@@ -34,2 +34,3 @@ const answer = await select({ | ||
| }, | ||
| new Separator(), | ||
| { | ||
@@ -51,8 +52,10 @@ name: 'jspm', | ||
| | Property | Type | Required | Description | | ||
| | -------- | --------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| | message | `string` | yes | The question to ask | | ||
| | choices | `Array<{ value: string, name?: string, description?: string, disabled?: boolean \| string }>` | yes | List of the available choices. The `value` will be returned as the answer, and used as display if no `name` is defined. Choices who're `disabled` will be displayed, but not selectable. The `description` will be displayed under the prompt when the cursor land over the choice. | | ||
| | pageSize | `number` | no | By default, lists of choice longer than 7 will be paginated. Use this option to control how many choices will appear on the screen at once. | | ||
| | Property | Type | Required | Description | | ||
| | -------- | ---------------------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| | message | `string` | yes | The question to ask | | ||
| | choices | `Array<{ value: string, name?: string, description?: string, disabled?: boolean \| string } \| Separator>` | yes | List of the available choices. The `value` will be returned as the answer, and used as display if no `name` is defined. Choices who're `disabled` will be displayed, but not selectable. The `description` will be displayed under the prompt when the cursor land over the choice. | | ||
| | pageSize | `number` | no | By default, lists of choice longer than 7 will be paginated. Use this option to control how many choices will appear on the screen at once. | | ||
| The `Separator` object can be used to render non-selectable lines in the choice list. By default it'll render a line, but you can provide the text as argument (`new Separator('-- Dependencies --')`). This option is often used to add labels to groups within long list of options. | ||
| # License | ||
@@ -59,0 +62,0 @@ |
| "use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const core_1 = require("@inquirer/core"); | ||
| const chalk_1 = __importDefault(require("chalk")); | ||
| const figures_1 = __importDefault(require("figures")); | ||
| const ansi_escapes_1 = __importDefault(require("ansi-escapes")); | ||
| exports.default = (0, core_1.createPrompt)((config, done) => { | ||
| const { choices } = config; | ||
| const startIndex = Math.max(choices.findIndex(({ disabled }) => !disabled), 0); | ||
| const paginator = (0, core_1.useRef)(new core_1.Paginator()).current; | ||
| const firstRender = (0, core_1.useRef)(true); | ||
| const prefix = (0, core_1.usePrefix)(); | ||
| const [status, setStatus] = (0, core_1.useState)('pending'); | ||
| const [cursorPosition, setCursorPos] = (0, core_1.useState)(startIndex); | ||
| (0, core_1.useKeypress)((key) => { | ||
| if ((0, core_1.isEnterKey)(key)) { | ||
| setStatus('done'); | ||
| done(choices[cursorPosition].value); | ||
| } | ||
| else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) { | ||
| let newCursorPosition = cursorPosition; | ||
| const offset = (0, core_1.isUpKey)(key) ? -1 : 1; | ||
| let selectedOption; | ||
| while (!selectedOption || selectedOption.disabled) { | ||
| newCursorPosition = | ||
| (newCursorPosition + offset + choices.length) % choices.length; | ||
| selectedOption = choices[newCursorPosition]; | ||
| } | ||
| setCursorPos(newCursorPosition); | ||
| } | ||
| else if ((0, core_1.isNumberKey)(key)) { | ||
| // Adjust index to start at 1 | ||
| const newCursorPosition = Number(key.name) - 1; | ||
| // Abort if the choice doesn't exists or if disabled | ||
| if (!choices[newCursorPosition] || choices[newCursorPosition].disabled) { | ||
| return; | ||
| } | ||
| setCursorPos(newCursorPosition); | ||
| } | ||
| }); | ||
| let message = chalk_1.default.bold(config.message); | ||
| if (firstRender.current) { | ||
| message += chalk_1.default.dim(' (Use arrow keys)'); | ||
| firstRender.current = false; | ||
| } | ||
| if (status === 'done') { | ||
| const choice = choices[cursorPosition]; | ||
| return `${prefix} ${message} ${chalk_1.default.cyan(choice.name || choice.value)}`; | ||
| } | ||
| const allChoices = choices | ||
| .map(({ name, value, disabled }, index) => { | ||
| const line = name || value; | ||
| if (disabled) { | ||
| return chalk_1.default.dim(`- ${line} ${typeof disabled === 'string' ? disabled : '(disabled)'}`); | ||
| } | ||
| if (index === cursorPosition) { | ||
| return chalk_1.default.cyan(`${figures_1.default.pointer} ${line}`); | ||
| } | ||
| return ` ${line}`; | ||
| }) | ||
| .join('\n'); | ||
| const windowedChoices = paginator.paginate(allChoices, cursorPosition, config.pageSize); | ||
| const choice = choices[cursorPosition]; | ||
| const choiceDescription = choice && choice.description ? `\n${choice.description}` : ``; | ||
| return `${prefix} ${message}\n${windowedChoices}${choiceDescription}${ansi_escapes_1.default.cursorHide}`; | ||
| }); |
14257
12.04%160
20.3%62
5.08%Updated