@inquirer/select 1.3.3 → 2.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 +26 -0
- package/dist/cjs/index.js +51 -19
- package/dist/cjs/types/index.d.ts +23 -3
- package/dist/esm/index.mjs +52 -20
- package/dist/esm/types/index.d.mts +23 -3
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -54,9 +54,35 @@ const answer = await select({
|
|
|
54
54
|
| default | `string` | no | Defines in front of which item the cursor will initially appear. When omitted, the cursor will appear on the first selectable item. |
|
|
55
55
|
| 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. |
|
|
56
56
|
| 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. |
|
|
57
|
+
| theme | [See Theming](#Theming) | no | Customize look of the prompt. |
|
|
57
58
|
|
|
58
59
|
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.
|
|
59
60
|
|
|
61
|
+
## Theming
|
|
62
|
+
|
|
63
|
+
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.
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
type Theme = {
|
|
67
|
+
prefix: string;
|
|
68
|
+
spinner: {
|
|
69
|
+
interval: number;
|
|
70
|
+
frames: string[];
|
|
71
|
+
};
|
|
72
|
+
style: {
|
|
73
|
+
answer: (text: string) => string;
|
|
74
|
+
message: (text: string) => string;
|
|
75
|
+
error: (text: string) => string;
|
|
76
|
+
help: (text: string) => string;
|
|
77
|
+
highlight: (text: string) => string;
|
|
78
|
+
disabled: (text: string) => string;
|
|
79
|
+
};
|
|
80
|
+
icon: {
|
|
81
|
+
cursor: string;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
```
|
|
85
|
+
|
|
60
86
|
# License
|
|
61
87
|
|
|
62
88
|
Copyright (c) 2023 Simon Boudrias (twitter: [@vaxilart](https://twitter.com/Vaxilart))<br/>
|
package/dist/cjs/index.js
CHANGED
|
@@ -9,27 +9,20 @@ Object.defineProperty(exports, "Separator", { enumerable: true, get: function ()
|
|
|
9
9
|
const chalk_1 = __importDefault(require("chalk"));
|
|
10
10
|
const figures_1 = __importDefault(require("figures"));
|
|
11
11
|
const ansi_escapes_1 = __importDefault(require("ansi-escapes"));
|
|
12
|
+
const selectTheme = {
|
|
13
|
+
icon: { cursor: figures_1.default.pointer },
|
|
14
|
+
style: { disabled: (text) => chalk_1.default.dim(`- ${text}`) },
|
|
15
|
+
};
|
|
12
16
|
function isSelectable(item) {
|
|
13
17
|
return !core_1.Separator.isSeparator(item) && !item.disabled;
|
|
14
18
|
}
|
|
15
|
-
function renderItem({ item, isActive }) {
|
|
16
|
-
if (core_1.Separator.isSeparator(item)) {
|
|
17
|
-
return ` ${item.separator}`;
|
|
18
|
-
}
|
|
19
|
-
const line = item.name || item.value;
|
|
20
|
-
if (item.disabled) {
|
|
21
|
-
const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)';
|
|
22
|
-
return chalk_1.default.dim(`- ${line} ${disabledLabel}`);
|
|
23
|
-
}
|
|
24
|
-
const color = isActive ? chalk_1.default.cyan : (x) => x;
|
|
25
|
-
const prefix = isActive ? figures_1.default.pointer : ` `;
|
|
26
|
-
return color(`${prefix} ${line}`);
|
|
27
|
-
}
|
|
28
19
|
exports.default = (0, core_1.createPrompt)((config, done) => {
|
|
29
20
|
const { choices: items, loop = true, pageSize = 7 } = config;
|
|
30
21
|
const firstRender = (0, core_1.useRef)(true);
|
|
31
|
-
const
|
|
22
|
+
const theme = (0, core_1.makeTheme)(selectTheme, config.theme);
|
|
23
|
+
const prefix = (0, core_1.usePrefix)({ theme });
|
|
32
24
|
const [status, setStatus] = (0, core_1.useState)('pending');
|
|
25
|
+
const searchTimeoutRef = (0, core_1.useRef)(undefined);
|
|
33
26
|
const bounds = (0, core_1.useMemo)(() => {
|
|
34
27
|
const first = items.findIndex(isSelectable);
|
|
35
28
|
// TODO: Replace with `findLastIndex` when it's available.
|
|
@@ -46,12 +39,14 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
|
|
|
46
39
|
const [active, setActive] = (0, core_1.useState)(defaultItemIndex === -1 ? bounds.first : defaultItemIndex);
|
|
47
40
|
// Safe to assume the cursor position always point to a Choice.
|
|
48
41
|
const selectedChoice = items[active];
|
|
49
|
-
(0, core_1.useKeypress)((key) => {
|
|
42
|
+
(0, core_1.useKeypress)((key, rl) => {
|
|
43
|
+
clearTimeout(searchTimeoutRef.current);
|
|
50
44
|
if ((0, core_1.isEnterKey)(key)) {
|
|
51
45
|
setStatus('done');
|
|
52
46
|
done(selectedChoice.value);
|
|
53
47
|
}
|
|
54
48
|
else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) {
|
|
49
|
+
rl.clearLine(0);
|
|
55
50
|
if (loop ||
|
|
56
51
|
((0, core_1.isUpKey)(key) && active !== bounds.first) ||
|
|
57
52
|
((0, core_1.isDownKey)(key) && active !== bounds.last)) {
|
|
@@ -64,28 +59,65 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
|
|
|
64
59
|
}
|
|
65
60
|
}
|
|
66
61
|
else if ((0, core_1.isNumberKey)(key)) {
|
|
62
|
+
rl.clearLine(0);
|
|
67
63
|
const position = Number(key.name) - 1;
|
|
68
64
|
const item = items[position];
|
|
69
65
|
if (item != null && isSelectable(item)) {
|
|
70
66
|
setActive(position);
|
|
71
67
|
}
|
|
72
68
|
}
|
|
69
|
+
else if ((0, core_1.isBackspaceKey)(key)) {
|
|
70
|
+
rl.clearLine(0);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
// Default to search
|
|
74
|
+
const searchTerm = rl.line.toLowerCase();
|
|
75
|
+
const matchIndex = items.findIndex((item) => {
|
|
76
|
+
if (core_1.Separator.isSeparator(item) || !isSelectable(item))
|
|
77
|
+
return false;
|
|
78
|
+
return String(item.name || item.value)
|
|
79
|
+
.toLowerCase()
|
|
80
|
+
.startsWith(searchTerm);
|
|
81
|
+
});
|
|
82
|
+
if (matchIndex >= 0) {
|
|
83
|
+
setActive(matchIndex);
|
|
84
|
+
}
|
|
85
|
+
searchTimeoutRef.current = setTimeout(() => {
|
|
86
|
+
rl.clearLine(0);
|
|
87
|
+
}, 700);
|
|
88
|
+
}
|
|
73
89
|
});
|
|
74
|
-
const message =
|
|
90
|
+
const message = theme.style.message(config.message);
|
|
75
91
|
let helpTip;
|
|
76
92
|
if (firstRender.current && items.length <= pageSize) {
|
|
77
93
|
firstRender.current = false;
|
|
78
|
-
helpTip =
|
|
94
|
+
helpTip = theme.style.help('(Use arrow keys)');
|
|
79
95
|
}
|
|
80
96
|
const page = (0, core_1.usePagination)({
|
|
81
97
|
items,
|
|
82
98
|
active,
|
|
83
|
-
renderItem,
|
|
99
|
+
renderItem({ item, isActive }) {
|
|
100
|
+
if (core_1.Separator.isSeparator(item)) {
|
|
101
|
+
return ` ${item.separator}`;
|
|
102
|
+
}
|
|
103
|
+
const line = item.name || item.value;
|
|
104
|
+
if (item.disabled) {
|
|
105
|
+
const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)';
|
|
106
|
+
return theme.style.disabled(`${line} ${disabledLabel}`);
|
|
107
|
+
}
|
|
108
|
+
const color = isActive ? theme.style.highlight : (x) => x;
|
|
109
|
+
const cursor = isActive ? theme.icon.cursor : ` `;
|
|
110
|
+
return color(`${cursor} ${line}`);
|
|
111
|
+
},
|
|
84
112
|
pageSize,
|
|
85
113
|
loop,
|
|
114
|
+
theme,
|
|
86
115
|
});
|
|
87
116
|
if (status === 'done') {
|
|
88
|
-
|
|
117
|
+
const answer = selectedChoice.name ||
|
|
118
|
+
// TODO: Could we enforce that at the type level? Name should be defined for non-string values.
|
|
119
|
+
String(selectedChoice.value);
|
|
120
|
+
return `${prefix} ${message} ${theme.style.answer(answer)}`;
|
|
89
121
|
}
|
|
90
122
|
const choiceDescription = selectedChoice.description
|
|
91
123
|
? `\n${selectedChoice.description}`
|
|
@@ -6,12 +6,32 @@ type Choice<Value> = {
|
|
|
6
6
|
disabled?: boolean | string;
|
|
7
7
|
type?: never;
|
|
8
8
|
};
|
|
9
|
-
declare const _default: <Value
|
|
10
|
-
message: string
|
|
9
|
+
declare const _default: <Value>(config: {
|
|
10
|
+
message: string;
|
|
11
11
|
choices: readonly (Separator | Choice<Value>)[];
|
|
12
12
|
pageSize?: number | undefined;
|
|
13
13
|
loop?: boolean | undefined;
|
|
14
|
-
default?:
|
|
14
|
+
default?: unknown;
|
|
15
|
+
theme?: {
|
|
16
|
+
icon?: {
|
|
17
|
+
cursor?: string | undefined;
|
|
18
|
+
} | undefined;
|
|
19
|
+
style?: {
|
|
20
|
+
disabled?: {} | undefined;
|
|
21
|
+
answer?: {} | undefined;
|
|
22
|
+
message?: {} | undefined;
|
|
23
|
+
error?: {} | undefined;
|
|
24
|
+
defaultAnswer?: {} | undefined;
|
|
25
|
+
help?: {} | undefined;
|
|
26
|
+
highlight?: {} | undefined;
|
|
27
|
+
key?: {} | undefined;
|
|
28
|
+
} | undefined;
|
|
29
|
+
prefix?: string | undefined;
|
|
30
|
+
spinner?: {
|
|
31
|
+
interval?: number | undefined;
|
|
32
|
+
frames?: (string | undefined)[] | undefined;
|
|
33
|
+
} | undefined;
|
|
34
|
+
} | undefined;
|
|
15
35
|
}, context?: import("@inquirer/type").Context | undefined) => import("@inquirer/type").CancelablePromise<Value>;
|
|
16
36
|
export default _default;
|
|
17
37
|
export { Separator };
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,28 +1,21 @@
|
|
|
1
|
-
import { createPrompt, useState, useKeypress, usePrefix, usePagination, useRef, useMemo, isEnterKey, isUpKey, isDownKey, isNumberKey, Separator, } from '@inquirer/core';
|
|
1
|
+
import { createPrompt, useState, useKeypress, usePrefix, usePagination, useRef, useMemo, isBackspaceKey, isEnterKey, isUpKey, isDownKey, isNumberKey, Separator, makeTheme, } from '@inquirer/core';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
3
|
import figures from 'figures';
|
|
4
4
|
import ansiEscapes from 'ansi-escapes';
|
|
5
|
+
const selectTheme = {
|
|
6
|
+
icon: { cursor: figures.pointer },
|
|
7
|
+
style: { disabled: (text) => chalk.dim(`- ${text}`) },
|
|
8
|
+
};
|
|
5
9
|
function isSelectable(item) {
|
|
6
10
|
return !Separator.isSeparator(item) && !item.disabled;
|
|
7
11
|
}
|
|
8
|
-
function renderItem({ item, isActive }) {
|
|
9
|
-
if (Separator.isSeparator(item)) {
|
|
10
|
-
return ` ${item.separator}`;
|
|
11
|
-
}
|
|
12
|
-
const line = item.name || item.value;
|
|
13
|
-
if (item.disabled) {
|
|
14
|
-
const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)';
|
|
15
|
-
return chalk.dim(`- ${line} ${disabledLabel}`);
|
|
16
|
-
}
|
|
17
|
-
const color = isActive ? chalk.cyan : (x) => x;
|
|
18
|
-
const prefix = isActive ? figures.pointer : ` `;
|
|
19
|
-
return color(`${prefix} ${line}`);
|
|
20
|
-
}
|
|
21
12
|
export default createPrompt((config, done) => {
|
|
22
13
|
const { choices: items, loop = true, pageSize = 7 } = config;
|
|
23
14
|
const firstRender = useRef(true);
|
|
24
|
-
const
|
|
15
|
+
const theme = makeTheme(selectTheme, config.theme);
|
|
16
|
+
const prefix = usePrefix({ theme });
|
|
25
17
|
const [status, setStatus] = useState('pending');
|
|
18
|
+
const searchTimeoutRef = useRef(undefined);
|
|
26
19
|
const bounds = useMemo(() => {
|
|
27
20
|
const first = items.findIndex(isSelectable);
|
|
28
21
|
// TODO: Replace with `findLastIndex` when it's available.
|
|
@@ -39,12 +32,14 @@ export default createPrompt((config, done) => {
|
|
|
39
32
|
const [active, setActive] = useState(defaultItemIndex === -1 ? bounds.first : defaultItemIndex);
|
|
40
33
|
// Safe to assume the cursor position always point to a Choice.
|
|
41
34
|
const selectedChoice = items[active];
|
|
42
|
-
useKeypress((key) => {
|
|
35
|
+
useKeypress((key, rl) => {
|
|
36
|
+
clearTimeout(searchTimeoutRef.current);
|
|
43
37
|
if (isEnterKey(key)) {
|
|
44
38
|
setStatus('done');
|
|
45
39
|
done(selectedChoice.value);
|
|
46
40
|
}
|
|
47
41
|
else if (isUpKey(key) || isDownKey(key)) {
|
|
42
|
+
rl.clearLine(0);
|
|
48
43
|
if (loop ||
|
|
49
44
|
(isUpKey(key) && active !== bounds.first) ||
|
|
50
45
|
(isDownKey(key) && active !== bounds.last)) {
|
|
@@ -57,28 +52,65 @@ export default createPrompt((config, done) => {
|
|
|
57
52
|
}
|
|
58
53
|
}
|
|
59
54
|
else if (isNumberKey(key)) {
|
|
55
|
+
rl.clearLine(0);
|
|
60
56
|
const position = Number(key.name) - 1;
|
|
61
57
|
const item = items[position];
|
|
62
58
|
if (item != null && isSelectable(item)) {
|
|
63
59
|
setActive(position);
|
|
64
60
|
}
|
|
65
61
|
}
|
|
62
|
+
else if (isBackspaceKey(key)) {
|
|
63
|
+
rl.clearLine(0);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
// Default to search
|
|
67
|
+
const searchTerm = rl.line.toLowerCase();
|
|
68
|
+
const matchIndex = items.findIndex((item) => {
|
|
69
|
+
if (Separator.isSeparator(item) || !isSelectable(item))
|
|
70
|
+
return false;
|
|
71
|
+
return String(item.name || item.value)
|
|
72
|
+
.toLowerCase()
|
|
73
|
+
.startsWith(searchTerm);
|
|
74
|
+
});
|
|
75
|
+
if (matchIndex >= 0) {
|
|
76
|
+
setActive(matchIndex);
|
|
77
|
+
}
|
|
78
|
+
searchTimeoutRef.current = setTimeout(() => {
|
|
79
|
+
rl.clearLine(0);
|
|
80
|
+
}, 700);
|
|
81
|
+
}
|
|
66
82
|
});
|
|
67
|
-
const message =
|
|
83
|
+
const message = theme.style.message(config.message);
|
|
68
84
|
let helpTip;
|
|
69
85
|
if (firstRender.current && items.length <= pageSize) {
|
|
70
86
|
firstRender.current = false;
|
|
71
|
-
helpTip =
|
|
87
|
+
helpTip = theme.style.help('(Use arrow keys)');
|
|
72
88
|
}
|
|
73
89
|
const page = usePagination({
|
|
74
90
|
items,
|
|
75
91
|
active,
|
|
76
|
-
renderItem,
|
|
92
|
+
renderItem({ item, isActive }) {
|
|
93
|
+
if (Separator.isSeparator(item)) {
|
|
94
|
+
return ` ${item.separator}`;
|
|
95
|
+
}
|
|
96
|
+
const line = item.name || item.value;
|
|
97
|
+
if (item.disabled) {
|
|
98
|
+
const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)';
|
|
99
|
+
return theme.style.disabled(`${line} ${disabledLabel}`);
|
|
100
|
+
}
|
|
101
|
+
const color = isActive ? theme.style.highlight : (x) => x;
|
|
102
|
+
const cursor = isActive ? theme.icon.cursor : ` `;
|
|
103
|
+
return color(`${cursor} ${line}`);
|
|
104
|
+
},
|
|
77
105
|
pageSize,
|
|
78
106
|
loop,
|
|
107
|
+
theme,
|
|
79
108
|
});
|
|
80
109
|
if (status === 'done') {
|
|
81
|
-
|
|
110
|
+
const answer = selectedChoice.name ||
|
|
111
|
+
// TODO: Could we enforce that at the type level? Name should be defined for non-string values.
|
|
112
|
+
String(selectedChoice.value);
|
|
113
|
+
return `${prefix} ${message} ${theme.style.answer(answer)}`;
|
|
82
114
|
}
|
|
83
115
|
const choiceDescription = selectedChoice.description
|
|
84
116
|
? `\n${selectedChoice.description}`
|
|
@@ -6,12 +6,32 @@ type Choice<Value> = {
|
|
|
6
6
|
disabled?: boolean | string;
|
|
7
7
|
type?: never;
|
|
8
8
|
};
|
|
9
|
-
declare const _default: <Value
|
|
10
|
-
message: string
|
|
9
|
+
declare const _default: <Value>(config: {
|
|
10
|
+
message: string;
|
|
11
11
|
choices: readonly (Separator | Choice<Value>)[];
|
|
12
12
|
pageSize?: number | undefined;
|
|
13
13
|
loop?: boolean | undefined;
|
|
14
|
-
default?:
|
|
14
|
+
default?: unknown;
|
|
15
|
+
theme?: {
|
|
16
|
+
icon?: {
|
|
17
|
+
cursor?: string | undefined;
|
|
18
|
+
} | undefined;
|
|
19
|
+
style?: {
|
|
20
|
+
disabled?: {} | undefined;
|
|
21
|
+
answer?: {} | undefined;
|
|
22
|
+
message?: {} | undefined;
|
|
23
|
+
error?: {} | undefined;
|
|
24
|
+
defaultAnswer?: {} | undefined;
|
|
25
|
+
help?: {} | undefined;
|
|
26
|
+
highlight?: {} | undefined;
|
|
27
|
+
key?: {} | undefined;
|
|
28
|
+
} | undefined;
|
|
29
|
+
prefix?: string | undefined;
|
|
30
|
+
spinner?: {
|
|
31
|
+
interval?: number | undefined;
|
|
32
|
+
frames?: (string | undefined)[] | undefined;
|
|
33
|
+
} | undefined;
|
|
34
|
+
} | undefined;
|
|
15
35
|
}, context?: import("@inquirer/type").Context | undefined) => import("@inquirer/type").CancelablePromise<Value>;
|
|
16
36
|
export default _default;
|
|
17
37
|
export { Separator };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/select",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Inquirer select/list prompt",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"typings": "./dist/cjs/types/index.d.ts",
|
|
@@ -54,14 +54,14 @@
|
|
|
54
54
|
"license": "MIT",
|
|
55
55
|
"homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/select/README.md",
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@inquirer/core": "^
|
|
58
|
-
"@inquirer/type": "^1.
|
|
57
|
+
"@inquirer/core": "^7.0.1",
|
|
58
|
+
"@inquirer/type": "^1.2.0",
|
|
59
59
|
"ansi-escapes": "^4.3.2",
|
|
60
60
|
"chalk": "^4.1.2",
|
|
61
61
|
"figures": "^3.2.0"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@inquirer/testing": "^2.1.
|
|
64
|
+
"@inquirer/testing": "^2.1.12"
|
|
65
65
|
},
|
|
66
66
|
"scripts": {
|
|
67
67
|
"tsc": "yarn run tsc:esm && yarn run tsc:cjs",
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
"access": "public"
|
|
73
73
|
},
|
|
74
74
|
"engines": {
|
|
75
|
-
"node": ">=
|
|
75
|
+
"node": ">=18"
|
|
76
76
|
},
|
|
77
77
|
"exports": {
|
|
78
78
|
".": {
|
|
@@ -86,5 +86,5 @@
|
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
},
|
|
89
|
-
"gitHead": "
|
|
89
|
+
"gitHead": "c91196e66689cfc05e9237763e6f95f3eb5342a1"
|
|
90
90
|
}
|