@reliverse/relinka 1.1.4 → 1.1.6
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-npm/components/anykey/index.js +77 -57
- package/dist-npm/components/block/block.js +61 -48
- package/dist-npm/components/checkbox/index.js +211 -178
- package/dist-npm/components/confirm/confirm-main.js +122 -86
- package/dist-npm/components/confirm/confirm-three.js +27 -24
- package/dist-npm/components/confirm/index.js +45 -36
- package/dist-npm/components/core/Separator.js +17 -15
- package/dist-npm/components/core/create-prompt.js +124 -101
- package/dist-npm/components/core/errors.js +15 -19
- package/dist-npm/components/core/hook-engine.js +120 -91
- package/dist-npm/components/core/index.js +12 -12
- package/dist-npm/components/core/key.js +6 -16
- package/dist-npm/components/core/lines.js +54 -45
- package/dist-npm/components/core/make-theme.js +30 -21
- package/dist-npm/components/core/position.js +23 -27
- package/dist-npm/components/core/promise-polyfill.js +14 -11
- package/dist-npm/components/core/screen-manager.js +74 -59
- package/dist-npm/components/core/theme.js +32 -22
- package/dist-npm/components/core/use-effect.js +15 -9
- package/dist-npm/components/core/use-keypress.js +23 -19
- package/dist-npm/components/core/use-memo.js +16 -10
- package/dist-npm/components/core/use-pagination.js +32 -33
- package/dist-npm/components/core/use-prefix.js +38 -40
- package/dist-npm/components/core/use-ref.js +5 -2
- package/dist-npm/components/core/use-state.js +23 -15
- package/dist-npm/components/core/useKeyPress.js +17 -14
- package/dist-npm/components/core/usePromptState.js +14 -8
- package/dist-npm/components/core/utils.js +16 -7
- package/dist-npm/components/date/date.js +204 -168
- package/dist-npm/components/editor/index.js +92 -71
- package/dist-npm/components/expand/index.js +124 -96
- package/dist-npm/components/figures/index.js +294 -283
- package/dist-npm/components/input/index.js +87 -61
- package/dist-npm/components/input/text-main.js +124 -86
- package/dist-npm/components/input/text.js +28 -24
- package/dist-npm/components/instance/basic.js +25 -17
- package/dist-npm/components/instance/browser.js +18 -14
- package/dist-npm/components/instance/reporter/basic.js +65 -46
- package/dist-npm/components/instance/reporter/browser.js +47 -44
- package/dist-npm/components/instance/reporter/fancy.js +96 -83
- package/dist-npm/components/instance/shared.js +2 -2
- package/dist-npm/components/mono/mono.js +62 -52
- package/dist-npm/components/mono/monoTwo.js +49 -35
- package/dist-npm/components/multiselect/group-multiselect.js +71 -55
- package/dist-npm/components/multiselect/multi-select-two.js +130 -98
- package/dist-npm/components/multiselect/multi-select.js +49 -43
- package/dist-npm/components/multiselect/multiselect-main.js +146 -120
- package/dist-npm/components/multiselect/num-multi-select.js +130 -98
- package/dist-npm/components/multiselect/num-multiselect-main.js +35 -24
- package/dist-npm/components/next-steps/next-steps.js +25 -24
- package/dist-npm/components/number/index.js +112 -78
- package/dist-npm/components/number/number-main.js +2 -96
- package/dist-npm/components/password/index.js +73 -54
- package/dist-npm/components/password/password-main.js +2 -120
- package/dist-npm/components/password/password-three.js +30 -26
- package/dist-npm/components/progressbar/ProgressBar.js +64 -45
- package/dist-npm/components/progressbar/helper.js +40 -33
- package/dist-npm/components/progressbar/index.js +1 -1
- package/dist-npm/components/prompts/create.js +44 -29
- package/dist-npm/components/prompts/index.js +45 -45
- package/dist-npm/components/prompts/prompt.js +260 -211
- package/dist-npm/components/prompts/promptTwo.js +605 -561
- package/dist-npm/components/prompts/relinka.js +295 -237
- package/dist-npm/components/range/range.js +294 -247
- package/dist-npm/components/rawlist/index.js +107 -87
- package/dist-npm/components/results/results.js +37 -31
- package/dist-npm/components/search/index.js +193 -148
- package/dist-npm/components/select/index.js +186 -148
- package/dist-npm/components/select/num-select-main.js +27 -27
- package/dist-npm/components/select/num-select.js +5 -125
- package/dist-npm/components/select/select-key.js +25 -24
- package/dist-npm/components/select/select-main.js +133 -109
- package/dist-npm/components/select/select-three.js +36 -32
- package/dist-npm/components/select/select-two.js +87 -95
- package/dist-npm/components/spinner/index.js +136 -107
- package/dist-npm/components/st-end/end.d.ts +1 -1
- package/dist-npm/components/st-end/end.js +26 -35
- package/dist-npm/components/st-end/start.d.ts +1 -1
- package/dist-npm/components/st-end/start.js +15 -30
- package/dist-npm/components/toggle/index.js +137 -113
- package/dist-npm/components/visual/animate/animate.js +10 -53
- package/dist-npm/components/visual/ascii-art/ascii-art.js +1 -12
- package/dist-npm/mod.js +2 -1
- package/dist-npm/testing/index.js +83 -58
- package/dist-npm/types/general.d.ts +2 -1
- package/dist-npm/types/general.js +1 -0
- package/dist-npm/types/index.js +3 -2
- package/dist-npm/types/keypress.js +36 -35
- package/dist-npm/types/readline.js +1 -0
- package/dist-npm/types/relinka.js +1 -0
- package/dist-npm/types/utils.js +1 -0
- package/dist-npm/utils/box.js +137 -135
- package/dist-npm/utils/color.js +74 -65
- package/dist-npm/utils/colorize.js +156 -124
- package/dist-npm/utils/component.js +657 -532
- package/dist-npm/utils/constants.js +63 -64
- package/dist-npm/utils/core.js +3 -2
- package/dist-npm/utils/decoder.js +223 -244
- package/dist-npm/utils/error.js +9 -4
- package/dist-npm/utils/errors.js +4 -14
- package/dist-npm/utils/format.js +24 -19
- package/dist-npm/utils/keypress.js +414 -316
- package/dist-npm/utils/log.js +15 -11
- package/dist-npm/utils/mapping.js +52 -45
- package/dist-npm/utils/messages.js +183 -183
- package/dist-npm/utils/platforms.js +20 -16
- package/dist-npm/utils/prompt-tmp.js +286 -235
- package/dist-npm/utils/prompt-two.js +286 -235
- package/dist-npm/utils/readline.js +7 -5
- package/dist-npm/utils/skeleton.js +170 -130
- package/dist-npm/utils/stream.js +2 -2
- package/dist-npm/utils/string.js +58 -44
- package/dist-npm/utils/terminal.js +34 -23
- package/dist-npm/utils/tree.js +41 -30
- package/dist-npm/utils/types.js +1 -0
- package/dist-npm/utils/utils.js +8 -8
- package/dist-npm/utils/variants.js +44 -36
- package/package.json +20 -28
|
@@ -1,176 +1,214 @@
|
|
|
1
|
-
import ansiEscapes from
|
|
2
|
-
import colors from
|
|
1
|
+
import ansiEscapes from 'ansi-escapes';
|
|
2
|
+
import colors from 'picocolors';
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} from
|
|
21
|
-
import figures from
|
|
4
|
+
createPrompt,
|
|
5
|
+
useState,
|
|
6
|
+
useKeypress,
|
|
7
|
+
usePrefix,
|
|
8
|
+
usePagination,
|
|
9
|
+
useRef,
|
|
10
|
+
useMemo,
|
|
11
|
+
useEffect,
|
|
12
|
+
isBackspaceKey,
|
|
13
|
+
isEnterKey,
|
|
14
|
+
isUpKey,
|
|
15
|
+
isDownKey,
|
|
16
|
+
isNumberKey,
|
|
17
|
+
Separator,
|
|
18
|
+
ValidationError,
|
|
19
|
+
makeTheme,
|
|
20
|
+
} from '../../components/core/index.js';
|
|
21
|
+
import figures from '../../components/figures/index.js';
|
|
22
|
+
|
|
23
|
+
const isString = (a) => typeof a === 'string';
|
|
24
|
+
|
|
22
25
|
const selectTheme = {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
icon: {
|
|
27
|
+
cursor: figures.pointer,
|
|
28
|
+
},
|
|
29
|
+
style: {
|
|
30
|
+
disabled: (text) => colors.dim(`- ${text}`),
|
|
31
|
+
description: (text) => colors.cyan(text),
|
|
32
|
+
},
|
|
33
|
+
helpMode: 'auto',
|
|
29
34
|
};
|
|
35
|
+
|
|
30
36
|
function isSelectable(item) {
|
|
31
|
-
|
|
37
|
+
return !Separator.isSeparator(item) && !item.disabled;
|
|
32
38
|
}
|
|
39
|
+
|
|
33
40
|
function normalizeChoices(choices) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
41
|
+
return choices.map((choice) => {
|
|
42
|
+
if (Separator.isSeparator(choice)) {
|
|
43
|
+
return choice;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (isString(choice)) {
|
|
47
|
+
return {
|
|
48
|
+
value: choice,
|
|
49
|
+
name: choice,
|
|
50
|
+
short: choice,
|
|
51
|
+
disabled: false,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const name = choice.name ?? String(choice.value);
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
value: choice.value,
|
|
59
|
+
name,
|
|
60
|
+
description: choice.description,
|
|
61
|
+
short: choice.short ?? name,
|
|
62
|
+
disabled: choice.disabled ?? false,
|
|
63
|
+
};
|
|
64
|
+
});
|
|
55
65
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const {
|
|
66
|
+
|
|
67
|
+
const select = createPrompt((config, done) => {
|
|
68
|
+
const {loop = true, pageSize = 7} = config;
|
|
59
69
|
const firstRender = useRef(true);
|
|
60
70
|
const theme = makeTheme(selectTheme, config.theme);
|
|
61
|
-
const [status, setStatus] = useState(
|
|
62
|
-
const prefix = usePrefix({
|
|
71
|
+
const [status, setStatus] = useState('idle');
|
|
72
|
+
const prefix = usePrefix({
|
|
73
|
+
status,
|
|
74
|
+
theme,
|
|
75
|
+
});
|
|
63
76
|
const searchTimeoutRef = useRef();
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
);
|
|
77
|
+
|
|
78
|
+
const items = useMemo(() => normalizeChoices(config.choices), [config.choices]);
|
|
79
|
+
|
|
68
80
|
const bounds = useMemo(() => {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
81
|
+
const first = items.findIndex(isSelectable);
|
|
82
|
+
const last = items.findLastIndex(isSelectable);
|
|
83
|
+
|
|
84
|
+
if (first === -1) {
|
|
85
|
+
throw new ValidationError('[select prompt] No selectable choices. All choices are disabled.');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
first,
|
|
90
|
+
last,
|
|
91
|
+
};
|
|
77
92
|
}, [items]);
|
|
93
|
+
|
|
78
94
|
const defaultItemIndex = useMemo(() => {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
(item) => isSelectable(item) && item.value === config.default
|
|
84
|
-
);
|
|
95
|
+
if (!('default' in config)) {
|
|
96
|
+
return -1;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return items.findIndex((item) => isSelectable(item) && item.value === config.default);
|
|
85
100
|
}, [config.default, items]);
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
101
|
+
|
|
102
|
+
const [active, setActive] = useState(defaultItemIndex === -1 ? bounds.first : defaultItemIndex);
|
|
103
|
+
|
|
89
104
|
const selectedChoice = items[active];
|
|
105
|
+
|
|
90
106
|
useKeypress((key, rl) => {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
107
|
+
clearTimeout(searchTimeoutRef.current);
|
|
108
|
+
if (isEnterKey(key)) {
|
|
109
|
+
setStatus('done');
|
|
110
|
+
done(selectedChoice.value);
|
|
111
|
+
} else if (isUpKey(key) || isDownKey(key)) {
|
|
112
|
+
rl.clearLine(0);
|
|
113
|
+
if (loop || isUpKey(key) && active !== bounds.first || isDownKey(key) && active !== bounds.last) {
|
|
114
|
+
const offset = isUpKey(key) ? -1 : 1;
|
|
115
|
+
let next = active;
|
|
116
|
+
|
|
117
|
+
do {
|
|
118
|
+
next = (next + offset + items.length) % items.length;
|
|
119
|
+
} while (!isSelectable(items[next]))
|
|
120
|
+
setActive(next);
|
|
121
|
+
}
|
|
122
|
+
} else if (isNumberKey(key)) {
|
|
123
|
+
rl.clearLine(0);
|
|
124
|
+
const position = Number(key.name) - 1;
|
|
125
|
+
const item = items[position];
|
|
126
|
+
|
|
127
|
+
if (item != null && isSelectable(item)) {
|
|
128
|
+
setActive(position);
|
|
129
|
+
}
|
|
130
|
+
} else if (isBackspaceKey(key)) {
|
|
131
|
+
rl.clearLine(0);
|
|
132
|
+
} else {
|
|
133
|
+
const searchTerm = rl.line.toLowerCase();
|
|
134
|
+
const matchIndex = items.findIndex((item) => {
|
|
135
|
+
if (Separator.isSeparator(item) || !isSelectable(item)) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return item
|
|
140
|
+
.name
|
|
141
|
+
.toLowerCase()
|
|
142
|
+
.startsWith(searchTerm);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
if (matchIndex !== -1) {
|
|
146
|
+
setActive(matchIndex);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
searchTimeoutRef.current = setTimeout(() => {
|
|
150
|
+
rl.clearLine(0);
|
|
151
|
+
}, 700);
|
|
124
152
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}, 700);
|
|
128
|
-
}
|
|
153
|
+
|
|
154
|
+
|
|
129
155
|
});
|
|
130
|
-
useEffect(
|
|
131
|
-
() => () => {
|
|
156
|
+
useEffect(() => () => {
|
|
132
157
|
clearTimeout(searchTimeoutRef.current);
|
|
133
|
-
|
|
134
|
-
[]
|
|
135
|
-
);
|
|
158
|
+
}, []);
|
|
136
159
|
const message = theme.style.message(config.message, status);
|
|
137
|
-
let helpTipTop =
|
|
138
|
-
let helpTipBottom =
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
160
|
+
let helpTipTop = '';
|
|
161
|
+
let helpTipBottom = '';
|
|
162
|
+
|
|
163
|
+
if (theme.helpMode === 'always' || theme.helpMode === 'auto' && firstRender.current) {
|
|
164
|
+
firstRender.current = false;
|
|
165
|
+
if (items.length > pageSize) {
|
|
166
|
+
helpTipBottom = `
|
|
167
|
+
${theme.style.help('(Use arrow keys to reveal more choices)')}`;
|
|
168
|
+
} else {
|
|
169
|
+
helpTipTop = theme.style.help('(Use arrow keys)');
|
|
170
|
+
}
|
|
147
171
|
}
|
|
172
|
+
|
|
148
173
|
const page = usePagination({
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
174
|
+
items,
|
|
175
|
+
active,
|
|
176
|
+
renderItem({item, isActive}) {
|
|
177
|
+
if (Separator.isSeparator(item)) {
|
|
178
|
+
return ` ${item.separator}`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (item.disabled) {
|
|
182
|
+
const disabledLabel = isString(item.disabled) ? item.disabled : '(disabled)';
|
|
183
|
+
return theme.style.disabled(`${item.name} ${disabledLabel}`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const color = isActive ? theme.style.highlight : (x) => x;
|
|
187
|
+
const cursor = isActive ? theme.icon.cursor : ` `;
|
|
188
|
+
|
|
189
|
+
return color(`${cursor} ${item.name}`);
|
|
190
|
+
},
|
|
191
|
+
pageSize,
|
|
192
|
+
loop,
|
|
165
193
|
});
|
|
166
|
-
|
|
167
|
-
|
|
194
|
+
|
|
195
|
+
if (status === 'done') {
|
|
196
|
+
return `${prefix} ${message} ${theme.style.answer(selectedChoice.short)}`;
|
|
168
197
|
}
|
|
198
|
+
|
|
169
199
|
const choiceDescription = selectedChoice.description ? `
|
|
170
200
|
${theme.style.description(selectedChoice.description)}` : ``;
|
|
171
|
-
|
|
201
|
+
|
|
202
|
+
return `${[
|
|
203
|
+
prefix,
|
|
204
|
+
message,
|
|
205
|
+
helpTipTop,
|
|
206
|
+
]
|
|
207
|
+
.filter(Boolean)
|
|
208
|
+
.join(' ')}
|
|
172
209
|
${page}${helpTipBottom}${choiceDescription}${ansiEscapes.cursorHide}`;
|
|
173
|
-
|
|
174
|
-
|
|
210
|
+
});
|
|
211
|
+
|
|
175
212
|
export default select;
|
|
176
|
-
|
|
213
|
+
|
|
214
|
+
export {Separator} from '../../components/core/index.js';
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const isDefault = option.value === initial ? "(default)" : "";
|
|
13
|
-
console.log(`${prefix} [${index2 + 1}] ${option.label} ${isDefault}`);
|
|
1
|
+
import {
|
|
2
|
+
stdin as input,
|
|
3
|
+
stdout as output,
|
|
4
|
+
} from 'node:process';
|
|
5
|
+
import readline from 'node:readline/promises';
|
|
6
|
+
|
|
7
|
+
export async function selectPrompt({options, initial}) {
|
|
8
|
+
const selectedIndex = initial ? options.findIndex((option) => option.value === initial) : 0;
|
|
9
|
+
const rl = readline.createInterface({
|
|
10
|
+
input,
|
|
11
|
+
output,
|
|
14
12
|
});
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
|
|
14
|
+
while (true) {
|
|
15
|
+
const promptMessage = initial !== void 0 ? `Please select an option by number (default ${selectedIndex + 1}): ` : 'Please select an option by number: ';
|
|
16
|
+
const response = await rl.question(promptMessage);
|
|
17
|
+
let index;
|
|
18
|
+
|
|
19
|
+
if (response.trim() === '' && initial !== void 0) {
|
|
20
|
+
index = selectedIndex;
|
|
21
|
+
} else {
|
|
22
|
+
index = parseInt(response.trim(), 10) - 1;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (index >= 0 && index < options.length) {
|
|
26
|
+
rl.close();
|
|
27
|
+
return options[index].value;
|
|
28
|
+
}
|
|
22
29
|
}
|
|
23
|
-
if (index >= 0 && index < options.length) {
|
|
24
|
-
rl.close();
|
|
25
|
-
return options[index].value;
|
|
26
|
-
} else {
|
|
27
|
-
console.log("Invalid selection. Please try again.");
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
30
|
}
|
|
@@ -1,126 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { colorize } from "../../utils/colorize.js";
|
|
5
|
-
import { fmt, msg, bar } from "../../utils/messages.js";
|
|
6
|
-
import {
|
|
7
|
-
countLines,
|
|
8
|
-
deleteLastLine,
|
|
9
|
-
deleteLastLines
|
|
10
|
-
} from "../../utils/terminal.js";
|
|
11
|
-
export async function numSelectPrompt(options) {
|
|
12
|
-
const {
|
|
13
|
-
title,
|
|
14
|
-
hint,
|
|
15
|
-
validate,
|
|
16
|
-
defaultValue,
|
|
17
|
-
schema,
|
|
18
|
-
titleColor = "cyanBright",
|
|
19
|
-
answerColor = "none",
|
|
20
|
-
titleTypography = "bold",
|
|
21
|
-
titleVariant,
|
|
22
|
-
content,
|
|
23
|
-
contentColor,
|
|
24
|
-
contentTypography,
|
|
25
|
-
contentVariant,
|
|
26
|
-
borderColor = "viceGradient",
|
|
27
|
-
variantOptions,
|
|
28
|
-
inline = true,
|
|
29
|
-
choices
|
|
30
|
-
} = options;
|
|
31
|
-
if (!choices || choices.length === 0) {
|
|
32
|
-
throw new Error("Choices are required for select prompt.");
|
|
33
|
-
}
|
|
34
|
-
const rl = readline.createInterface({ input, output });
|
|
35
|
-
const formattedBar = bar({ borderColor });
|
|
36
|
-
let linesToDelete = 0;
|
|
37
|
-
let errorMessage = "";
|
|
38
|
-
while (true) {
|
|
39
|
-
if (linesToDelete > 0) {
|
|
40
|
-
deleteLastLines(linesToDelete);
|
|
1
|
+
export async function numSelectPrompt({choices}) {
|
|
2
|
+
if (!choices || !choices.length) {
|
|
3
|
+
throw Error('Choices are required for select prompt.');
|
|
41
4
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
title,
|
|
45
|
-
titleColor,
|
|
46
|
-
titleTypography,
|
|
47
|
-
titleVariant,
|
|
48
|
-
content,
|
|
49
|
-
contentColor,
|
|
50
|
-
contentTypography,
|
|
51
|
-
contentVariant,
|
|
52
|
-
borderColor,
|
|
53
|
-
hint,
|
|
54
|
-
variantOptions,
|
|
55
|
-
errorMessage
|
|
56
|
-
});
|
|
57
|
-
let choicesText = "";
|
|
58
|
-
if (inline) {
|
|
59
|
-
choicesText = choices.map(
|
|
60
|
-
(choice, index) => `${index + 1}) ${choice.title}${choice.description ? ` (${choice.description})` : ""}`
|
|
61
|
-
).join(" / ");
|
|
62
|
-
} else {
|
|
63
|
-
choicesText = choices.map(
|
|
64
|
-
(choice, index) => `${index + 1}) ${choice.title}${choice.description ? ` - ${choice.description}` : ""}`
|
|
65
|
-
).join(`
|
|
66
|
-
${formattedBar} `);
|
|
67
|
-
}
|
|
68
|
-
const formattedPrompt = fmt({
|
|
69
|
-
type: "M_NULL",
|
|
70
|
-
title: `${question}${choicesText}
|
|
71
|
-
${formattedBar} ${colorize(
|
|
72
|
-
`Enter your choice:`,
|
|
73
|
-
contentColor
|
|
74
|
-
)}
|
|
75
|
-
${formattedBar} `
|
|
76
|
-
});
|
|
77
|
-
const questionLines = countLines(formattedPrompt);
|
|
78
|
-
linesToDelete = questionLines + 1;
|
|
79
|
-
const prompt = await rl.question(formattedPrompt);
|
|
80
|
-
const answer = prompt.trim() || defaultValue;
|
|
81
|
-
if (!prompt.trim() && defaultValue !== void 0) {
|
|
82
|
-
deleteLastLine();
|
|
83
|
-
msg({
|
|
84
|
-
type: "M_MIDDLE",
|
|
85
|
-
title: ` ${defaultValue}`,
|
|
86
|
-
titleColor: answerColor
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
const num = Number(answer);
|
|
90
|
-
if (isNaN(num) || num < 1 || num > choices.length) {
|
|
91
|
-
errorMessage = `Please enter a number between 1 and ${choices.length}.`;
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
const selectedChoice = choices[num - 1];
|
|
95
|
-
const selectedValue = selectedChoice?.id ?? num;
|
|
96
|
-
let isValid = true;
|
|
97
|
-
errorMessage = "";
|
|
98
|
-
if (schema) {
|
|
99
|
-
isValid = Value.Check(schema, selectedValue);
|
|
100
|
-
if (!isValid) {
|
|
101
|
-
const errors = [...Value.Errors(schema, selectedValue)];
|
|
102
|
-
if (errors.length > 0) {
|
|
103
|
-
errorMessage = errors[0]?.message ?? "Invalid input.";
|
|
104
|
-
} else {
|
|
105
|
-
errorMessage = "Invalid input.";
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
if (validate && isValid) {
|
|
110
|
-
const validation = await validate(selectedValue);
|
|
111
|
-
if (validation !== true) {
|
|
112
|
-
isValid = false;
|
|
113
|
-
errorMessage = typeof validation === "string" ? validation : "Invalid input.";
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (isValid) {
|
|
117
|
-
msg({ type: "M_NEWLINE" });
|
|
118
|
-
rl.close();
|
|
119
|
-
if (selectedChoice?.action) {
|
|
120
|
-
await selectedChoice.action();
|
|
121
|
-
}
|
|
122
|
-
return selectedValue;
|
|
123
|
-
} else {
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
5
|
+
|
|
6
|
+
while (true) {}}
|
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
import Prompt from
|
|
1
|
+
import Prompt from '../prompts/prompt.js';
|
|
2
|
+
|
|
2
3
|
export default class SelectKeyPrompt extends Prompt {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
4
|
+
options;
|
|
5
|
+
cursor = 0;
|
|
6
|
+
constructor(opts) {
|
|
7
|
+
super(opts, false);
|
|
8
|
+
this.options = opts.options;
|
|
9
|
+
|
|
10
|
+
const keys = this.options.map(({value: [initial]}) => initial?.toLowerCase());
|
|
11
|
+
|
|
12
|
+
this.cursor = Math.max(keys.indexOf(opts.initialValue), 0);
|
|
13
|
+
this.on('key', (key) => {
|
|
14
|
+
if (!keys.includes(key)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const value = this.options.find(({value: [initial]}) => key === initial?.toLowerCase());
|
|
19
|
+
|
|
20
|
+
if (value) {
|
|
21
|
+
this.value = value.value;
|
|
22
|
+
this.state = 'submit';
|
|
23
|
+
this.emit('submit');
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
26
27
|
}
|