@inquirer/select 4.4.1 → 5.0.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 CHANGED
@@ -152,7 +152,7 @@ type Theme = {
152
152
 
153
153
  This function allows you to customize the keyboard shortcuts help tip displayed below the prompt. It receives an array of key-action pairs and should return a formatted string. You can also hook here to localize the labels to different languages.
154
154
 
155
- It can also returns `undefined` to hide the help tip entirely. This is the replacement for the deprecated theme option `helpMode: 'never'`.
155
+ It can also returns `undefined` to hide the help tip entirely.
156
156
 
157
157
  ```js
158
158
  theme: {
@@ -0,0 +1,32 @@
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
+ indexMode: 'hidden' | 'number';
13
+ keybindings: ReadonlyArray<Keybinding>;
14
+ };
15
+ type Choice<Value> = {
16
+ value: Value;
17
+ name?: string;
18
+ description?: string;
19
+ short?: string;
20
+ disabled?: boolean | string;
21
+ type?: never;
22
+ };
23
+ declare const _default: <Value>(config: {
24
+ message: string;
25
+ choices: readonly (string | Separator)[] | readonly (Separator | Choice<Value>)[];
26
+ pageSize?: number | undefined;
27
+ loop?: boolean | undefined;
28
+ default?: unknown;
29
+ theme?: PartialDeep<Theme<SelectTheme>> | undefined;
30
+ }, context?: import("@inquirer/type").Context) => Promise<Value>;
31
+ export default _default;
32
+ export { Separator } from '@inquirer/core';
package/dist/index.js ADDED
@@ -0,0 +1,174 @@
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
+ indexMode: 'hidden',
15
+ keybindings: [],
16
+ };
17
+ function isSelectable(item) {
18
+ return !Separator.isSeparator(item) && !item.disabled;
19
+ }
20
+ function normalizeChoices(choices) {
21
+ return choices.map((choice) => {
22
+ if (Separator.isSeparator(choice))
23
+ return choice;
24
+ if (typeof choice === 'string') {
25
+ return {
26
+ value: choice,
27
+ name: choice,
28
+ short: choice,
29
+ disabled: false,
30
+ };
31
+ }
32
+ const name = choice.name ?? String(choice.value);
33
+ const normalizedChoice = {
34
+ value: choice.value,
35
+ name,
36
+ short: choice.short ?? name,
37
+ disabled: choice.disabled ?? false,
38
+ };
39
+ if (choice.description) {
40
+ normalizedChoice.description = choice.description;
41
+ }
42
+ return normalizedChoice;
43
+ });
44
+ }
45
+ export default createPrompt((config, done) => {
46
+ const { loop = true, pageSize = 7 } = config;
47
+ const theme = makeTheme(selectTheme, config.theme);
48
+ const { keybindings } = theme;
49
+ const [status, setStatus] = useState('idle');
50
+ const prefix = usePrefix({ status, theme });
51
+ const searchTimeoutRef = useRef();
52
+ // Vim keybindings (j/k) conflict with typing those letters in search,
53
+ // so search must be disabled when vim bindings are enabled
54
+ const searchEnabled = !keybindings.includes('vim');
55
+ const items = useMemo(() => normalizeChoices(config.choices), [config.choices]);
56
+ const bounds = useMemo(() => {
57
+ const first = items.findIndex(isSelectable);
58
+ const last = items.findLastIndex(isSelectable);
59
+ if (first === -1) {
60
+ throw new ValidationError('[select prompt] No selectable choices. All choices are disabled.');
61
+ }
62
+ return { first, last };
63
+ }, [items]);
64
+ const defaultItemIndex = useMemo(() => {
65
+ if (!('default' in config))
66
+ return -1;
67
+ return items.findIndex((item) => isSelectable(item) && item.value === config.default);
68
+ }, [config.default, items]);
69
+ const [active, setActive] = useState(defaultItemIndex === -1 ? bounds.first : defaultItemIndex);
70
+ // Safe to assume the cursor position always point to a Choice.
71
+ const selectedChoice = items[active];
72
+ useKeypress((key, rl) => {
73
+ clearTimeout(searchTimeoutRef.current);
74
+ if (isEnterKey(key)) {
75
+ setStatus('done');
76
+ done(selectedChoice.value);
77
+ }
78
+ else if (isUpKey(key, keybindings) || isDownKey(key, keybindings)) {
79
+ rl.clearLine(0);
80
+ if (loop ||
81
+ (isUpKey(key, keybindings) && active !== bounds.first) ||
82
+ (isDownKey(key, keybindings) && active !== bounds.last)) {
83
+ const offset = isUpKey(key, keybindings) ? -1 : 1;
84
+ let next = active;
85
+ do {
86
+ next = (next + offset + items.length) % items.length;
87
+ } while (!isSelectable(items[next]));
88
+ setActive(next);
89
+ }
90
+ }
91
+ else if (isNumberKey(key) && !Number.isNaN(Number(rl.line))) {
92
+ const selectedIndex = Number(rl.line) - 1;
93
+ // Find the nth item (ignoring separators)
94
+ let selectableIndex = -1;
95
+ const position = items.findIndex((item) => {
96
+ if (Separator.isSeparator(item))
97
+ return false;
98
+ selectableIndex++;
99
+ return selectableIndex === selectedIndex;
100
+ });
101
+ const item = items[position];
102
+ if (item != null && isSelectable(item)) {
103
+ setActive(position);
104
+ }
105
+ searchTimeoutRef.current = setTimeout(() => {
106
+ rl.clearLine(0);
107
+ }, 700);
108
+ }
109
+ else if (isBackspaceKey(key)) {
110
+ rl.clearLine(0);
111
+ }
112
+ else if (searchEnabled) {
113
+ const searchTerm = rl.line.toLowerCase();
114
+ const matchIndex = items.findIndex((item) => {
115
+ if (Separator.isSeparator(item) || !isSelectable(item))
116
+ return false;
117
+ return item.name.toLowerCase().startsWith(searchTerm);
118
+ });
119
+ if (matchIndex !== -1) {
120
+ setActive(matchIndex);
121
+ }
122
+ searchTimeoutRef.current = setTimeout(() => {
123
+ rl.clearLine(0);
124
+ }, 700);
125
+ }
126
+ });
127
+ useEffect(() => () => {
128
+ clearTimeout(searchTimeoutRef.current);
129
+ }, []);
130
+ const message = theme.style.message(config.message, status);
131
+ const helpLine = theme.style.keysHelpTip([
132
+ ['↑↓', 'navigate'],
133
+ ['⏎', 'select'],
134
+ ]);
135
+ let separatorCount = 0;
136
+ const page = usePagination({
137
+ items,
138
+ active,
139
+ renderItem({ item, isActive, index }) {
140
+ if (Separator.isSeparator(item)) {
141
+ separatorCount++;
142
+ return ` ${item.separator}`;
143
+ }
144
+ const indexLabel = theme.indexMode === 'number' ? `${index + 1 - separatorCount}. ` : '';
145
+ if (item.disabled) {
146
+ const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)';
147
+ return theme.style.disabled(`${indexLabel}${item.name} ${disabledLabel}`);
148
+ }
149
+ const color = isActive ? theme.style.highlight : (x) => x;
150
+ const cursor = isActive ? theme.icon.cursor : ` `;
151
+ return color(`${cursor} ${indexLabel}${item.name}`);
152
+ },
153
+ pageSize,
154
+ loop,
155
+ });
156
+ if (status === 'done') {
157
+ return [prefix, message, theme.style.answer(selectedChoice.short)]
158
+ .filter(Boolean)
159
+ .join(' ');
160
+ }
161
+ const { description } = selectedChoice;
162
+ const lines = [
163
+ [prefix, message].filter(Boolean).join(' '),
164
+ page,
165
+ ' ',
166
+ description ? theme.style.description(description) : '',
167
+ helpLine,
168
+ ]
169
+ .filter(Boolean)
170
+ .join('\n')
171
+ .trimEnd();
172
+ return `${lines}${cursorHide}`;
173
+ });
174
+ export { Separator } from '@inquirer/core';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/select",
3
- "version": "4.4.1",
3
+ "version": "5.0.0",
4
4
  "description": "Inquirer select/list prompt",
5
5
  "keywords": [
6
6
  "answer",
@@ -52,53 +52,36 @@
52
52
  "type": "module",
53
53
  "exports": {
54
54
  "./package.json": "./package.json",
55
- ".": {
56
- "import": {
57
- "types": "./dist/esm/index.d.ts",
58
- "default": "./dist/esm/index.js"
59
- },
60
- "require": {
61
- "types": "./dist/commonjs/index.d.ts",
62
- "default": "./dist/commonjs/index.js"
63
- }
64
- }
55
+ ".": "./src/index.ts"
65
56
  },
66
- "main": "./dist/commonjs/index.js",
67
- "module": "./dist/esm/index.js",
68
- "types": "./dist/commonjs/index.d.ts",
69
57
  "files": [
70
58
  "dist"
71
59
  ],
72
60
  "scripts": {
73
- "attw": "attw --pack",
74
- "tsc": "tshy"
61
+ "tsc": "tsc"
75
62
  },
76
63
  "dependencies": {
77
- "@inquirer/ansi": "^1.0.2",
78
- "@inquirer/core": "^10.3.1",
79
- "@inquirer/figures": "^1.0.15",
80
- "@inquirer/type": "^3.0.10",
81
- "yoctocolors-cjs": "^2.1.3"
64
+ "@inquirer/ansi": "^2.0.0",
65
+ "@inquirer/core": "^11.0.0",
66
+ "@inquirer/figures": "^2.0.0",
67
+ "@inquirer/type": "^4.0.0"
82
68
  },
83
69
  "devDependencies": {
84
- "@arethetypeswrong/cli": "^0.18.2",
85
- "@inquirer/testing": "^2.1.52",
70
+ "@inquirer/testing": "^3.0.0",
86
71
  "@repo/tsconfig": "0.0.0",
87
- "tshy": "^3.0.3"
72
+ "typescript": "^5.9.3"
88
73
  },
89
74
  "engines": {
90
- "node": ">=18"
75
+ "node": ">=23.5.0 || ^22.13.0 || ^21.7.0 || ^20.12.0"
91
76
  },
92
77
  "publishConfig": {
93
- "access": "public"
94
- },
95
- "tshy": {
96
- "exclude": [
97
- "src/**/*.test.ts"
98
- ],
78
+ "access": "public",
99
79
  "exports": {
100
80
  "./package.json": "./package.json",
101
- ".": "./src/index.ts"
81
+ ".": {
82
+ "types": "./dist/index.d.ts",
83
+ "default": "./dist/index.js"
84
+ }
102
85
  }
103
86
  },
104
87
  "peerDependencies": {
@@ -109,5 +92,5 @@
109
92
  "optional": true
110
93
  }
111
94
  },
112
- "gitHead": "6881993e517e76fa891b72e1f5086fd11f7676ac"
95
+ "gitHead": "676685d33374a30340c1b9f0831c7eae2b2357dd"
113
96
  }