@reliverse/rempts 1.7.52 → 1.7.53
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/bin/libs/animate/animate-mod.d.ts +14 -0
- package/bin/libs/animate/animate-mod.js +60 -0
- package/bin/libs/anykey/anykey-mod.d.ts +12 -0
- package/bin/libs/anykey/anykey-mod.js +125 -0
- package/bin/libs/cancel/cancel.d.ts +45 -0
- package/bin/libs/cancel/cancel.js +72 -0
- package/bin/libs/confirm/confirm-alias.d.ts +2 -0
- package/bin/libs/confirm/confirm-alias.js +2 -0
- package/bin/libs/confirm/confirm-mod.d.ts +5 -0
- package/bin/libs/confirm/confirm-mod.js +179 -0
- package/bin/libs/date/date.d.ts +2 -0
- package/bin/libs/date/date.js +214 -0
- package/bin/libs/editor/editor-mod.d.ts +25 -0
- package/bin/libs/editor/editor-mod.js +1133 -0
- package/bin/libs/figures/figures-mod.d.ts +461 -0
- package/bin/libs/figures/figures-mod.js +285 -0
- package/bin/libs/group/group-mod.d.ts +33 -0
- package/bin/libs/group/group-mod.js +89 -0
- package/bin/libs/input/input-alias.d.ts +5 -0
- package/bin/libs/input/input-alias.js +4 -0
- package/bin/libs/input/input-mod.d.ts +16 -0
- package/bin/libs/input/input-mod.js +372 -0
- package/bin/libs/intro/intro-alias.d.ts +3 -0
- package/bin/libs/intro/intro-alias.js +3 -0
- package/bin/libs/intro/intro-mod.d.ts +20 -0
- package/bin/libs/intro/intro-mod.js +77 -0
- package/bin/libs/launcher/command-runner.d.ts +31 -0
- package/bin/libs/launcher/command-runner.js +229 -0
- package/bin/libs/launcher/launcher-alias.d.ts +2 -0
- package/bin/libs/launcher/launcher-alias.js +2 -0
- package/bin/libs/launcher/launcher-mod.d.ts +66 -0
- package/bin/libs/launcher/launcher-mod.js +975 -0
- package/bin/libs/launcher/launcher-types.d.ts +176 -0
- package/bin/libs/launcher/launcher-types.js +0 -0
- package/bin/libs/log/log-alias.d.ts +1 -0
- package/bin/libs/log/log-alias.js +2 -0
- package/bin/libs/msg-fmt/colors.d.ts +30 -0
- package/bin/libs/msg-fmt/colors.js +42 -0
- package/bin/libs/msg-fmt/logger.d.ts +17 -0
- package/bin/libs/msg-fmt/logger.js +103 -0
- package/bin/libs/msg-fmt/mapping.d.ts +3 -0
- package/bin/libs/msg-fmt/mapping.js +41 -0
- package/bin/libs/msg-fmt/messages.d.ts +35 -0
- package/bin/libs/msg-fmt/messages.js +305 -0
- package/bin/libs/msg-fmt/terminal.d.ts +15 -0
- package/bin/libs/msg-fmt/terminal.js +60 -0
- package/bin/libs/msg-fmt/variants.d.ts +11 -0
- package/bin/libs/msg-fmt/variants.js +52 -0
- package/bin/libs/multiselect/multiselect-alias.d.ts +2 -0
- package/bin/libs/multiselect/multiselect-alias.js +2 -0
- package/bin/libs/multiselect/multiselect-prompt.d.ts +2 -0
- package/bin/libs/multiselect/multiselect-prompt.js +340 -0
- package/bin/libs/next-steps/next-steps.d.ts +13 -0
- package/bin/libs/next-steps/next-steps.js +24 -0
- package/bin/libs/number/number-mod.d.ts +28 -0
- package/bin/libs/number/number-mod.js +194 -0
- package/bin/libs/outro/outro-alias.d.ts +3 -0
- package/bin/libs/outro/outro-alias.js +3 -0
- package/bin/libs/outro/outro-mod.d.ts +8 -0
- package/bin/libs/outro/outro-mod.js +55 -0
- package/bin/libs/reliarg/reliarg-mod.d.ts +76 -0
- package/bin/libs/reliarg/reliarg-mod.js +276 -0
- package/bin/libs/results/results.d.ts +7 -0
- package/bin/libs/results/results.js +27 -0
- package/bin/libs/select/nummultiselect-prompt.d.ts +6 -0
- package/bin/libs/select/nummultiselect-prompt.js +101 -0
- package/bin/libs/select/numselect-prompt.d.ts +7 -0
- package/bin/libs/select/numselect-prompt.js +111 -0
- package/bin/libs/select/select-alias.d.ts +9 -0
- package/bin/libs/select/select-alias.js +9 -0
- package/bin/libs/select/select-prompt.d.ts +5 -0
- package/bin/libs/select/select-prompt.js +308 -0
- package/bin/libs/select/toggle-prompt.d.ts +5 -0
- package/bin/libs/select/toggle-prompt.js +207 -0
- package/bin/libs/spinner/spinner-alias.d.ts +2 -0
- package/bin/libs/spinner/spinner-alias.js +2 -0
- package/bin/libs/spinner/spinner-mod.d.ts +106 -0
- package/bin/libs/spinner/spinner-mod.js +255 -0
- package/bin/libs/task/progress.d.ts +2 -0
- package/bin/libs/task/progress.js +57 -0
- package/bin/libs/task/task-spin.d.ts +15 -0
- package/bin/libs/task/task-spin.js +106 -0
- package/bin/libs/utils/colorize.d.ts +2 -0
- package/bin/libs/utils/colorize.js +122 -0
- package/bin/libs/utils/errors.d.ts +1 -0
- package/bin/libs/utils/errors.js +17 -0
- package/bin/libs/utils/prevent.d.ts +8 -0
- package/bin/libs/utils/prevent.js +62 -0
- package/bin/libs/utils/prompt-end.d.ts +8 -0
- package/bin/libs/utils/prompt-end.js +34 -0
- package/bin/libs/utils/stream-text.d.ts +18 -0
- package/bin/libs/utils/stream-text.js +137 -0
- package/bin/libs/utils/system.d.ts +6 -0
- package/bin/libs/utils/system.js +7 -0
- package/bin/libs/utils/validate.d.ts +21 -0
- package/bin/libs/utils/validate.js +17 -0
- package/bin/libs/visual/visual-mod.d.ts +6 -0
- package/bin/libs/visual/visual-mod.js +13 -0
- package/bin/mod.d.ts +53 -0
- package/bin/mod.js +107 -0
- package/bin/types.d.ts +371 -0
- package/bin/types.js +0 -0
- package/package.json +34 -59
- package/dist-npm/bin/mod.d.mts +0 -1753
- package/dist-npm/bin/mod.mjs +0 -6779
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { stdin as input, stdout as output } from "node:process";
|
|
2
|
+
import readline from "node:readline";
|
|
3
|
+
import { re } from "@reliverse/relico";
|
|
4
|
+
import { relinka } from "@reliverse/relinka";
|
|
5
|
+
import { msg, symbols } from "../msg-fmt/messages.js";
|
|
6
|
+
import { deleteLastLine } from "../msg-fmt/terminal.js";
|
|
7
|
+
import { completePrompt } from "../utils/prompt-end.js";
|
|
8
|
+
import { streamText } from "../utils/stream-text.js";
|
|
9
|
+
function isSelectOption(option) {
|
|
10
|
+
return !("separator" in option);
|
|
11
|
+
}
|
|
12
|
+
async function renderPromptUI(params) {
|
|
13
|
+
const {
|
|
14
|
+
title,
|
|
15
|
+
content,
|
|
16
|
+
options,
|
|
17
|
+
selectedIndex,
|
|
18
|
+
errorMessage,
|
|
19
|
+
displayInstructions,
|
|
20
|
+
allDisabled,
|
|
21
|
+
instructions,
|
|
22
|
+
titleColor,
|
|
23
|
+
contentColor,
|
|
24
|
+
contentTypography,
|
|
25
|
+
debug,
|
|
26
|
+
titleVariant,
|
|
27
|
+
titleTypography,
|
|
28
|
+
isRerender = false,
|
|
29
|
+
shouldStream = false,
|
|
30
|
+
streamDelay = 20
|
|
31
|
+
} = params;
|
|
32
|
+
if (!isRerender) {
|
|
33
|
+
if (shouldStream) {
|
|
34
|
+
deleteLastLine();
|
|
35
|
+
msg({ type: "M_BAR", title: "" });
|
|
36
|
+
await streamText({
|
|
37
|
+
text: title,
|
|
38
|
+
delay: streamDelay,
|
|
39
|
+
color: titleColor,
|
|
40
|
+
newline: false,
|
|
41
|
+
clearLine: true
|
|
42
|
+
});
|
|
43
|
+
process.stdout.write("\r");
|
|
44
|
+
msg({
|
|
45
|
+
type: "M_GENERAL",
|
|
46
|
+
title,
|
|
47
|
+
titleColor,
|
|
48
|
+
titleVariant,
|
|
49
|
+
titleTypography
|
|
50
|
+
});
|
|
51
|
+
if (content) {
|
|
52
|
+
msg({
|
|
53
|
+
type: "M_NULL",
|
|
54
|
+
content,
|
|
55
|
+
contentColor,
|
|
56
|
+
contentTypography
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
msg({ type: "M_GENERAL", title, titleColor });
|
|
61
|
+
if (content) {
|
|
62
|
+
msg({
|
|
63
|
+
type: "M_NULL",
|
|
64
|
+
content,
|
|
65
|
+
contentColor,
|
|
66
|
+
contentTypography
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
let uiLineCount = 0;
|
|
72
|
+
if (errorMessage) {
|
|
73
|
+
msg({
|
|
74
|
+
type: "M_ERROR",
|
|
75
|
+
title: `${re.redBright(symbols.step_error)} ${re.redBright(errorMessage)}`
|
|
76
|
+
});
|
|
77
|
+
uiLineCount++;
|
|
78
|
+
} else if (allDisabled) {
|
|
79
|
+
msg({ type: "M_ERROR", title: re.redBright("All options are disabled.") });
|
|
80
|
+
uiLineCount++;
|
|
81
|
+
} else if (displayInstructions && !isRerender) {
|
|
82
|
+
msg({ type: "M_NULL", title: re.blue(instructions) });
|
|
83
|
+
uiLineCount++;
|
|
84
|
+
}
|
|
85
|
+
for (let index = 0; index < options.length; index++) {
|
|
86
|
+
const option = options[index];
|
|
87
|
+
if (!option) continue;
|
|
88
|
+
if (!isSelectOption(option)) {
|
|
89
|
+
const width = option.width ?? 20;
|
|
90
|
+
const symbolKey = option.symbol ?? "line";
|
|
91
|
+
const lineSymbol = symbolKey in symbols ? symbols[symbolKey] : "\u2500";
|
|
92
|
+
msg({ type: "M_NULL", title: re.dim(lineSymbol.repeat(width)) });
|
|
93
|
+
uiLineCount++;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
const isSelected = index === selectedIndex;
|
|
97
|
+
const isDisabled = option.disabled;
|
|
98
|
+
const prefix = isSelected ? re.dim(re.reset("> ")) : " ";
|
|
99
|
+
const labelColor = isDisabled ? re.dim(re.reset(option.label)) : isSelected ? re.reset(re.yellow(option.label)) : re.reset(option.label);
|
|
100
|
+
const hint = option.hint ? re.reset(` (${isDisabled ? re.dim(option.hint) : re.italic(re.dim(option.hint))})`) : "";
|
|
101
|
+
msg({ type: "M_NULL", title: `${prefix}${labelColor}${hint}` });
|
|
102
|
+
uiLineCount++;
|
|
103
|
+
}
|
|
104
|
+
if (debug) {
|
|
105
|
+
relinka("log", "", { optionsCount: options.length });
|
|
106
|
+
}
|
|
107
|
+
return uiLineCount;
|
|
108
|
+
}
|
|
109
|
+
export async function selectPrompt(params) {
|
|
110
|
+
const {
|
|
111
|
+
title = "",
|
|
112
|
+
message,
|
|
113
|
+
content = "",
|
|
114
|
+
options,
|
|
115
|
+
optionsArray,
|
|
116
|
+
defaultValue,
|
|
117
|
+
initialValue,
|
|
118
|
+
required = false,
|
|
119
|
+
borderColor = "dim",
|
|
120
|
+
titleColor = "cyan",
|
|
121
|
+
titleTypography = "none",
|
|
122
|
+
titleVariant,
|
|
123
|
+
contentColor = "dim",
|
|
124
|
+
contentTypography = "italic",
|
|
125
|
+
border = true,
|
|
126
|
+
endTitle = "",
|
|
127
|
+
endTitleColor = "dim",
|
|
128
|
+
debug = false,
|
|
129
|
+
terminalWidth: customTerminalWidth = 90,
|
|
130
|
+
displayInstructions = false,
|
|
131
|
+
shouldStream = false,
|
|
132
|
+
streamDelay = 20
|
|
133
|
+
} = params;
|
|
134
|
+
const finalTitle = message && title ? `${title}: ${message}` : message ?? title ?? "Select option";
|
|
135
|
+
const finalDefaultValue = defaultValue ?? initialValue;
|
|
136
|
+
const finalOptions = options ?? optionsArray?.map((opt) => ({
|
|
137
|
+
value: opt.value,
|
|
138
|
+
label: opt.label ?? opt.value,
|
|
139
|
+
hint: opt.hint,
|
|
140
|
+
disabled: false
|
|
141
|
+
})) ?? [];
|
|
142
|
+
let selectedIndex = finalDefaultValue ? finalOptions.findIndex(
|
|
143
|
+
(option) => isSelectOption(option) && option.value === finalDefaultValue && !option.disabled
|
|
144
|
+
) : -1;
|
|
145
|
+
if (selectedIndex === -1) {
|
|
146
|
+
selectedIndex = finalOptions.findIndex((option) => isSelectOption(option) && !option.disabled);
|
|
147
|
+
}
|
|
148
|
+
if (selectedIndex === -1) {
|
|
149
|
+
selectedIndex = 0;
|
|
150
|
+
}
|
|
151
|
+
const rl = readline.createInterface({ input, output });
|
|
152
|
+
readline.emitKeypressEvents(input, rl);
|
|
153
|
+
if (typeof input.setRawMode === "function") {
|
|
154
|
+
input.setRawMode(true);
|
|
155
|
+
}
|
|
156
|
+
const instructions = "Use <\u2191/\u2193> or <k/j> to navigate, <Enter> to select, <Ctrl+C> to exit";
|
|
157
|
+
let errorMessage = "";
|
|
158
|
+
const allDisabled = finalOptions.every((option) => isSelectOption(option) && option.disabled);
|
|
159
|
+
let lastUILineCount = 0;
|
|
160
|
+
function renderOptions() {
|
|
161
|
+
if (lastUILineCount > 0) {
|
|
162
|
+
for (let i = 0; i < lastUILineCount; i++) {
|
|
163
|
+
process.stdout.write("\x1B[1A\x1B[2K");
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
void renderPromptUI({
|
|
167
|
+
title: finalTitle,
|
|
168
|
+
content,
|
|
169
|
+
options: finalOptions,
|
|
170
|
+
selectedIndex,
|
|
171
|
+
errorMessage,
|
|
172
|
+
displayInstructions,
|
|
173
|
+
allDisabled,
|
|
174
|
+
instructions,
|
|
175
|
+
borderColor,
|
|
176
|
+
titleColor,
|
|
177
|
+
contentColor,
|
|
178
|
+
contentTypography,
|
|
179
|
+
debug,
|
|
180
|
+
titleVariant,
|
|
181
|
+
titleTypography,
|
|
182
|
+
terminalWidth: customTerminalWidth,
|
|
183
|
+
isRerender: true,
|
|
184
|
+
shouldStream,
|
|
185
|
+
streamDelay
|
|
186
|
+
}).then((count) => {
|
|
187
|
+
lastUILineCount = count;
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
lastUILineCount = await renderPromptUI({
|
|
191
|
+
title: finalTitle,
|
|
192
|
+
content,
|
|
193
|
+
options: finalOptions,
|
|
194
|
+
selectedIndex,
|
|
195
|
+
errorMessage,
|
|
196
|
+
displayInstructions,
|
|
197
|
+
allDisabled,
|
|
198
|
+
instructions,
|
|
199
|
+
borderColor,
|
|
200
|
+
titleColor,
|
|
201
|
+
contentColor,
|
|
202
|
+
contentTypography,
|
|
203
|
+
debug,
|
|
204
|
+
titleVariant,
|
|
205
|
+
titleTypography,
|
|
206
|
+
terminalWidth: customTerminalWidth,
|
|
207
|
+
isRerender: false,
|
|
208
|
+
shouldStream,
|
|
209
|
+
streamDelay
|
|
210
|
+
});
|
|
211
|
+
return new Promise((resolve) => {
|
|
212
|
+
function handleKeypress(_str, key) {
|
|
213
|
+
if (allDisabled) {
|
|
214
|
+
if (key.name === "c" && key.ctrl) {
|
|
215
|
+
void endPrompt(true);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (key.name === "up" || key.name === "k") {
|
|
221
|
+
moveSelectionUp();
|
|
222
|
+
} else if (key.name === "down" || key.name === "j") {
|
|
223
|
+
moveSelectionDown();
|
|
224
|
+
} else if (key.name === "return") {
|
|
225
|
+
void confirmSelection();
|
|
226
|
+
} else if (key.name === "c" && key.ctrl) {
|
|
227
|
+
void endPrompt(true);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function moveSelectionUp() {
|
|
231
|
+
const originalPointer = selectedIndex;
|
|
232
|
+
do {
|
|
233
|
+
selectedIndex = (selectedIndex - 1 + finalOptions.length) % finalOptions.length;
|
|
234
|
+
const option = finalOptions[selectedIndex];
|
|
235
|
+
if (option && isSelectOption(option) && !option.disabled) break;
|
|
236
|
+
} while (selectedIndex !== originalPointer);
|
|
237
|
+
renderOptions();
|
|
238
|
+
}
|
|
239
|
+
function moveSelectionDown() {
|
|
240
|
+
const originalPointer = selectedIndex;
|
|
241
|
+
do {
|
|
242
|
+
selectedIndex = (selectedIndex + 1) % finalOptions.length;
|
|
243
|
+
const option = finalOptions[selectedIndex];
|
|
244
|
+
if (option && isSelectOption(option) && !option.disabled) break;
|
|
245
|
+
} while (selectedIndex !== originalPointer);
|
|
246
|
+
renderOptions();
|
|
247
|
+
}
|
|
248
|
+
async function confirmSelection() {
|
|
249
|
+
const option = finalOptions[selectedIndex];
|
|
250
|
+
if (!option || !isSelectOption(option)) {
|
|
251
|
+
errorMessage = "This option is not selectable.";
|
|
252
|
+
renderOptions();
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (option.disabled) {
|
|
256
|
+
errorMessage = "This option is disabled";
|
|
257
|
+
renderOptions();
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (required && (!option.value || option.value === "")) {
|
|
261
|
+
errorMessage = "You must select a valid option.";
|
|
262
|
+
renderOptions();
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
cleanup();
|
|
266
|
+
resolve(option.value);
|
|
267
|
+
deleteLastLine();
|
|
268
|
+
await completePrompt(
|
|
269
|
+
"select",
|
|
270
|
+
false,
|
|
271
|
+
endTitle,
|
|
272
|
+
endTitleColor,
|
|
273
|
+
titleTypography,
|
|
274
|
+
titleVariant ? titleVariant : void 0,
|
|
275
|
+
border,
|
|
276
|
+
borderColor,
|
|
277
|
+
void 0,
|
|
278
|
+
true
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
async function endPrompt(isCtrlC = false) {
|
|
282
|
+
await completePrompt(
|
|
283
|
+
"select",
|
|
284
|
+
isCtrlC,
|
|
285
|
+
endTitle,
|
|
286
|
+
endTitleColor,
|
|
287
|
+
titleTypography,
|
|
288
|
+
titleVariant ? titleVariant : void 0,
|
|
289
|
+
border,
|
|
290
|
+
borderColor,
|
|
291
|
+
void 0,
|
|
292
|
+
false
|
|
293
|
+
);
|
|
294
|
+
cleanup(isCtrlC);
|
|
295
|
+
}
|
|
296
|
+
function cleanup(isCtrlC = false) {
|
|
297
|
+
if (typeof input.setRawMode === "function") {
|
|
298
|
+
input.setRawMode(false);
|
|
299
|
+
}
|
|
300
|
+
rl.close();
|
|
301
|
+
input.removeListener("keypress", handleKeypress);
|
|
302
|
+
if (isCtrlC) {
|
|
303
|
+
process.exit(0);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
input.on("keypress", handleKeypress);
|
|
307
|
+
});
|
|
308
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { stdin as input, stdout as output } from "node:process";
|
|
2
|
+
import readline from "node:readline";
|
|
3
|
+
import { re } from "@reliverse/relico";
|
|
4
|
+
import { relinka } from "@reliverse/relinka";
|
|
5
|
+
import { msg } from "../msg-fmt/messages.js";
|
|
6
|
+
import { deleteLastLine } from "../msg-fmt/terminal.js";
|
|
7
|
+
import { completePrompt } from "../utils/prompt-end.js";
|
|
8
|
+
function renderTogglePrompt(params) {
|
|
9
|
+
const {
|
|
10
|
+
title,
|
|
11
|
+
content,
|
|
12
|
+
options,
|
|
13
|
+
selectedIndex,
|
|
14
|
+
errorMessage,
|
|
15
|
+
instructions,
|
|
16
|
+
titleColor,
|
|
17
|
+
titleTypography,
|
|
18
|
+
titleVariant,
|
|
19
|
+
contentColor,
|
|
20
|
+
contentTypography,
|
|
21
|
+
displayInstructions,
|
|
22
|
+
debug,
|
|
23
|
+
isRerender = false
|
|
24
|
+
} = params;
|
|
25
|
+
if (!isRerender) {
|
|
26
|
+
msg({
|
|
27
|
+
type: "M_GENERAL",
|
|
28
|
+
title,
|
|
29
|
+
titleColor,
|
|
30
|
+
titleTypography,
|
|
31
|
+
...titleVariant ? { titleVariant } : {}
|
|
32
|
+
});
|
|
33
|
+
if (content) {
|
|
34
|
+
msg({
|
|
35
|
+
type: "M_NULL",
|
|
36
|
+
content: content.split("\n").map((line) => line.trim()).join("\n"),
|
|
37
|
+
contentColor,
|
|
38
|
+
contentTypography
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
let uiLineCount = 0;
|
|
43
|
+
if (errorMessage) {
|
|
44
|
+
msg({
|
|
45
|
+
type: "M_NULL",
|
|
46
|
+
title: re.redBright(errorMessage)
|
|
47
|
+
});
|
|
48
|
+
uiLineCount++;
|
|
49
|
+
} else if (displayInstructions && !isRerender) {
|
|
50
|
+
msg({
|
|
51
|
+
type: "M_NULL",
|
|
52
|
+
title: re.yellow(instructions)
|
|
53
|
+
});
|
|
54
|
+
uiLineCount++;
|
|
55
|
+
}
|
|
56
|
+
const displayString = options.map((option, index) => index === selectedIndex ? re.yellow(option) : re.reset(option)).join(re.dim(re.reset(" / ")));
|
|
57
|
+
msg({ type: "M_NULL", title: displayString });
|
|
58
|
+
uiLineCount++;
|
|
59
|
+
if (debug) {
|
|
60
|
+
relinka("log", "", {
|
|
61
|
+
selectedIndex,
|
|
62
|
+
displayOptions: options
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return uiLineCount;
|
|
66
|
+
}
|
|
67
|
+
export async function togglePrompt(params) {
|
|
68
|
+
const {
|
|
69
|
+
title = "",
|
|
70
|
+
content = "",
|
|
71
|
+
options = ["Yes", "No"],
|
|
72
|
+
defaultValue = "Yes",
|
|
73
|
+
borderColor = "dim",
|
|
74
|
+
titleColor = "cyan",
|
|
75
|
+
titleTypography = "none",
|
|
76
|
+
titleVariant,
|
|
77
|
+
contentColor = "dim",
|
|
78
|
+
contentTypography = "italic",
|
|
79
|
+
border = true,
|
|
80
|
+
endTitle = "",
|
|
81
|
+
endTitleColor = "dim",
|
|
82
|
+
displayInstructions = false,
|
|
83
|
+
debug = false
|
|
84
|
+
} = params;
|
|
85
|
+
if (options.length !== 2) {
|
|
86
|
+
throw new Error("togglePrompt requires exactly two options.");
|
|
87
|
+
}
|
|
88
|
+
let selectedIndex = options.indexOf(defaultValue);
|
|
89
|
+
if (selectedIndex === -1) {
|
|
90
|
+
selectedIndex = 0;
|
|
91
|
+
}
|
|
92
|
+
const rl = readline.createInterface({ input, output });
|
|
93
|
+
readline.emitKeypressEvents(input, rl);
|
|
94
|
+
if (typeof input.setRawMode === "function") {
|
|
95
|
+
input.setRawMode(true);
|
|
96
|
+
}
|
|
97
|
+
const instructions = "Use <\u2190/\u2192> or <h/l> to navigate, <Enter> to select, <Ctrl+C> to exit";
|
|
98
|
+
let errorMessage = "";
|
|
99
|
+
let lastUILineCount = 0;
|
|
100
|
+
function renderOptions() {
|
|
101
|
+
if (lastUILineCount > 0) {
|
|
102
|
+
for (let i = 0; i < lastUILineCount; i++) {
|
|
103
|
+
process.stdout.write("\x1B[1A\x1B[2K");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
lastUILineCount = renderTogglePrompt({
|
|
107
|
+
title,
|
|
108
|
+
content,
|
|
109
|
+
options,
|
|
110
|
+
selectedIndex,
|
|
111
|
+
errorMessage,
|
|
112
|
+
instructions,
|
|
113
|
+
borderColor,
|
|
114
|
+
titleColor,
|
|
115
|
+
titleTypography,
|
|
116
|
+
...titleVariant ? { titleVariant } : {},
|
|
117
|
+
contentColor,
|
|
118
|
+
contentTypography,
|
|
119
|
+
border,
|
|
120
|
+
displayInstructions,
|
|
121
|
+
debug,
|
|
122
|
+
isRerender: true
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
lastUILineCount = renderTogglePrompt({
|
|
126
|
+
title,
|
|
127
|
+
content,
|
|
128
|
+
options,
|
|
129
|
+
selectedIndex,
|
|
130
|
+
errorMessage,
|
|
131
|
+
instructions,
|
|
132
|
+
borderColor,
|
|
133
|
+
titleColor,
|
|
134
|
+
titleTypography,
|
|
135
|
+
...titleVariant ? { titleVariant } : {},
|
|
136
|
+
contentColor,
|
|
137
|
+
contentTypography,
|
|
138
|
+
border,
|
|
139
|
+
displayInstructions,
|
|
140
|
+
debug,
|
|
141
|
+
isRerender: false
|
|
142
|
+
});
|
|
143
|
+
return new Promise((resolve) => {
|
|
144
|
+
function cleanup(isCtrlC = false) {
|
|
145
|
+
if (typeof input.setRawMode === "function") {
|
|
146
|
+
input.setRawMode(false);
|
|
147
|
+
}
|
|
148
|
+
rl.close();
|
|
149
|
+
input.removeListener("keypress", handleKeypress);
|
|
150
|
+
if (isCtrlC) {
|
|
151
|
+
process.exit(0);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function endPrompt(isCtrlC = false) {
|
|
155
|
+
if (endTitle !== "") {
|
|
156
|
+
msg({
|
|
157
|
+
type: "M_END",
|
|
158
|
+
title: endTitle,
|
|
159
|
+
titleColor: endTitleColor,
|
|
160
|
+
titleTypography,
|
|
161
|
+
...titleVariant ? { titleVariant } : {},
|
|
162
|
+
border,
|
|
163
|
+
borderColor
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
cleanup(isCtrlC);
|
|
167
|
+
}
|
|
168
|
+
function handleKeypress(_str, key) {
|
|
169
|
+
if (key.name === "left" || key.name === "h") {
|
|
170
|
+
selectedIndex = (selectedIndex - 1 + options.length) % options.length;
|
|
171
|
+
errorMessage = "";
|
|
172
|
+
renderOptions();
|
|
173
|
+
} else if (key.name === "right" || key.name === "l") {
|
|
174
|
+
selectedIndex = (selectedIndex + 1) % options.length;
|
|
175
|
+
errorMessage = "";
|
|
176
|
+
renderOptions();
|
|
177
|
+
} else if (key.name === "return") {
|
|
178
|
+
const selectedOption = options[selectedIndex];
|
|
179
|
+
if (!selectedOption) {
|
|
180
|
+
errorMessage = "You must select an option.";
|
|
181
|
+
renderOptions();
|
|
182
|
+
} else {
|
|
183
|
+
cleanup();
|
|
184
|
+
const booleanValue = selectedIndex === 0;
|
|
185
|
+
void completePrompt(
|
|
186
|
+
"toggle",
|
|
187
|
+
false,
|
|
188
|
+
endTitle,
|
|
189
|
+
endTitleColor,
|
|
190
|
+
titleTypography,
|
|
191
|
+
titleVariant ? titleVariant : void 0,
|
|
192
|
+
border,
|
|
193
|
+
borderColor,
|
|
194
|
+
void 0,
|
|
195
|
+
booleanValue
|
|
196
|
+
);
|
|
197
|
+
resolve(booleanValue);
|
|
198
|
+
deleteLastLine();
|
|
199
|
+
msg({ type: "M_BAR", borderColor });
|
|
200
|
+
}
|
|
201
|
+
} else if (key.name === "c" && key.ctrl) {
|
|
202
|
+
endPrompt(true);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
input.on("keypress", handleKeypress);
|
|
206
|
+
});
|
|
207
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
interface SpinnerOptions {
|
|
2
|
+
cancelMessage?: string;
|
|
3
|
+
color?: string;
|
|
4
|
+
delay?: number;
|
|
5
|
+
errorMessage?: string;
|
|
6
|
+
failText?: string;
|
|
7
|
+
frames?: string[];
|
|
8
|
+
hideCursor?: boolean;
|
|
9
|
+
indicator?: "dots" | "timer";
|
|
10
|
+
onCancel?: () => void;
|
|
11
|
+
prefixText?: string;
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
silent?: boolean;
|
|
14
|
+
spinner?: string;
|
|
15
|
+
successText?: string;
|
|
16
|
+
text: string;
|
|
17
|
+
}
|
|
18
|
+
interface ProgressOptions {
|
|
19
|
+
current: number;
|
|
20
|
+
total: number;
|
|
21
|
+
format?: "percentage" | "count" | "both";
|
|
22
|
+
}
|
|
23
|
+
interface SpinnerControls {
|
|
24
|
+
start: (text?: string) => SpinnerControls;
|
|
25
|
+
stop: (text?: string, code?: number) => void;
|
|
26
|
+
setText: (text: string) => void;
|
|
27
|
+
setProgress: (progress: ProgressOptions) => void;
|
|
28
|
+
succeed: (text?: string) => void;
|
|
29
|
+
fail: (text?: string) => void;
|
|
30
|
+
warn: (text?: string) => void;
|
|
31
|
+
info: (text?: string) => void;
|
|
32
|
+
isSpinning: () => boolean;
|
|
33
|
+
clear: () => void;
|
|
34
|
+
getElapsedTime: () => number;
|
|
35
|
+
pause: () => void;
|
|
36
|
+
resume: () => void;
|
|
37
|
+
dispose: () => void;
|
|
38
|
+
isCancelled: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a terminal spinner with enhanced controls and styling options.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* // Basic usage
|
|
46
|
+
* const spinner = useSpinner({ text: "Loading..." }).start();
|
|
47
|
+
* spinner.stop();
|
|
48
|
+
*
|
|
49
|
+
* // With progress tracking
|
|
50
|
+
* const spinner = useSpinner({ text: "Processing files..." }).start();
|
|
51
|
+
* for (let i = 0; i < files.length; i++) {
|
|
52
|
+
* spinner.setProgress({ current: i + 1, total: files.length });
|
|
53
|
+
* await processFile(files[i]);
|
|
54
|
+
* }
|
|
55
|
+
* spinner.succeed();
|
|
56
|
+
*
|
|
57
|
+
* // With custom color and spinner
|
|
58
|
+
* const spinner = useSpinner({
|
|
59
|
+
* text: "Processing...",
|
|
60
|
+
* color: "cyan",
|
|
61
|
+
* spinner: "dots"
|
|
62
|
+
* }).start();
|
|
63
|
+
*
|
|
64
|
+
* // With success/failure states
|
|
65
|
+
* const spinner = useSpinner({
|
|
66
|
+
* text: "Uploading...",
|
|
67
|
+
* successText: "Upload complete!",
|
|
68
|
+
* failText: "Upload failed!"
|
|
69
|
+
* }).start();
|
|
70
|
+
* try {
|
|
71
|
+
* await uploadFile();
|
|
72
|
+
* spinner.succeed();
|
|
73
|
+
* } catch (error) {
|
|
74
|
+
* spinner.fail();
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* // Using the wrapper for async operations
|
|
78
|
+
* await useSpinner.promise(
|
|
79
|
+
* async (spinner) => {
|
|
80
|
+
* await longOperation();
|
|
81
|
+
* spinner.setProgress({ current: 50, total: 100 });
|
|
82
|
+
* },
|
|
83
|
+
* {
|
|
84
|
+
* text: "Working...",
|
|
85
|
+
* successText: "Done!",
|
|
86
|
+
* failText: "Failed!"
|
|
87
|
+
* }
|
|
88
|
+
* );
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare function useSpinner(options: SpinnerOptions): SpinnerControls;
|
|
92
|
+
export declare namespace useSpinner {
|
|
93
|
+
var promise: <T>(operation: (spinner: SpinnerControls) => Promise<T>, options: SpinnerOptions) => Promise<T>;
|
|
94
|
+
var nested: (parentOptions: SpinnerOptions) => {
|
|
95
|
+
start: () => {
|
|
96
|
+
child: (childOptions: SpinnerOptions) => SpinnerControls;
|
|
97
|
+
finish: (success: boolean, text?: string) => void;
|
|
98
|
+
dispose: () => void;
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
var withTiming: <T>(operation: (spinner: SpinnerControls) => Promise<T>, options: SpinnerOptions) => Promise<{
|
|
102
|
+
result: T;
|
|
103
|
+
duration: number;
|
|
104
|
+
}>;
|
|
105
|
+
}
|
|
106
|
+
export {};
|