@reliverse/relinka 1.1.10 → 1.2.1

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 (224) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +38 -42
  3. package/dist-npm/components/core/core.d.ts +2 -0
  4. package/dist-npm/components/core/core.js +2 -0
  5. package/dist-npm/{utils/constants.d.ts → components/levels/levels.d.ts} +1 -1
  6. package/dist-npm/components/{prompts/create.d.ts → modes/basic.d.ts} +3 -3
  7. package/dist-npm/components/{instance → modes}/basic.js +4 -4
  8. package/dist-npm/components/{instance → modes}/browser.d.ts +2 -2
  9. package/dist-npm/components/modes/browser.js +12 -0
  10. package/dist-npm/components/modes/shared.d.ts +5 -0
  11. package/dist-npm/components/modes/shared.js +2 -0
  12. package/dist-npm/components/{prompts → relinka}/relinka.d.ts +2 -4
  13. package/dist-npm/components/{prompts → relinka}/relinka.js +1 -7
  14. package/dist-npm/components/{instance/reporter → reporters}/basic.d.ts +1 -1
  15. package/dist-npm/components/{instance/reporter → reporters}/basic.js +2 -2
  16. package/dist-npm/components/{instance/reporter → reporters}/browser.d.ts +1 -1
  17. package/dist-npm/components/{instance/reporter → reporters}/fancy.d.ts +3 -3
  18. package/dist-npm/components/{instance/reporter → reporters}/fancy.js +5 -7
  19. package/dist-npm/main.d.ts +8 -1
  20. package/dist-npm/main.js +34 -1
  21. package/dist-npm/types/mod.d.ts +42 -0
  22. package/dist-npm/utils/color.js +4 -1
  23. package/dist-npm/utils/mod.d.ts +3 -0
  24. package/dist-npm/utils/mod.js +9 -0
  25. package/package.json +3 -2
  26. package/dist-npm/components/anykey/index.d.ts +0 -7
  27. package/dist-npm/components/anykey/index.js +0 -61
  28. package/dist-npm/components/block/block.d.ts +0 -10
  29. package/dist-npm/components/block/block.js +0 -52
  30. package/dist-npm/components/checkbox/index.d.ts +0 -3
  31. package/dist-npm/components/checkbox/index.js +0 -217
  32. package/dist-npm/components/confirm/confirm-main.d.ts +0 -3
  33. package/dist-npm/components/confirm/confirm-main.js +0 -98
  34. package/dist-npm/components/confirm/confirm-three.d.ts +0 -13
  35. package/dist-npm/components/confirm/confirm-three.js +0 -26
  36. package/dist-npm/components/confirm/index.d.ts +0 -2
  37. package/dist-npm/components/confirm/index.js +0 -41
  38. package/dist-npm/components/core/Separator.d.ts +0 -6
  39. package/dist-npm/components/core/Separator.js +0 -18
  40. package/dist-npm/components/core/create-prompt.d.ts +0 -4
  41. package/dist-npm/components/core/create-prompt.js +0 -107
  42. package/dist-npm/components/core/errors.d.ts +0 -20
  43. package/dist-npm/components/core/errors.js +0 -21
  44. package/dist-npm/components/core/hook-engine.d.ts +0 -23
  45. package/dist-npm/components/core/hook-engine.js +0 -109
  46. package/dist-npm/components/core/index.d.ts +0 -13
  47. package/dist-npm/components/core/index.js +0 -12
  48. package/dist-npm/components/core/key.d.ts +0 -10
  49. package/dist-npm/components/core/key.js +0 -16
  50. package/dist-npm/components/core/lines.d.ts +0 -14
  51. package/dist-npm/components/core/lines.js +0 -52
  52. package/dist-npm/components/core/make-theme.d.ts +0 -3
  53. package/dist-npm/components/core/make-theme.js +0 -28
  54. package/dist-npm/components/core/position.d.ts +0 -12
  55. package/dist-npm/components/core/position.js +0 -29
  56. package/dist-npm/components/core/promise-polyfill.d.ts +0 -7
  57. package/dist-npm/components/core/promise-polyfill.js +0 -13
  58. package/dist-npm/components/core/screen-manager.d.ts +0 -14
  59. package/dist-npm/components/core/screen-manager.js +0 -67
  60. package/dist-npm/components/core/theme.d.ts +0 -21
  61. package/dist-npm/components/core/theme.js +0 -24
  62. package/dist-npm/components/core/use-effect.d.ts +0 -2
  63. package/dist-npm/components/core/use-effect.js +0 -11
  64. package/dist-npm/components/core/use-keypress.d.ts +0 -3
  65. package/dist-npm/components/core/use-keypress.js +0 -21
  66. package/dist-npm/components/core/use-memo.d.ts +0 -1
  67. package/dist-npm/components/core/use-memo.js +0 -12
  68. package/dist-npm/components/core/use-pagination.d.ts +0 -11
  69. package/dist-npm/components/core/use-pagination.js +0 -34
  70. package/dist-npm/components/core/use-prefix.d.ts +0 -5
  71. package/dist-npm/components/core/use-prefix.js +0 -42
  72. package/dist-npm/components/core/use-ref.d.ts +0 -6
  73. package/dist-npm/components/core/use-ref.js +0 -4
  74. package/dist-npm/components/core/use-state.d.ts +0 -4
  75. package/dist-npm/components/core/use-state.js +0 -17
  76. package/dist-npm/components/core/useKeyPress.d.ts +0 -4
  77. package/dist-npm/components/core/useKeyPress.js +0 -16
  78. package/dist-npm/components/core/usePromptState.d.ts +0 -6
  79. package/dist-npm/components/core/usePromptState.js +0 -10
  80. package/dist-npm/components/core/utils.d.ts +0 -2
  81. package/dist-npm/components/core/utils.js +0 -11
  82. package/dist-npm/components/date/date.d.ts +0 -6
  83. package/dist-npm/components/date/date.js +0 -210
  84. package/dist-npm/components/editor/index.d.ts +0 -2
  85. package/dist-npm/components/editor/index.js +0 -78
  86. package/dist-npm/components/expand/index.d.ts +0 -3
  87. package/dist-npm/components/expand/index.js +0 -119
  88. package/dist-npm/components/figures/index.d.ts +0 -465
  89. package/dist-npm/components/figures/index.js +0 -299
  90. package/dist-npm/components/input/index.d.ts +0 -2
  91. package/dist-npm/components/input/index.js +0 -66
  92. package/dist-npm/components/input/text-main.d.ts +0 -3
  93. package/dist-npm/components/input/text-main.js +0 -103
  94. package/dist-npm/components/input/text.d.ts +0 -11
  95. package/dist-npm/components/input/text.js +0 -26
  96. package/dist-npm/components/instance/basic.d.ts +0 -8
  97. package/dist-npm/components/instance/browser.js +0 -18
  98. package/dist-npm/components/instance/shared.d.ts +0 -6
  99. package/dist-npm/components/instance/shared.js +0 -2
  100. package/dist-npm/components/mono/mono.d.ts +0 -5
  101. package/dist-npm/components/mono/mono.js +0 -64
  102. package/dist-npm/components/mono/monoTwo.d.ts +0 -30
  103. package/dist-npm/components/mono/monoTwo.js +0 -37
  104. package/dist-npm/components/multiselect/group-multiselect.d.ts +0 -23
  105. package/dist-npm/components/multiselect/group-multiselect.js +0 -58
  106. package/dist-npm/components/multiselect/multi-select-two.d.ts +0 -3
  107. package/dist-npm/components/multiselect/multi-select-two.js +0 -111
  108. package/dist-npm/components/multiselect/multi-select.d.ts +0 -21
  109. package/dist-npm/components/multiselect/multi-select.js +0 -45
  110. package/dist-npm/components/multiselect/multiselect-main.d.ts +0 -18
  111. package/dist-npm/components/multiselect/multiselect-main.js +0 -166
  112. package/dist-npm/components/multiselect/num-multi-select.d.ts +0 -3
  113. package/dist-npm/components/multiselect/num-multi-select.js +0 -111
  114. package/dist-npm/components/multiselect/num-multiselect-main.d.ts +0 -6
  115. package/dist-npm/components/multiselect/num-multiselect-main.js +0 -28
  116. package/dist-npm/components/next-steps/next-steps.d.ts +0 -2
  117. package/dist-npm/components/next-steps/next-steps.js +0 -24
  118. package/dist-npm/components/number/index.d.ts +0 -2
  119. package/dist-npm/components/number/index.js +0 -100
  120. package/dist-npm/components/number/number-main.d.ts +0 -3
  121. package/dist-npm/components/number/number-main.js +0 -95
  122. package/dist-npm/components/password/index.d.ts +0 -2
  123. package/dist-npm/components/password/index.js +0 -59
  124. package/dist-npm/components/password/password-main.d.ts +0 -3
  125. package/dist-npm/components/password/password-main.js +0 -119
  126. package/dist-npm/components/password/password-three.d.ts +0 -13
  127. package/dist-npm/components/password/password-three.js +0 -28
  128. package/dist-npm/components/progressbar/ProgressBar.d.ts +0 -22
  129. package/dist-npm/components/progressbar/ProgressBar.js +0 -53
  130. package/dist-npm/components/progressbar/helper.d.ts +0 -6
  131. package/dist-npm/components/progressbar/helper.js +0 -38
  132. package/dist-npm/components/progressbar/index.d.ts +0 -2
  133. package/dist-npm/components/progressbar/index.js +0 -1
  134. package/dist-npm/components/prompts/create.js +0 -35
  135. package/dist-npm/components/prompts/index.d.ts +0 -49
  136. package/dist-npm/components/prompts/index.js +0 -46
  137. package/dist-npm/components/prompts/prompt.d.ts +0 -36
  138. package/dist-npm/components/prompts/prompt.js +0 -232
  139. package/dist-npm/components/prompts/promptTwo.d.ts +0 -92
  140. package/dist-npm/components/prompts/promptTwo.js +0 -653
  141. package/dist-npm/components/range/range.d.ts +0 -62
  142. package/dist-npm/components/range/range.js +0 -270
  143. package/dist-npm/components/rawlist/index.d.ts +0 -3
  144. package/dist-npm/components/rawlist/index.js +0 -104
  145. package/dist-npm/components/results/results.d.ts +0 -10
  146. package/dist-npm/components/results/results.js +0 -34
  147. package/dist-npm/components/search/index.d.ts +0 -3
  148. package/dist-npm/components/search/index.js +0 -186
  149. package/dist-npm/components/select/index.d.ts +0 -3
  150. package/dist-npm/components/select/index.js +0 -176
  151. package/dist-npm/components/select/num-select-main.d.ts +0 -8
  152. package/dist-npm/components/select/num-select-main.js +0 -30
  153. package/dist-npm/components/select/num-select.d.ts +0 -5
  154. package/dist-npm/components/select/num-select.js +0 -125
  155. package/dist-npm/components/select/select-key.d.ts +0 -15
  156. package/dist-npm/components/select/select-key.js +0 -26
  157. package/dist-npm/components/select/select-main.d.ts +0 -19
  158. package/dist-npm/components/select/select-main.js +0 -151
  159. package/dist-npm/components/select/select-three.d.ts +0 -18
  160. package/dist-npm/components/select/select-three.js +0 -35
  161. package/dist-npm/components/select/select-two.d.ts +0 -2
  162. package/dist-npm/components/select/select-two.js +0 -98
  163. package/dist-npm/components/spinner/index.d.ts +0 -15
  164. package/dist-npm/components/spinner/index.js +0 -110
  165. package/dist-npm/components/st-end/end.d.ts +0 -2
  166. package/dist-npm/components/st-end/end.js +0 -35
  167. package/dist-npm/components/st-end/start.d.ts +0 -2
  168. package/dist-npm/components/st-end/start.js +0 -32
  169. package/dist-npm/components/toggle/index.d.ts +0 -13
  170. package/dist-npm/components/toggle/index.js +0 -121
  171. package/dist-npm/components/visual/animate/animate.d.ts +0 -13
  172. package/dist-npm/components/visual/animate/animate.js +0 -55
  173. package/dist-npm/components/visual/ascii-art/ascii-art.d.ts +0 -6
  174. package/dist-npm/components/visual/ascii-art/ascii-art.js +0 -12
  175. package/dist-npm/testing/index.d.ts +0 -18
  176. package/dist-npm/testing/index.js +0 -64
  177. package/dist-npm/types/general.d.ts +0 -134
  178. package/dist-npm/types/index.d.ts +0 -2
  179. package/dist-npm/types/index.js +0 -2
  180. package/dist-npm/types/keypress.d.ts +0 -52
  181. package/dist-npm/types/keypress.js +0 -37
  182. package/dist-npm/types/readline.d.ts +0 -30
  183. package/dist-npm/types/readline.js +0 -0
  184. package/dist-npm/types/relinka.d.ts +0 -30
  185. package/dist-npm/types/relinka.js +0 -0
  186. package/dist-npm/types/utils.d.ts +0 -17
  187. package/dist-npm/types/utils.js +0 -0
  188. package/dist-npm/utils/colorize.d.ts +0 -2
  189. package/dist-npm/utils/colorize.js +0 -130
  190. package/dist-npm/utils/component.d.ts +0 -101
  191. package/dist-npm/utils/component.js +0 -630
  192. package/dist-npm/utils/core.d.ts +0 -2
  193. package/dist-npm/utils/core.js +0 -2
  194. package/dist-npm/utils/decoder.d.ts +0 -14
  195. package/dist-npm/utils/decoder.js +0 -281
  196. package/dist-npm/utils/errors.d.ts +0 -1
  197. package/dist-npm/utils/errors.js +0 -15
  198. package/dist-npm/utils/keypress.d.ts +0 -7
  199. package/dist-npm/utils/keypress.js +0 -327
  200. package/dist-npm/utils/mapping.d.ts +0 -4
  201. package/dist-npm/utils/mapping.js +0 -49
  202. package/dist-npm/utils/messages.d.ts +0 -17
  203. package/dist-npm/utils/messages.js +0 -213
  204. package/dist-npm/utils/platforms.d.ts +0 -1
  205. package/dist-npm/utils/platforms.js +0 -22
  206. package/dist-npm/utils/prompt-tmp.d.ts +0 -13
  207. package/dist-npm/utils/prompt-tmp.js +0 -254
  208. package/dist-npm/utils/prompt-two.d.ts +0 -13
  209. package/dist-npm/utils/prompt-two.js +0 -254
  210. package/dist-npm/utils/readline.d.ts +0 -2
  211. package/dist-npm/utils/readline.js +0 -9
  212. package/dist-npm/utils/skeleton.d.ts +0 -7
  213. package/dist-npm/utils/skeleton.js +0 -145
  214. package/dist-npm/utils/terminal.d.ts +0 -5
  215. package/dist-npm/utils/terminal.js +0 -33
  216. package/dist-npm/utils/types.d.ts +0 -0
  217. package/dist-npm/utils/types.js +0 -0
  218. package/dist-npm/utils/utils.d.ts +0 -3
  219. package/dist-npm/utils/utils.js +0 -9
  220. package/dist-npm/utils/variants.d.ts +0 -9
  221. package/dist-npm/utils/variants.js +0 -49
  222. /package/dist-npm/{utils/constants.js → components/levels/levels.js} +0 -0
  223. /package/dist-npm/components/{instance/reporter → reporters}/browser.js +0 -0
  224. /package/dist-npm/types/{general.js → mod.js} +0 -0
@@ -1,176 +0,0 @@
1
- import ansiEscapes from "ansi-escapes";
2
- import colors from "picocolors";
3
- import {
4
- createPrompt,
5
- useState,
6
- useKeypress,
7
- usePrefix,
8
- usePagination,
9
- useRef,
10
- useMemo,
11
- useEffect,
12
- isBackspaceKey,
13
- isEnterKey,
14
- isUpKey,
15
- isDownKey,
16
- isNumberKey,
17
- Separator,
18
- ValidationError,
19
- makeTheme
20
- } from "../../components/core/index.js";
21
- import figures from "../../components/figures/index.js";
22
- const selectTheme = {
23
- icon: { cursor: figures.pointer },
24
- style: {
25
- disabled: (text) => colors.dim(`- ${text}`),
26
- description: (text) => colors.cyan(text)
27
- },
28
- helpMode: "auto"
29
- };
30
- function isSelectable(item) {
31
- return !Separator.isSeparator(item) && !item.disabled;
32
- }
33
- function normalizeChoices(choices) {
34
- return choices.map((choice) => {
35
- if (Separator.isSeparator(choice)) {
36
- return choice;
37
- }
38
- if (typeof choice === "string") {
39
- return {
40
- value: choice,
41
- name: choice,
42
- short: choice,
43
- disabled: false
44
- };
45
- }
46
- const name = choice.name ?? String(choice.value);
47
- return {
48
- value: choice.value,
49
- name,
50
- description: choice.description,
51
- short: choice.short ?? name,
52
- disabled: choice.disabled ?? false
53
- };
54
- });
55
- }
56
- const select = createPrompt(
57
- (config, done) => {
58
- const { loop = true, pageSize = 7 } = config;
59
- const firstRender = useRef(true);
60
- const theme = makeTheme(selectTheme, config.theme);
61
- const [status, setStatus] = useState("idle");
62
- const prefix = usePrefix({ status, theme });
63
- const searchTimeoutRef = useRef();
64
- const items = useMemo(
65
- () => normalizeChoices(config.choices),
66
- [config.choices]
67
- );
68
- const bounds = useMemo(() => {
69
- const first = items.findIndex(isSelectable);
70
- const last = items.findLastIndex(isSelectable);
71
- if (first === -1) {
72
- throw new ValidationError(
73
- "[select prompt] No selectable choices. All choices are disabled."
74
- );
75
- }
76
- return { first, last };
77
- }, [items]);
78
- const defaultItemIndex = useMemo(() => {
79
- if (!("default" in config)) {
80
- return -1;
81
- }
82
- return items.findIndex(
83
- (item) => isSelectable(item) && item.value === config.default
84
- );
85
- }, [config.default, items]);
86
- const [active, setActive] = useState(
87
- defaultItemIndex === -1 ? bounds.first : defaultItemIndex
88
- );
89
- const selectedChoice = items[active];
90
- useKeypress((key, rl) => {
91
- clearTimeout(searchTimeoutRef.current);
92
- if (isEnterKey(key)) {
93
- setStatus("done");
94
- done(selectedChoice.value);
95
- } else if (isUpKey(key) || isDownKey(key)) {
96
- rl.clearLine(0);
97
- if (loop || isUpKey(key) && active !== bounds.first || isDownKey(key) && active !== bounds.last) {
98
- const offset = isUpKey(key) ? -1 : 1;
99
- let next = active;
100
- do {
101
- next = (next + offset + items.length) % items.length;
102
- } while (!isSelectable(items[next]));
103
- setActive(next);
104
- }
105
- } else if (isNumberKey(key)) {
106
- rl.clearLine(0);
107
- const position = Number(key.name) - 1;
108
- const item = items[position];
109
- if (item != null && isSelectable(item)) {
110
- setActive(position);
111
- }
112
- } else if (isBackspaceKey(key)) {
113
- rl.clearLine(0);
114
- } else {
115
- const searchTerm = rl.line.toLowerCase();
116
- const matchIndex = items.findIndex((item) => {
117
- if (Separator.isSeparator(item) || !isSelectable(item)) {
118
- return false;
119
- }
120
- return item.name.toLowerCase().startsWith(searchTerm);
121
- });
122
- if (matchIndex !== -1) {
123
- setActive(matchIndex);
124
- }
125
- searchTimeoutRef.current = setTimeout(() => {
126
- rl.clearLine(0);
127
- }, 700);
128
- }
129
- });
130
- useEffect(
131
- () => () => {
132
- clearTimeout(searchTimeoutRef.current);
133
- },
134
- []
135
- );
136
- const message = theme.style.message(config.message, status);
137
- let helpTipTop = "";
138
- let helpTipBottom = "";
139
- if (theme.helpMode === "always" || theme.helpMode === "auto" && firstRender.current) {
140
- firstRender.current = false;
141
- if (items.length > pageSize) {
142
- helpTipBottom = `
143
- ${theme.style.help("(Use arrow keys to reveal more choices)")}`;
144
- } else {
145
- helpTipTop = theme.style.help("(Use arrow keys)");
146
- }
147
- }
148
- const page = usePagination({
149
- items,
150
- active,
151
- renderItem({ item, isActive }) {
152
- if (Separator.isSeparator(item)) {
153
- return ` ${item.separator}`;
154
- }
155
- if (item.disabled) {
156
- const disabledLabel = typeof item.disabled === "string" ? item.disabled : "(disabled)";
157
- return theme.style.disabled(`${item.name} ${disabledLabel}`);
158
- }
159
- const color = isActive ? theme.style.highlight : (x) => x;
160
- const cursor = isActive ? theme.icon.cursor : ` `;
161
- return color(`${cursor} ${item.name}`);
162
- },
163
- pageSize,
164
- loop
165
- });
166
- if (status === "done") {
167
- return `${prefix} ${message} ${theme.style.answer(selectedChoice.short)}`;
168
- }
169
- const choiceDescription = selectedChoice.description ? `
170
- ${theme.style.description(selectedChoice.description)}` : ``;
171
- return `${[prefix, message, helpTipTop].filter(Boolean).join(" ")}
172
- ${page}${helpTipBottom}${choiceDescription}${ansiEscapes.cursorHide}`;
173
- }
174
- );
175
- export default select;
176
- export { Separator } from "../../components/core/index.js";
@@ -1,8 +0,0 @@
1
- export declare function selectPrompt<T extends string>(params: {
2
- message: string;
3
- options: {
4
- label: string;
5
- value: T;
6
- }[];
7
- initial?: T;
8
- }): Promise<T>;
@@ -1,30 +0,0 @@
1
- import { stdin as input, stdout as output } from "process";
2
- import readline from "readline/promises";
3
- export async function selectPrompt(params) {
4
- const { message, options, initial } = params;
5
- const selectedIndex = initial ? options.findIndex((option) => option.value === initial) : 0;
6
- const rl = readline.createInterface({ input, output });
7
- while (true) {
8
- console.log(`
9
- ${message}`);
10
- options.forEach((option, index2) => {
11
- const prefix = index2 === selectedIndex ? "->" : " ";
12
- const isDefault = option.value === initial ? "(default)" : "";
13
- console.log(`${prefix} [${index2 + 1}] ${option.label} ${isDefault}`);
14
- });
15
- const promptMessage = initial !== void 0 ? `Please select an option by number (default ${selectedIndex + 1}): ` : "Please select an option by number: ";
16
- const response = await rl.question(promptMessage);
17
- let index;
18
- if (response.trim() === "" && initial !== void 0) {
19
- index = selectedIndex;
20
- } else {
21
- index = parseInt(response.trim(), 10) - 1;
22
- }
23
- if (index >= 0 && index < options.length) {
24
- rl.close();
25
- return options[index].value;
26
- } else {
27
- console.log("Invalid selection. Please try again.");
28
- }
29
- }
30
- }
@@ -1,5 +0,0 @@
1
- import type { TSchema, Static } from "@sinclair/typebox";
2
- import type { PromptOptions } from "../../types/general.js";
3
- export declare function numSelectPrompt<T extends TSchema>(options: PromptOptions<T> & {
4
- inline?: boolean;
5
- }): Promise<Static<T>>;
@@ -1,125 +0,0 @@
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 { fmt, msg, bar } from "../../utils/messages.js";
6
- import {
7
- countLines,
8
- deleteLastLine,
9
- deleteLastLines
10
- } from "../../utils/terminal.js";
11
- export async function numSelectPrompt(options) {
12
- const {
13
- title,
14
- hint,
15
- validate,
16
- defaultValue,
17
- schema,
18
- titleColor = "cyanBright",
19
- titleTypography = "bold",
20
- titleVariant,
21
- content,
22
- contentColor,
23
- contentTypography,
24
- contentVariant,
25
- borderColor = "viceGradient",
26
- variantOptions,
27
- inline = true,
28
- choices
29
- } = options;
30
- if (!choices || choices.length === 0) {
31
- throw new Error("Choices are required for select prompt.");
32
- }
33
- const rl = readline.createInterface({ input, output });
34
- const formattedBar = bar({ borderColor });
35
- let linesToDelete = 0;
36
- let errorMessage = "";
37
- while (true) {
38
- if (linesToDelete > 0) {
39
- deleteLastLines(linesToDelete);
40
- }
41
- const question = fmt({
42
- type: errorMessage !== "" ? "M_ERROR" : "M_GENERAL",
43
- title,
44
- titleColor,
45
- titleTypography,
46
- titleVariant,
47
- content,
48
- contentColor,
49
- contentTypography,
50
- contentVariant,
51
- borderColor,
52
- hint,
53
- variantOptions,
54
- errorMessage
55
- });
56
- let choicesText = "";
57
- if (inline) {
58
- choicesText = choices.map(
59
- (choice, index) => `${index + 1}) ${choice.title}${choice.description ? ` (${choice.description})` : ""}`
60
- ).join(" / ");
61
- } else {
62
- choicesText = choices.map(
63
- (choice, index) => `${index + 1}) ${choice.title}${choice.description ? ` - ${choice.description}` : ""}`
64
- ).join(`
65
- ${formattedBar} `);
66
- }
67
- const formattedPrompt = fmt({
68
- type: "M_NULL",
69
- title: `${question}${choicesText}
70
- ${formattedBar} ${colorize(
71
- `Enter your choice:`,
72
- contentColor
73
- )}
74
- ${formattedBar} `
75
- });
76
- const questionLines = countLines(formattedPrompt);
77
- linesToDelete = questionLines + 1;
78
- const prompt = await rl.question(formattedPrompt);
79
- const answer = prompt.trim() || defaultValue;
80
- if (!prompt.trim() && defaultValue !== void 0) {
81
- deleteLastLine();
82
- msg({
83
- type: "M_MIDDLE",
84
- title: ` ${defaultValue}`,
85
- titleColor: "none"
86
- });
87
- }
88
- const num = Number(answer);
89
- if (isNaN(num) || num < 1 || num > choices.length) {
90
- errorMessage = `Please enter a number between 1 and ${choices.length}.`;
91
- continue;
92
- }
93
- const selectedChoice = choices[num - 1];
94
- const selectedValue = selectedChoice?.id ?? num;
95
- let isValid = true;
96
- errorMessage = "";
97
- if (schema) {
98
- isValid = Value.Check(schema, selectedValue);
99
- if (!isValid) {
100
- const errors = [...Value.Errors(schema, selectedValue)];
101
- if (errors.length > 0) {
102
- errorMessage = errors[0]?.message ?? "Invalid input.";
103
- } else {
104
- errorMessage = "Invalid input.";
105
- }
106
- }
107
- }
108
- if (validate && isValid) {
109
- const validation = await validate(selectedValue);
110
- if (validation !== true) {
111
- isValid = false;
112
- errorMessage = typeof validation === "string" ? validation : "Invalid input.";
113
- }
114
- }
115
- if (isValid) {
116
- msg({ type: "M_NEWLINE" });
117
- rl.close();
118
- if (selectedChoice?.action) {
119
- await selectedChoice.action();
120
- }
121
- return selectedValue;
122
- } else {
123
- }
124
- }
125
- }
@@ -1,15 +0,0 @@
1
- import type { PromptOptions } from "../prompts/prompt.js";
2
- import Prompt from "../prompts/prompt.js";
3
- type SelectKeyOptions<T extends {
4
- value: any;
5
- }> = {
6
- options: T[];
7
- } & PromptOptions<SelectKeyPrompt<T>>;
8
- export default class SelectKeyPrompt<T extends {
9
- value: any;
10
- }> extends Prompt {
11
- options: T[];
12
- cursor: number;
13
- constructor(opts: SelectKeyOptions<T>);
14
- }
15
- export {};
@@ -1,26 +0,0 @@
1
- import Prompt from "../prompts/prompt.js";
2
- export default class SelectKeyPrompt extends Prompt {
3
- options;
4
- cursor = 0;
5
- constructor(opts) {
6
- super(opts, false);
7
- this.options = opts.options;
8
- const keys = this.options.map(
9
- ({ value: [initial] }) => initial?.toLowerCase()
10
- );
11
- this.cursor = Math.max(keys.indexOf(opts.initialValue), 0);
12
- this.on("key", (key) => {
13
- if (!keys.includes(key)) {
14
- return;
15
- }
16
- const value = this.options.find(
17
- ({ value: [initial] }) => initial?.toLowerCase() === key
18
- );
19
- if (value) {
20
- this.value = value.value;
21
- this.state = "submit";
22
- this.emit("submit");
23
- }
24
- });
25
- }
26
- }
@@ -1,19 +0,0 @@
1
- import type { ColorName, Variant, TypographyName } from "../../types/general.js";
2
- export declare function selectPrompt<T extends string>(params: {
3
- title: string;
4
- options: {
5
- label: string;
6
- value: T;
7
- hint?: string;
8
- }[];
9
- initial?: T;
10
- required?: boolean;
11
- borderColor?: ColorName;
12
- titleColor?: ColorName;
13
- titleTypography?: TypographyName;
14
- titleVariant?: Variant;
15
- border?: boolean;
16
- endTitle?: string;
17
- endTitleColor?: ColorName;
18
- maxItems?: number;
19
- }): Promise<T>;
@@ -1,151 +0,0 @@
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 { deleteLastLine } from "../../main.js";
5
- import { bar, fmt, msg, symbols } from "../../utils/messages.js";
6
- export async function selectPrompt(params) {
7
- const {
8
- title,
9
- options,
10
- initial,
11
- required = false,
12
- borderColor = "viceGradient",
13
- titleColor = "cyanBright",
14
- titleTypography = "bold",
15
- titleVariant,
16
- border = true,
17
- endTitle = "\u{1F44B}",
18
- endTitleColor = "passionGradient",
19
- maxItems
20
- } = params;
21
- let selectedIndex = initial ? options.findIndex((option) => option.value === initial) : 0;
22
- if (selectedIndex === -1) {
23
- selectedIndex = 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, <Enter> to select, <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)} ${fmt({
39
- type: "M_NULL",
40
- title,
41
- titleColor
42
- })}
43
- `;
44
- if (errorMessage) {
45
- outputStr += `${redBright(symbols.step_error)} ${errorMessage}
46
- `;
47
- } else {
48
- outputStr += `${formattedBar} ${dim(instructions)}
49
- `;
50
- }
51
- const terminalHeight = process.stdout.rows || 24;
52
- const availableHeight = terminalHeight - 4;
53
- const computedMaxItems = Math.min(
54
- maxItems ?? Infinity,
55
- availableHeight > 0 ? availableHeight : Infinity,
56
- options.length
57
- );
58
- const minItems = 3;
59
- const displayItems = Math.max(computedMaxItems, minItems);
60
- let startIdx = 0;
61
- let endIdx = options.length - 1;
62
- if (options.length > displayItems) {
63
- const half = Math.floor(displayItems / 2);
64
- startIdx = selectedIndex - half;
65
- endIdx = selectedIndex + (displayItems - half - 1);
66
- if (startIdx < 0) {
67
- startIdx = 0;
68
- endIdx = displayItems - 1;
69
- } else if (endIdx >= options.length) {
70
- endIdx = options.length - 1;
71
- startIdx = options.length - displayItems;
72
- }
73
- }
74
- const shouldRenderTopEllipsis = startIdx > 0;
75
- const shouldRenderBottomEllipsis = endIdx < options.length - 1;
76
- if (shouldRenderTopEllipsis) {
77
- outputStr += `${formattedBar} ${dim("...")}
78
- `;
79
- }
80
- for (let index = startIdx; index <= endIdx; index++) {
81
- const option = options[index];
82
- const isSelected = index === selectedIndex;
83
- const prefix = isSelected ? "> " : " ";
84
- const optionLabel = isSelected ? cyanBright(option.label) : option.label;
85
- const hint = option.hint ? ` (${option.hint})` : "";
86
- outputStr += `${formattedBar} ${prefix}${optionLabel}${dim(hint)}
87
- `;
88
- }
89
- if (shouldRenderBottomEllipsis) {
90
- outputStr += `${formattedBar} ${dim("...")}
91
- `;
92
- }
93
- linesRendered = 1 + // Title
94
- 1 + // Instructions or error message
95
- (shouldRenderTopEllipsis ? 1 : 0) + // Top ellipsis
96
- (endIdx - startIdx + 1) + // Displayed options
97
- (shouldRenderBottomEllipsis ? 1 : 0);
98
- process.stdout.write(outputStr);
99
- }
100
- renderOptions();
101
- return new Promise((resolve) => {
102
- function onKeyPress(str, key) {
103
- if (key.name === "up" || key.name === "k") {
104
- selectedIndex = (selectedIndex - 1 + options.length) % options.length;
105
- errorMessage = "";
106
- renderOptions();
107
- } else if (key.name === "down" || key.name === "j") {
108
- selectedIndex = (selectedIndex + 1) % options.length;
109
- errorMessage = "";
110
- renderOptions();
111
- } else if (key.name === "return") {
112
- if (required && !options[selectedIndex].value) {
113
- deleteLastLine();
114
- errorMessage = "You must select an option.";
115
- renderOptions();
116
- } else {
117
- cleanup();
118
- resolve(options[selectedIndex].value);
119
- deleteLastLine();
120
- deleteLastLine();
121
- }
122
- } else if (key.name === "c" && key.ctrl) {
123
- msg({ type: "M_NEWLINE" });
124
- msg({
125
- type: "M_END",
126
- title: endTitle,
127
- titleColor: endTitleColor,
128
- titleTypography,
129
- titleVariant,
130
- border,
131
- borderColor
132
- });
133
- cleanup(true);
134
- }
135
- }
136
- function cleanup(isCtrlC = false) {
137
- if (typeof input.setRawMode === "function") {
138
- input.setRawMode(false);
139
- }
140
- rl.close();
141
- input.removeListener("keypress", onKeyPress);
142
- process.stdout.write(`\x1B[${linesRendered}B`);
143
- if (isCtrlC) {
144
- process.exit();
145
- } else {
146
- console.log();
147
- }
148
- }
149
- input.on("keypress", onKeyPress);
150
- });
151
- }
@@ -1,18 +0,0 @@
1
- import type { PromptOptions } from "../../components/prompts/prompt.js";
2
- import Prompt from "../../components/prompts/prompt.js";
3
- type SelectOptions<T extends {
4
- value: any;
5
- }> = {
6
- options: T[];
7
- initialValue?: T["value"];
8
- } & PromptOptions<SelectPrompt<T>>;
9
- export default class SelectPrompt<T extends {
10
- value: any;
11
- }> extends Prompt {
12
- options: T[];
13
- cursor: number;
14
- private get _value();
15
- private changeValue;
16
- constructor(opts: SelectOptions<T>);
17
- }
18
- export {};
@@ -1,35 +0,0 @@
1
- import Prompt from "../../components/prompts/prompt.js";
2
- export default class SelectPrompt extends Prompt {
3
- options;
4
- cursor = 0;
5
- get _value() {
6
- return this.options[this.cursor];
7
- }
8
- changeValue() {
9
- this.value = this._value.value;
10
- }
11
- constructor(opts) {
12
- super(opts, false);
13
- this.options = opts.options;
14
- this.cursor = this.options.findIndex(
15
- ({ value }) => value === opts.initialValue
16
- );
17
- if (this.cursor === -1) {
18
- this.cursor = 0;
19
- }
20
- this.changeValue();
21
- this.on("cursor", (key) => {
22
- switch (key) {
23
- case "left":
24
- case "up":
25
- this.cursor = this.cursor === 0 ? this.options.length - 1 : this.cursor - 1;
26
- break;
27
- case "down":
28
- case "right":
29
- this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1;
30
- break;
31
- }
32
- this.changeValue();
33
- });
34
- }
35
- }
@@ -1,2 +0,0 @@
1
- import type { PromptOptions } from "../../types/general.js";
2
- export declare function selectPrompt(options: PromptOptions): Promise<string>;