@reliverse/relinka 1.1.6 → 1.1.8
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 +173 -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,81 +1,61 @@
|
|
|
1
|
-
import logUpdate from
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const isNumber = (a) => typeof a === 'number';
|
|
7
|
-
const isFn = (a) => typeof a === 'function';
|
|
8
|
-
const DEFAULT_MESSAGE = 'Press any key to continue...';
|
|
1
|
+
import logUpdate from "log-update";
|
|
2
|
+
import { fmt } from "../../utils/messages.js";
|
|
3
|
+
import { restoreCursor } from "../../utils/terminal.js";
|
|
4
|
+
const DEFAULT_MESSAGE = "Press any key to continue...";
|
|
9
5
|
const CTRL_C_CODE = 3;
|
|
10
|
-
|
|
11
6
|
export async function anykeyPrompt(message = DEFAULT_MESSAGE, options = {}) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
message = fmt({
|
|
20
|
-
type: 'M_GENERAL',
|
|
21
|
-
title: message,
|
|
22
|
-
titleColor: 'dim',
|
|
23
|
-
dontRemoveBar: true,
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (message && !hideMessage) {
|
|
28
|
-
logUpdate(message);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return new Promise((resolve, reject) => {
|
|
32
|
-
const cleanup = () => {
|
|
33
|
-
process.stdin.removeListener('data', handler);
|
|
34
|
-
if (process.stdin.isTTY && isFn(process.stdin.setRawMode)) {
|
|
35
|
-
process.stdin.setRawMode(false);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
process.stdin.pause();
|
|
39
|
-
restoreCursor();
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const handleCtrlC = () => {
|
|
43
|
-
cleanup();
|
|
44
|
-
if (ctrlC === 'reject') {
|
|
45
|
-
reject(Error('User pressed CTRL+C'));
|
|
46
|
-
} else if (!ctrlC) {
|
|
47
|
-
resolve();
|
|
48
|
-
} else if (isNumber(ctrlC)) {
|
|
49
|
-
process.exit(ctrlC);
|
|
50
|
-
} else {
|
|
51
|
-
throw TypeError('Invalid ctrlC option');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const handler = (buffer) => {
|
|
57
|
-
cleanup();
|
|
58
|
-
if (message && !preserveLog) {
|
|
59
|
-
logUpdate.clear();
|
|
60
|
-
} else {
|
|
61
|
-
logUpdate.done();
|
|
62
|
-
process.stdout.write('\n');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const [firstByte] = buffer;
|
|
66
|
-
|
|
67
|
-
if (firstByte === CTRL_C_CODE) {
|
|
68
|
-
handleCtrlC();
|
|
69
|
-
} else {
|
|
70
|
-
resolve();
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
process.stdin.resume();
|
|
75
|
-
if (process.stdin.isTTY && isFn(process.stdin.setRawMode)) {
|
|
76
|
-
process.stdin.setRawMode(true);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
process.stdin.once('data', handler);
|
|
7
|
+
const { ctrlC = 1, preserveLog = false, hideMessage = false } = options;
|
|
8
|
+
if (message) {
|
|
9
|
+
message = fmt({
|
|
10
|
+
type: "M_GENERAL",
|
|
11
|
+
title: message,
|
|
12
|
+
titleColor: "dim",
|
|
13
|
+
dontRemoveBar: true
|
|
80
14
|
});
|
|
15
|
+
}
|
|
16
|
+
if (message && !hideMessage) {
|
|
17
|
+
logUpdate(message);
|
|
18
|
+
}
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
const cleanup = () => {
|
|
21
|
+
process.stdin.removeListener("data", handler);
|
|
22
|
+
if (process.stdin.isTTY && typeof process.stdin.setRawMode === "function") {
|
|
23
|
+
process.stdin.setRawMode(false);
|
|
24
|
+
}
|
|
25
|
+
process.stdin.pause();
|
|
26
|
+
restoreCursor();
|
|
27
|
+
};
|
|
28
|
+
const handleCtrlC = () => {
|
|
29
|
+
cleanup();
|
|
30
|
+
if (ctrlC === "reject") {
|
|
31
|
+
reject(new Error("User pressed CTRL+C"));
|
|
32
|
+
} else if (ctrlC === false) {
|
|
33
|
+
resolve();
|
|
34
|
+
} else if (typeof ctrlC === "number") {
|
|
35
|
+
process.exit(ctrlC);
|
|
36
|
+
} else {
|
|
37
|
+
throw new TypeError("Invalid ctrlC option");
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const handler = (buffer) => {
|
|
41
|
+
cleanup();
|
|
42
|
+
if (message && !preserveLog) {
|
|
43
|
+
logUpdate.clear();
|
|
44
|
+
} else {
|
|
45
|
+
logUpdate.done();
|
|
46
|
+
process.stdout.write("\n");
|
|
47
|
+
}
|
|
48
|
+
const [firstByte] = buffer;
|
|
49
|
+
if (firstByte === CTRL_C_CODE) {
|
|
50
|
+
handleCtrlC();
|
|
51
|
+
} else {
|
|
52
|
+
resolve();
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
process.stdin.resume();
|
|
56
|
+
if (process.stdin.isTTY && typeof process.stdin.setRawMode === "function") {
|
|
57
|
+
process.stdin.setRawMode(true);
|
|
58
|
+
}
|
|
59
|
+
process.stdin.once("data", handler);
|
|
60
|
+
});
|
|
81
61
|
}
|
|
@@ -1,65 +1,52 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as readline from
|
|
3
|
-
import {removeCursor, restoreCursor} from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
import { stdin, stdout } from "node:process";
|
|
2
|
+
import * as readline from "node:readline";
|
|
3
|
+
import { removeCursor, restoreCursor } from "../../utils/terminal.js";
|
|
4
|
+
const isWindows = globalThis.process.platform.startsWith("win");
|
|
5
|
+
export default function block({
|
|
6
|
+
input = stdin,
|
|
7
|
+
output = stdout,
|
|
8
|
+
overwrite = true,
|
|
9
|
+
hideCursor = true
|
|
10
|
+
} = {}) {
|
|
11
|
+
const rl = readline.createInterface({
|
|
12
|
+
input,
|
|
13
|
+
output,
|
|
14
|
+
prompt: "",
|
|
15
|
+
tabSize: 1
|
|
16
|
+
});
|
|
17
|
+
readline.emitKeypressEvents(input, rl);
|
|
18
|
+
if (input.isTTY) {
|
|
19
|
+
input.setRawMode(true);
|
|
20
|
+
}
|
|
21
|
+
const clear = (data, { name }) => {
|
|
22
|
+
const str = String(data);
|
|
23
|
+
if (str === "") {
|
|
24
|
+
process.exit(0);
|
|
25
|
+
}
|
|
26
|
+
if (!overwrite) {
|
|
27
|
+
return;
|
|
24
28
|
}
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
readline.moveCursor(output, dx, dy, () => {
|
|
41
|
-
readline.clearLine(output, 1, () => {
|
|
42
|
-
input.once('keypress', clear);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
|
|
29
|
+
const dx = name === "return" ? 0 : -1;
|
|
30
|
+
const dy = name === "return" ? -1 : 0;
|
|
31
|
+
readline.moveCursor(output, dx, dy, () => {
|
|
32
|
+
readline.clearLine(output, 1, () => {
|
|
33
|
+
input.once("keypress", clear);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
if (hideCursor) {
|
|
38
|
+
removeCursor();
|
|
39
|
+
}
|
|
40
|
+
input.once("keypress", clear);
|
|
41
|
+
return () => {
|
|
42
|
+
input.off("keypress", clear);
|
|
47
43
|
if (hideCursor) {
|
|
48
|
-
|
|
44
|
+
restoreCursor();
|
|
45
|
+
}
|
|
46
|
+
if (input.isTTY && !isWindows) {
|
|
47
|
+
input.setRawMode(false);
|
|
49
48
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
input.off('keypress', clear);
|
|
54
|
-
if (hideCursor) {
|
|
55
|
-
restoreCursor();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (input.isTTY && !isWindows) {
|
|
59
|
-
input.setRawMode(false);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
rl.terminal = false;
|
|
63
|
-
rl.close();
|
|
64
|
-
};
|
|
49
|
+
rl.terminal = false;
|
|
50
|
+
rl.close();
|
|
51
|
+
};
|
|
65
52
|
}
|
|
@@ -1,250 +1,217 @@
|
|
|
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
|
|
21
|
-
|
|
22
|
-
const isString = (a) => typeof a === 'string';
|
|
23
|
-
|
|
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";
|
|
24
21
|
const checkboxTheme = {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
22
|
+
icon: {
|
|
23
|
+
checked: colors.green(figures.circleFilled),
|
|
24
|
+
unchecked: figures.circle,
|
|
25
|
+
cursor: figures.pointer
|
|
26
|
+
},
|
|
27
|
+
style: {
|
|
28
|
+
disabledChoice: (text) => colors.dim(`- ${text}`),
|
|
29
|
+
renderSelectedChoices: (selectedChoices) => selectedChoices.map((choice) => choice.short).join(", "),
|
|
30
|
+
description: (text) => colors.cyan(text)
|
|
31
|
+
},
|
|
32
|
+
helpMode: "auto"
|
|
36
33
|
};
|
|
37
|
-
|
|
38
34
|
function isSelectable(item) {
|
|
39
|
-
|
|
35
|
+
return !Separator.isSeparator(item) && !item.disabled;
|
|
40
36
|
}
|
|
41
|
-
|
|
42
37
|
function isChecked(item) {
|
|
43
|
-
|
|
38
|
+
return isSelectable(item) && Boolean(item.checked);
|
|
44
39
|
}
|
|
45
|
-
|
|
46
40
|
function toggle(item) {
|
|
47
|
-
|
|
48
|
-
checked: !item.checked} : item;
|
|
41
|
+
return isSelectable(item) ? { ...item, checked: !item.checked } : item;
|
|
49
42
|
}
|
|
50
|
-
|
|
51
43
|
function check(checked) {
|
|
52
|
-
|
|
53
|
-
checked} : item;
|
|
44
|
+
return function(item) {
|
|
45
|
+
return isSelectable(item) ? { ...item, checked } : item;
|
|
46
|
+
};
|
|
54
47
|
}
|
|
55
|
-
|
|
56
48
|
function normalizeChoices(choices) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
};
|
|
86
|
-
});
|
|
49
|
+
return choices.map((choice) => {
|
|
50
|
+
if (Separator.isSeparator(choice)) {
|
|
51
|
+
return choice;
|
|
52
|
+
}
|
|
53
|
+
if (typeof choice === "string") {
|
|
54
|
+
return {
|
|
55
|
+
value: choice,
|
|
56
|
+
name: choice,
|
|
57
|
+
short: choice,
|
|
58
|
+
disabled: false,
|
|
59
|
+
checked: false
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const name = choice.name ?? String(choice.value);
|
|
63
|
+
return {
|
|
64
|
+
|
|
65
|
+
value: choice.value,
|
|
66
|
+
name,
|
|
67
|
+
|
|
68
|
+
short: choice.short ?? name,
|
|
69
|
+
|
|
70
|
+
description: choice.description,
|
|
71
|
+
|
|
72
|
+
disabled: choice.disabled ?? false,
|
|
73
|
+
|
|
74
|
+
checked: choice.checked ?? false
|
|
75
|
+
};
|
|
76
|
+
});
|
|
87
77
|
}
|
|
88
|
-
|
|
89
|
-
|
|
78
|
+
const checkbox = createPrompt(
|
|
79
|
+
(config, done) => {
|
|
90
80
|
const {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
81
|
+
instructions,
|
|
82
|
+
pageSize = 7,
|
|
83
|
+
loop = true,
|
|
84
|
+
required,
|
|
85
|
+
validate = () => true
|
|
96
86
|
} = config;
|
|
97
|
-
|
|
98
87
|
const theme = makeTheme(checkboxTheme, config.theme);
|
|
99
88
|
const firstRender = useRef(true);
|
|
100
|
-
const [status, setStatus] = useState(
|
|
101
|
-
const prefix = usePrefix({
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const [items, setItems] = useState(normalizeChoices(config.choices));
|
|
107
|
-
|
|
89
|
+
const [status, setStatus] = useState("idle");
|
|
90
|
+
const prefix = usePrefix({ status, theme });
|
|
91
|
+
const [items, setItems] = useState(
|
|
92
|
+
normalizeChoices(config.choices)
|
|
93
|
+
);
|
|
108
94
|
const bounds = useMemo(() => {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
first,
|
|
118
|
-
last,
|
|
119
|
-
};
|
|
95
|
+
const first = items.findIndex(isSelectable);
|
|
96
|
+
const last = items.findLastIndex(isSelectable);
|
|
97
|
+
if (first === -1) {
|
|
98
|
+
throw new ValidationError(
|
|
99
|
+
"[checkbox prompt] No selectable choices. All choices are disabled."
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return { first, last };
|
|
120
103
|
}, [items]);
|
|
121
|
-
|
|
122
104
|
const [active, setActive] = useState(bounds.first);
|
|
123
105
|
const [showHelpTip, setShowHelpTip] = useState(true);
|
|
124
106
|
const [errorMsg, setError] = useState();
|
|
125
|
-
|
|
126
107
|
useKeypress(async (key) => {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
}
|
|
108
|
+
if (isEnterKey(key)) {
|
|
109
|
+
const selection = items.filter(isChecked);
|
|
110
|
+
const isValid = await validate([...selection]);
|
|
111
|
+
if (required && !items.some(isChecked)) {
|
|
112
|
+
setError("At least one choice must be selected");
|
|
113
|
+
} else if (isValid === true) {
|
|
114
|
+
setStatus("done");
|
|
115
|
+
done(selection.map((choice) => choice.value));
|
|
116
|
+
} else {
|
|
117
|
+
setError(isValid || "You must select a valid value");
|
|
167
118
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
119
|
+
} else if (isUpKey(key) || isDownKey(key)) {
|
|
120
|
+
if (loop || isUpKey(key) && active !== bounds.first || isDownKey(key) && active !== bounds.last) {
|
|
121
|
+
const offset = isUpKey(key) ? -1 : 1;
|
|
122
|
+
let next = active;
|
|
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
|
+
}
|
|
172
153
|
});
|
|
173
154
|
const message = theme.style.message(config.message, status);
|
|
174
155
|
let description;
|
|
175
|
-
|
|
176
156
|
const page = usePagination({
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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,
|
|
201
|
-
});
|
|
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}`;
|
|
208
|
-
}
|
|
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(', ')})`;
|
|
157
|
+
items,
|
|
158
|
+
active,
|
|
159
|
+
renderItem({ item, isActive }) {
|
|
160
|
+
if (Separator.isSeparator(item)) {
|
|
161
|
+
return ` ${item.separator}`;
|
|
162
|
+
}
|
|
163
|
+
if (item.disabled) {
|
|
164
|
+
const disabledLabel = typeof item.disabled === "string" ? item.disabled : "(disabled)";
|
|
165
|
+
return theme.style.disabledChoice(`${item.name} ${disabledLabel}`);
|
|
225
166
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
helpTipBottom = `
|
|
229
|
-
${theme.style.help('(Use arrow keys to reveal more choices)')}`;
|
|
230
|
-
firstRender.current = false;
|
|
167
|
+
if (isActive) {
|
|
168
|
+
description = item.description;
|
|
231
169
|
}
|
|
170
|
+
const checkbox2 = item.checked ? theme.icon.checked : theme.icon.unchecked;
|
|
171
|
+
const color = isActive ? theme.style.highlight : (x) => x;
|
|
172
|
+
const cursor = isActive ? theme.icon.cursor : " ";
|
|
173
|
+
return color(`${cursor}${checkbox2} ${item.name}`);
|
|
174
|
+
},
|
|
175
|
+
pageSize,
|
|
176
|
+
loop
|
|
177
|
+
});
|
|
178
|
+
if (status === "done") {
|
|
179
|
+
const selection = items.filter(isChecked);
|
|
180
|
+
const answer = theme.style.answer(
|
|
181
|
+
theme.style.renderSelectedChoices(selection, items)
|
|
182
|
+
);
|
|
183
|
+
return `${prefix} ${message} ${answer}`;
|
|
184
|
+
}
|
|
185
|
+
let helpTipTop = "";
|
|
186
|
+
let helpTipBottom = "";
|
|
187
|
+
if (theme.helpMode === "always" || theme.helpMode === "auto" && showHelpTip && (instructions === void 0 || instructions)) {
|
|
188
|
+
if (typeof instructions === "string") {
|
|
189
|
+
helpTipTop = instructions;
|
|
190
|
+
} else {
|
|
191
|
+
const keys = [
|
|
192
|
+
`${theme.style.key("space")} to select`,
|
|
193
|
+
`${theme.style.key("a")} to toggle all`,
|
|
194
|
+
`${theme.style.key("i")} to invert selection`,
|
|
195
|
+
`and ${theme.style.key("enter")} to proceed`
|
|
196
|
+
];
|
|
197
|
+
helpTipTop = ` (Press ${keys.join(", ")})`;
|
|
198
|
+
}
|
|
199
|
+
if (items.length > pageSize && (theme.helpMode === "always" || theme.helpMode === "auto" && firstRender.current)) {
|
|
200
|
+
helpTipBottom = `
|
|
201
|
+
${theme.style.help("(Use arrow keys to reveal more choices)")}`;
|
|
202
|
+
firstRender.current = false;
|
|
203
|
+
}
|
|
232
204
|
}
|
|
233
|
-
|
|
234
205
|
const choiceDescription = description ? `
|
|
235
206
|
${theme.style.description(description)}` : ``;
|
|
236
|
-
|
|
237
|
-
let error = '';
|
|
238
|
-
|
|
207
|
+
let error = "";
|
|
239
208
|
if (errorMsg) {
|
|
240
|
-
|
|
209
|
+
error = `
|
|
241
210
|
${theme.style.error(errorMsg)}`;
|
|
242
211
|
}
|
|
243
|
-
|
|
244
212
|
return `${prefix} ${message}${helpTipTop}
|
|
245
213
|
${page}${helpTipBottom}${choiceDescription}${error}${ansiEscapes.cursorHide}`;
|
|
246
|
-
}
|
|
247
|
-
|
|
214
|
+
}
|
|
215
|
+
);
|
|
248
216
|
export default checkbox;
|
|
249
|
-
|
|
250
|
-
export {Separator} from '../../components/core/index.js';
|
|
217
|
+
export { Separator } from "../../components/core/index.js";
|