firest commit
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
Copyright (c) 2025 Simon Boudrias
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
files (the "Software"), to deal in the Software without
|
||||
restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
# `@inquirer/rawlist`
|
||||
|
||||
Simple interactive command line prompt to display a raw list of choices (single value select) with minimal interaction.
|
||||
|
||||

|
||||
|
||||
# Installation
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>npm</th>
|
||||
<th>yarn</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
```sh
|
||||
npm install @inquirer/prompts
|
||||
```
|
||||
|
||||
</td>
|
||||
<td>
|
||||
|
||||
```sh
|
||||
yarn add @inquirer/prompts
|
||||
```
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colSpan="2" align="center">Or</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
```sh
|
||||
npm install @inquirer/rawlist
|
||||
```
|
||||
|
||||
</td>
|
||||
<td>
|
||||
|
||||
```sh
|
||||
yarn add @inquirer/rawlist
|
||||
```
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
# Usage
|
||||
|
||||
```js
|
||||
import { rawlist } from '@inquirer/prompts';
|
||||
// Or
|
||||
// import rawlist from '@inquirer/rawlist';
|
||||
|
||||
const answer = await rawlist({
|
||||
message: 'Select a package manager',
|
||||
choices: [
|
||||
{ name: 'npm', value: 'npm' },
|
||||
{ name: 'yarn', value: 'yarn' },
|
||||
{ name: 'pnpm', value: 'pnpm' },
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
| Property | Type | Required | Description |
|
||||
| -------- | ----------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| message | `string` | yes | The question to ask |
|
||||
| choices | `Choice[]` | yes | List of the available choices. |
|
||||
| loop | `boolean` | no | Defaults to `true`. When set to `false`, the cursor will be constrained to the top and bottom of the choice list without looping. |
|
||||
| default | `Value` | no | The value of the choice to preselect. If the value is not found, no choice is preselected. |
|
||||
| theme | [See Theming](#Theming) | no | Customize look of the prompt. |
|
||||
|
||||
`Separator` objects can be used in the `choices` array 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.
|
||||
|
||||
### `Choice` object
|
||||
|
||||
The `Choice` object is typed as
|
||||
|
||||
```ts
|
||||
type Choice<Value> = {
|
||||
value: Value;
|
||||
name?: string;
|
||||
short?: string;
|
||||
key?: string;
|
||||
description?: string;
|
||||
};
|
||||
```
|
||||
|
||||
Here's each property:
|
||||
|
||||
- `value`: The value is what will be returned by `await rawlist()`.
|
||||
- `name`: This is the string displayed in the choice list.
|
||||
- `short`: Once the prompt is done (press enter), we'll use `short` if defined to render next to the question. By default we'll use `name`.
|
||||
- `key`: The key of the choice. Displayed as `key) name`.
|
||||
- `description`: Option description which appears below the list when the choice is selected.
|
||||
|
||||
`choices` can also be an array of string, in which case the string will be used both as the `value` and the `name`.
|
||||
|
||||
## Theming
|
||||
|
||||
You can theme a prompt by passing a `theme` object option. The theme object only need to includes the keys you wish to modify, we'll fallback on the defaults for the rest.
|
||||
|
||||
```ts
|
||||
type Theme = {
|
||||
prefix: string | { idle: string; done: string };
|
||||
spinner: {
|
||||
interval: number;
|
||||
frames: string[];
|
||||
};
|
||||
style: {
|
||||
answer: (text: string) => string;
|
||||
message: (text: string, status: 'idle' | 'done' | 'loading') => string;
|
||||
error: (text: string) => string;
|
||||
highlight: (text: string) => string;
|
||||
description: (text: string) => string;
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
# License
|
||||
|
||||
Copyright (c) 2023 Simon Boudrias (twitter: [@vaxilart](https://twitter.com/Vaxilart))<br/>
|
||||
Licensed under the MIT license.
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
import { Separator, type Theme } from '@inquirer/core';
|
||||
import type { PartialDeep } from '@inquirer/type';
|
||||
type RawlistTheme = {
|
||||
style: {
|
||||
description: (text: string) => string;
|
||||
};
|
||||
};
|
||||
type Choice<Value> = {
|
||||
value: Value;
|
||||
name?: string;
|
||||
short?: string;
|
||||
key?: string;
|
||||
description?: string;
|
||||
};
|
||||
declare const _default: <Value>(config: {
|
||||
message: string;
|
||||
choices: readonly (Separator | Value | Choice<Value>)[];
|
||||
loop?: boolean | undefined;
|
||||
theme?: PartialDeep<Theme<RawlistTheme>> | undefined;
|
||||
default?: NoInfer<Value> | undefined;
|
||||
}, context?: import("@inquirer/type").Context) => Promise<Value>;
|
||||
export default _default;
|
||||
export { Separator } from '@inquirer/core';
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
import { createPrompt, useMemo, useState, useKeypress, usePrefix, isDownKey, isEnterKey, isUpKey, Separator, makeTheme, ValidationError, } from '@inquirer/core';
|
||||
import { styleText } from 'node:util';
|
||||
const numberRegex = /\d+/;
|
||||
const rawlistTheme = {
|
||||
style: {
|
||||
description: (text) => styleText('cyan', text),
|
||||
},
|
||||
};
|
||||
function isSelectableChoice(choice) {
|
||||
return choice != null && !Separator.isSeparator(choice);
|
||||
}
|
||||
function normalizeChoices(choices) {
|
||||
let index = 0;
|
||||
return choices.map((choice) => {
|
||||
if (Separator.isSeparator(choice))
|
||||
return choice;
|
||||
index += 1;
|
||||
if (typeof choice !== 'object' || choice === null || !('value' in choice)) {
|
||||
const name = String(choice);
|
||||
return {
|
||||
value: choice,
|
||||
name,
|
||||
short: name,
|
||||
key: String(index),
|
||||
};
|
||||
}
|
||||
const name = choice.name ?? String(choice.value);
|
||||
return {
|
||||
value: choice.value,
|
||||
name,
|
||||
short: choice.short ?? name,
|
||||
key: choice.key ?? String(index),
|
||||
description: choice.description,
|
||||
};
|
||||
});
|
||||
}
|
||||
function getSelectedChoice(input, choices) {
|
||||
let selectedChoice;
|
||||
const selectableChoices = choices.filter(isSelectableChoice);
|
||||
// First, try to match by custom key (exact match)
|
||||
selectedChoice = selectableChoices.find((choice) => choice.key === input);
|
||||
// If no custom key match and input is numeric, try 1-based index
|
||||
if (!selectedChoice && numberRegex.test(input)) {
|
||||
const answer = Number.parseInt(input, 10) - 1;
|
||||
selectedChoice = selectableChoices[answer];
|
||||
}
|
||||
return selectedChoice
|
||||
? [selectedChoice, choices.indexOf(selectedChoice)]
|
||||
: [undefined, undefined];
|
||||
}
|
||||
export default createPrompt((config, done) => {
|
||||
const { loop = true } = config;
|
||||
const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
|
||||
const [status, setStatus] = useState('idle');
|
||||
const [value, setValue] = useState(() => {
|
||||
const defaultChoice = config.default == null
|
||||
? undefined
|
||||
: choices.find((choice) => isSelectableChoice(choice) && choice.value === config.default);
|
||||
return defaultChoice?.key ?? '';
|
||||
});
|
||||
const [errorMsg, setError] = useState();
|
||||
const theme = makeTheme(rawlistTheme, config.theme);
|
||||
const prefix = usePrefix({ status, theme });
|
||||
const bounds = useMemo(() => {
|
||||
const first = choices.findIndex(isSelectableChoice);
|
||||
const last = choices.findLastIndex(isSelectableChoice);
|
||||
if (first === -1) {
|
||||
throw new ValidationError('[select prompt] No selectable choices. All choices are disabled.');
|
||||
}
|
||||
return { first, last };
|
||||
}, [choices]);
|
||||
useKeypress((key, rl) => {
|
||||
if (isEnterKey(key)) {
|
||||
const [selectedChoice] = getSelectedChoice(value, choices);
|
||||
if (isSelectableChoice(selectedChoice)) {
|
||||
setValue(selectedChoice.short);
|
||||
setStatus('done');
|
||||
done(selectedChoice.value);
|
||||
}
|
||||
else if (value === '') {
|
||||
setError('Please input a value');
|
||||
}
|
||||
else {
|
||||
setError(`"${styleText('red', value)}" isn't an available option`);
|
||||
}
|
||||
}
|
||||
else if (isUpKey(key) || isDownKey(key)) {
|
||||
rl.clearLine(0);
|
||||
const [selectedChoice, active] = getSelectedChoice(value, choices);
|
||||
if (!selectedChoice) {
|
||||
const firstChoice = isDownKey(key)
|
||||
? choices.find(isSelectableChoice)
|
||||
: choices.findLast(isSelectableChoice);
|
||||
setValue(firstChoice.key);
|
||||
}
|
||||
else if (loop ||
|
||||
(isUpKey(key) && active !== bounds.first) ||
|
||||
(isDownKey(key) && active !== bounds.last)) {
|
||||
const offset = isUpKey(key) ? -1 : 1;
|
||||
let next = active;
|
||||
do {
|
||||
next = (next + offset + choices.length) % choices.length;
|
||||
} while (!isSelectableChoice(choices[next]));
|
||||
setValue(choices[next].key);
|
||||
}
|
||||
}
|
||||
else {
|
||||
setValue(rl.line);
|
||||
setError(undefined);
|
||||
}
|
||||
});
|
||||
const message = theme.style.message(config.message, status);
|
||||
if (status === 'done') {
|
||||
return `${prefix} ${message} ${theme.style.answer(value)}`;
|
||||
}
|
||||
const choicesStr = choices
|
||||
.map((choice) => {
|
||||
if (Separator.isSeparator(choice)) {
|
||||
return ` ${choice.separator}`;
|
||||
}
|
||||
const line = ` ${choice.key}) ${choice.name}`;
|
||||
if (choice.key === value) {
|
||||
return theme.style.highlight(line);
|
||||
}
|
||||
return line;
|
||||
})
|
||||
.join('\n');
|
||||
let error = '';
|
||||
if (errorMsg) {
|
||||
error = theme.style.error(errorMsg);
|
||||
}
|
||||
const [selectedChoice] = getSelectedChoice(value, choices);
|
||||
let description = '';
|
||||
if (!errorMsg && selectedChoice?.description) {
|
||||
description = theme.style.description(selectedChoice.description);
|
||||
}
|
||||
return [
|
||||
`${prefix} ${message} ${value}`,
|
||||
[choicesStr, error, description].filter(Boolean).join('\n'),
|
||||
];
|
||||
});
|
||||
export { Separator } from '@inquirer/core';
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
{
|
||||
"name": "@inquirer/rawlist",
|
||||
"version": "5.2.0",
|
||||
"description": "Inquirer rawlist prompt",
|
||||
"keywords": [
|
||||
"answer",
|
||||
"answers",
|
||||
"ask",
|
||||
"base",
|
||||
"cli",
|
||||
"command",
|
||||
"command-line",
|
||||
"confirm",
|
||||
"enquirer",
|
||||
"generate",
|
||||
"generator",
|
||||
"hyper",
|
||||
"input",
|
||||
"inquire",
|
||||
"inquirer",
|
||||
"interface",
|
||||
"iterm",
|
||||
"javascript",
|
||||
"menu",
|
||||
"node",
|
||||
"nodejs",
|
||||
"prompt",
|
||||
"promptly",
|
||||
"prompts",
|
||||
"question",
|
||||
"readline",
|
||||
"scaffold",
|
||||
"scaffolder",
|
||||
"scaffolding",
|
||||
"stdin",
|
||||
"stdout",
|
||||
"terminal",
|
||||
"tty",
|
||||
"ui",
|
||||
"yeoman",
|
||||
"yo",
|
||||
"zsh"
|
||||
],
|
||||
"homepage": "https://github.com/SBoudrias/Inquirer.js/blob/main/packages/rawlist/README.md",
|
||||
"license": "MIT",
|
||||
"author": "Simon Boudrias <admin@simonboudrias.com>",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/SBoudrias/Inquirer.js.git"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"type": "module",
|
||||
"sideEffects": false,
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"default": "./dist/index.js"
|
||||
},
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"scripts": {
|
||||
"tsc": "tsc"
|
||||
},
|
||||
"dependencies": {
|
||||
"@inquirer/core": "^11.1.1",
|
||||
"@inquirer/type": "^4.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@inquirer/testing": "^3.0.4",
|
||||
"typescript": "^5.9.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/node": ">=18"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/node": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=23.5.0 || ^22.13.0 || ^21.7.0 || ^20.12.0"
|
||||
},
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"gitHead": "99d00a9adc53be8b7edf5926b2ec4ba0b792f68f"
|
||||
}
|
||||
Reference in New Issue
Block a user