@reliverse/relinka 1.1.5 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/dist-npm/components/anykey/index.js +77 -57
  2. package/dist-npm/components/block/block.js +61 -48
  3. package/dist-npm/components/checkbox/index.js +211 -178
  4. package/dist-npm/components/confirm/confirm-main.js +122 -85
  5. package/dist-npm/components/confirm/confirm-three.js +27 -24
  6. package/dist-npm/components/confirm/index.js +45 -36
  7. package/dist-npm/components/core/Separator.js +17 -15
  8. package/dist-npm/components/core/create-prompt.js +124 -101
  9. package/dist-npm/components/core/errors.js +15 -19
  10. package/dist-npm/components/core/hook-engine.js +120 -91
  11. package/dist-npm/components/core/index.js +12 -12
  12. package/dist-npm/components/core/key.js +6 -16
  13. package/dist-npm/components/core/lines.js +54 -45
  14. package/dist-npm/components/core/make-theme.js +30 -21
  15. package/dist-npm/components/core/position.js +23 -27
  16. package/dist-npm/components/core/promise-polyfill.js +14 -11
  17. package/dist-npm/components/core/screen-manager.js +74 -59
  18. package/dist-npm/components/core/theme.js +32 -22
  19. package/dist-npm/components/core/use-effect.js +15 -9
  20. package/dist-npm/components/core/use-keypress.js +23 -19
  21. package/dist-npm/components/core/use-memo.js +16 -10
  22. package/dist-npm/components/core/use-pagination.js +32 -33
  23. package/dist-npm/components/core/use-prefix.js +38 -40
  24. package/dist-npm/components/core/use-ref.js +5 -2
  25. package/dist-npm/components/core/use-state.js +23 -15
  26. package/dist-npm/components/core/useKeyPress.js +17 -14
  27. package/dist-npm/components/core/usePromptState.js +14 -8
  28. package/dist-npm/components/core/utils.js +16 -7
  29. package/dist-npm/components/date/date.js +204 -167
  30. package/dist-npm/components/editor/index.js +92 -71
  31. package/dist-npm/components/expand/index.js +124 -96
  32. package/dist-npm/components/figures/index.js +294 -283
  33. package/dist-npm/components/input/index.js +87 -61
  34. package/dist-npm/components/input/text-main.js +124 -97
  35. package/dist-npm/components/input/text.js +28 -24
  36. package/dist-npm/components/instance/basic.js +25 -17
  37. package/dist-npm/components/instance/browser.js +18 -14
  38. package/dist-npm/components/instance/reporter/basic.js +65 -46
  39. package/dist-npm/components/instance/reporter/browser.js +47 -44
  40. package/dist-npm/components/instance/reporter/fancy.js +96 -83
  41. package/dist-npm/components/instance/shared.js +2 -2
  42. package/dist-npm/components/mono/mono.js +62 -52
  43. package/dist-npm/components/mono/monoTwo.js +49 -35
  44. package/dist-npm/components/multiselect/group-multiselect.js +71 -55
  45. package/dist-npm/components/multiselect/multi-select-two.js +130 -97
  46. package/dist-npm/components/multiselect/multi-select.js +49 -43
  47. package/dist-npm/components/multiselect/multiselect-main.js +146 -120
  48. package/dist-npm/components/multiselect/num-multi-select.js +130 -97
  49. package/dist-npm/components/multiselect/num-multiselect-main.js +35 -24
  50. package/dist-npm/components/next-steps/next-steps.js +25 -23
  51. package/dist-npm/components/number/index.js +112 -78
  52. package/dist-npm/components/number/number-main.js +2 -95
  53. package/dist-npm/components/password/index.js +73 -54
  54. package/dist-npm/components/password/password-main.js +2 -119
  55. package/dist-npm/components/password/password-three.js +30 -26
  56. package/dist-npm/components/progressbar/ProgressBar.js +64 -45
  57. package/dist-npm/components/progressbar/helper.js +40 -33
  58. package/dist-npm/components/progressbar/index.js +1 -1
  59. package/dist-npm/components/prompts/create.js +44 -29
  60. package/dist-npm/components/prompts/index.js +45 -45
  61. package/dist-npm/components/prompts/prompt.js +260 -211
  62. package/dist-npm/components/prompts/promptTwo.js +605 -561
  63. package/dist-npm/components/prompts/relinka.js +295 -237
  64. package/dist-npm/components/range/range.js +294 -247
  65. package/dist-npm/components/rawlist/index.js +107 -87
  66. package/dist-npm/components/results/results.js +37 -31
  67. package/dist-npm/components/search/index.js +193 -148
  68. package/dist-npm/components/select/index.js +186 -148
  69. package/dist-npm/components/select/num-select-main.js +27 -27
  70. package/dist-npm/components/select/num-select.js +5 -124
  71. package/dist-npm/components/select/select-key.js +25 -24
  72. package/dist-npm/components/select/select-main.js +133 -109
  73. package/dist-npm/components/select/select-three.js +36 -32
  74. package/dist-npm/components/select/select-two.js +87 -94
  75. package/dist-npm/components/spinner/index.js +136 -107
  76. package/dist-npm/components/st-end/end.js +26 -34
  77. package/dist-npm/components/st-end/start.js +15 -29
  78. package/dist-npm/components/toggle/index.js +137 -113
  79. package/dist-npm/components/visual/animate/animate.js +10 -53
  80. package/dist-npm/components/visual/ascii-art/ascii-art.js +1 -12
  81. package/dist-npm/mod.js +2 -1
  82. package/dist-npm/testing/index.js +83 -58
  83. package/dist-npm/types/general.js +1 -0
  84. package/dist-npm/types/index.js +3 -2
  85. package/dist-npm/types/keypress.js +36 -35
  86. package/dist-npm/types/readline.js +1 -0
  87. package/dist-npm/types/relinka.js +1 -0
  88. package/dist-npm/types/utils.js +1 -0
  89. package/dist-npm/utils/box.js +137 -135
  90. package/dist-npm/utils/color.js +74 -65
  91. package/dist-npm/utils/colorize.js +156 -124
  92. package/dist-npm/utils/component.js +657 -532
  93. package/dist-npm/utils/constants.js +63 -64
  94. package/dist-npm/utils/core.js +3 -2
  95. package/dist-npm/utils/decoder.js +223 -244
  96. package/dist-npm/utils/error.js +9 -4
  97. package/dist-npm/utils/errors.js +4 -14
  98. package/dist-npm/utils/format.js +24 -19
  99. package/dist-npm/utils/keypress.js +414 -316
  100. package/dist-npm/utils/log.js +15 -11
  101. package/dist-npm/utils/mapping.js +52 -45
  102. package/dist-npm/utils/messages.js +183 -183
  103. package/dist-npm/utils/platforms.js +20 -16
  104. package/dist-npm/utils/prompt-tmp.js +286 -235
  105. package/dist-npm/utils/prompt-two.js +286 -235
  106. package/dist-npm/utils/readline.js +7 -5
  107. package/dist-npm/utils/skeleton.js +170 -130
  108. package/dist-npm/utils/stream.js +2 -2
  109. package/dist-npm/utils/string.js +58 -44
  110. package/dist-npm/utils/terminal.js +34 -23
  111. package/dist-npm/utils/tree.js +41 -30
  112. package/dist-npm/utils/types.js +1 -0
  113. package/dist-npm/utils/utils.js +8 -8
  114. package/dist-npm/utils/variants.js +44 -36
  115. package/package.json +20 -28
@@ -1,111 +1,144 @@
1
- import { Value } from "@sinclair/typebox/value";
2
- import { stdin as input, stdout as output } from "node:process";
3
- import readline from "node:readline/promises";
4
- import { colorize } from "../../utils/colorize.js";
5
- import { bar, fmt, msg } from "../../utils/messages.js";
1
+ import {Value} from '@sinclair/typebox/value';
6
2
  import {
7
- countLines,
8
- deleteLastLine,
9
- deleteLastLines
10
- } from "../../utils/terminal.js";
3
+ stdin as input,
4
+ stdout as output,
5
+ } from 'node:process';
6
+ import readline from 'node:readline/promises';
7
+ import {colorize} from '../../utils/colorize.js';
8
+ import {
9
+ bar,
10
+ fmt,
11
+ msg,
12
+ } from '../../utils/messages.js';
13
+ import {
14
+ countLines,
15
+ deleteLastLine,
16
+ deleteLastLines,
17
+ } from '../../utils/terminal.js';
18
+
11
19
  export async function multiselectPrompt(options) {
12
- const {
13
- title,
14
- choices,
15
- schema,
16
- defaultValue,
17
- titleColor = "cyanBright",
18
- titleTypography = "bold",
19
- titleVariant,
20
- hint,
21
- content,
22
- contentColor = "dim",
23
- contentTypography,
24
- contentVariant,
25
- borderColor = "viceGradient",
26
- variantOptions
27
- } = options;
28
- if (!choices || choices.length === 0) {
29
- throw new Error("Choices are required for multiselect prompt.");
30
- }
31
- const rl = readline.createInterface({ input, output });
32
- const formattedBar = bar({ borderColor });
33
- let linesToDelete = 0;
34
- let errorMessage = "";
35
- try {
36
- while (true) {
37
- if (linesToDelete > 0) {
38
- deleteLastLines(linesToDelete);
39
- }
40
- const question = fmt({
41
- type: errorMessage !== "" ? "M_ERROR" : "M_GENERAL",
42
- title: `${title}${defaultValue ? ` [Default: ${Array.isArray(defaultValue) ? defaultValue.join(", ") : defaultValue}]` : ""}`,
43
- titleColor,
44
- titleTypography,
20
+ const {
21
+ title,
22
+ choices,
23
+ schema,
24
+ defaultValue,
25
+ titleColor = 'cyanBright',
26
+ titleTypography = 'bold',
45
27
  titleVariant,
28
+ hint,
46
29
  content,
47
- contentColor,
30
+ contentColor = 'dim',
48
31
  contentTypography,
49
32
  contentVariant,
50
- borderColor,
51
- hint,
33
+ borderColor = 'viceGradient',
52
34
  variantOptions,
53
- errorMessage,
54
- addNewLineBefore: false,
55
- addNewLineAfter: false
56
- });
57
- const choicesText = choices.map(
58
- (choice, index) => `${formattedBar} ${index + 1}) ${choice.title}${choice.description ? ` - ${choice.description}` : ""}`
59
- ).join("\n");
60
- const fullPrompt = `${question}
35
+ } = options;
36
+
37
+ if (!choices || !choices.length) {
38
+ throw Error('Choices are required for multiselect prompt.');
39
+ }
40
+
41
+ const rl = readline.createInterface({
42
+ input,
43
+ output,
44
+ });
45
+ const formattedBar = bar({
46
+ borderColor,
47
+ });
48
+ let linesToDelete = 0;
49
+ let errorMessage = '';
50
+
51
+ try {
52
+ while (true) {
53
+ if (linesToDelete > 0) {
54
+ deleteLastLines(linesToDelete);
55
+ }
56
+
57
+ const question = fmt({
58
+ type: errorMessage !== '' ? 'M_ERROR' : 'M_GENERAL',
59
+ title: `${title}${defaultValue ? ` [Default: ${Array.isArray(defaultValue) ? defaultValue.join(', ') : defaultValue}]` : ''}`,
60
+ titleColor,
61
+ titleTypography,
62
+ titleVariant,
63
+ content,
64
+ contentColor,
65
+ contentTypography,
66
+ contentVariant,
67
+ borderColor,
68
+ hint,
69
+ variantOptions,
70
+ errorMessage,
71
+ addNewLineBefore: false,
72
+ addNewLineAfter: false,
73
+ });
74
+
75
+ const choicesText = choices
76
+ .map((choice, index) => `${formattedBar} ${index + 1}) ${choice.title}${choice.description ? ` - ${choice.description}` : ''}`)
77
+ .join('\n');
78
+
79
+ const fullPrompt = `${question}
61
80
  ${choicesText}
62
81
  ${formattedBar} ${colorize(`Enter your choices (comma-separated numbers between 1-${choices.length})`, contentColor)}:
63
82
  ${formattedBar} `;
64
- const formattedPrompt = fmt({
65
- type: "M_NULL",
66
- title: fullPrompt
67
- });
68
- const questionLines = countLines(formattedPrompt);
69
- linesToDelete = questionLines + 1;
70
- const answer = (await rl.question(formattedPrompt)).trim();
71
- if (!answer && defaultValue !== void 0) {
72
- deleteLastLine();
73
- msg({
74
- type: "M_MIDDLE",
75
- title: ` ${Array.isArray(defaultValue) ? defaultValue.join(", ") : defaultValue}`,
76
- titleColor: "none"
77
- });
78
- msg({ type: "M_NEWLINE" });
79
- return defaultValue;
80
- }
81
- const selections = answer.split(",").map((s) => s.trim());
82
- const invalidSelections = selections.filter((s) => {
83
- const num = Number(s);
84
- return isNaN(num) || num < 1 || num > choices.length;
85
- });
86
- if (invalidSelections.length > 0) {
87
- errorMessage = `Invalid selections: ${invalidSelections.join(
88
- ", "
89
- )}. Please enter numbers between 1 and ${choices.length}.`;
90
- continue;
91
- }
92
- const selectedValues = selections.map((s) => choices[Number(s) - 1]?.id);
93
- let isValid = true;
94
- errorMessage = "";
95
- if (schema) {
96
- isValid = Value.Check(schema, selectedValues);
97
- if (!isValid) {
98
- const errors = [...Value.Errors(schema, selectedValues)];
99
- errorMessage = errors.length > 0 ? errors[0]?.message ?? "Invalid input." : "Invalid input.";
83
+
84
+ const formattedPrompt = fmt({
85
+ type: 'M_NULL',
86
+ title: fullPrompt,
87
+ });
88
+
89
+ const questionLines = countLines(formattedPrompt);
90
+
91
+ linesToDelete = questionLines + 1;
92
+ const answer = (await rl.question(formattedPrompt)).trim();
93
+
94
+ if (!answer && defaultValue !== void 0) {
95
+ deleteLastLine();
96
+ msg({
97
+ type: 'M_MIDDLE',
98
+ title: ` ${Array.isArray(defaultValue) ? defaultValue.join(', ') : defaultValue}`,
99
+ titleColor: 'none',
100
+ });
101
+ msg({
102
+ type: 'M_NEWLINE',
103
+ });
104
+ return defaultValue;
105
+ }
106
+
107
+ const selections = answer
108
+ .split(',')
109
+ .map((s) => s.trim());
110
+
111
+ const invalidSelections = selections.filter((s) => {
112
+ const num = Number(s);
113
+ return isNaN(num) || num < 1 || num > choices.length;
114
+ });
115
+
116
+ if (invalidSelections.length > 0) {
117
+ errorMessage = `Invalid selections: ${invalidSelections.join(', ')}. Please enter numbers between 1 and ${choices.length}.`;
118
+ continue;
119
+ }
120
+
121
+ const selectedValues = selections.map((s) => choices[Number(s) - 1]?.id);
122
+ let isValid = true;
123
+
124
+ errorMessage = '';
125
+ if (schema) {
126
+ isValid = Value.Check(schema, selectedValues);
127
+ if (!isValid) {
128
+ const errors = Value.Errors(schema, selectedValues);
129
+ errorMessage = errors.length > 0 ? errors[0]?.message ?? 'Invalid input.' : 'Invalid input.';
130
+ }
131
+ }
132
+
133
+ if (isValid) {
134
+ msg({
135
+ type: 'M_NEWLINE',
136
+ });
137
+ rl.close();
138
+ return selectedValues;
139
+ }
100
140
  }
101
- }
102
- if (isValid) {
103
- msg({ type: "M_NEWLINE" });
141
+ } finally {
104
142
  rl.close();
105
- return selectedValues;
106
- }
107
143
  }
108
- } finally {
109
- rl.close();
110
- }
111
144
  }
@@ -1,45 +1,51 @@
1
- import Prompt from "../../components/prompts/prompt.js";
1
+ import Prompt from '../../components/prompts/prompt.js';
2
+
2
3
  export default class MultiSelectPrompt extends Prompt {
3
- options;
4
- cursor = 0;
5
- get _value() {
6
- return this.options[this.cursor].value;
7
- }
8
- toggleAll() {
9
- const allSelected = this.value.length === this.options.length;
10
- this.value = allSelected ? [] : this.options.map((v) => v.value);
11
- }
12
- toggleValue() {
13
- const selected = this.value.includes(this._value);
14
- this.value = selected ? this.value.filter((value) => value !== this._value) : [...this.value, this._value];
15
- }
16
- constructor(opts) {
17
- super(opts, false);
18
- this.options = opts.options;
19
- this.value = [...opts.initialValues ?? []];
20
- this.cursor = Math.max(
21
- this.options.findIndex(({ value }) => value === opts.cursorAt),
22
- 0
23
- );
24
- this.on("key", (char) => {
25
- if (char === "a") {
26
- this.toggleAll();
27
- }
28
- });
29
- this.on("cursor", (key) => {
30
- switch (key) {
31
- case "left":
32
- case "up":
33
- this.cursor = this.cursor === 0 ? this.options.length - 1 : this.cursor - 1;
34
- break;
35
- case "down":
36
- case "right":
37
- this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1;
38
- break;
39
- case "space":
40
- this.toggleValue();
41
- break;
42
- }
43
- });
44
- }
4
+ options;
5
+ cursor = 0;
6
+ get _value() {
7
+ return this.options[this.cursor].value;
8
+ }
9
+
10
+ toggleAll() {
11
+ const allSelected = this.value.length === this.options.length;
12
+ this.value = allSelected ? [] : this.options.map((v) => v.value);
13
+ }
14
+
15
+ toggleValue() {
16
+ const selected = this.value.includes(this._value);
17
+ this.value = selected ? this.value.filter((value) => value !== this._value) : [
18
+ ...this.value,
19
+ this._value,
20
+ ];
21
+ }
22
+
23
+ constructor(opts) {
24
+ super(opts, false);
25
+ this.options = opts.options;
26
+ this.value = opts.initialValues ?? [].slice();
27
+ this.cursor = Math.max(this.options.findIndex(({value}) => value === opts.cursorAt), 0);
28
+ this.on('key', (char) => {
29
+ if (char === 'a') {
30
+ this.toggleAll();
31
+ }
32
+ });
33
+ this.on('cursor', (key) => {
34
+ switch(key) {
35
+ case 'left':
36
+ case 'up':
37
+ this.cursor = !this.cursor ? this.options.length - 1 : this.cursor - 1;
38
+ break;
39
+
40
+ case 'down':
41
+ case 'right':
42
+ this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1;
43
+ break;
44
+
45
+ case 'space':
46
+ this.toggleValue();
47
+ break;
48
+ }
49
+ });
50
+ }
45
51
  }
@@ -1,127 +1,153 @@
1
- import { stdin as input, stdout as output } from "node:process";
2
- import readline from "node:readline";
3
- import { cyanBright, dim, greenBright, redBright } from "picocolors";
4
- import { colorize } from "../../mod.js";
5
- import { bar, symbols, msg } from "../../utils/messages.js";
6
- import { deleteLastLine } from "../../utils/terminal.js";
7
- export async function multiselectPrompt(params) {
8
- const {
9
- title,
10
- options,
11
- required = false,
12
- initial = [],
13
- borderColor = "viceGradient",
14
- titleColor = "cyanBright",
15
- titleTypography = "bold",
16
- titleVariant,
17
- border = true,
18
- endTitle = "\u{1F44B}",
19
- endTitleColor = "passionGradient"
20
- } = params;
21
- let pointer = 0;
22
- const selectedOptions = new Set(
23
- initial.map((opt) => options.findIndex((o) => o.value === opt)).filter((i) => i >= 0)
24
- );
25
- const rl = readline.createInterface({ input, output });
26
- readline.emitKeypressEvents(input, rl);
27
- if (typeof input.setRawMode === "function") {
28
- input.setRawMode(true);
29
- }
30
- const formattedBar = bar({ borderColor });
31
- let linesRendered = 0;
32
- const instructions = `Use <\u2191/\u2193> or <k/j> to navigate, <Space> to select/deselect, <Enter> to confirm, <Ctrl+C> to exit`;
33
- let errorMessage = "";
34
- function renderOptions() {
35
- if (linesRendered > 0) {
36
- process.stdout.write(`\x1B[${linesRendered}A`);
37
- }
38
- let outputStr = `${greenBright(symbols.step_active)} ${colorize(title, titleColor, titleTypography)}
1
+ import process, {
2
+ stdin as input,
3
+ stdout as output,
4
+ } from 'node:process';
5
+ import readline from 'node:readline';
6
+ import {
7
+ cyanBright,
8
+ dim,
9
+ greenBright,
10
+ redBright,
11
+ } from 'picocolors';
12
+ import {colorize} from '../../mod.js';
13
+ import {
14
+ bar,
15
+ symbols,
16
+ msg,
17
+ } from '../../utils/messages.js';
18
+ import {deleteLastLine} from '../../utils/terminal.js';
19
+
20
+ export function multiselectPrompt(params) {
21
+ const {
22
+ title,
23
+ options,
24
+ required = false,
25
+ initial = [],
26
+ borderColor = 'viceGradient',
27
+ titleColor = 'cyanBright',
28
+ titleTypography = 'bold',
29
+ titleVariant,
30
+ border = true,
31
+ endTitle = '\u{1F44B}',
32
+ endTitleColor = 'passionGradient',
33
+ } = params;
34
+
35
+ let pointer = 0;
36
+ const selectedOptions = new Set(initial
37
+ .map((opt) => options.findIndex((o) => o.value === opt))
38
+ .filter((i) => i >= 0));
39
+
40
+ const rl = readline.createInterface({
41
+ input,
42
+ output,
43
+ });
44
+
45
+ readline.emitKeypressEvents(input, rl);
46
+ input.setRawMode?.(true);
47
+ const formattedBar = bar({
48
+ borderColor,
49
+ });
50
+ let linesRendered = 0;
51
+ const instructions = `Use <\u2191/\u2193> or <k/j> to navigate, <Space> to select/deselect, <Enter> to confirm, <Ctrl+C> to exit`;
52
+ let errorMessage = '';
53
+
54
+ function renderOptions() {
55
+ if (linesRendered > 0) {
56
+ process.stdout.write(`\x1B[${linesRendered}A`);
57
+ }
58
+
59
+ let outputStr = `${greenBright(symbols.step_active)} ${colorize(title, titleColor, titleTypography)}
39
60
  `;
40
- if (errorMessage) {
41
- outputStr += redBright(`${symbols.step_error} ${errorMessage}
61
+
62
+ if (errorMessage) {
63
+ outputStr += redBright(`${symbols.step_error} ${errorMessage}
42
64
  `);
43
- } else {
44
- outputStr += `${formattedBar} ${dim(instructions)}
45
- `;
46
- }
47
- options.forEach((option, index) => {
48
- const isSelected = selectedOptions.has(index);
49
- const isHighlighted = index === pointer;
50
- const checkbox = isSelected ? "[x]" : "[ ]";
51
- const prefix = isHighlighted ? "> " : " ";
52
- const optionLabel = isHighlighted ? cyanBright(option.value) : option.value;
53
- const hint = option.hint ? ` (${option.hint})` : "";
54
- outputStr += `${formattedBar} ${prefix}${checkbox} ${optionLabel}${dim(hint)}
55
- `;
56
- });
57
- process.stdout.write(outputStr);
58
- linesRendered = 1 + 1 + options.length;
59
- }
60
- renderOptions();
61
- return new Promise((resolve) => {
62
- function onKeyPress(str, key) {
63
- if (key.name === "up" || key.name === "k") {
64
- pointer = (pointer - 1 + options.length) % options.length;
65
- errorMessage = "";
66
- renderOptions();
67
- } else if (key.name === "down" || key.name === "j") {
68
- pointer = (pointer + 1) % options.length;
69
- errorMessage = "";
70
- renderOptions();
71
- } else if (key.name === "space") {
72
- if (selectedOptions.has(pointer)) {
73
- selectedOptions.delete(pointer);
74
65
  } else {
75
- selectedOptions.add(pointer);
66
+ outputStr += `${formattedBar} ${dim(instructions)}
67
+ `;
76
68
  }
77
- errorMessage = "";
78
- renderOptions();
79
- } else if (key.name === "return") {
80
- if (!required || selectedOptions.size > 0) {
81
- const selectedValues = Array.from(selectedOptions).map(
82
- (index) => options[index].value
83
- );
84
- cleanup();
85
- resolve(selectedValues);
86
- deleteLastLine();
87
- deleteLastLine();
88
- msg({
89
- type: "M_MIDDLE"
90
- });
91
- } else {
92
- deleteLastLine();
93
- errorMessage = "You must select at least one option.\x1B[K";
94
- renderOptions();
69
+
70
+ for (const [index, option] of options.entries()) {
71
+ const isSelected = selectedOptions.has(index);
72
+ const isHighlighted = index === pointer;
73
+ const checkbox = isSelected ? '[x]' : '[ ]';
74
+ const prefix = isHighlighted ? '> ' : ' ';
75
+ const optionLabel = isHighlighted ? cyanBright(option.value) : option.value;
76
+ const hint = option.hint ? ` (${option.hint})` : '';
77
+
78
+ outputStr += `${formattedBar} ${prefix}${checkbox} ${optionLabel}${dim(hint)}
79
+ `;
95
80
  }
96
- } else if (key.name === "c" && key.ctrl) {
97
- msg({
98
- type: "M_NEWLINE"
99
- });
100
- msg({
101
- type: "M_END",
102
- title: endTitle,
103
- titleColor: endTitleColor,
104
- titleTypography,
105
- titleVariant,
106
- border,
107
- borderColor
108
- });
109
- cleanup(true);
110
- }
111
- }
112
- function cleanup(isCtrlC = false) {
113
- if (typeof input.setRawMode === "function") {
114
- input.setRawMode(false);
115
- }
116
- rl.close();
117
- input.removeListener("keypress", onKeyPress);
118
- process.stdout.write(`\x1B[${linesRendered}B`);
119
- if (isCtrlC) {
120
- process.exit();
121
- } else {
122
- console.log();
123
- }
81
+
82
+ process.stdout.write(outputStr);
83
+ linesRendered = 1 + 1 + options.length;
124
84
  }
125
- input.on("keypress", onKeyPress);
126
- });
85
+
86
+ renderOptions();
87
+ return new Promise((resolve) => {
88
+ function onKeyPress(str, key) {
89
+ if (key.name === 'up' || key.name === 'k') {
90
+ pointer = (pointer - 1 + options.length) % options.length;
91
+ errorMessage = '';
92
+ renderOptions();
93
+ } else if (key.name === 'down' || key.name === 'j') {
94
+ pointer = (pointer + 1) % options.length;
95
+ errorMessage = '';
96
+ renderOptions();
97
+ } else if (key.name === 'space') {
98
+ if (selectedOptions.has(pointer)) {
99
+ selectedOptions.delete(pointer);
100
+ } else {
101
+ selectedOptions.add(pointer);
102
+ }
103
+
104
+ errorMessage = '';
105
+ renderOptions();
106
+ } else if (key.name === 'return') {
107
+ if (!required || selectedOptions.size > 0) {
108
+ const selectedValues = Array
109
+ .from(selectedOptions)
110
+ .map((index) => options[index].value);
111
+
112
+ cleanup();
113
+ resolve(selectedValues);
114
+ deleteLastLine();
115
+ deleteLastLine();
116
+ msg({
117
+ type: 'M_MIDDLE',
118
+ });
119
+ } else {
120
+ deleteLastLine();
121
+ errorMessage = 'You must select at least one option.\x1B[K';
122
+ renderOptions();
123
+ }
124
+ } else if (key.name === 'c' && key.ctrl) {
125
+ msg({
126
+ type: 'M_NEWLINE',
127
+ });
128
+ msg({
129
+ type: 'M_END',
130
+ title: endTitle,
131
+ titleColor: endTitleColor,
132
+ titleTypography,
133
+ titleVariant,
134
+ border,
135
+ borderColor,
136
+ });
137
+ cleanup();
138
+ }
139
+
140
+
141
+
142
+ }
143
+
144
+ function cleanup() {
145
+ input.setRawMode?.(false);
146
+ rl.close();
147
+ input.removeListener('keypress', onKeyPress);
148
+ process.stdout.write(`\x1B[${linesRendered}B`);
149
+ }
150
+
151
+ input.on('keypress', onKeyPress);
152
+ });
127
153
  }