@reliverse/relinka 1.1.5 → 1.1.7
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,217 +1,250 @@
|
|
|
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
|
-
} from
|
|
20
|
-
import figures from
|
|
4
|
+
createPrompt,
|
|
5
|
+
useState,
|
|
6
|
+
useKeypress,
|
|
7
|
+
usePrefix,
|
|
8
|
+
usePagination,
|
|
9
|
+
useRef,
|
|
10
|
+
useMemo,
|
|
11
|
+
makeTheme,
|
|
12
|
+
isUpKey,
|
|
13
|
+
isDownKey,
|
|
14
|
+
isSpaceKey,
|
|
15
|
+
isNumberKey,
|
|
16
|
+
isEnterKey,
|
|
17
|
+
ValidationError,
|
|
18
|
+
Separator,
|
|
19
|
+
} from '../../components/core/index.js';
|
|
20
|
+
import figures from '../../components/figures/index.js';
|
|
21
|
+
|
|
22
|
+
const isString = (a) => typeof a === 'string';
|
|
23
|
+
|
|
21
24
|
const checkboxTheme = {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
icon: {
|
|
26
|
+
checked: colors.green(figures.circleFilled),
|
|
27
|
+
unchecked: figures.circle,
|
|
28
|
+
cursor: figures.pointer,
|
|
29
|
+
},
|
|
30
|
+
style: {
|
|
31
|
+
disabledChoice: (text) => colors.dim(`- ${text}`),
|
|
32
|
+
renderSelectedChoices: (selectedChoices) => selectedChoices.map((choice) => choice.short).join(', '),
|
|
33
|
+
description: (text) => colors.cyan(text),
|
|
34
|
+
},
|
|
35
|
+
helpMode: 'auto',
|
|
33
36
|
};
|
|
37
|
+
|
|
34
38
|
function isSelectable(item) {
|
|
35
|
-
|
|
39
|
+
return !Separator.isSeparator(item) && !item.disabled;
|
|
36
40
|
}
|
|
41
|
+
|
|
37
42
|
function isChecked(item) {
|
|
38
|
-
|
|
43
|
+
return isSelectable(item) && Boolean(item.checked);
|
|
39
44
|
}
|
|
45
|
+
|
|
40
46
|
function toggle(item) {
|
|
41
|
-
|
|
47
|
+
return isSelectable(item) ? { ...item,
|
|
48
|
+
checked: !item.checked} : item;
|
|
42
49
|
}
|
|
50
|
+
|
|
43
51
|
function check(checked) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
};
|
|
52
|
+
return (item) => isSelectable(item) ? { ...item,
|
|
53
|
+
checked} : item;
|
|
47
54
|
}
|
|
55
|
+
|
|
48
56
|
function normalizeChoices(choices) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
57
|
+
return choices.map((choice) => {
|
|
58
|
+
if (Separator.isSeparator(choice)) {
|
|
59
|
+
return choice;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (isString(choice)) {
|
|
63
|
+
return {
|
|
64
|
+
value: choice,
|
|
65
|
+
name: choice,
|
|
66
|
+
short: choice,
|
|
67
|
+
disabled: false,
|
|
68
|
+
checked: false,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const name = choice.name ?? String(choice.value);
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
value: choice.value,
|
|
76
|
+
name,
|
|
77
|
+
|
|
78
|
+
short: choice.short ?? name,
|
|
79
|
+
|
|
80
|
+
description: choice.description,
|
|
81
|
+
|
|
82
|
+
disabled: choice.disabled ?? false,
|
|
83
|
+
|
|
84
|
+
checked: choice.checked ?? false,
|
|
85
|
+
};
|
|
86
|
+
});
|
|
77
87
|
}
|
|
78
|
-
|
|
79
|
-
|
|
88
|
+
|
|
89
|
+
const checkbox = createPrompt((config, done) => {
|
|
80
90
|
const {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
91
|
+
instructions,
|
|
92
|
+
pageSize = 7,
|
|
93
|
+
loop = true,
|
|
94
|
+
required,
|
|
95
|
+
validate = () => true,
|
|
86
96
|
} = config;
|
|
97
|
+
|
|
87
98
|
const theme = makeTheme(checkboxTheme, config.theme);
|
|
88
99
|
const firstRender = useRef(true);
|
|
89
|
-
const [status, setStatus] = useState(
|
|
90
|
-
const prefix = usePrefix({
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
);
|
|
100
|
+
const [status, setStatus] = useState('idle');
|
|
101
|
+
const prefix = usePrefix({
|
|
102
|
+
status,
|
|
103
|
+
theme,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const [items, setItems] = useState(normalizeChoices(config.choices));
|
|
107
|
+
|
|
94
108
|
const bounds = useMemo(() => {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
109
|
+
const first = items.findIndex(isSelectable);
|
|
110
|
+
const last = items.findLastIndex(isSelectable);
|
|
111
|
+
|
|
112
|
+
if (first === -1) {
|
|
113
|
+
throw new ValidationError('[checkbox prompt] No selectable choices. All choices are disabled.');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
first,
|
|
118
|
+
last,
|
|
119
|
+
};
|
|
103
120
|
}, [items]);
|
|
121
|
+
|
|
104
122
|
const [active, setActive] = useState(bounds.first);
|
|
105
123
|
const [showHelpTip, setShowHelpTip] = useState(true);
|
|
106
124
|
const [errorMsg, setError] = useState();
|
|
125
|
+
|
|
107
126
|
useKeypress(async (key) => {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
127
|
+
if (isEnterKey(key)) {
|
|
128
|
+
const selection = items.filter(isChecked);
|
|
129
|
+
const isValid = await validate(selection.slice());
|
|
130
|
+
|
|
131
|
+
if (required && !items.some(isChecked)) {
|
|
132
|
+
setError('At least one choice must be selected');
|
|
133
|
+
} else if (isValid) {
|
|
134
|
+
setStatus('done');
|
|
135
|
+
done(selection.map((choice) => choice.value));
|
|
136
|
+
} else {
|
|
137
|
+
setError(isValid || 'You must select a valid value');
|
|
138
|
+
}
|
|
139
|
+
} else if (isUpKey(key) || isDownKey(key)) {
|
|
140
|
+
if (loop || isUpKey(key) && active !== bounds.first || isDownKey(key) && active !== bounds.last) {
|
|
141
|
+
const offset = isUpKey(key) ? -1 : 1;
|
|
142
|
+
let next = active;
|
|
143
|
+
|
|
144
|
+
do {
|
|
145
|
+
next = (next + offset + items.length) % items.length;
|
|
146
|
+
} while (!isSelectable(items[next]))
|
|
147
|
+
setActive(next);
|
|
148
|
+
}
|
|
149
|
+
} else if (isSpaceKey(key)) {
|
|
150
|
+
setError(void 0);
|
|
151
|
+
setShowHelpTip(false);
|
|
152
|
+
setItems(items.map((choice, i) => i === active ? toggle(choice) : choice));
|
|
153
|
+
} else if (key.name === 'a') {
|
|
154
|
+
const selectAll = items.some((choice) => isSelectable(choice) && !choice.checked);
|
|
155
|
+
|
|
156
|
+
setItems(items.map(check(selectAll)));
|
|
157
|
+
} else if (key.name === 'i') {
|
|
158
|
+
setItems(items.map(toggle));
|
|
159
|
+
} else if (isNumberKey(key)) {
|
|
160
|
+
const position = Number(key.name) - 1;
|
|
161
|
+
const item = items[position];
|
|
162
|
+
|
|
163
|
+
if (item != null && isSelectable(item)) {
|
|
164
|
+
setActive(position);
|
|
165
|
+
setItems(items.map((choice, i) => i === position ? toggle(choice) : choice));
|
|
166
|
+
}
|
|
118
167
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
do {
|
|
124
|
-
next = (next + offset + items.length) % items.length;
|
|
125
|
-
} while (!isSelectable(items[next]));
|
|
126
|
-
setActive(next);
|
|
127
|
-
}
|
|
128
|
-
} else if (isSpaceKey(key)) {
|
|
129
|
-
setError(void 0);
|
|
130
|
-
setShowHelpTip(false);
|
|
131
|
-
setItems(
|
|
132
|
-
items.map((choice, i) => i === active ? toggle(choice) : choice)
|
|
133
|
-
);
|
|
134
|
-
} else if (key.name === "a") {
|
|
135
|
-
const selectAll = items.some(
|
|
136
|
-
(choice) => isSelectable(choice) && !choice.checked
|
|
137
|
-
);
|
|
138
|
-
setItems(items.map(check(selectAll)));
|
|
139
|
-
} else if (key.name === "i") {
|
|
140
|
-
setItems(items.map(toggle));
|
|
141
|
-
} else if (isNumberKey(key)) {
|
|
142
|
-
const position = Number(key.name) - 1;
|
|
143
|
-
const item = items[position];
|
|
144
|
-
if (item != null && isSelectable(item)) {
|
|
145
|
-
setActive(position);
|
|
146
|
-
setItems(
|
|
147
|
-
items.map(
|
|
148
|
-
(choice, i) => i === position ? toggle(choice) : choice
|
|
149
|
-
)
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
153
172
|
});
|
|
154
173
|
const message = theme.style.message(config.message, status);
|
|
155
174
|
let description;
|
|
175
|
+
|
|
156
176
|
const page = usePagination({
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
+
items,
|
|
178
|
+
active,
|
|
179
|
+
renderItem({item, isActive}) {
|
|
180
|
+
if (Separator.isSeparator(item)) {
|
|
181
|
+
return ` ${item.separator}`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (item.disabled) {
|
|
185
|
+
const disabledLabel = isString(item.disabled) ? item.disabled : '(disabled)';
|
|
186
|
+
return theme.style.disabledChoice(`${item.name} ${disabledLabel}`);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (isActive) {
|
|
190
|
+
description = item.description;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const checkbox2 = item.checked ? theme.icon.checked : theme.icon.unchecked;
|
|
194
|
+
const color = isActive ? theme.style.highlight : (x) => x;
|
|
195
|
+
const cursor = isActive ? theme.icon.cursor : ' ';
|
|
196
|
+
|
|
197
|
+
return color(`${cursor}${checkbox2} ${item.name}`);
|
|
198
|
+
},
|
|
199
|
+
pageSize,
|
|
200
|
+
loop,
|
|
177
201
|
});
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
theme.style.renderSelectedChoices(selection, items)
|
|
182
|
-
|
|
183
|
-
|
|
202
|
+
|
|
203
|
+
if (status === 'done') {
|
|
204
|
+
const selection = items.filter(isChecked);
|
|
205
|
+
const answer = theme.style.answer(theme.style.renderSelectedChoices(selection, items));
|
|
206
|
+
|
|
207
|
+
return `${prefix} ${message} ${answer}`;
|
|
184
208
|
}
|
|
185
|
-
|
|
186
|
-
let
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
209
|
+
|
|
210
|
+
let helpTipTop = '';
|
|
211
|
+
let helpTipBottom = '';
|
|
212
|
+
|
|
213
|
+
if (theme.helpMode === 'always' || theme.helpMode === 'auto' && showHelpTip && (instructions === void 0 || instructions)) {
|
|
214
|
+
if (isString(instructions)) {
|
|
215
|
+
helpTipTop = instructions;
|
|
216
|
+
} else {
|
|
217
|
+
const keys = [
|
|
218
|
+
`${theme.style.key('space')} to select`,
|
|
219
|
+
`${theme.style.key('a')} to toggle all`,
|
|
220
|
+
`${theme.style.key('i')} to invert selection`,
|
|
221
|
+
`and ${theme.style.key('enter')} to proceed`,
|
|
222
|
+
];
|
|
223
|
+
|
|
224
|
+
helpTipTop = ` (Press ${keys.join(', ')})`;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (items.length > pageSize && (theme.helpMode === 'always' || theme.helpMode === 'auto' && firstRender.current)) {
|
|
228
|
+
helpTipBottom = `
|
|
229
|
+
${theme.style.help('(Use arrow keys to reveal more choices)')}`;
|
|
230
|
+
firstRender.current = false;
|
|
231
|
+
}
|
|
204
232
|
}
|
|
233
|
+
|
|
205
234
|
const choiceDescription = description ? `
|
|
206
235
|
${theme.style.description(description)}` : ``;
|
|
207
|
-
|
|
236
|
+
|
|
237
|
+
let error = '';
|
|
238
|
+
|
|
208
239
|
if (errorMsg) {
|
|
209
|
-
|
|
240
|
+
error = `
|
|
210
241
|
${theme.style.error(errorMsg)}`;
|
|
211
242
|
}
|
|
243
|
+
|
|
212
244
|
return `${prefix} ${message}${helpTipTop}
|
|
213
245
|
${page}${helpTipBottom}${choiceDescription}${error}${ansiEscapes.cursorHide}`;
|
|
214
|
-
|
|
215
|
-
|
|
246
|
+
});
|
|
247
|
+
|
|
216
248
|
export default checkbox;
|
|
217
|
-
|
|
249
|
+
|
|
250
|
+
export {Separator} from '../../components/core/index.js';
|
|
@@ -1,98 +1,135 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import
|
|
1
|
+
import {Value} from '@sinclair/typebox/value';
|
|
2
|
+
import process, {
|
|
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
|
+
deleteLastLines,
|
|
16
|
+
} from '../../utils/terminal.js';
|
|
17
|
+
|
|
7
18
|
export async function confirmPrompt(options) {
|
|
8
|
-
|
|
9
|
-
title,
|
|
10
|
-
defaultValue,
|
|
11
|
-
schema,
|
|
12
|
-
titleColor = "cyanBright",
|
|
13
|
-
titleTypography = "bold",
|
|
14
|
-
titleVariant,
|
|
15
|
-
content,
|
|
16
|
-
contentColor,
|
|
17
|
-
contentTypography,
|
|
18
|
-
contentVariant,
|
|
19
|
-
borderColor = "viceGradient",
|
|
20
|
-
hintColor = "dim",
|
|
21
|
-
variantOptions,
|
|
22
|
-
action
|
|
23
|
-
} = options;
|
|
24
|
-
const rl = readline.createInterface({ input, output });
|
|
25
|
-
let linesToDelete = 0;
|
|
26
|
-
let errorMessage = "";
|
|
27
|
-
try {
|
|
28
|
-
while (true) {
|
|
29
|
-
if (linesToDelete > 0) {
|
|
30
|
-
deleteLastLines(linesToDelete);
|
|
31
|
-
}
|
|
32
|
-
const question = fmt({
|
|
33
|
-
type: errorMessage !== "" ? "M_ERROR" : "M_GENERAL",
|
|
19
|
+
const {
|
|
34
20
|
title,
|
|
35
|
-
|
|
36
|
-
|
|
21
|
+
defaultValue,
|
|
22
|
+
schema,
|
|
23
|
+
titleColor = 'cyanBright',
|
|
24
|
+
titleTypography = 'bold',
|
|
37
25
|
titleVariant,
|
|
38
26
|
content,
|
|
39
27
|
contentColor,
|
|
40
28
|
contentTypography,
|
|
41
29
|
contentVariant,
|
|
42
|
-
borderColor,
|
|
30
|
+
borderColor = 'viceGradient',
|
|
31
|
+
hintColor = 'dim',
|
|
43
32
|
variantOptions,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
33
|
+
action,
|
|
34
|
+
} = options;
|
|
35
|
+
|
|
36
|
+
const rl = readline.createInterface({
|
|
37
|
+
input,
|
|
38
|
+
output,
|
|
39
|
+
});
|
|
40
|
+
let linesToDelete = 0;
|
|
41
|
+
let errorMessage = '';
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
while (true) {
|
|
45
|
+
if (linesToDelete > 0) {
|
|
46
|
+
deleteLastLines(linesToDelete);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const question = fmt({
|
|
50
|
+
type: errorMessage !== '' ? 'M_ERROR' : 'M_GENERAL',
|
|
51
|
+
title,
|
|
52
|
+
titleColor,
|
|
53
|
+
titleTypography,
|
|
54
|
+
titleVariant,
|
|
55
|
+
content,
|
|
56
|
+
contentColor,
|
|
57
|
+
contentTypography,
|
|
58
|
+
contentVariant,
|
|
59
|
+
borderColor,
|
|
60
|
+
variantOptions,
|
|
61
|
+
errorMessage,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
let defaultHint = '';
|
|
65
|
+
|
|
66
|
+
if (defaultValue) {
|
|
67
|
+
defaultHint = '[Y/n]';
|
|
68
|
+
} else if (!defaultValue) {
|
|
69
|
+
defaultHint = '[y/N]';
|
|
70
|
+
} else {
|
|
71
|
+
defaultHint = '[y/n]';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
const fullPrompt = `${question}${colorize(defaultHint, hintColor)}: `;
|
|
76
|
+
|
|
77
|
+
const formattedPrompt = fmt({
|
|
78
|
+
type: 'M_NULL',
|
|
79
|
+
title: fullPrompt,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const questionLines = countLines(formattedPrompt);
|
|
83
|
+
|
|
84
|
+
linesToDelete = questionLines + 1;
|
|
85
|
+
const answer = (await rl.question(formattedPrompt))
|
|
86
|
+
.toLowerCase()
|
|
87
|
+
.trim();
|
|
88
|
+
let value;
|
|
89
|
+
const formattedBar = bar({
|
|
90
|
+
borderColor,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
if (!answer && defaultValue !== void 0) {
|
|
94
|
+
const injectedAnswer = defaultValue ? 'y' : 'n';
|
|
95
|
+
|
|
96
|
+
process.stdout.write(`${formattedBar} ${injectedAnswer}
|
|
67
97
|
`);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
98
|
+
value = defaultValue;
|
|
99
|
+
} else if (answer === 'y' || answer === 'yes') {
|
|
100
|
+
value = true;
|
|
101
|
+
} else if (answer === 'n' || answer === 'no') {
|
|
102
|
+
value = false;
|
|
103
|
+
} else {
|
|
104
|
+
errorMessage = 'Please answer with "y" or "n".';
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
let isValid = true;
|
|
110
|
+
|
|
111
|
+
errorMessage = '';
|
|
112
|
+
if (schema) {
|
|
113
|
+
isValid = Value.Check(schema, value);
|
|
114
|
+
if (!isValid) {
|
|
115
|
+
const errors = Value.Errors(schema, value);
|
|
116
|
+
errorMessage = errors.length > 0 ? errors[0]?.message ?? 'Invalid input.' : 'Invalid input.';
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (isValid) {
|
|
121
|
+
msg({
|
|
122
|
+
type: 'M_NEWLINE',
|
|
123
|
+
});
|
|
124
|
+
rl.close();
|
|
125
|
+
if (action && value) {
|
|
126
|
+
await action();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return value;
|
|
130
|
+
}
|
|
84
131
|
}
|
|
85
|
-
|
|
86
|
-
if (isValid) {
|
|
87
|
-
msg({ type: "M_NEWLINE" });
|
|
132
|
+
} finally {
|
|
88
133
|
rl.close();
|
|
89
|
-
if (action && value) {
|
|
90
|
-
await action();
|
|
91
|
-
}
|
|
92
|
-
return value;
|
|
93
|
-
}
|
|
94
134
|
}
|
|
95
|
-
} finally {
|
|
96
|
-
rl.close();
|
|
97
|
-
}
|
|
98
135
|
}
|