@inquirer/rawlist 0.0.2-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +40 -0
  3. package/demo.js +55 -0
  4. package/index.js +70 -0
  5. package/package.json +28 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2022 Simon Boudrias
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # `@inquirer/rawlist`
2
+
3
+ Simple interactive command line prompt to display a raw list of choices (single value select) with minimal interaction.
4
+
5
+ ![rawlist prompt](https://cdn.rawgit.com/SBoudrias/Inquirer.js/28ae8337ba51d93e359ef4f7ee24e79b69898962/assets/screenshots/rawlist.svg)
6
+
7
+ # Installation
8
+
9
+ ```sh
10
+ npm install @inquirer/rawlist
11
+
12
+ yarn add @inquirer/rawlist
13
+ ```
14
+
15
+ # Usage
16
+
17
+ ```js
18
+ import rawlist from '@inquirer/rawlist';
19
+
20
+ const answer = await rawlist({
21
+ message: 'Select a package manager',
22
+ choices: [
23
+ { name: 'npm', value: 'npm' },
24
+ { name: 'yarn', value: 'yarn' },
25
+ { name: 'pnpm', value: 'pnpm' },
26
+ ],
27
+ });
28
+ ```
29
+
30
+ ## Options
31
+
32
+ | Property | Type | Required | Description |
33
+ | -------- | --------- | -------- | ------------------------------ |
34
+ | message | `string` | yes | The question to ask |
35
+ | choices | `Array<{ value: string, name?: string, key?: string }>` | yes | List of the available choices. The `value` will be returned as the answer, and used as display if no `name` is defined. By default, choices will be selected by index. This can be customized by using the `key` option. |
36
+
37
+ # License
38
+
39
+ Copyright (c) 2022 Simon Boudrias (twitter: [@vaxilart](https://twitter.com/Vaxilart))
40
+ Licensed under the MIT license.
package/demo.js ADDED
@@ -0,0 +1,55 @@
1
+ const rawlist = require('.');
2
+
3
+ (async () => {
4
+ let answer;
5
+
6
+ answer = await rawlist({
7
+ message: '(no keys) Conflict on `file.js`:',
8
+ choices: [
9
+ {
10
+ name: 'Overwrite',
11
+ value: 'overwrite',
12
+ },
13
+ {
14
+ name: 'Overwrite this one and all next',
15
+ value: 'overwrite_all',
16
+ },
17
+ {
18
+ name: 'Show diff',
19
+ value: 'diff',
20
+ },
21
+ {
22
+ name: 'Abort',
23
+ value: 'abort',
24
+ },
25
+ ],
26
+ });
27
+ console.log('Answer:', answer);
28
+
29
+ answer = await rawlist({
30
+ message: '(with keys) Conflict on `file.js`:',
31
+ choices: [
32
+ {
33
+ key: 'y',
34
+ name: 'Overwrite',
35
+ value: 'overwrite',
36
+ },
37
+ {
38
+ key: 'a',
39
+ name: 'Overwrite this one and all next',
40
+ value: 'overwrite_all',
41
+ },
42
+ {
43
+ key: 'd',
44
+ name: 'Show diff',
45
+ value: 'diff',
46
+ },
47
+ {
48
+ key: 'x',
49
+ name: 'Abort',
50
+ value: 'abort',
51
+ },
52
+ ],
53
+ });
54
+ console.log('Answer:', answer);
55
+ })();
package/index.js ADDED
@@ -0,0 +1,70 @@
1
+ const { createPrompt, useState, useKeypress } = require('@inquirer/core/hooks');
2
+ const { usePrefix } = require('@inquirer/core/lib/prefix');
3
+ const { isEnterKey } = require('@inquirer/core/lib/key');
4
+ const chalk = require('chalk');
5
+
6
+ const numberRegex = /[0-9]+/;
7
+
8
+ module.exports = createPrompt((config, done) => {
9
+ const { choices } = config;
10
+ const [status, setStatus] = useState('pending');
11
+ const [value, setValue] = useState('');
12
+ const [errorMsg, setError] = useState();
13
+ const prefix = usePrefix();
14
+
15
+ useKeypress((key, rl) => {
16
+ if (isEnterKey(key)) {
17
+ let selectedChoice;
18
+ if (numberRegex.test(value)) {
19
+ const answer = parseInt(value, 10) - 1;
20
+ selectedChoice = choices[answer];
21
+ } else {
22
+ const answer = value.toLowerCase();
23
+ selectedChoice = choices.find(({ key }) => key === answer);
24
+ }
25
+
26
+ if (selectedChoice) {
27
+ const finalValue = selectedChoice.value || selectedChoice.name;
28
+ setValue(finalValue);
29
+ setStatus('done');
30
+ done(finalValue);
31
+ } else if (value === '') {
32
+ setError('Please input a value');
33
+ } else {
34
+ setError(`"${chalk.red(value)}" isn't an available option`);
35
+ }
36
+ } else {
37
+ setValue(rl.line);
38
+ setError(undefined);
39
+ }
40
+ });
41
+
42
+ const message = chalk.bold(config.message);
43
+
44
+ if (status === 'done') {
45
+ return `${prefix} ${message} ${chalk.cyan(value)}`;
46
+ }
47
+
48
+ const choicesStr = choices
49
+ .map((choice, index) => {
50
+ const humanIndex = index + 1;
51
+ const line = ` ${choice.key || humanIndex}) ${choice.name || choice.value}`;
52
+
53
+ if (choice.key === value.toLowerCase() || String(humanIndex) === value) {
54
+ return chalk.cyan(line);
55
+ }
56
+
57
+ return line;
58
+ })
59
+ .join('\n');
60
+
61
+ let error = '';
62
+ if (errorMsg) {
63
+ error = chalk.red(`> ${errorMsg}`);
64
+ }
65
+
66
+ return [
67
+ `${prefix} ${message} ${value}`,
68
+ [choicesStr, error].filter(Boolean).join('\n'),
69
+ ];
70
+ });
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@inquirer/rawlist",
3
+ "version": "0.0.2-alpha.0",
4
+ "description": "Inquirer rawlist prompt",
5
+ "main": "index.js",
6
+ "repository": "SBoudrias/Inquirer.js",
7
+ "keywords": [
8
+ "cli",
9
+ "command",
10
+ "line",
11
+ "list",
12
+ "form",
13
+ "input",
14
+ "prompt",
15
+ "terminal"
16
+ ],
17
+ "author": "Simon Boudrias <admin@simonboudrias.com>",
18
+ "license": "MIT",
19
+ "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/rawlist/README.md",
20
+ "dependencies": {
21
+ "@inquirer/core": "^0.0.19-alpha.0",
22
+ "chalk": "^4.1.1"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "gitHead": "9baee326b04b4e8565e815b1fa577c36e0b93727"
28
+ }