@inquirer/select 5.1.1 → 5.1.2
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/dist/index.d.ts +35 -0
- package/dist/index.js +191 -0
- package/package.json +6 -6
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Separator, type Theme, type Keybinding } from '@inquirer/core';
|
|
2
|
+
import type { PartialDeep } from '@inquirer/type';
|
|
3
|
+
type SelectTheme = {
|
|
4
|
+
icon: {
|
|
5
|
+
cursor: string;
|
|
6
|
+
};
|
|
7
|
+
style: {
|
|
8
|
+
disabled: (text: string) => string;
|
|
9
|
+
description: (text: string) => string;
|
|
10
|
+
keysHelpTip: (keys: [key: string, action: string][]) => string | undefined;
|
|
11
|
+
};
|
|
12
|
+
i18n: {
|
|
13
|
+
disabledError: string;
|
|
14
|
+
};
|
|
15
|
+
indexMode: 'hidden' | 'number';
|
|
16
|
+
keybindings: ReadonlyArray<Keybinding>;
|
|
17
|
+
};
|
|
18
|
+
type Choice<Value> = {
|
|
19
|
+
value: Value;
|
|
20
|
+
name?: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
short?: string;
|
|
23
|
+
disabled?: boolean | string;
|
|
24
|
+
type?: never;
|
|
25
|
+
};
|
|
26
|
+
declare const _default: <Value>(config: {
|
|
27
|
+
message: string;
|
|
28
|
+
choices: readonly (Separator | Value | Choice<Value>)[];
|
|
29
|
+
pageSize?: number | undefined;
|
|
30
|
+
loop?: boolean | undefined;
|
|
31
|
+
default?: NoInfer<Value> | undefined;
|
|
32
|
+
theme?: PartialDeep<Theme<SelectTheme>> | undefined;
|
|
33
|
+
}, context?: import("@inquirer/type").Context) => Promise<Value>;
|
|
34
|
+
export default _default;
|
|
35
|
+
export { Separator } from '@inquirer/core';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { createPrompt, useState, useKeypress, usePrefix, usePagination, useRef, useMemo, useEffect, isBackspaceKey, isEnterKey, isUpKey, isDownKey, isNumberKey, Separator, ValidationError, makeTheme, } from '@inquirer/core';
|
|
2
|
+
import { cursorHide } from '@inquirer/ansi';
|
|
3
|
+
import { styleText } from 'node:util';
|
|
4
|
+
import figures from '@inquirer/figures';
|
|
5
|
+
const selectTheme = {
|
|
6
|
+
icon: { cursor: figures.pointer },
|
|
7
|
+
style: {
|
|
8
|
+
disabled: (text) => styleText('dim', text),
|
|
9
|
+
description: (text) => styleText('cyan', text),
|
|
10
|
+
keysHelpTip: (keys) => keys
|
|
11
|
+
.map(([key, action]) => `${styleText('bold', key)} ${styleText('dim', action)}`)
|
|
12
|
+
.join(styleText('dim', ' • ')),
|
|
13
|
+
},
|
|
14
|
+
i18n: { disabledError: 'This option is disabled and cannot be selected.' },
|
|
15
|
+
indexMode: 'hidden',
|
|
16
|
+
keybindings: [],
|
|
17
|
+
};
|
|
18
|
+
function isSelectable(item) {
|
|
19
|
+
return !Separator.isSeparator(item) && !item.disabled;
|
|
20
|
+
}
|
|
21
|
+
function isNavigable(item) {
|
|
22
|
+
return !Separator.isSeparator(item);
|
|
23
|
+
}
|
|
24
|
+
function normalizeChoices(choices) {
|
|
25
|
+
return choices.map((choice) => {
|
|
26
|
+
if (Separator.isSeparator(choice))
|
|
27
|
+
return choice;
|
|
28
|
+
if (typeof choice !== 'object' || choice === null || !('value' in choice)) {
|
|
29
|
+
// It's a raw value (string, number, etc.)
|
|
30
|
+
const name = String(choice);
|
|
31
|
+
return {
|
|
32
|
+
value: choice,
|
|
33
|
+
name,
|
|
34
|
+
short: name,
|
|
35
|
+
disabled: false,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const name = choice.name ?? String(choice.value);
|
|
39
|
+
const normalizedChoice = {
|
|
40
|
+
value: choice.value,
|
|
41
|
+
name,
|
|
42
|
+
short: choice.short ?? name,
|
|
43
|
+
disabled: choice.disabled ?? false,
|
|
44
|
+
};
|
|
45
|
+
if (choice.description) {
|
|
46
|
+
normalizedChoice.description = choice.description;
|
|
47
|
+
}
|
|
48
|
+
return normalizedChoice;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
export default createPrompt((config, done) => {
|
|
52
|
+
const { loop = true, pageSize = 7 } = config;
|
|
53
|
+
const theme = makeTheme(selectTheme, config.theme);
|
|
54
|
+
const { keybindings } = theme;
|
|
55
|
+
const [status, setStatus] = useState('idle');
|
|
56
|
+
const prefix = usePrefix({ status, theme });
|
|
57
|
+
const searchTimeoutRef = useRef();
|
|
58
|
+
// Vim keybindings (j/k) conflict with typing those letters in search,
|
|
59
|
+
// so search must be disabled when vim bindings are enabled
|
|
60
|
+
const searchEnabled = !keybindings.includes('vim');
|
|
61
|
+
const items = useMemo(() => normalizeChoices(config.choices), [config.choices]);
|
|
62
|
+
const bounds = useMemo(() => {
|
|
63
|
+
const first = items.findIndex(isNavigable);
|
|
64
|
+
const last = items.findLastIndex(isNavigable);
|
|
65
|
+
if (first === -1) {
|
|
66
|
+
throw new ValidationError('[select prompt] No selectable choices. All choices are disabled.');
|
|
67
|
+
}
|
|
68
|
+
return { first, last };
|
|
69
|
+
}, [items]);
|
|
70
|
+
const defaultItemIndex = useMemo(() => {
|
|
71
|
+
if (!('default' in config))
|
|
72
|
+
return -1;
|
|
73
|
+
return items.findIndex((item) => isSelectable(item) && item.value === config.default);
|
|
74
|
+
}, [config.default, items]);
|
|
75
|
+
const [active, setActive] = useState(defaultItemIndex === -1 ? bounds.first : defaultItemIndex);
|
|
76
|
+
// Safe to assume the cursor position always point to a Choice.
|
|
77
|
+
const selectedChoice = items[active];
|
|
78
|
+
const [errorMsg, setError] = useState();
|
|
79
|
+
useKeypress((key, rl) => {
|
|
80
|
+
clearTimeout(searchTimeoutRef.current);
|
|
81
|
+
if (errorMsg) {
|
|
82
|
+
setError(undefined);
|
|
83
|
+
}
|
|
84
|
+
if (isEnterKey(key)) {
|
|
85
|
+
if (selectedChoice.disabled) {
|
|
86
|
+
setError(theme.i18n.disabledError);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
setStatus('done');
|
|
90
|
+
done(selectedChoice.value);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else if (isUpKey(key, keybindings) || isDownKey(key, keybindings)) {
|
|
94
|
+
rl.clearLine(0);
|
|
95
|
+
if (loop ||
|
|
96
|
+
(isUpKey(key, keybindings) && active !== bounds.first) ||
|
|
97
|
+
(isDownKey(key, keybindings) && active !== bounds.last)) {
|
|
98
|
+
const offset = isUpKey(key, keybindings) ? -1 : 1;
|
|
99
|
+
let next = active;
|
|
100
|
+
do {
|
|
101
|
+
next = (next + offset + items.length) % items.length;
|
|
102
|
+
} while (!isNavigable(items[next]));
|
|
103
|
+
setActive(next);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (isNumberKey(key) && !Number.isNaN(Number(rl.line))) {
|
|
107
|
+
const selectedIndex = Number(rl.line) - 1;
|
|
108
|
+
// Find the nth item (ignoring separators)
|
|
109
|
+
let selectableIndex = -1;
|
|
110
|
+
const position = items.findIndex((item) => {
|
|
111
|
+
if (Separator.isSeparator(item))
|
|
112
|
+
return false;
|
|
113
|
+
selectableIndex++;
|
|
114
|
+
return selectableIndex === selectedIndex;
|
|
115
|
+
});
|
|
116
|
+
const item = items[position];
|
|
117
|
+
if (item != null && isSelectable(item)) {
|
|
118
|
+
setActive(position);
|
|
119
|
+
}
|
|
120
|
+
searchTimeoutRef.current = setTimeout(() => {
|
|
121
|
+
rl.clearLine(0);
|
|
122
|
+
}, 700);
|
|
123
|
+
}
|
|
124
|
+
else if (isBackspaceKey(key)) {
|
|
125
|
+
rl.clearLine(0);
|
|
126
|
+
}
|
|
127
|
+
else if (searchEnabled) {
|
|
128
|
+
const searchTerm = rl.line.toLowerCase();
|
|
129
|
+
const matchIndex = items.findIndex((item) => {
|
|
130
|
+
if (Separator.isSeparator(item) || !isSelectable(item))
|
|
131
|
+
return false;
|
|
132
|
+
return item.name.toLowerCase().startsWith(searchTerm);
|
|
133
|
+
});
|
|
134
|
+
if (matchIndex !== -1) {
|
|
135
|
+
setActive(matchIndex);
|
|
136
|
+
}
|
|
137
|
+
searchTimeoutRef.current = setTimeout(() => {
|
|
138
|
+
rl.clearLine(0);
|
|
139
|
+
}, 700);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
useEffect(() => () => {
|
|
143
|
+
clearTimeout(searchTimeoutRef.current);
|
|
144
|
+
}, []);
|
|
145
|
+
const message = theme.style.message(config.message, status);
|
|
146
|
+
const helpLine = theme.style.keysHelpTip([
|
|
147
|
+
['↑↓', 'navigate'],
|
|
148
|
+
['⏎', 'select'],
|
|
149
|
+
]);
|
|
150
|
+
let separatorCount = 0;
|
|
151
|
+
const page = usePagination({
|
|
152
|
+
items,
|
|
153
|
+
active,
|
|
154
|
+
renderItem({ item, isActive, index }) {
|
|
155
|
+
if (Separator.isSeparator(item)) {
|
|
156
|
+
separatorCount++;
|
|
157
|
+
return ` ${item.separator}`;
|
|
158
|
+
}
|
|
159
|
+
const cursor = isActive ? theme.icon.cursor : ' ';
|
|
160
|
+
const indexLabel = theme.indexMode === 'number' ? `${index + 1 - separatorCount}. ` : '';
|
|
161
|
+
if (item.disabled) {
|
|
162
|
+
const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)';
|
|
163
|
+
const disabledCursor = isActive ? theme.icon.cursor : '-';
|
|
164
|
+
return theme.style.disabled(`${disabledCursor} ${indexLabel}${item.name} ${disabledLabel}`);
|
|
165
|
+
}
|
|
166
|
+
const color = isActive ? theme.style.highlight : (x) => x;
|
|
167
|
+
return color(`${cursor} ${indexLabel}${item.name}`);
|
|
168
|
+
},
|
|
169
|
+
pageSize,
|
|
170
|
+
loop,
|
|
171
|
+
});
|
|
172
|
+
if (status === 'done') {
|
|
173
|
+
return [prefix, message, theme.style.answer(selectedChoice.short)]
|
|
174
|
+
.filter(Boolean)
|
|
175
|
+
.join(' ');
|
|
176
|
+
}
|
|
177
|
+
const { description } = selectedChoice;
|
|
178
|
+
const lines = [
|
|
179
|
+
[prefix, message].filter(Boolean).join(' '),
|
|
180
|
+
page,
|
|
181
|
+
' ',
|
|
182
|
+
description ? theme.style.description(description) : '',
|
|
183
|
+
errorMsg ? theme.style.error(errorMsg) : '',
|
|
184
|
+
helpLine,
|
|
185
|
+
]
|
|
186
|
+
.filter(Boolean)
|
|
187
|
+
.join('\n')
|
|
188
|
+
.trimEnd();
|
|
189
|
+
return `${lines}${cursorHide}`;
|
|
190
|
+
});
|
|
191
|
+
export { Separator } from '@inquirer/core';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/select",
|
|
3
|
-
"version": "5.1.
|
|
3
|
+
"version": "5.1.2",
|
|
4
4
|
"description": "Inquirer select/list prompt",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"answer",
|
|
@@ -67,10 +67,10 @@
|
|
|
67
67
|
"tsc": "tsc"
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@inquirer/ansi": "^2.0.
|
|
71
|
-
"@inquirer/core": "^11.1.
|
|
72
|
-
"@inquirer/figures": "^2.0.
|
|
73
|
-
"@inquirer/type": "^4.0.
|
|
70
|
+
"@inquirer/ansi": "^2.0.4",
|
|
71
|
+
"@inquirer/core": "^11.1.7",
|
|
72
|
+
"@inquirer/figures": "^2.0.4",
|
|
73
|
+
"@inquirer/type": "^4.0.4"
|
|
74
74
|
},
|
|
75
75
|
"devDependencies": {
|
|
76
76
|
"typescript": "^5.9.3"
|
|
@@ -88,5 +88,5 @@
|
|
|
88
88
|
},
|
|
89
89
|
"main": "./dist/index.js",
|
|
90
90
|
"types": "./dist/index.d.ts",
|
|
91
|
-
"gitHead": "
|
|
91
|
+
"gitHead": "b218fcc4afe888a58957aa78c9a032f9bd2d60cb"
|
|
92
92
|
}
|