@reliverse/relinka 1.1.7 → 1.1.9
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 +57 -77
- package/dist-npm/components/block/block.js +48 -61
- package/dist-npm/components/checkbox/index.js +178 -211
- package/dist-npm/components/confirm/confirm-main.js +85 -122
- package/dist-npm/components/confirm/confirm-three.js +24 -27
- package/dist-npm/components/confirm/index.js +36 -45
- package/dist-npm/components/core/Separator.js +15 -17
- package/dist-npm/components/core/create-prompt.js +101 -124
- package/dist-npm/components/core/errors.js +19 -15
- package/dist-npm/components/core/hook-engine.js +91 -120
- package/dist-npm/components/core/index.js +12 -12
- package/dist-npm/components/core/key.js +16 -6
- package/dist-npm/components/core/lines.js +45 -54
- package/dist-npm/components/core/make-theme.js +21 -30
- package/dist-npm/components/core/position.js +27 -23
- package/dist-npm/components/core/promise-polyfill.js +11 -14
- package/dist-npm/components/core/screen-manager.js +59 -74
- package/dist-npm/components/core/theme.js +22 -32
- package/dist-npm/components/core/use-effect.js +9 -15
- package/dist-npm/components/core/use-keypress.js +19 -23
- package/dist-npm/components/core/use-memo.js +10 -16
- package/dist-npm/components/core/use-pagination.js +33 -32
- package/dist-npm/components/core/use-prefix.js +40 -38
- package/dist-npm/components/core/use-ref.js +2 -5
- package/dist-npm/components/core/use-state.js +15 -23
- package/dist-npm/components/core/useKeyPress.js +14 -17
- package/dist-npm/components/core/usePromptState.js +8 -14
- package/dist-npm/components/core/utils.js +7 -16
- package/dist-npm/components/date/date.js +187 -204
- package/dist-npm/components/editor/index.js +71 -92
- package/dist-npm/components/expand/index.js +96 -124
- package/dist-npm/components/figures/index.js +283 -294
- package/dist-npm/components/input/index.js +61 -87
- package/dist-npm/components/input/text-main.js +97 -124
- package/dist-npm/components/input/text.js +24 -28
- package/dist-npm/components/instance/basic.js +17 -25
- package/dist-npm/components/instance/browser.js +14 -18
- package/dist-npm/components/instance/reporter/basic.js +46 -65
- package/dist-npm/components/instance/reporter/browser.js +44 -47
- package/dist-npm/components/instance/reporter/fancy.js +83 -96
- package/dist-npm/components/instance/shared.js +2 -2
- package/dist-npm/components/mono/mono.js +52 -62
- package/dist-npm/components/mono/monoTwo.js +35 -49
- package/dist-npm/components/multiselect/group-multiselect.js +55 -71
- package/dist-npm/components/multiselect/multi-select-two.js +97 -130
- package/dist-npm/components/multiselect/multi-select.js +43 -49
- package/dist-npm/components/multiselect/multiselect-main.d.ts +1 -0
- package/dist-npm/components/multiselect/multiselect-main.js +158 -145
- package/dist-npm/components/multiselect/num-multi-select.js +97 -130
- package/dist-npm/components/multiselect/num-multiselect-main.js +24 -35
- package/dist-npm/components/next-steps/next-steps.js +23 -25
- package/dist-npm/components/number/index.js +78 -112
- package/dist-npm/components/number/number-main.js +95 -2
- package/dist-npm/components/password/index.js +54 -73
- package/dist-npm/components/password/password-main.js +119 -2
- package/dist-npm/components/password/password-three.js +26 -30
- package/dist-npm/components/progressbar/ProgressBar.js +45 -64
- package/dist-npm/components/progressbar/helper.js +33 -40
- package/dist-npm/components/progressbar/index.js +1 -1
- package/dist-npm/components/prompts/create.js +29 -44
- package/dist-npm/components/prompts/index.d.ts +1 -0
- package/dist-npm/components/prompts/index.js +46 -45
- package/dist-npm/components/prompts/prompt.js +211 -260
- package/dist-npm/components/prompts/promptTwo.js +561 -605
- package/dist-npm/components/prompts/relinka.js +237 -295
- package/dist-npm/components/range/range.js +247 -294
- package/dist-npm/components/rawlist/index.js +87 -107
- package/dist-npm/components/results/results.js +31 -37
- package/dist-npm/components/search/index.js +148 -193
- package/dist-npm/components/select/index.js +148 -186
- package/dist-npm/components/select/num-select-main.js +27 -27
- package/dist-npm/components/select/num-select.js +124 -5
- package/dist-npm/components/select/select-key.js +24 -25
- package/dist-npm/components/select/select-main.d.ts +1 -0
- package/dist-npm/components/select/select-main.js +143 -133
- package/dist-npm/components/select/select-three.js +32 -36
- package/dist-npm/components/select/select-two.js +94 -87
- package/dist-npm/components/spinner/index.js +107 -136
- package/dist-npm/components/st-end/end.js +34 -26
- package/dist-npm/components/st-end/start.js +29 -15
- package/dist-npm/components/toggle/index.js +113 -137
- package/dist-npm/components/visual/animate/animate.js +53 -10
- package/dist-npm/components/visual/ascii-art/ascii-art.js +12 -1
- package/dist-npm/main.js +1 -0
- package/dist-npm/testing/index.js +58 -83
- package/dist-npm/types/general.d.ts +1 -1
- package/dist-npm/types/general.js +0 -1
- package/dist-npm/types/index.js +2 -3
- package/dist-npm/types/keypress.js +35 -36
- package/dist-npm/types/readline.js +0 -1
- package/dist-npm/types/relinka.js +0 -1
- package/dist-npm/types/utils.js +0 -1
- package/dist-npm/utils/box.js +135 -137
- package/dist-npm/utils/color.js +65 -74
- package/dist-npm/utils/colorize.js +124 -156
- package/dist-npm/utils/component.js +532 -657
- package/dist-npm/utils/constants.js +64 -63
- package/dist-npm/utils/core.js +2 -3
- package/dist-npm/utils/decoder.js +244 -223
- package/dist-npm/utils/error.js +4 -9
- package/dist-npm/utils/errors.js +14 -4
- package/dist-npm/utils/format.js +19 -24
- package/dist-npm/utils/keypress.js +316 -414
- package/dist-npm/utils/log.js +11 -15
- package/dist-npm/utils/mapping.js +45 -52
- package/dist-npm/utils/messages.js +196 -183
- package/dist-npm/utils/platforms.js +16 -20
- package/dist-npm/utils/prompt-tmp.js +235 -286
- package/dist-npm/utils/prompt-two.js +235 -286
- package/dist-npm/utils/readline.js +5 -7
- package/dist-npm/utils/skeleton.js +130 -170
- package/dist-npm/utils/stream.js +2 -2
- package/dist-npm/utils/string.js +44 -58
- package/dist-npm/utils/terminal.js +23 -34
- package/dist-npm/utils/tree.js +30 -41
- package/dist-npm/utils/types.js +0 -1
- package/dist-npm/utils/utils.js +8 -8
- package/dist-npm/utils/variants.js +36 -44
- package/package.json +14 -14
- package/dist-npm/mod.js +0 -2
- /package/dist-npm/{mod.d.ts → main.d.ts} +0 -0
|
@@ -1,227 +1,210 @@
|
|
|
1
|
-
import {Type} from
|
|
2
|
-
import {Value} from
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import { Value } from "@sinclair/typebox/value";
|
|
3
|
+
import { stdin as input, stdout as output } from "node:process";
|
|
4
|
+
import readline from "node:readline/promises";
|
|
5
|
+
import { cyanBright } from "picocolors";
|
|
3
6
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
buildRegExp,
|
|
8
|
+
digit,
|
|
9
|
+
endOfString,
|
|
10
|
+
repeat,
|
|
11
|
+
startOfString
|
|
12
|
+
} from "ts-regex-builder";
|
|
13
|
+
import { bar, fmt, msg } from "../../utils/messages.js";
|
|
8
14
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
startOfString,
|
|
14
|
-
} from 'ts-regex-builder';
|
|
15
|
-
import {fmt, msg} from '../../utils/messages.js';
|
|
16
|
-
import {
|
|
17
|
-
countLines,
|
|
18
|
-
deleteLastLine,
|
|
19
|
-
deleteLastLines,
|
|
20
|
-
} from '../../utils/terminal.js';
|
|
21
|
-
|
|
22
|
-
const isString = (a) => typeof a === 'string';
|
|
23
|
-
|
|
15
|
+
countLines,
|
|
16
|
+
deleteLastLine,
|
|
17
|
+
deleteLastLines
|
|
18
|
+
} from "../../utils/terminal.js";
|
|
24
19
|
const twoDigits = repeat(digit, 2);
|
|
25
20
|
const fourDigits = repeat(digit, 4);
|
|
26
|
-
const separatorDot =
|
|
27
|
-
const separatorSlash =
|
|
28
|
-
|
|
21
|
+
const separatorDot = ".";
|
|
22
|
+
const separatorSlash = "/";
|
|
29
23
|
const regexDDMMYYYY = buildRegExp([
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
24
|
+
startOfString,
|
|
25
|
+
twoDigits,
|
|
26
|
+
|
|
27
|
+
separatorDot,
|
|
28
|
+
twoDigits,
|
|
29
|
+
|
|
30
|
+
separatorDot,
|
|
31
|
+
fourDigits,
|
|
32
|
+
|
|
33
|
+
endOfString
|
|
37
34
|
]);
|
|
38
|
-
|
|
39
35
|
const regexMMDDYYYY = buildRegExp([
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
36
|
+
startOfString,
|
|
37
|
+
twoDigits,
|
|
38
|
+
|
|
39
|
+
separatorSlash,
|
|
40
|
+
twoDigits,
|
|
41
|
+
|
|
42
|
+
separatorSlash,
|
|
43
|
+
fourDigits,
|
|
44
|
+
|
|
45
|
+
endOfString
|
|
47
46
|
]);
|
|
48
|
-
|
|
49
47
|
const regexYYYYMMDD = buildRegExp([
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
48
|
+
startOfString,
|
|
49
|
+
fourDigits,
|
|
50
|
+
|
|
51
|
+
separatorDot,
|
|
52
|
+
twoDigits,
|
|
53
|
+
|
|
54
|
+
separatorDot,
|
|
55
|
+
twoDigits,
|
|
56
|
+
|
|
57
|
+
endOfString
|
|
57
58
|
]);
|
|
58
|
-
|
|
59
59
|
const dateFormatSchema = Type.Union([
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
Type.RegExp(regexMMDDYYYY, {
|
|
64
|
-
description: 'MM/DD/YYYY',
|
|
65
|
-
}),
|
|
66
|
-
Type.RegExp(regexYYYYMMDD, {
|
|
67
|
-
description: 'YYYY.MM.DD',
|
|
68
|
-
}),
|
|
60
|
+
Type.RegExp(regexDDMMYYYY, { description: "DD.MM.YYYY" }),
|
|
61
|
+
Type.RegExp(regexMMDDYYYY, { description: "MM/DD/YYYY" }),
|
|
62
|
+
Type.RegExp(regexYYYYMMDD, { description: "YYYY.MM.DD" })
|
|
69
63
|
]);
|
|
70
|
-
|
|
71
64
|
export async function datePrompt(options) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
65
|
+
const {
|
|
66
|
+
title,
|
|
67
|
+
dateFormat,
|
|
68
|
+
dateKind,
|
|
69
|
+
hint,
|
|
70
|
+
validate,
|
|
71
|
+
defaultValue,
|
|
72
|
+
schema,
|
|
73
|
+
titleColor = "cyanBright",
|
|
74
|
+
titleTypography = "bold",
|
|
75
|
+
titleVariant,
|
|
76
|
+
content,
|
|
77
|
+
contentColor,
|
|
78
|
+
contentTypography,
|
|
79
|
+
contentVariant,
|
|
80
|
+
borderColor = "viceGradient",
|
|
81
|
+
variantOptions
|
|
82
|
+
} = options;
|
|
83
|
+
const rl = readline.createInterface({ input, output });
|
|
84
|
+
const formattedBar = bar({ borderColor });
|
|
85
|
+
let linesToDelete = 0;
|
|
86
|
+
let errorMessage = "";
|
|
87
|
+
try {
|
|
88
|
+
while (true) {
|
|
89
|
+
if (linesToDelete > 0) {
|
|
90
|
+
deleteLastLines(linesToDelete);
|
|
91
|
+
}
|
|
92
|
+
const questionText = fmt({
|
|
93
|
+
type: errorMessage !== "" ? "M_ERROR_NULL" : "M_GENERAL_NULL",
|
|
94
|
+
title: `${title} [Format: ${dateFormat}]`,
|
|
95
|
+
titleColor,
|
|
96
|
+
titleTypography,
|
|
82
97
|
titleVariant,
|
|
83
98
|
content,
|
|
84
99
|
contentColor,
|
|
85
100
|
contentTypography,
|
|
86
101
|
contentVariant,
|
|
87
|
-
borderColor
|
|
102
|
+
borderColor,
|
|
103
|
+
hint,
|
|
88
104
|
variantOptions,
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
errorMessage = '';
|
|
187
|
-
if (schema) {
|
|
188
|
-
isValid = Value.Check(schema, answer);
|
|
189
|
-
if (!isValid) {
|
|
190
|
-
const errors = Value.Errors(schema, answer);
|
|
191
|
-
|
|
192
|
-
errorMessage = errors[0]?.message ?? 'Invalid input.';
|
|
193
|
-
msg({
|
|
194
|
-
type: 'M_ERROR',
|
|
195
|
-
title: errorMessage,
|
|
196
|
-
});
|
|
197
|
-
continue;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (validate && isValid) {
|
|
202
|
-
const validation = await validate(answer);
|
|
203
|
-
|
|
204
|
-
if (!validation) {
|
|
205
|
-
isValid = false;
|
|
206
|
-
errorMessage = isString(validation) ? validation : 'Invalid input.';
|
|
207
|
-
msg({
|
|
208
|
-
type: 'M_ERROR',
|
|
209
|
-
title: errorMessage,
|
|
210
|
-
});
|
|
211
|
-
continue;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (isValid) {
|
|
216
|
-
msg({
|
|
217
|
-
type: 'M_NEWLINE',
|
|
218
|
-
title: '',
|
|
219
|
-
});
|
|
220
|
-
rl.close();
|
|
221
|
-
return answer;
|
|
222
|
-
}
|
|
105
|
+
errorMessage,
|
|
106
|
+
addNewLineBefore: false
|
|
107
|
+
});
|
|
108
|
+
const questionLines = countLines(questionText);
|
|
109
|
+
linesToDelete = questionLines + 1;
|
|
110
|
+
msg({
|
|
111
|
+
type: errorMessage !== "" ? "M_ERROR" : "M_GENERAL",
|
|
112
|
+
title: questionText
|
|
113
|
+
});
|
|
114
|
+
deleteLastLine();
|
|
115
|
+
deleteLastLine();
|
|
116
|
+
const answer = (await rl.question(`${cyanBright(">")} `)).trim() || defaultValue;
|
|
117
|
+
if (answer === defaultValue && defaultValue) {
|
|
118
|
+
deleteLastLine();
|
|
119
|
+
msg({
|
|
120
|
+
type: "M_MIDDLE",
|
|
121
|
+
title: ` ${defaultValue}`,
|
|
122
|
+
titleColor: "none"
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
if (!Value.Check(dateFormatSchema, answer)) {
|
|
126
|
+
deleteLastLine();
|
|
127
|
+
deleteLastLine();
|
|
128
|
+
errorMessage = `Please enter a valid date in ${dateFormat} format.`;
|
|
129
|
+
msg({ type: "M_ERROR", title: errorMessage });
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
let matchedFormat = null;
|
|
133
|
+
if (regexDDMMYYYY.test(answer)) {
|
|
134
|
+
matchedFormat = "DD.MM.YYYY";
|
|
135
|
+
} else if (regexMMDDYYYY.test(answer)) {
|
|
136
|
+
matchedFormat = "MM/DD/YYYY";
|
|
137
|
+
} else if (regexYYYYMMDD.test(answer)) {
|
|
138
|
+
matchedFormat = "YYYY.MM.DD";
|
|
139
|
+
}
|
|
140
|
+
if (dateKind === "birthday" && matchedFormat) {
|
|
141
|
+
const parts = answer.split(/[./-]/);
|
|
142
|
+
let date;
|
|
143
|
+
if (matchedFormat === "DD.MM.YYYY") {
|
|
144
|
+
date = new Date(
|
|
145
|
+
Number(parts[2]),
|
|
146
|
+
Number(parts[1]) - 1,
|
|
147
|
+
Number(parts[0])
|
|
148
|
+
);
|
|
149
|
+
} else if (matchedFormat === "MM/DD/YYYY") {
|
|
150
|
+
date = new Date(
|
|
151
|
+
Number(parts[2]),
|
|
152
|
+
Number(parts[0]) - 1,
|
|
153
|
+
Number(parts[1])
|
|
154
|
+
);
|
|
155
|
+
} else if (matchedFormat === "YYYY.MM.DD") {
|
|
156
|
+
date = new Date(
|
|
157
|
+
Number(parts[0]),
|
|
158
|
+
Number(parts[1]) - 1,
|
|
159
|
+
Number(parts[2])
|
|
160
|
+
);
|
|
161
|
+
} else {
|
|
162
|
+
date = new Date(answer);
|
|
163
|
+
}
|
|
164
|
+
if (isNaN(date.getTime()) || date.getFullYear() < 1900 || date > new Date()) {
|
|
165
|
+
errorMessage = "Please enter a valid birthday date (e.g., 14.09.1999).";
|
|
166
|
+
msg({ type: "M_ERROR", title: errorMessage });
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
let isValid = true;
|
|
171
|
+
let gotError = false;
|
|
172
|
+
if (errorMessage !== "") {
|
|
173
|
+
gotError = true;
|
|
174
|
+
}
|
|
175
|
+
errorMessage = "";
|
|
176
|
+
if (schema) {
|
|
177
|
+
isValid = Value.Check(schema, answer);
|
|
178
|
+
if (!isValid) {
|
|
179
|
+
const errors = [...Value.Errors(schema, answer)];
|
|
180
|
+
errorMessage = errors[0]?.message ?? "Invalid input.";
|
|
181
|
+
msg({ type: "M_ERROR", title: errorMessage });
|
|
182
|
+
gotError = true;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (validate && isValid) {
|
|
187
|
+
const validation = await validate(answer);
|
|
188
|
+
if (validation !== true) {
|
|
189
|
+
isValid = false;
|
|
190
|
+
errorMessage = typeof validation === "string" ? validation : "Invalid input.";
|
|
191
|
+
msg({ type: "M_ERROR", title: errorMessage });
|
|
192
|
+
gotError = true;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (isValid) {
|
|
197
|
+
if (gotError) {
|
|
198
|
+
deleteLastLine();
|
|
199
|
+
deleteLastLine();
|
|
200
|
+
msg({ type: "M_MIDDLE", title: ` ${answer}` });
|
|
223
201
|
}
|
|
224
|
-
|
|
202
|
+
msg({ type: "M_NEWLINE", title: "" });
|
|
225
203
|
rl.close();
|
|
204
|
+
return answer;
|
|
205
|
+
}
|
|
226
206
|
}
|
|
207
|
+
} finally {
|
|
208
|
+
rl.close();
|
|
209
|
+
}
|
|
227
210
|
}
|
|
@@ -1,99 +1,78 @@
|
|
|
1
|
-
import {editAsync} from
|
|
2
|
-
import {AsyncResource} from
|
|
1
|
+
import { editAsync } from "external-editor";
|
|
2
|
+
import { AsyncResource } from "node:async_hooks";
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from
|
|
12
|
-
|
|
4
|
+
createPrompt,
|
|
5
|
+
useEffect,
|
|
6
|
+
useState,
|
|
7
|
+
useKeypress,
|
|
8
|
+
usePrefix,
|
|
9
|
+
isEnterKey,
|
|
10
|
+
makeTheme
|
|
11
|
+
} from "../../components/core/index.js";
|
|
13
12
|
export default createPrompt((config, done) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
} else {
|
|
44
|
-
setValue(answer);
|
|
45
|
-
setError(isValid || 'You must provide a valid value');
|
|
46
|
-
setStatus('idle');
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
editAsync(value, (error2, answer) => void editCallback(error2, answer), {
|
|
52
|
-
postfix,
|
|
53
|
-
...fileProps,
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
useEffect((rl) => {
|
|
58
|
-
if (!waitForUseInput) {
|
|
59
|
-
startEditor(rl);
|
|
60
|
-
}
|
|
61
|
-
}, []);
|
|
62
|
-
useKeypress((key, rl) => {
|
|
63
|
-
if (status !== 'idle') {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (isEnterKey(key)) {
|
|
68
|
-
startEditor(rl);
|
|
13
|
+
const {
|
|
14
|
+
waitForUseInput = true,
|
|
15
|
+
file: { postfix = config.postfix ?? ".txt", ...fileProps } = {},
|
|
16
|
+
validate = () => true
|
|
17
|
+
} = config;
|
|
18
|
+
const theme = makeTheme(config.theme);
|
|
19
|
+
const [status, setStatus] = useState("idle");
|
|
20
|
+
const [value, setValue] = useState(config.default || "");
|
|
21
|
+
const [errorMsg, setError] = useState();
|
|
22
|
+
const prefix = usePrefix({ status, theme });
|
|
23
|
+
function startEditor(rl) {
|
|
24
|
+
rl.pause();
|
|
25
|
+
const editCallback = AsyncResource.bind(
|
|
26
|
+
async (error2, answer) => {
|
|
27
|
+
rl.resume();
|
|
28
|
+
if (error2) {
|
|
29
|
+
setError(error2.toString());
|
|
30
|
+
} else {
|
|
31
|
+
setStatus("loading");
|
|
32
|
+
const isValid = await validate(answer);
|
|
33
|
+
if (isValid === true) {
|
|
34
|
+
setError(void 0);
|
|
35
|
+
setStatus("done");
|
|
36
|
+
done(answer);
|
|
37
|
+
} else {
|
|
38
|
+
setValue(answer);
|
|
39
|
+
setError(isValid || "You must provide a valid value");
|
|
40
|
+
setStatus("idle");
|
|
41
|
+
}
|
|
69
42
|
}
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
editAsync(value, (error2, answer) => void editCallback(error2, answer), {
|
|
46
|
+
postfix,
|
|
47
|
+
...fileProps
|
|
70
48
|
});
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
49
|
+
}
|
|
50
|
+
useEffect((rl) => {
|
|
51
|
+
if (!waitForUseInput) {
|
|
52
|
+
startEditor(rl);
|
|
53
|
+
}
|
|
54
|
+
}, []);
|
|
55
|
+
useKeypress((key, rl) => {
|
|
56
|
+
if (status !== "idle") {
|
|
57
|
+
return;
|
|
80
58
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
let error = '';
|
|
84
|
-
|
|
85
|
-
if (errorMsg) {
|
|
86
|
-
error = theme.style.error(errorMsg);
|
|
59
|
+
if (isEnterKey(key)) {
|
|
60
|
+
startEditor(rl);
|
|
87
61
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
62
|
+
});
|
|
63
|
+
const message = theme.style.message(config.message, status);
|
|
64
|
+
let helpTip = "";
|
|
65
|
+
if (status === "loading") {
|
|
66
|
+
helpTip = theme.style.help("Received");
|
|
67
|
+
} else if (status === "idle") {
|
|
68
|
+
const enterKey = theme.style.key("enter");
|
|
69
|
+
helpTip = theme.style.help(
|
|
70
|
+
`Press ${enterKey} to launch your preferred editor.`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
let error = "";
|
|
74
|
+
if (errorMsg) {
|
|
75
|
+
error = theme.style.error(errorMsg);
|
|
76
|
+
}
|
|
77
|
+
return [[prefix, message, helpTip].filter(Boolean).join(" "), error];
|
|
99
78
|
});
|