@reliverse/relinka 1.1.5 → 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 -85
- 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 -167
- 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 -97
- 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 -97
- 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 -97
- package/dist-npm/components/multiselect/num-multiselect-main.js +35 -24
- package/dist-npm/components/next-steps/next-steps.js +25 -23
- package/dist-npm/components/number/index.js +112 -78
- package/dist-npm/components/number/number-main.js +2 -95
- package/dist-npm/components/password/index.js +73 -54
- package/dist-npm/components/password/password-main.js +2 -119
- 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 -124
- 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 -94
- package/dist-npm/components/spinner/index.js +136 -107
- package/dist-npm/components/st-end/end.js +26 -34
- package/dist-npm/components/st-end/start.js +15 -29
- 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.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,111 +1,144 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { stdin as input, stdout as output } from "node:process";
|
|
3
|
-
import readline from "node:readline/promises";
|
|
4
|
-
import { colorize } from "../../utils/colorize.js";
|
|
5
|
-
import { bar, fmt, msg } from "../../utils/messages.js";
|
|
1
|
+
import {Value} from '@sinclair/typebox/value';
|
|
6
2
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
stdin as input,
|
|
4
|
+
stdout as output,
|
|
5
|
+
} from 'node:process';
|
|
6
|
+
import readline from 'node:readline/promises';
|
|
7
|
+
import {colorize} from '../../utils/colorize.js';
|
|
8
|
+
import {
|
|
9
|
+
bar,
|
|
10
|
+
fmt,
|
|
11
|
+
msg,
|
|
12
|
+
} from '../../utils/messages.js';
|
|
13
|
+
import {
|
|
14
|
+
countLines,
|
|
15
|
+
deleteLastLine,
|
|
16
|
+
deleteLastLines,
|
|
17
|
+
} from '../../utils/terminal.js';
|
|
18
|
+
|
|
11
19
|
export async function numMultiSelectPrompt(options) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
titleVariant,
|
|
20
|
-
hint,
|
|
21
|
-
content,
|
|
22
|
-
contentColor = "dim",
|
|
23
|
-
contentTypography,
|
|
24
|
-
contentVariant,
|
|
25
|
-
borderColor = "viceGradient",
|
|
26
|
-
variantOptions
|
|
27
|
-
} = options;
|
|
28
|
-
if (!choices || choices.length === 0) {
|
|
29
|
-
throw new Error("Choices are required for multiselect prompt.");
|
|
30
|
-
}
|
|
31
|
-
const rl = readline.createInterface({ input, output });
|
|
32
|
-
const formattedBar = bar({ borderColor });
|
|
33
|
-
let linesToDelete = 0;
|
|
34
|
-
let errorMessage = "";
|
|
35
|
-
try {
|
|
36
|
-
while (true) {
|
|
37
|
-
if (linesToDelete > 0) {
|
|
38
|
-
deleteLastLines(linesToDelete);
|
|
39
|
-
}
|
|
40
|
-
const question = fmt({
|
|
41
|
-
type: errorMessage !== "" ? "M_ERROR" : "M_GENERAL",
|
|
42
|
-
title: `${title}${defaultValue ? ` [Default: ${Array.isArray(defaultValue) ? defaultValue.join(", ") : defaultValue}]` : ""}`,
|
|
43
|
-
titleColor,
|
|
44
|
-
titleTypography,
|
|
20
|
+
const {
|
|
21
|
+
title,
|
|
22
|
+
choices,
|
|
23
|
+
schema,
|
|
24
|
+
defaultValue,
|
|
25
|
+
titleColor = 'cyanBright',
|
|
26
|
+
titleTypography = 'bold',
|
|
45
27
|
titleVariant,
|
|
28
|
+
hint,
|
|
46
29
|
content,
|
|
47
|
-
contentColor,
|
|
30
|
+
contentColor = 'dim',
|
|
48
31
|
contentTypography,
|
|
49
32
|
contentVariant,
|
|
50
|
-
borderColor,
|
|
51
|
-
hint,
|
|
33
|
+
borderColor = 'viceGradient',
|
|
52
34
|
variantOptions,
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
35
|
+
} = options;
|
|
36
|
+
|
|
37
|
+
if (!choices || !choices.length) {
|
|
38
|
+
throw Error('Choices are required for multiselect prompt.');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const rl = readline.createInterface({
|
|
42
|
+
input,
|
|
43
|
+
output,
|
|
44
|
+
});
|
|
45
|
+
const formattedBar = bar({
|
|
46
|
+
borderColor,
|
|
47
|
+
});
|
|
48
|
+
let linesToDelete = 0;
|
|
49
|
+
let errorMessage = '';
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
while (true) {
|
|
53
|
+
if (linesToDelete > 0) {
|
|
54
|
+
deleteLastLines(linesToDelete);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const question = fmt({
|
|
58
|
+
type: errorMessage !== '' ? 'M_ERROR' : 'M_GENERAL',
|
|
59
|
+
title: `${title}${defaultValue ? ` [Default: ${Array.isArray(defaultValue) ? defaultValue.join(', ') : defaultValue}]` : ''}`,
|
|
60
|
+
titleColor,
|
|
61
|
+
titleTypography,
|
|
62
|
+
titleVariant,
|
|
63
|
+
content,
|
|
64
|
+
contentColor,
|
|
65
|
+
contentTypography,
|
|
66
|
+
contentVariant,
|
|
67
|
+
borderColor,
|
|
68
|
+
hint,
|
|
69
|
+
variantOptions,
|
|
70
|
+
errorMessage,
|
|
71
|
+
addNewLineBefore: false,
|
|
72
|
+
addNewLineAfter: false,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const choicesText = choices
|
|
76
|
+
.map((choice, index) => `${formattedBar} ${index + 1}) ${choice.title}${choice.description ? ` - ${choice.description}` : ''}`)
|
|
77
|
+
.join('\n');
|
|
78
|
+
|
|
79
|
+
const fullPrompt = `${question}
|
|
61
80
|
${choicesText}
|
|
62
81
|
${formattedBar} ${colorize(`Enter your choices (comma-separated numbers between 1-${choices.length})`, contentColor)}:
|
|
63
82
|
${formattedBar} `;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
83
|
+
|
|
84
|
+
const formattedPrompt = fmt({
|
|
85
|
+
type: 'M_NULL',
|
|
86
|
+
title: fullPrompt,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const questionLines = countLines(formattedPrompt);
|
|
90
|
+
|
|
91
|
+
linesToDelete = questionLines + 1;
|
|
92
|
+
const answer = (await rl.question(formattedPrompt)).trim();
|
|
93
|
+
|
|
94
|
+
if (!answer && defaultValue !== void 0) {
|
|
95
|
+
deleteLastLine();
|
|
96
|
+
msg({
|
|
97
|
+
type: 'M_MIDDLE',
|
|
98
|
+
title: ` ${Array.isArray(defaultValue) ? defaultValue.join(', ') : defaultValue}`,
|
|
99
|
+
titleColor: 'none',
|
|
100
|
+
});
|
|
101
|
+
msg({
|
|
102
|
+
type: 'M_NEWLINE',
|
|
103
|
+
});
|
|
104
|
+
return defaultValue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const selections = answer
|
|
108
|
+
.split(',')
|
|
109
|
+
.map((s) => s.trim());
|
|
110
|
+
|
|
111
|
+
const invalidSelections = selections.filter((s) => {
|
|
112
|
+
const num = Number(s);
|
|
113
|
+
return isNaN(num) || num < 1 || num > choices.length;
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
if (invalidSelections.length > 0) {
|
|
117
|
+
errorMessage = `Invalid selections: ${invalidSelections.join(', ')}. Please enter numbers between 1 and ${choices.length}.`;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const selectedValues = selections.map((s) => choices[Number(s) - 1]?.id);
|
|
122
|
+
let isValid = true;
|
|
123
|
+
|
|
124
|
+
errorMessage = '';
|
|
125
|
+
if (schema) {
|
|
126
|
+
isValid = Value.Check(schema, selectedValues);
|
|
127
|
+
if (!isValid) {
|
|
128
|
+
const errors = Value.Errors(schema, selectedValues);
|
|
129
|
+
errorMessage = errors.length > 0 ? errors[0]?.message ?? 'Invalid input.' : 'Invalid input.';
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (isValid) {
|
|
134
|
+
msg({
|
|
135
|
+
type: 'M_NEWLINE',
|
|
136
|
+
});
|
|
137
|
+
rl.close();
|
|
138
|
+
return selectedValues;
|
|
139
|
+
}
|
|
100
140
|
}
|
|
101
|
-
|
|
102
|
-
if (isValid) {
|
|
103
|
-
msg({ type: "M_NEWLINE" });
|
|
141
|
+
} finally {
|
|
104
142
|
rl.close();
|
|
105
|
-
return selectedValues;
|
|
106
|
-
}
|
|
107
143
|
}
|
|
108
|
-
} finally {
|
|
109
|
-
rl.close();
|
|
110
|
-
}
|
|
111
144
|
}
|
|
@@ -1,28 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
stdin as input,
|
|
3
|
+
stdout as output,
|
|
4
|
+
} from 'node:process';
|
|
5
|
+
import readline from 'node:readline/promises';
|
|
6
|
+
|
|
3
7
|
export async function multiselectPrompt(params) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const {
|
|
9
|
+
options,
|
|
10
|
+
required = false,
|
|
11
|
+
initial = [],
|
|
12
|
+
} = params;
|
|
13
|
+
const rl = readline.createInterface({
|
|
14
|
+
input,
|
|
15
|
+
output,
|
|
12
16
|
});
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
|
|
18
|
+
while (true) {
|
|
19
|
+
const promptMessage = initial.length > 0 ? `Please select options by number (separated by commas, default ${initial
|
|
20
|
+
.map((opt) => options.indexOf(opt) + 1)
|
|
21
|
+
.join(', ')}): ` : 'Please select options by number (separated by commas): ';
|
|
22
|
+
const response = await rl.question(promptMessage);
|
|
23
|
+
let indices;
|
|
24
|
+
|
|
25
|
+
if (response.trim() === '' && initial.length > 0) {
|
|
26
|
+
indices = initial.map((opt) => options.indexOf(opt));
|
|
27
|
+
} else {
|
|
28
|
+
indices = response
|
|
29
|
+
.split(',')
|
|
30
|
+
.map((num) => parseInt(num.trim(), 10) - 1)
|
|
31
|
+
.filter((index) => index >= 0 && index < options.length);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (indices.length > 0 || !required && !indices.length) {
|
|
35
|
+
rl.close();
|
|
36
|
+
return indices.map((index) => options[index]);
|
|
37
|
+
}
|
|
20
38
|
}
|
|
21
|
-
if (indices.length > 0 || !required && indices.length === 0) {
|
|
22
|
-
rl.close();
|
|
23
|
-
return indices.map((index) => options[index]);
|
|
24
|
-
} else {
|
|
25
|
-
console.log("Invalid selection. Please try again.");
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
39
|
}
|
|
@@ -1,24 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
import {msg} from '../../utils/messages.js';
|
|
2
|
+
|
|
3
|
+
export function nextStepsPrompt(options) {
|
|
4
|
+
const {
|
|
5
|
+
title,
|
|
6
|
+
titleColor = 'cyanBright',
|
|
7
|
+
titleVariant,
|
|
8
|
+
titleTypography = 'bold',
|
|
9
|
+
content,
|
|
10
|
+
contentColor,
|
|
11
|
+
contentVariant,
|
|
12
|
+
contentTypography,
|
|
13
|
+
} = options;
|
|
14
|
+
|
|
15
|
+
msg({
|
|
16
|
+
type: 'M_INFO',
|
|
17
|
+
title,
|
|
18
|
+
titleColor,
|
|
19
|
+
titleVariant,
|
|
20
|
+
titleTypography,
|
|
21
|
+
content,
|
|
22
|
+
contentColor,
|
|
23
|
+
contentVariant,
|
|
24
|
+
contentTypography,
|
|
25
|
+
});
|
|
24
26
|
}
|
|
@@ -1,100 +1,134 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from
|
|
2
|
+
createPrompt,
|
|
3
|
+
useState,
|
|
4
|
+
useKeypress,
|
|
5
|
+
usePrefix,
|
|
6
|
+
isEnterKey,
|
|
7
|
+
isBackspaceKey,
|
|
8
|
+
makeTheme,
|
|
9
|
+
} from '../../components/core/index.js';
|
|
10
|
+
|
|
10
11
|
function isStepOf(value, step, min) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
const valuePow = value * 10 ** 6;
|
|
13
|
+
const stepPow = step * 10 ** 6;
|
|
14
|
+
const minPow = min * 10 ** 6;
|
|
15
|
+
|
|
16
|
+
return !((valuePow - (Number.isFinite(min) ? minPow : 0)) % stepPow);
|
|
15
17
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
|
|
19
|
+
function validateNumber(value, {min, max, step}) {
|
|
20
|
+
if (value == null || Number.isNaN(value)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (value < min || value > max) {
|
|
25
|
+
return `Value must be between ${min} and ${max}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (step !== 'any' && !isStepOf(value, step, min)) {
|
|
29
|
+
return `Value must be a multiple of ${step}${Number.isFinite(min) ? ` starting from ${min}` : ''}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return true;
|
|
29
33
|
}
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
|
|
35
|
+
export default createPrompt((config, done) => {
|
|
32
36
|
const {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
validate = () => true,
|
|
38
|
+
min = -Infinity,
|
|
39
|
+
max = Infinity,
|
|
40
|
+
step = 1,
|
|
41
|
+
required = false,
|
|
38
42
|
} = config;
|
|
43
|
+
|
|
39
44
|
const theme = makeTheme(config.theme);
|
|
40
|
-
const [status, setStatus] = useState(
|
|
41
|
-
const [value, setValue] = useState(
|
|
42
|
-
const validDefault = validateNumber(config.default, {
|
|
43
|
-
|
|
45
|
+
const [status, setStatus] = useState('idle');
|
|
46
|
+
const [value, setValue] = useState('');
|
|
47
|
+
const validDefault = validateNumber(config.default, {
|
|
48
|
+
min,
|
|
49
|
+
max,
|
|
50
|
+
step,
|
|
51
|
+
}) ? config.default?.toString() : void 0;
|
|
52
|
+
const [defaultValue = '', setDefaultValue] = useState(validDefault);
|
|
44
53
|
const [errorMsg, setError] = useState();
|
|
45
|
-
const prefix = usePrefix({
|
|
54
|
+
const prefix = usePrefix({
|
|
55
|
+
status,
|
|
56
|
+
theme,
|
|
57
|
+
});
|
|
58
|
+
|
|
46
59
|
useKeypress(async (key, rl) => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
if (isEnterKey(key)) {
|
|
51
|
-
const input = value || defaultValue;
|
|
52
|
-
const answer = input === "" ? void 0 : Number(input);
|
|
53
|
-
setStatus("loading");
|
|
54
|
-
let isValid = true;
|
|
55
|
-
if (required || answer != null) {
|
|
56
|
-
isValid = validateNumber(answer, { min, max, step });
|
|
57
|
-
}
|
|
58
|
-
if (isValid === true) {
|
|
59
|
-
isValid = await validate(answer);
|
|
60
|
+
if (status !== 'idle') {
|
|
61
|
+
return;
|
|
60
62
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
|
|
64
|
+
if (isEnterKey(key)) {
|
|
65
|
+
const input = value || defaultValue;
|
|
66
|
+
const answer = input === '' ? void 0 : Number(input);
|
|
67
|
+
setStatus('loading');
|
|
68
|
+
let isValid = true;
|
|
69
|
+
|
|
70
|
+
if (required || answer != null) {
|
|
71
|
+
isValid = validateNumber(answer, {
|
|
72
|
+
min,
|
|
73
|
+
max,
|
|
74
|
+
step,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (isValid) {
|
|
79
|
+
isValid = await validate(answer);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (isValid) {
|
|
83
|
+
setValue(String(answer ?? ''));
|
|
84
|
+
setStatus('done');
|
|
85
|
+
done(answer);
|
|
86
|
+
} else {
|
|
87
|
+
rl.write(value);
|
|
88
|
+
setError(isValid || 'You must provide a valid numeric value');
|
|
89
|
+
setStatus('idle');
|
|
90
|
+
}
|
|
91
|
+
} else if (isBackspaceKey(key) && !value) {
|
|
92
|
+
setDefaultValue(void 0);
|
|
93
|
+
} else if (key.name === 'tab' && !value) {
|
|
94
|
+
setDefaultValue(void 0);
|
|
95
|
+
rl.clearLine(0);
|
|
96
|
+
rl.write(defaultValue);
|
|
97
|
+
setValue(defaultValue);
|
|
65
98
|
} else {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
setStatus("idle");
|
|
99
|
+
setValue(rl.line);
|
|
100
|
+
setError(void 0);
|
|
69
101
|
}
|
|
70
|
-
|
|
71
|
-
setDefaultValue(void 0);
|
|
72
|
-
} else if (key.name === "tab" && !value) {
|
|
73
|
-
setDefaultValue(void 0);
|
|
74
|
-
rl.clearLine(0);
|
|
75
|
-
rl.write(defaultValue);
|
|
76
|
-
setValue(defaultValue);
|
|
77
|
-
} else {
|
|
78
|
-
setValue(rl.line);
|
|
79
|
-
setError(void 0);
|
|
80
|
-
}
|
|
102
|
+
|
|
81
103
|
});
|
|
82
104
|
const message = theme.style.message(config.message, status);
|
|
83
105
|
let formattedValue = value;
|
|
84
|
-
|
|
85
|
-
|
|
106
|
+
|
|
107
|
+
if (status === 'done') {
|
|
108
|
+
formattedValue = theme.style.answer(value);
|
|
86
109
|
}
|
|
110
|
+
|
|
87
111
|
let defaultStr;
|
|
88
|
-
|
|
89
|
-
|
|
112
|
+
|
|
113
|
+
if (defaultValue && status !== 'done' && !value) {
|
|
114
|
+
defaultStr = theme.style.defaultAnswer(defaultValue);
|
|
90
115
|
}
|
|
91
|
-
|
|
116
|
+
|
|
117
|
+
let error = '';
|
|
118
|
+
|
|
92
119
|
if (errorMsg) {
|
|
93
|
-
|
|
120
|
+
error = theme.style.error(errorMsg);
|
|
94
121
|
}
|
|
122
|
+
|
|
95
123
|
return [
|
|
96
|
-
|
|
97
|
-
|
|
124
|
+
[
|
|
125
|
+
prefix,
|
|
126
|
+
message,
|
|
127
|
+
defaultStr,
|
|
128
|
+
formattedValue,
|
|
129
|
+
]
|
|
130
|
+
.filter((v) => v !== void 0)
|
|
131
|
+
.join(' '),
|
|
132
|
+
error,
|
|
98
133
|
];
|
|
99
|
-
|
|
100
|
-
);
|
|
134
|
+
});
|
|
@@ -1,95 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import readline from "node:readline/promises";
|
|
4
|
-
import { fmt, msg } from "../../utils/messages.js";
|
|
5
|
-
import {
|
|
6
|
-
countLines,
|
|
7
|
-
deleteLastLine,
|
|
8
|
-
deleteLastLines
|
|
9
|
-
} from "../../utils/terminal.js";
|
|
10
|
-
export async function numberPrompt(options) {
|
|
11
|
-
const {
|
|
12
|
-
title,
|
|
13
|
-
hint,
|
|
14
|
-
validate,
|
|
15
|
-
defaultValue,
|
|
16
|
-
schema,
|
|
17
|
-
titleColor = "cyanBright",
|
|
18
|
-
titleTypography = "bold",
|
|
19
|
-
titleVariant,
|
|
20
|
-
content,
|
|
21
|
-
contentColor,
|
|
22
|
-
contentTypography,
|
|
23
|
-
contentVariant,
|
|
24
|
-
borderColor = "viceGradient",
|
|
25
|
-
variantOptions
|
|
26
|
-
} = options;
|
|
27
|
-
const rl = readline.createInterface({ input, output });
|
|
28
|
-
let linesToDelete = 0;
|
|
29
|
-
let errorMessage = "";
|
|
30
|
-
while (true) {
|
|
31
|
-
if (linesToDelete > 0) {
|
|
32
|
-
deleteLastLines(linesToDelete);
|
|
33
|
-
}
|
|
34
|
-
const question = fmt({
|
|
35
|
-
type: errorMessage !== "" ? "M_ERROR" : "M_GENERAL",
|
|
36
|
-
title,
|
|
37
|
-
titleColor,
|
|
38
|
-
titleTypography,
|
|
39
|
-
titleVariant,
|
|
40
|
-
content,
|
|
41
|
-
contentColor,
|
|
42
|
-
contentTypography,
|
|
43
|
-
contentVariant,
|
|
44
|
-
borderColor,
|
|
45
|
-
hint,
|
|
46
|
-
variantOptions,
|
|
47
|
-
errorMessage
|
|
48
|
-
});
|
|
49
|
-
const questionLines = countLines(question);
|
|
50
|
-
const prompt = await rl.question(question);
|
|
51
|
-
linesToDelete = questionLines + 1;
|
|
52
|
-
const answer = prompt.trim() || defaultValue;
|
|
53
|
-
if (prompt.trim() === "" && defaultValue !== void 0) {
|
|
54
|
-
deleteLastLine();
|
|
55
|
-
const defaultMsg = fmt({
|
|
56
|
-
type: "M_MIDDLE",
|
|
57
|
-
title: ` ${defaultValue}`,
|
|
58
|
-
borderColor
|
|
59
|
-
});
|
|
60
|
-
console.log(defaultMsg);
|
|
61
|
-
linesToDelete += countLines(defaultMsg);
|
|
62
|
-
}
|
|
63
|
-
const num = Number(answer);
|
|
64
|
-
if (isNaN(num)) {
|
|
65
|
-
errorMessage = "Please enter a valid number.";
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
let isValid = true;
|
|
69
|
-
errorMessage = "";
|
|
70
|
-
if (schema) {
|
|
71
|
-
isValid = Value.Check(schema, num);
|
|
72
|
-
if (!isValid) {
|
|
73
|
-
const errors = [...Value.Errors(schema, num)];
|
|
74
|
-
if (errors.length > 0) {
|
|
75
|
-
errorMessage = errors[0]?.message ?? "Invalid input.";
|
|
76
|
-
} else {
|
|
77
|
-
errorMessage = "Invalid input.";
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
if (validate && isValid) {
|
|
82
|
-
const validation = await validate(num);
|
|
83
|
-
if (validation !== true) {
|
|
84
|
-
isValid = false;
|
|
85
|
-
errorMessage = typeof validation === "string" ? validation : "Invalid input.";
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
if (isValid) {
|
|
89
|
-
msg({ type: "M_NEWLINE" });
|
|
90
|
-
rl.close();
|
|
91
|
-
return num;
|
|
92
|
-
} else {
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
1
|
+
export function numberPrompt() {
|
|
2
|
+
while (true) {}}
|