@reliverse/relinka 1.1.4 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-npm/components/anykey/index.js +77 -57
- package/dist-npm/components/block/block.js +61 -48
- package/dist-npm/components/checkbox/index.js +211 -178
- package/dist-npm/components/confirm/confirm-main.js +122 -86
- package/dist-npm/components/confirm/confirm-three.js +27 -24
- package/dist-npm/components/confirm/index.js +45 -36
- package/dist-npm/components/core/Separator.js +17 -15
- package/dist-npm/components/core/create-prompt.js +124 -101
- package/dist-npm/components/core/errors.js +15 -19
- package/dist-npm/components/core/hook-engine.js +120 -91
- package/dist-npm/components/core/index.js +12 -12
- package/dist-npm/components/core/key.js +6 -16
- package/dist-npm/components/core/lines.js +54 -45
- package/dist-npm/components/core/make-theme.js +30 -21
- package/dist-npm/components/core/position.js +23 -27
- package/dist-npm/components/core/promise-polyfill.js +14 -11
- package/dist-npm/components/core/screen-manager.js +74 -59
- package/dist-npm/components/core/theme.js +32 -22
- package/dist-npm/components/core/use-effect.js +15 -9
- package/dist-npm/components/core/use-keypress.js +23 -19
- package/dist-npm/components/core/use-memo.js +16 -10
- package/dist-npm/components/core/use-pagination.js +32 -33
- package/dist-npm/components/core/use-prefix.js +38 -40
- package/dist-npm/components/core/use-ref.js +5 -2
- package/dist-npm/components/core/use-state.js +23 -15
- package/dist-npm/components/core/useKeyPress.js +17 -14
- package/dist-npm/components/core/usePromptState.js +14 -8
- package/dist-npm/components/core/utils.js +16 -7
- package/dist-npm/components/date/date.js +204 -168
- package/dist-npm/components/editor/index.js +92 -71
- package/dist-npm/components/expand/index.js +124 -96
- package/dist-npm/components/figures/index.js +294 -283
- package/dist-npm/components/input/index.js +87 -61
- package/dist-npm/components/input/text-main.js +124 -86
- package/dist-npm/components/input/text.js +28 -24
- package/dist-npm/components/instance/basic.js +25 -17
- package/dist-npm/components/instance/browser.js +18 -14
- package/dist-npm/components/instance/reporter/basic.js +65 -46
- package/dist-npm/components/instance/reporter/browser.js +47 -44
- package/dist-npm/components/instance/reporter/fancy.js +96 -83
- package/dist-npm/components/instance/shared.js +2 -2
- package/dist-npm/components/mono/mono.js +62 -52
- package/dist-npm/components/mono/monoTwo.js +49 -35
- package/dist-npm/components/multiselect/group-multiselect.js +71 -55
- package/dist-npm/components/multiselect/multi-select-two.js +130 -98
- package/dist-npm/components/multiselect/multi-select.js +49 -43
- package/dist-npm/components/multiselect/multiselect-main.js +146 -120
- package/dist-npm/components/multiselect/num-multi-select.js +130 -98
- package/dist-npm/components/multiselect/num-multiselect-main.js +35 -24
- package/dist-npm/components/next-steps/next-steps.js +25 -24
- package/dist-npm/components/number/index.js +112 -78
- package/dist-npm/components/number/number-main.js +2 -96
- package/dist-npm/components/password/index.js +73 -54
- package/dist-npm/components/password/password-main.js +2 -120
- package/dist-npm/components/password/password-three.js +30 -26
- package/dist-npm/components/progressbar/ProgressBar.js +64 -45
- package/dist-npm/components/progressbar/helper.js +40 -33
- package/dist-npm/components/progressbar/index.js +1 -1
- package/dist-npm/components/prompts/create.js +44 -29
- package/dist-npm/components/prompts/index.js +45 -45
- package/dist-npm/components/prompts/prompt.js +260 -211
- package/dist-npm/components/prompts/promptTwo.js +605 -561
- package/dist-npm/components/prompts/relinka.js +295 -237
- package/dist-npm/components/range/range.js +294 -247
- package/dist-npm/components/rawlist/index.js +107 -87
- package/dist-npm/components/results/results.js +37 -31
- package/dist-npm/components/search/index.js +193 -148
- package/dist-npm/components/select/index.js +186 -148
- package/dist-npm/components/select/num-select-main.js +27 -27
- package/dist-npm/components/select/num-select.js +5 -125
- package/dist-npm/components/select/select-key.js +25 -24
- package/dist-npm/components/select/select-main.js +133 -109
- package/dist-npm/components/select/select-three.js +36 -32
- package/dist-npm/components/select/select-two.js +87 -95
- package/dist-npm/components/spinner/index.js +136 -107
- package/dist-npm/components/st-end/end.d.ts +1 -1
- package/dist-npm/components/st-end/end.js +26 -35
- package/dist-npm/components/st-end/start.d.ts +1 -1
- package/dist-npm/components/st-end/start.js +15 -30
- package/dist-npm/components/toggle/index.js +137 -113
- package/dist-npm/components/visual/animate/animate.js +10 -53
- package/dist-npm/components/visual/ascii-art/ascii-art.js +1 -12
- package/dist-npm/mod.js +2 -1
- package/dist-npm/testing/index.js +83 -58
- package/dist-npm/types/general.d.ts +2 -1
- package/dist-npm/types/general.js +1 -0
- package/dist-npm/types/index.js +3 -2
- package/dist-npm/types/keypress.js +36 -35
- package/dist-npm/types/readline.js +1 -0
- package/dist-npm/types/relinka.js +1 -0
- package/dist-npm/types/utils.js +1 -0
- package/dist-npm/utils/box.js +137 -135
- package/dist-npm/utils/color.js +74 -65
- package/dist-npm/utils/colorize.js +156 -124
- package/dist-npm/utils/component.js +657 -532
- package/dist-npm/utils/constants.js +63 -64
- package/dist-npm/utils/core.js +3 -2
- package/dist-npm/utils/decoder.js +223 -244
- package/dist-npm/utils/error.js +9 -4
- package/dist-npm/utils/errors.js +4 -14
- package/dist-npm/utils/format.js +24 -19
- package/dist-npm/utils/keypress.js +414 -316
- package/dist-npm/utils/log.js +15 -11
- package/dist-npm/utils/mapping.js +52 -45
- package/dist-npm/utils/messages.js +183 -183
- package/dist-npm/utils/platforms.js +20 -16
- package/dist-npm/utils/prompt-tmp.js +286 -235
- package/dist-npm/utils/prompt-two.js +286 -235
- package/dist-npm/utils/readline.js +7 -5
- package/dist-npm/utils/skeleton.js +170 -130
- package/dist-npm/utils/stream.js +2 -2
- package/dist-npm/utils/string.js +58 -44
- package/dist-npm/utils/terminal.js +34 -23
- package/dist-npm/utils/tree.js +41 -30
- package/dist-npm/utils/types.js +1 -0
- package/dist-npm/utils/utils.js +8 -8
- package/dist-npm/utils/variants.js +44 -36
- package/package.json +20 -28
|
@@ -1,110 +1,139 @@
|
|
|
1
|
-
import process from
|
|
2
|
-
import ora from
|
|
3
|
-
import color from
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
import ora from 'ora';
|
|
3
|
+
import color from 'picocolors';
|
|
4
|
+
import {cursor, erase} from 'sisteransi';
|
|
5
|
+
import {msg} from '../../utils/messages.js';
|
|
6
|
+
|
|
7
|
+
const isFn = (a) => typeof a === 'function';
|
|
8
|
+
|
|
6
9
|
export async function spinner(options) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
10
|
+
const {
|
|
11
|
+
initialMessage,
|
|
12
|
+
successMessage = 'Task completed successfully.',
|
|
13
|
+
errorMessage = 'An error occurred during the task.',
|
|
14
|
+
delay = 100,
|
|
15
|
+
spinnerSolution,
|
|
16
|
+
spinnerType,
|
|
17
|
+
action,
|
|
18
|
+
} = options;
|
|
19
|
+
|
|
20
|
+
let message = initialMessage;
|
|
21
|
+
let interval = null;
|
|
22
|
+
let frameIndex = 0;
|
|
23
|
+
|
|
24
|
+
if (spinnerSolution === 'ora') {
|
|
25
|
+
const oraSpinner = ora({
|
|
26
|
+
text: initialMessage,
|
|
27
|
+
spinner: spinnerType,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
oraSpinner.start();
|
|
32
|
+
await action((newMessage) => {
|
|
33
|
+
message = newMessage;
|
|
34
|
+
oraSpinner.text = newMessage;
|
|
35
|
+
});
|
|
36
|
+
oraSpinner.stop();
|
|
37
|
+
msg({
|
|
38
|
+
type: 'M_INFO',
|
|
39
|
+
title: successMessage,
|
|
40
|
+
titleColor: 'green',
|
|
41
|
+
});
|
|
42
|
+
} catch(error) {
|
|
43
|
+
oraSpinner.stopAndPersist({
|
|
44
|
+
symbol: color.red('\u2716'),
|
|
45
|
+
text: errorMessage,
|
|
46
|
+
});
|
|
47
|
+
msg({
|
|
48
|
+
type: 'M_ERROR',
|
|
49
|
+
title: error instanceof Error ? error.message : 'An unknown error occurred.',
|
|
50
|
+
titleColor: 'red',
|
|
51
|
+
});
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
const simpleSpinners = {
|
|
56
|
+
default: [
|
|
57
|
+
'\u280B',
|
|
58
|
+
'\u2819',
|
|
59
|
+
'\u2839',
|
|
60
|
+
'\u2838',
|
|
61
|
+
'\u283C',
|
|
62
|
+
'\u2834',
|
|
63
|
+
'\u2826',
|
|
64
|
+
'\u2827',
|
|
65
|
+
'\u2807',
|
|
66
|
+
'\u280F',
|
|
67
|
+
],
|
|
68
|
+
dottedCircle: [
|
|
69
|
+
'\u25CB',
|
|
70
|
+
'\u25D4',
|
|
71
|
+
'\u25D1',
|
|
72
|
+
'\u25D5',
|
|
73
|
+
'\u25CF',
|
|
74
|
+
],
|
|
75
|
+
boxSpinner: [
|
|
76
|
+
'\u2596',
|
|
77
|
+
'\u2598',
|
|
78
|
+
'\u259D',
|
|
79
|
+
'\u2597',
|
|
80
|
+
],
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const frames = spinnerType && spinnerType in simpleSpinners ? simpleSpinners[spinnerType] : simpleSpinners.default;
|
|
84
|
+
const handleInput = (data) => {
|
|
85
|
+
const key = data.toString();
|
|
86
|
+
|
|
87
|
+
if (key === '\r' || key === '\n') {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
if (process.stdin.isTTY && isFn(process.stdin.setRawMode)) {
|
|
94
|
+
process.stdin.setRawMode(true);
|
|
95
|
+
process.stdin.resume();
|
|
96
|
+
process.stdin.on('data', handleInput);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interval = setInterval(() => {
|
|
100
|
+
const frame = color.magenta(frames[frameIndex]);
|
|
101
|
+
|
|
102
|
+
process.stdout.write(`${cursor.move(-999, 0)}${erase.line}${frame} ${color.cyan(message)}`);
|
|
103
|
+
frameIndex = (frameIndex + 1) % frames.length;
|
|
104
|
+
}, delay);
|
|
105
|
+
await action((newMessage) => {
|
|
106
|
+
message = newMessage;
|
|
107
|
+
});
|
|
108
|
+
clearInterval(interval);
|
|
109
|
+
interval = null;
|
|
110
|
+
process.stdout.write(`\r${erase.line}${color.green('\u2714')} ${successMessage}
|
|
111
|
+
`);
|
|
112
|
+
msg({
|
|
113
|
+
type: 'M_INFO',
|
|
114
|
+
title: successMessage,
|
|
115
|
+
titleColor: 'green',
|
|
116
|
+
});
|
|
117
|
+
} catch(error) {
|
|
118
|
+
if (interval) {
|
|
119
|
+
clearInterval(interval);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
process.stdout.write(`\r${erase.line}${color.red('\u2716')} ${error instanceof Error ? errorMessage : 'An unknown error occurred.'}
|
|
123
|
+
`);
|
|
124
|
+
msg({
|
|
125
|
+
type: 'M_ERROR',
|
|
126
|
+
title: error instanceof Error ? error.message : 'An unknown error occurred.',
|
|
127
|
+
titleColor: 'red',
|
|
128
|
+
});
|
|
129
|
+
process.exit(1);
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
if (process.stdin.isTTY && isFn(process.stdin.setRawMode)) {
|
|
133
|
+
process.stdin.setRawMode(false);
|
|
134
|
+
process.stdin.pause();
|
|
135
|
+
process.stdin.removeListener('data', handleInput);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
47
138
|
}
|
|
48
|
-
} else {
|
|
49
|
-
const simpleSpinners = {
|
|
50
|
-
default: ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"],
|
|
51
|
-
dottedCircle: ["\u25CB", "\u25D4", "\u25D1", "\u25D5", "\u25CF"],
|
|
52
|
-
boxSpinner: ["\u2596", "\u2598", "\u259D", "\u2597"]
|
|
53
|
-
};
|
|
54
|
-
const frames = spinnerType && spinnerType in simpleSpinners ? simpleSpinners[spinnerType] : simpleSpinners.default;
|
|
55
|
-
const handleInput = (data) => {
|
|
56
|
-
const key = data.toString();
|
|
57
|
-
if (key === "\r" || key === "\n") {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
try {
|
|
62
|
-
if (process.stdin.isTTY && typeof process.stdin.setRawMode === "function") {
|
|
63
|
-
process.stdin.setRawMode(true);
|
|
64
|
-
process.stdin.resume();
|
|
65
|
-
process.stdin.on("data", handleInput);
|
|
66
|
-
}
|
|
67
|
-
interval = setInterval(() => {
|
|
68
|
-
const frame = color.magenta(frames[frameIndex]);
|
|
69
|
-
process.stdout.write(
|
|
70
|
-
`${cursor.move(-999, 0)}${erase.line}${frame} ${color.cyan(message)}`
|
|
71
|
-
);
|
|
72
|
-
frameIndex = (frameIndex + 1) % frames.length;
|
|
73
|
-
}, delay);
|
|
74
|
-
await action((newMessage) => {
|
|
75
|
-
message = newMessage;
|
|
76
|
-
});
|
|
77
|
-
clearInterval(interval);
|
|
78
|
-
interval = null;
|
|
79
|
-
process.stdout.write(
|
|
80
|
-
`\r${erase.line}${color.green("\u2714")} ${successMessage}
|
|
81
|
-
`
|
|
82
|
-
);
|
|
83
|
-
msg({
|
|
84
|
-
type: "M_INFO",
|
|
85
|
-
title: successMessage,
|
|
86
|
-
titleColor: "green"
|
|
87
|
-
});
|
|
88
|
-
} catch (error) {
|
|
89
|
-
if (interval) {
|
|
90
|
-
clearInterval(interval);
|
|
91
|
-
}
|
|
92
|
-
process.stdout.write(
|
|
93
|
-
`\r${erase.line}${color.red("\u2716")} ${error instanceof Error ? errorMessage : "An unknown error occurred."}
|
|
94
|
-
`
|
|
95
|
-
);
|
|
96
|
-
msg({
|
|
97
|
-
type: "M_ERROR",
|
|
98
|
-
title: error instanceof Error ? error.message : "An unknown error occurred.",
|
|
99
|
-
titleColor: "red"
|
|
100
|
-
});
|
|
101
|
-
process.exit(1);
|
|
102
|
-
} finally {
|
|
103
|
-
if (process.stdin.isTTY && typeof process.stdin.setRawMode === "function") {
|
|
104
|
-
process.stdin.setRawMode(false);
|
|
105
|
-
process.stdin.pause();
|
|
106
|
-
process.stdin.removeListener("data", handleInput);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
139
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { PromptOptions } from "../../types/general.js";
|
|
2
|
-
export declare function endPrompt({ title, titleColor,
|
|
2
|
+
export declare function endPrompt({ title, titleColor, titleTypography, titleVariant, titleAnimation, titleAnimationDelay, border, borderColor, }: PromptOptions): Promise<void>;
|
|
@@ -1,36 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
type: "M_END",
|
|
28
|
-
title,
|
|
29
|
-
titleColor,
|
|
30
|
-
titleTypography,
|
|
31
|
-
titleVariant,
|
|
32
|
-
border,
|
|
33
|
-
borderColor
|
|
34
|
-
});
|
|
35
|
-
}
|
|
1
|
+
import {animateText} from '../../components/visual/animate/animate.js';
|
|
2
|
+
import {msg} from '../../utils/messages.js';
|
|
3
|
+
|
|
4
|
+
export async function endPrompt({title, titleColor = 'cyanBright', titleTypography = 'bold', titleVariant, titleAnimation, titleAnimationDelay, border = true, borderColor = 'viceGradient'}) {
|
|
5
|
+
if (titleAnimation) {
|
|
6
|
+
await animateText({
|
|
7
|
+
title,
|
|
8
|
+
anim: titleAnimation,
|
|
9
|
+
delay: titleAnimationDelay,
|
|
10
|
+
type: 'M_END',
|
|
11
|
+
titleColor,
|
|
12
|
+
titleTypography,
|
|
13
|
+
border,
|
|
14
|
+
borderColor,
|
|
15
|
+
});
|
|
16
|
+
} else {
|
|
17
|
+
msg({
|
|
18
|
+
type: 'M_END',
|
|
19
|
+
title,
|
|
20
|
+
titleColor,
|
|
21
|
+
titleTypography,
|
|
22
|
+
titleVariant,
|
|
23
|
+
border,
|
|
24
|
+
borderColor,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
36
27
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { PromptOptions } from "../../types/general.js";
|
|
2
|
-
export declare function startPrompt({ title, titleColor,
|
|
2
|
+
export declare function startPrompt({ title, titleColor, titleTypography, titleVariant, borderColor, clearConsole, }: PromptOptions): Promise<void>;
|
|
@@ -1,33 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
answerColor = "none",
|
|
6
|
-
titleTypography = "bold",
|
|
7
|
-
titleVariant,
|
|
8
|
-
borderColor = "viceGradient",
|
|
9
|
-
clearConsole = true
|
|
10
|
-
}) {
|
|
11
|
-
if (clearConsole) {
|
|
12
|
-
console.clear();
|
|
13
|
-
console.log();
|
|
14
|
-
} else {
|
|
15
|
-
console.log();
|
|
16
|
-
}
|
|
17
|
-
msg({
|
|
18
|
-
type: "M_START",
|
|
19
|
-
title: ` ${title} `,
|
|
20
|
-
titleColor,
|
|
21
|
-
titleTypography,
|
|
22
|
-
titleVariant,
|
|
23
|
-
borderColor
|
|
24
|
-
});
|
|
25
|
-
if (!process.stdout.isTTY) {
|
|
26
|
-
console.error(
|
|
27
|
-
"\u2502 Your terminal does not support cursor manipulations.\n\u2502 It's recommended to use a terminal which supports TTY."
|
|
28
|
-
);
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
import {msg} from '../../utils/messages.js';
|
|
3
|
+
|
|
4
|
+
export function startPrompt({title, titleColor = 'cyanBright', titleTypography = 'bold', titleVariant, borderColor = 'viceGradient'}) {
|
|
29
5
|
msg({
|
|
30
|
-
|
|
6
|
+
type: 'M_START',
|
|
7
|
+
title: ` ${title} `,
|
|
8
|
+
titleColor,
|
|
9
|
+
titleTypography,
|
|
10
|
+
titleVariant,
|
|
11
|
+
borderColor,
|
|
31
12
|
});
|
|
32
|
-
|
|
13
|
+
if (!process.stdout.isTTY) {
|
|
14
|
+
msg({
|
|
15
|
+
type: 'M_NEWLINE',
|
|
16
|
+
});
|
|
17
|
+
}
|
|
33
18
|
}
|
|
@@ -1,121 +1,145 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
1
|
+
import {
|
|
2
|
+
cyanBright,
|
|
3
|
+
dim,
|
|
4
|
+
greenBright,
|
|
5
|
+
redBright,
|
|
6
|
+
} from 'picocolors';
|
|
7
|
+
import process, {
|
|
8
|
+
stdin as input,
|
|
9
|
+
stdout as output,
|
|
10
|
+
} from 'node:process';
|
|
11
|
+
import readline from 'node:readline';
|
|
12
|
+
import {
|
|
13
|
+
bar,
|
|
14
|
+
fmt,
|
|
15
|
+
msg,
|
|
16
|
+
symbols,
|
|
17
|
+
} from '../../utils/messages.js';
|
|
18
|
+
import {deleteLastLine} from '../../utils/terminal.js';
|
|
19
|
+
|
|
20
|
+
export function togglePrompt(params) {
|
|
21
|
+
const {
|
|
22
|
+
title,
|
|
23
|
+
options,
|
|
24
|
+
initial,
|
|
25
|
+
borderColor = 'viceGradient',
|
|
26
|
+
titleColor = 'cyanBright',
|
|
27
|
+
titleTypography = 'bold',
|
|
28
|
+
titleVariant,
|
|
29
|
+
border = true,
|
|
30
|
+
endTitle = '\u{1F44B}',
|
|
31
|
+
endTitleColor = 'passionGradient',
|
|
32
|
+
} = params;
|
|
33
|
+
|
|
34
|
+
let selectedIndex = initial ? options.findIndex((option) => option === initial) : 0;
|
|
35
|
+
|
|
36
|
+
if (selectedIndex === -1) {
|
|
37
|
+
selectedIndex = 0;
|
|
35
38
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
titleTypography,
|
|
41
|
-
titleVariant
|
|
42
|
-
})}
|
|
43
|
-
`;
|
|
44
|
-
if (errorMessage) {
|
|
45
|
-
outputStr += `${redBright(symbols.step_error)} ${errorMessage}
|
|
46
|
-
`;
|
|
47
|
-
} else {
|
|
48
|
-
outputStr += `${formattedBar} ${dim(instructions)}
|
|
49
|
-
`;
|
|
50
|
-
}
|
|
51
|
-
const [leftOption, rightOption] = options;
|
|
52
|
-
const displayOptions = options.map((option, index) => {
|
|
53
|
-
if (index === selectedIndex) {
|
|
54
|
-
return cyanBright(option);
|
|
55
|
-
}
|
|
56
|
-
return option;
|
|
39
|
+
|
|
40
|
+
const rl = readline.createInterface({
|
|
41
|
+
input,
|
|
42
|
+
output,
|
|
57
43
|
});
|
|
58
|
-
|
|
59
|
-
|
|
44
|
+
readline.emitKeypressEvents(input, rl);
|
|
45
|
+
input.setRawMode?.(true);
|
|
46
|
+
const formattedBar = bar({
|
|
47
|
+
borderColor,
|
|
48
|
+
});
|
|
49
|
+
let linesRendered = 0;
|
|
50
|
+
const instructions = `Use <\u2190/\u2192> or <h/l> to navigate, <Enter> to select, <Ctrl+C> to exit`;
|
|
51
|
+
let errorMessage = '';
|
|
52
|
+
|
|
53
|
+
function renderOption() {
|
|
54
|
+
if (linesRendered > 0) {
|
|
55
|
+
process.stdout.write(`\x1B[${linesRendered}A`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let outputStr = `${greenBright(symbols.step_active)} ${fmt({
|
|
59
|
+
type: 'M_NULL',
|
|
60
|
+
title,
|
|
61
|
+
titleColor,
|
|
62
|
+
titleTypography,
|
|
63
|
+
titleVariant,
|
|
64
|
+
})}
|
|
65
|
+
`;
|
|
66
|
+
|
|
67
|
+
if (errorMessage) {
|
|
68
|
+
outputStr += `${redBright(symbols.step_error)} ${errorMessage}
|
|
60
69
|
`;
|
|
61
|
-
process.stdout.write(outputStr);
|
|
62
|
-
linesRendered = 1 + 1 + 1;
|
|
63
|
-
}
|
|
64
|
-
renderOption();
|
|
65
|
-
return new Promise((resolve) => {
|
|
66
|
-
function onKeyPress(str, key) {
|
|
67
|
-
if (key.name === "left" || key.name === "h") {
|
|
68
|
-
selectedIndex = (selectedIndex - 1 + options.length) % options.length;
|
|
69
|
-
errorMessage = "";
|
|
70
|
-
renderOption();
|
|
71
|
-
} else if (key.name === "right" || key.name === "l") {
|
|
72
|
-
selectedIndex = (selectedIndex + 1) % options.length;
|
|
73
|
-
errorMessage = "";
|
|
74
|
-
renderOption();
|
|
75
|
-
} else if (key.name === "return") {
|
|
76
|
-
if (!options[selectedIndex]) {
|
|
77
|
-
deleteLastLine();
|
|
78
|
-
errorMessage = "You must select an option.";
|
|
79
|
-
renderOption();
|
|
80
70
|
} else {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
deleteLastLine();
|
|
84
|
-
deleteLastLine();
|
|
85
|
-
msg({
|
|
86
|
-
type: "M_MIDDLE"
|
|
87
|
-
});
|
|
71
|
+
outputStr += `${formattedBar} ${dim(instructions)}
|
|
72
|
+
`;
|
|
88
73
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
titleColor: endTitleColor,
|
|
97
|
-
titleTypography,
|
|
98
|
-
titleVariant,
|
|
99
|
-
border,
|
|
100
|
-
borderColor,
|
|
101
|
-
addNewLineBefore: true
|
|
74
|
+
|
|
75
|
+
const displayOptions = options.map((option, index) => {
|
|
76
|
+
if (index === selectedIndex) {
|
|
77
|
+
return cyanBright(option);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return option;
|
|
102
81
|
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
rl.close();
|
|
111
|
-
input.removeListener("keypress", onKeyPress);
|
|
112
|
-
process.stdout.write(`\x1B[${linesRendered}B`);
|
|
113
|
-
if (isCtrlC) {
|
|
114
|
-
process.exit();
|
|
115
|
-
} else {
|
|
116
|
-
console.log();
|
|
117
|
-
}
|
|
82
|
+
|
|
83
|
+
const displayString = displayOptions.join(' / ');
|
|
84
|
+
|
|
85
|
+
outputStr += `${formattedBar} ${displayString}
|
|
86
|
+
`;
|
|
87
|
+
process.stdout.write(outputStr);
|
|
88
|
+
linesRendered = 1 + 1 + 1;
|
|
118
89
|
}
|
|
119
|
-
|
|
120
|
-
|
|
90
|
+
|
|
91
|
+
renderOption();
|
|
92
|
+
return new Promise((resolve) => {
|
|
93
|
+
function onKeyPress(str, key) {
|
|
94
|
+
if (key.name === 'left' || key.name === 'h') {
|
|
95
|
+
selectedIndex = (selectedIndex - 1 + options.length) % options.length;
|
|
96
|
+
errorMessage = '';
|
|
97
|
+
renderOption();
|
|
98
|
+
} else if (key.name === 'right' || key.name === 'l') {
|
|
99
|
+
selectedIndex = (selectedIndex + 1) % options.length;
|
|
100
|
+
errorMessage = '';
|
|
101
|
+
renderOption();
|
|
102
|
+
} else if (key.name === 'return') {
|
|
103
|
+
if (!options[selectedIndex]) {
|
|
104
|
+
deleteLastLine();
|
|
105
|
+
errorMessage = 'You must select an option.';
|
|
106
|
+
renderOption();
|
|
107
|
+
} else {
|
|
108
|
+
cleanup();
|
|
109
|
+
resolve(options[selectedIndex]);
|
|
110
|
+
deleteLastLine();
|
|
111
|
+
deleteLastLine();
|
|
112
|
+
msg({
|
|
113
|
+
type: 'M_MIDDLE',
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
} else if (key.name === 'c' && key.ctrl) {
|
|
117
|
+
msg({
|
|
118
|
+
type: 'M_NEWLINE',
|
|
119
|
+
});
|
|
120
|
+
msg({
|
|
121
|
+
type: 'M_END',
|
|
122
|
+
title: endTitle,
|
|
123
|
+
titleColor: endTitleColor,
|
|
124
|
+
titleTypography,
|
|
125
|
+
titleVariant,
|
|
126
|
+
border,
|
|
127
|
+
borderColor,
|
|
128
|
+
addNewLineBefore: true,
|
|
129
|
+
});
|
|
130
|
+
cleanup();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function cleanup() {
|
|
137
|
+
input.setRawMode?.(false);
|
|
138
|
+
rl.close();
|
|
139
|
+
input.removeListener('keypress', onKeyPress);
|
|
140
|
+
process.stdout.write(`\x1B[${linesRendered}B`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
input.on('keypress', onKeyPress);
|
|
144
|
+
});
|
|
121
145
|
}
|