@inquirer/rawlist 4.1.10 → 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/dist/index.d.ts +16 -0
- package/dist/index.js +125 -0
- package/package.json +14 -31
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Separator, type Theme } from '@inquirer/core';
|
|
2
|
+
import type { PartialDeep } from '@inquirer/type';
|
|
3
|
+
type Choice<Value> = {
|
|
4
|
+
value: Value;
|
|
5
|
+
name?: string;
|
|
6
|
+
short?: string;
|
|
7
|
+
key?: string;
|
|
8
|
+
};
|
|
9
|
+
declare const _default: <Value>(config: {
|
|
10
|
+
message: string;
|
|
11
|
+
choices: readonly (string | Separator)[] | readonly (Separator | Choice<Value>)[];
|
|
12
|
+
loop?: boolean | undefined;
|
|
13
|
+
theme?: PartialDeep<Theme> | undefined;
|
|
14
|
+
}, context?: import("@inquirer/type").Context) => Promise<Value>;
|
|
15
|
+
export default _default;
|
|
16
|
+
export { Separator } from '@inquirer/core';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { createPrompt, useMemo, useState, useKeypress, usePrefix, isDownKey, isEnterKey, isUpKey, Separator, makeTheme, ValidationError, } from '@inquirer/core';
|
|
2
|
+
import { styleText } from 'node:util';
|
|
3
|
+
const numberRegex = /\d+/;
|
|
4
|
+
function isSelectableChoice(choice) {
|
|
5
|
+
return choice != null && !Separator.isSeparator(choice);
|
|
6
|
+
}
|
|
7
|
+
function normalizeChoices(choices) {
|
|
8
|
+
let index = 0;
|
|
9
|
+
return choices.map((choice) => {
|
|
10
|
+
if (Separator.isSeparator(choice))
|
|
11
|
+
return choice;
|
|
12
|
+
index += 1;
|
|
13
|
+
if (typeof choice === 'string') {
|
|
14
|
+
return {
|
|
15
|
+
value: choice,
|
|
16
|
+
name: choice,
|
|
17
|
+
short: choice,
|
|
18
|
+
key: String(index),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const name = choice.name ?? String(choice.value);
|
|
22
|
+
return {
|
|
23
|
+
value: choice.value,
|
|
24
|
+
name,
|
|
25
|
+
short: choice.short ?? name,
|
|
26
|
+
key: choice.key ?? String(index),
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function getSelectedChoice(input, choices) {
|
|
31
|
+
let selectedChoice;
|
|
32
|
+
const selectableChoices = choices.filter(isSelectableChoice);
|
|
33
|
+
// First, try to match by custom key (exact match)
|
|
34
|
+
selectedChoice = selectableChoices.find((choice) => choice.key === input);
|
|
35
|
+
// If no custom key match and input is numeric, try 1-based index
|
|
36
|
+
if (!selectedChoice && numberRegex.test(input)) {
|
|
37
|
+
const answer = Number.parseInt(input, 10) - 1;
|
|
38
|
+
selectedChoice = selectableChoices[answer];
|
|
39
|
+
}
|
|
40
|
+
return selectedChoice
|
|
41
|
+
? [selectedChoice, choices.indexOf(selectedChoice)]
|
|
42
|
+
: [undefined, undefined];
|
|
43
|
+
}
|
|
44
|
+
export default createPrompt((config, done) => {
|
|
45
|
+
const { loop = true } = config;
|
|
46
|
+
const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
|
|
47
|
+
const [status, setStatus] = useState('idle');
|
|
48
|
+
const [value, setValue] = useState('');
|
|
49
|
+
const [errorMsg, setError] = useState();
|
|
50
|
+
const theme = makeTheme(config.theme);
|
|
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]);
|
|
60
|
+
useKeypress((key, rl) => {
|
|
61
|
+
if (isEnterKey(key)) {
|
|
62
|
+
const [selectedChoice] = getSelectedChoice(value, choices);
|
|
63
|
+
if (isSelectableChoice(selectedChoice)) {
|
|
64
|
+
setValue(selectedChoice.short);
|
|
65
|
+
setStatus('done');
|
|
66
|
+
done(selectedChoice.value);
|
|
67
|
+
}
|
|
68
|
+
else if (value === '') {
|
|
69
|
+
setError('Please input a value');
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
setError(`"${styleText('red', value)}" isn't an available option`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else if (isUpKey(key) || isDownKey(key)) {
|
|
76
|
+
rl.clearLine(0);
|
|
77
|
+
const [selectedChoice, active] = getSelectedChoice(value, choices);
|
|
78
|
+
if (!selectedChoice) {
|
|
79
|
+
const firstChoice = isDownKey(key)
|
|
80
|
+
? choices.find(isSelectableChoice)
|
|
81
|
+
: choices.findLast(isSelectableChoice);
|
|
82
|
+
setValue(firstChoice.key);
|
|
83
|
+
}
|
|
84
|
+
else if (loop ||
|
|
85
|
+
(isUpKey(key) && active !== bounds.first) ||
|
|
86
|
+
(isDownKey(key) && active !== bounds.last)) {
|
|
87
|
+
const offset = isUpKey(key) ? -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
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
setValue(rl.line);
|
|
97
|
+
setError(undefined);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
const message = theme.style.message(config.message, status);
|
|
101
|
+
if (status === 'done') {
|
|
102
|
+
return `${prefix} ${message} ${theme.style.answer(value)}`;
|
|
103
|
+
}
|
|
104
|
+
const choicesStr = choices
|
|
105
|
+
.map((choice) => {
|
|
106
|
+
if (Separator.isSeparator(choice)) {
|
|
107
|
+
return ` ${choice.separator}`;
|
|
108
|
+
}
|
|
109
|
+
const line = ` ${choice.key}) ${choice.name}`;
|
|
110
|
+
if (choice.key === value.toLowerCase()) {
|
|
111
|
+
return theme.style.highlight(line);
|
|
112
|
+
}
|
|
113
|
+
return line;
|
|
114
|
+
})
|
|
115
|
+
.join('\n');
|
|
116
|
+
let error = '';
|
|
117
|
+
if (errorMsg) {
|
|
118
|
+
error = theme.style.error(errorMsg);
|
|
119
|
+
}
|
|
120
|
+
return [
|
|
121
|
+
`${prefix} ${message} ${value}`,
|
|
122
|
+
[choicesStr, error].filter(Boolean).join('\n'),
|
|
123
|
+
];
|
|
124
|
+
});
|
|
125
|
+
export { Separator } from '@inquirer/core';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/rawlist",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Inquirer rawlist prompt",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"answer",
|
|
@@ -52,51 +52,34 @@
|
|
|
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
|
-
"
|
|
74
|
-
"tsc": "tshy"
|
|
61
|
+
"tsc": "tsc"
|
|
75
62
|
},
|
|
76
63
|
"dependencies": {
|
|
77
|
-
"@inquirer/core": "^
|
|
78
|
-
"@inquirer/type": "^
|
|
79
|
-
"yoctocolors-cjs": "^2.1.3"
|
|
64
|
+
"@inquirer/core": "^11.0.0",
|
|
65
|
+
"@inquirer/type": "^4.0.0"
|
|
80
66
|
},
|
|
81
67
|
"devDependencies": {
|
|
82
|
-
"@
|
|
83
|
-
"@inquirer/testing": "^2.1.52",
|
|
68
|
+
"@inquirer/testing": "^3.0.0",
|
|
84
69
|
"@repo/tsconfig": "0.0.0",
|
|
85
|
-
"
|
|
70
|
+
"typescript": "^5.9.3"
|
|
86
71
|
},
|
|
87
72
|
"engines": {
|
|
88
|
-
"node": ">=
|
|
73
|
+
"node": ">=23.5.0 || ^22.13.0 || ^21.7.0 || ^20.12.0"
|
|
89
74
|
},
|
|
90
75
|
"publishConfig": {
|
|
91
|
-
"access": "public"
|
|
92
|
-
},
|
|
93
|
-
"tshy": {
|
|
94
|
-
"exclude": [
|
|
95
|
-
"src/**/*.test.ts"
|
|
96
|
-
],
|
|
76
|
+
"access": "public",
|
|
97
77
|
"exports": {
|
|
98
78
|
"./package.json": "./package.json",
|
|
99
|
-
".":
|
|
79
|
+
".": {
|
|
80
|
+
"types": "./dist/index.d.ts",
|
|
81
|
+
"default": "./dist/index.js"
|
|
82
|
+
}
|
|
100
83
|
}
|
|
101
84
|
},
|
|
102
85
|
"peerDependencies": {
|
|
@@ -107,5 +90,5 @@
|
|
|
107
90
|
"optional": true
|
|
108
91
|
}
|
|
109
92
|
},
|
|
110
|
-
"gitHead": "
|
|
93
|
+
"gitHead": "676685d33374a30340c1b9f0831c7eae2b2357dd"
|
|
111
94
|
}
|