@inquirer/rawlist 4.0.11 → 4.1.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.
- package/README.md +16 -5
- package/dist/commonjs/index.d.ts +1 -0
- package/dist/commonjs/index.js +44 -8
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +45 -9
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -4,6 +4,16 @@ Simple interactive command line prompt to display a raw list of choices (single
|
|
|
4
4
|
|
|
5
5
|

|
|
6
6
|
|
|
7
|
+
# Special Thanks
|
|
8
|
+
|
|
9
|
+
<div align="center" markdown="1">
|
|
10
|
+
|
|
11
|
+
[](https://graphite.dev/?utm_source=npmjs&utm_medium=repo&utm_campaign=inquirerjs)<br>
|
|
12
|
+
|
|
13
|
+
### [Graphite is the AI developer productivity platform helping teams on GitHub ship higher quality software, faster](https://graphite.dev/?utm_source=npmjs&utm_medium=repo&utm_campaign=inquirerjs)
|
|
14
|
+
|
|
15
|
+
</div>
|
|
16
|
+
|
|
7
17
|
# Installation
|
|
8
18
|
|
|
9
19
|
<table>
|
|
@@ -67,11 +77,12 @@ const answer = await rawlist({
|
|
|
67
77
|
|
|
68
78
|
## Options
|
|
69
79
|
|
|
70
|
-
| Property | Type | Required | Description
|
|
71
|
-
| -------- | ----------------------- | -------- |
|
|
72
|
-
| message | `string` | yes | The question to ask
|
|
73
|
-
| choices | `Choice[]` | yes | List of the available choices.
|
|
74
|
-
|
|
|
80
|
+
| Property | Type | Required | Description |
|
|
81
|
+
| -------- | ----------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------- |
|
|
82
|
+
| message | `string` | yes | The question to ask |
|
|
83
|
+
| choices | `Choice[]` | yes | List of the available choices. |
|
|
84
|
+
| 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. |
|
|
85
|
+
| theme | [See Theming](#Theming) | no | Customize look of the prompt. |
|
|
75
86
|
|
|
76
87
|
`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.
|
|
77
88
|
|
package/dist/commonjs/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ type Choice<Value> = {
|
|
|
9
9
|
declare const _default: <Value>(config: {
|
|
10
10
|
message: string;
|
|
11
11
|
choices: readonly (string | Separator)[] | readonly (Separator | Choice<Value>)[];
|
|
12
|
+
loop?: boolean | undefined;
|
|
12
13
|
theme?: PartialDeep<Theme> | undefined;
|
|
13
14
|
}, context?: import("@inquirer/type").Context) => Promise<Value> & {
|
|
14
15
|
cancel: () => void;
|
package/dist/commonjs/index.js
CHANGED
|
@@ -33,23 +33,39 @@ function normalizeChoices(choices) {
|
|
|
33
33
|
};
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
|
+
function getSelectedChoice(input, choices) {
|
|
37
|
+
let selectedChoice;
|
|
38
|
+
const selectableChoices = choices.filter(isSelectableChoice);
|
|
39
|
+
if (numberRegex.test(input)) {
|
|
40
|
+
const answer = Number.parseInt(input, 10) - 1;
|
|
41
|
+
selectedChoice = selectableChoices[answer];
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
selectedChoice = selectableChoices.find((choice) => choice.key === input);
|
|
45
|
+
}
|
|
46
|
+
return selectedChoice
|
|
47
|
+
? [selectedChoice, choices.indexOf(selectedChoice)]
|
|
48
|
+
: [undefined, undefined];
|
|
49
|
+
}
|
|
36
50
|
exports.default = (0, core_1.createPrompt)((config, done) => {
|
|
51
|
+
const { loop = true } = config;
|
|
37
52
|
const choices = (0, core_1.useMemo)(() => normalizeChoices(config.choices), [config.choices]);
|
|
38
53
|
const [status, setStatus] = (0, core_1.useState)('idle');
|
|
39
54
|
const [value, setValue] = (0, core_1.useState)('');
|
|
40
55
|
const [errorMsg, setError] = (0, core_1.useState)();
|
|
41
56
|
const theme = (0, core_1.makeTheme)(config.theme);
|
|
42
57
|
const prefix = (0, core_1.usePrefix)({ status, theme });
|
|
58
|
+
const bounds = (0, core_1.useMemo)(() => {
|
|
59
|
+
const first = choices.findIndex(isSelectableChoice);
|
|
60
|
+
const last = choices.findLastIndex(isSelectableChoice);
|
|
61
|
+
if (first === -1) {
|
|
62
|
+
throw new core_1.ValidationError('[select prompt] No selectable choices. All choices are disabled.');
|
|
63
|
+
}
|
|
64
|
+
return { first, last };
|
|
65
|
+
}, [choices]);
|
|
43
66
|
(0, core_1.useKeypress)((key, rl) => {
|
|
44
67
|
if ((0, core_1.isEnterKey)(key)) {
|
|
45
|
-
|
|
46
|
-
if (numberRegex.test(value)) {
|
|
47
|
-
const answer = Number.parseInt(value, 10) - 1;
|
|
48
|
-
selectedChoice = choices.filter(isSelectableChoice)[answer];
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
selectedChoice = choices.find((choice) => isSelectableChoice(choice) && choice.key === value);
|
|
52
|
-
}
|
|
68
|
+
const [selectedChoice] = getSelectedChoice(value, choices);
|
|
53
69
|
if (isSelectableChoice(selectedChoice)) {
|
|
54
70
|
setValue(selectedChoice.short);
|
|
55
71
|
setStatus('done');
|
|
@@ -62,6 +78,26 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
|
|
|
62
78
|
setError(`"${yoctocolors_cjs_1.default.red(value)}" isn't an available option`);
|
|
63
79
|
}
|
|
64
80
|
}
|
|
81
|
+
else if (key.name === 'up' || key.name === 'down') {
|
|
82
|
+
rl.clearLine(0);
|
|
83
|
+
const [selectedChoice, active] = getSelectedChoice(value, choices);
|
|
84
|
+
if (!selectedChoice) {
|
|
85
|
+
const firstChoice = key.name === 'down'
|
|
86
|
+
? choices.find(isSelectableChoice)
|
|
87
|
+
: choices.findLast(isSelectableChoice);
|
|
88
|
+
setValue(firstChoice.key);
|
|
89
|
+
}
|
|
90
|
+
else if (loop ||
|
|
91
|
+
(key.name === 'up' && active !== bounds.first) ||
|
|
92
|
+
(key.name === 'down' && active !== bounds.last)) {
|
|
93
|
+
const offset = key.name === 'up' ? -1 : 1;
|
|
94
|
+
let next = active;
|
|
95
|
+
do {
|
|
96
|
+
next = (next + offset + choices.length) % choices.length;
|
|
97
|
+
} while (!isSelectableChoice(choices[next]));
|
|
98
|
+
setValue(choices[next].key);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
65
101
|
else {
|
|
66
102
|
setValue(rl.line);
|
|
67
103
|
setError(undefined);
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ type Choice<Value> = {
|
|
|
9
9
|
declare const _default: <Value>(config: {
|
|
10
10
|
message: string;
|
|
11
11
|
choices: readonly (string | Separator)[] | readonly (Separator | Choice<Value>)[];
|
|
12
|
+
loop?: boolean | undefined;
|
|
12
13
|
theme?: PartialDeep<Theme> | undefined;
|
|
13
14
|
}, context?: import("@inquirer/type").Context) => Promise<Value> & {
|
|
14
15
|
cancel: () => void;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createPrompt, useMemo, useState, useKeypress, usePrefix, isEnterKey, Separator, makeTheme, } from '@inquirer/core';
|
|
1
|
+
import { createPrompt, useMemo, useState, useKeypress, usePrefix, isEnterKey, Separator, makeTheme, ValidationError, } from '@inquirer/core';
|
|
2
2
|
import colors from 'yoctocolors-cjs';
|
|
3
3
|
const numberRegex = /\d+/;
|
|
4
4
|
function isSelectableChoice(choice) {
|
|
@@ -27,23 +27,39 @@ function normalizeChoices(choices) {
|
|
|
27
27
|
};
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
|
+
function getSelectedChoice(input, choices) {
|
|
31
|
+
let selectedChoice;
|
|
32
|
+
const selectableChoices = choices.filter(isSelectableChoice);
|
|
33
|
+
if (numberRegex.test(input)) {
|
|
34
|
+
const answer = Number.parseInt(input, 10) - 1;
|
|
35
|
+
selectedChoice = selectableChoices[answer];
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
selectedChoice = selectableChoices.find((choice) => choice.key === input);
|
|
39
|
+
}
|
|
40
|
+
return selectedChoice
|
|
41
|
+
? [selectedChoice, choices.indexOf(selectedChoice)]
|
|
42
|
+
: [undefined, undefined];
|
|
43
|
+
}
|
|
30
44
|
export default createPrompt((config, done) => {
|
|
45
|
+
const { loop = true } = config;
|
|
31
46
|
const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
|
|
32
47
|
const [status, setStatus] = useState('idle');
|
|
33
48
|
const [value, setValue] = useState('');
|
|
34
49
|
const [errorMsg, setError] = useState();
|
|
35
50
|
const theme = makeTheme(config.theme);
|
|
36
51
|
const prefix = usePrefix({ status, theme });
|
|
52
|
+
const bounds = useMemo(() => {
|
|
53
|
+
const first = choices.findIndex(isSelectableChoice);
|
|
54
|
+
const last = choices.findLastIndex(isSelectableChoice);
|
|
55
|
+
if (first === -1) {
|
|
56
|
+
throw new ValidationError('[select prompt] No selectable choices. All choices are disabled.');
|
|
57
|
+
}
|
|
58
|
+
return { first, last };
|
|
59
|
+
}, [choices]);
|
|
37
60
|
useKeypress((key, rl) => {
|
|
38
61
|
if (isEnterKey(key)) {
|
|
39
|
-
|
|
40
|
-
if (numberRegex.test(value)) {
|
|
41
|
-
const answer = Number.parseInt(value, 10) - 1;
|
|
42
|
-
selectedChoice = choices.filter(isSelectableChoice)[answer];
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
selectedChoice = choices.find((choice) => isSelectableChoice(choice) && choice.key === value);
|
|
46
|
-
}
|
|
62
|
+
const [selectedChoice] = getSelectedChoice(value, choices);
|
|
47
63
|
if (isSelectableChoice(selectedChoice)) {
|
|
48
64
|
setValue(selectedChoice.short);
|
|
49
65
|
setStatus('done');
|
|
@@ -56,6 +72,26 @@ export default createPrompt((config, done) => {
|
|
|
56
72
|
setError(`"${colors.red(value)}" isn't an available option`);
|
|
57
73
|
}
|
|
58
74
|
}
|
|
75
|
+
else if (key.name === 'up' || key.name === 'down') {
|
|
76
|
+
rl.clearLine(0);
|
|
77
|
+
const [selectedChoice, active] = getSelectedChoice(value, choices);
|
|
78
|
+
if (!selectedChoice) {
|
|
79
|
+
const firstChoice = key.name === 'down'
|
|
80
|
+
? choices.find(isSelectableChoice)
|
|
81
|
+
: choices.findLast(isSelectableChoice);
|
|
82
|
+
setValue(firstChoice.key);
|
|
83
|
+
}
|
|
84
|
+
else if (loop ||
|
|
85
|
+
(key.name === 'up' && active !== bounds.first) ||
|
|
86
|
+
(key.name === 'down' && active !== bounds.last)) {
|
|
87
|
+
const offset = key.name === 'up' ? -1 : 1;
|
|
88
|
+
let next = active;
|
|
89
|
+
do {
|
|
90
|
+
next = (next + offset + choices.length) % choices.length;
|
|
91
|
+
} while (!isSelectableChoice(choices[next]));
|
|
92
|
+
setValue(choices[next].key);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
59
95
|
else {
|
|
60
96
|
setValue(rl.line);
|
|
61
97
|
setError(undefined);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/rawlist",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "Inquirer rawlist prompt",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"answer",
|
|
@@ -74,13 +74,13 @@
|
|
|
74
74
|
"tsc": "tshy"
|
|
75
75
|
},
|
|
76
76
|
"dependencies": {
|
|
77
|
-
"@inquirer/core": "^10.1.
|
|
78
|
-
"@inquirer/type": "^3.0.
|
|
77
|
+
"@inquirer/core": "^10.1.10",
|
|
78
|
+
"@inquirer/type": "^3.0.6",
|
|
79
79
|
"yoctocolors-cjs": "^2.1.2"
|
|
80
80
|
},
|
|
81
81
|
"devDependencies": {
|
|
82
82
|
"@arethetypeswrong/cli": "^0.17.4",
|
|
83
|
-
"@inquirer/testing": "^2.1.
|
|
83
|
+
"@inquirer/testing": "^2.1.46",
|
|
84
84
|
"@repo/tsconfig": "workspace:*",
|
|
85
85
|
"tshy": "^3.0.2"
|
|
86
86
|
},
|
|
@@ -107,5 +107,5 @@
|
|
|
107
107
|
"optional": true
|
|
108
108
|
}
|
|
109
109
|
},
|
|
110
|
-
"gitHead": "
|
|
110
|
+
"gitHead": "238c3af2ecf5fefb9b010fda99bb3a532fa4f61a"
|
|
111
111
|
}
|