@reliverse/relinka 1.2.0 → 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 (223) hide show
  1. package/README.md +37 -41
  2. package/dist-npm/components/core/core.d.ts +2 -0
  3. package/dist-npm/components/core/core.js +2 -0
  4. package/dist-npm/{utils/constants.d.ts → components/levels/levels.d.ts} +1 -1
  5. package/dist-npm/components/{prompts/create.d.ts → modes/basic.d.ts} +3 -3
  6. package/dist-npm/components/{instance → modes}/basic.js +4 -4
  7. package/dist-npm/components/{instance → modes}/browser.d.ts +2 -2
  8. package/dist-npm/components/modes/browser.js +12 -0
  9. package/dist-npm/components/modes/shared.d.ts +5 -0
  10. package/dist-npm/components/modes/shared.js +2 -0
  11. package/dist-npm/components/{prompts → relinka}/relinka.d.ts +2 -4
  12. package/dist-npm/components/{prompts → relinka}/relinka.js +1 -7
  13. package/dist-npm/components/{instance/reporter → reporters}/basic.d.ts +1 -1
  14. package/dist-npm/components/{instance/reporter → reporters}/basic.js +2 -2
  15. package/dist-npm/components/{instance/reporter → reporters}/browser.d.ts +1 -1
  16. package/dist-npm/components/{instance/reporter → reporters}/fancy.d.ts +3 -3
  17. package/dist-npm/components/{instance/reporter → reporters}/fancy.js +5 -7
  18. package/dist-npm/main.d.ts +8 -1
  19. package/dist-npm/main.js +34 -1
  20. package/dist-npm/types/mod.d.ts +42 -0
  21. package/dist-npm/utils/color.js +4 -1
  22. package/dist-npm/utils/mod.d.ts +3 -0
  23. package/dist-npm/utils/mod.js +9 -0
  24. package/package.json +1 -1
  25. package/dist-npm/components/anykey/index.d.ts +0 -7
  26. package/dist-npm/components/anykey/index.js +0 -61
  27. package/dist-npm/components/block/block.d.ts +0 -10
  28. package/dist-npm/components/block/block.js +0 -52
  29. package/dist-npm/components/checkbox/index.d.ts +0 -3
  30. package/dist-npm/components/checkbox/index.js +0 -217
  31. package/dist-npm/components/confirm/confirm-main.d.ts +0 -3
  32. package/dist-npm/components/confirm/confirm-main.js +0 -98
  33. package/dist-npm/components/confirm/confirm-three.d.ts +0 -13
  34. package/dist-npm/components/confirm/confirm-three.js +0 -26
  35. package/dist-npm/components/confirm/index.d.ts +0 -2
  36. package/dist-npm/components/confirm/index.js +0 -41
  37. package/dist-npm/components/core/Separator.d.ts +0 -6
  38. package/dist-npm/components/core/Separator.js +0 -18
  39. package/dist-npm/components/core/create-prompt.d.ts +0 -4
  40. package/dist-npm/components/core/create-prompt.js +0 -107
  41. package/dist-npm/components/core/errors.d.ts +0 -20
  42. package/dist-npm/components/core/errors.js +0 -21
  43. package/dist-npm/components/core/hook-engine.d.ts +0 -23
  44. package/dist-npm/components/core/hook-engine.js +0 -109
  45. package/dist-npm/components/core/index.d.ts +0 -13
  46. package/dist-npm/components/core/index.js +0 -12
  47. package/dist-npm/components/core/key.d.ts +0 -10
  48. package/dist-npm/components/core/key.js +0 -16
  49. package/dist-npm/components/core/lines.d.ts +0 -14
  50. package/dist-npm/components/core/lines.js +0 -52
  51. package/dist-npm/components/core/make-theme.d.ts +0 -3
  52. package/dist-npm/components/core/make-theme.js +0 -28
  53. package/dist-npm/components/core/position.d.ts +0 -12
  54. package/dist-npm/components/core/position.js +0 -29
  55. package/dist-npm/components/core/promise-polyfill.d.ts +0 -7
  56. package/dist-npm/components/core/promise-polyfill.js +0 -13
  57. package/dist-npm/components/core/screen-manager.d.ts +0 -14
  58. package/dist-npm/components/core/screen-manager.js +0 -67
  59. package/dist-npm/components/core/theme.d.ts +0 -21
  60. package/dist-npm/components/core/theme.js +0 -24
  61. package/dist-npm/components/core/use-effect.d.ts +0 -2
  62. package/dist-npm/components/core/use-effect.js +0 -11
  63. package/dist-npm/components/core/use-keypress.d.ts +0 -3
  64. package/dist-npm/components/core/use-keypress.js +0 -21
  65. package/dist-npm/components/core/use-memo.d.ts +0 -1
  66. package/dist-npm/components/core/use-memo.js +0 -12
  67. package/dist-npm/components/core/use-pagination.d.ts +0 -11
  68. package/dist-npm/components/core/use-pagination.js +0 -34
  69. package/dist-npm/components/core/use-prefix.d.ts +0 -5
  70. package/dist-npm/components/core/use-prefix.js +0 -42
  71. package/dist-npm/components/core/use-ref.d.ts +0 -6
  72. package/dist-npm/components/core/use-ref.js +0 -4
  73. package/dist-npm/components/core/use-state.d.ts +0 -4
  74. package/dist-npm/components/core/use-state.js +0 -17
  75. package/dist-npm/components/core/useKeyPress.d.ts +0 -4
  76. package/dist-npm/components/core/useKeyPress.js +0 -16
  77. package/dist-npm/components/core/usePromptState.d.ts +0 -6
  78. package/dist-npm/components/core/usePromptState.js +0 -10
  79. package/dist-npm/components/core/utils.d.ts +0 -2
  80. package/dist-npm/components/core/utils.js +0 -11
  81. package/dist-npm/components/date/date.d.ts +0 -6
  82. package/dist-npm/components/date/date.js +0 -210
  83. package/dist-npm/components/editor/index.d.ts +0 -2
  84. package/dist-npm/components/editor/index.js +0 -78
  85. package/dist-npm/components/expand/index.d.ts +0 -3
  86. package/dist-npm/components/expand/index.js +0 -119
  87. package/dist-npm/components/figures/index.d.ts +0 -465
  88. package/dist-npm/components/figures/index.js +0 -299
  89. package/dist-npm/components/input/index.d.ts +0 -2
  90. package/dist-npm/components/input/index.js +0 -66
  91. package/dist-npm/components/input/text-main.d.ts +0 -3
  92. package/dist-npm/components/input/text-main.js +0 -103
  93. package/dist-npm/components/input/text.d.ts +0 -11
  94. package/dist-npm/components/input/text.js +0 -26
  95. package/dist-npm/components/instance/basic.d.ts +0 -8
  96. package/dist-npm/components/instance/browser.js +0 -18
  97. package/dist-npm/components/instance/shared.d.ts +0 -6
  98. package/dist-npm/components/instance/shared.js +0 -2
  99. package/dist-npm/components/mono/mono.d.ts +0 -5
  100. package/dist-npm/components/mono/mono.js +0 -64
  101. package/dist-npm/components/mono/monoTwo.d.ts +0 -30
  102. package/dist-npm/components/mono/monoTwo.js +0 -37
  103. package/dist-npm/components/multiselect/group-multiselect.d.ts +0 -23
  104. package/dist-npm/components/multiselect/group-multiselect.js +0 -58
  105. package/dist-npm/components/multiselect/multi-select-two.d.ts +0 -3
  106. package/dist-npm/components/multiselect/multi-select-two.js +0 -111
  107. package/dist-npm/components/multiselect/multi-select.d.ts +0 -21
  108. package/dist-npm/components/multiselect/multi-select.js +0 -45
  109. package/dist-npm/components/multiselect/multiselect-main.d.ts +0 -18
  110. package/dist-npm/components/multiselect/multiselect-main.js +0 -166
  111. package/dist-npm/components/multiselect/num-multi-select.d.ts +0 -3
  112. package/dist-npm/components/multiselect/num-multi-select.js +0 -111
  113. package/dist-npm/components/multiselect/num-multiselect-main.d.ts +0 -6
  114. package/dist-npm/components/multiselect/num-multiselect-main.js +0 -28
  115. package/dist-npm/components/next-steps/next-steps.d.ts +0 -2
  116. package/dist-npm/components/next-steps/next-steps.js +0 -24
  117. package/dist-npm/components/number/index.d.ts +0 -2
  118. package/dist-npm/components/number/index.js +0 -100
  119. package/dist-npm/components/number/number-main.d.ts +0 -3
  120. package/dist-npm/components/number/number-main.js +0 -95
  121. package/dist-npm/components/password/index.d.ts +0 -2
  122. package/dist-npm/components/password/index.js +0 -59
  123. package/dist-npm/components/password/password-main.d.ts +0 -3
  124. package/dist-npm/components/password/password-main.js +0 -119
  125. package/dist-npm/components/password/password-three.d.ts +0 -13
  126. package/dist-npm/components/password/password-three.js +0 -28
  127. package/dist-npm/components/progressbar/ProgressBar.d.ts +0 -22
  128. package/dist-npm/components/progressbar/ProgressBar.js +0 -53
  129. package/dist-npm/components/progressbar/helper.d.ts +0 -6
  130. package/dist-npm/components/progressbar/helper.js +0 -38
  131. package/dist-npm/components/progressbar/index.d.ts +0 -2
  132. package/dist-npm/components/progressbar/index.js +0 -1
  133. package/dist-npm/components/prompts/create.js +0 -35
  134. package/dist-npm/components/prompts/index.d.ts +0 -49
  135. package/dist-npm/components/prompts/index.js +0 -46
  136. package/dist-npm/components/prompts/prompt.d.ts +0 -36
  137. package/dist-npm/components/prompts/prompt.js +0 -232
  138. package/dist-npm/components/prompts/promptTwo.d.ts +0 -92
  139. package/dist-npm/components/prompts/promptTwo.js +0 -653
  140. package/dist-npm/components/range/range.d.ts +0 -62
  141. package/dist-npm/components/range/range.js +0 -270
  142. package/dist-npm/components/rawlist/index.d.ts +0 -3
  143. package/dist-npm/components/rawlist/index.js +0 -104
  144. package/dist-npm/components/results/results.d.ts +0 -10
  145. package/dist-npm/components/results/results.js +0 -34
  146. package/dist-npm/components/search/index.d.ts +0 -3
  147. package/dist-npm/components/search/index.js +0 -186
  148. package/dist-npm/components/select/index.d.ts +0 -3
  149. package/dist-npm/components/select/index.js +0 -176
  150. package/dist-npm/components/select/num-select-main.d.ts +0 -8
  151. package/dist-npm/components/select/num-select-main.js +0 -30
  152. package/dist-npm/components/select/num-select.d.ts +0 -5
  153. package/dist-npm/components/select/num-select.js +0 -125
  154. package/dist-npm/components/select/select-key.d.ts +0 -15
  155. package/dist-npm/components/select/select-key.js +0 -26
  156. package/dist-npm/components/select/select-main.d.ts +0 -19
  157. package/dist-npm/components/select/select-main.js +0 -151
  158. package/dist-npm/components/select/select-three.d.ts +0 -18
  159. package/dist-npm/components/select/select-three.js +0 -35
  160. package/dist-npm/components/select/select-two.d.ts +0 -2
  161. package/dist-npm/components/select/select-two.js +0 -98
  162. package/dist-npm/components/spinner/index.d.ts +0 -15
  163. package/dist-npm/components/spinner/index.js +0 -110
  164. package/dist-npm/components/st-end/end.d.ts +0 -2
  165. package/dist-npm/components/st-end/end.js +0 -35
  166. package/dist-npm/components/st-end/start.d.ts +0 -2
  167. package/dist-npm/components/st-end/start.js +0 -32
  168. package/dist-npm/components/toggle/index.d.ts +0 -13
  169. package/dist-npm/components/toggle/index.js +0 -121
  170. package/dist-npm/components/visual/animate/animate.d.ts +0 -13
  171. package/dist-npm/components/visual/animate/animate.js +0 -55
  172. package/dist-npm/components/visual/ascii-art/ascii-art.d.ts +0 -6
  173. package/dist-npm/components/visual/ascii-art/ascii-art.js +0 -12
  174. package/dist-npm/testing/index.d.ts +0 -18
  175. package/dist-npm/testing/index.js +0 -64
  176. package/dist-npm/types/general.d.ts +0 -134
  177. package/dist-npm/types/index.d.ts +0 -2
  178. package/dist-npm/types/index.js +0 -2
  179. package/dist-npm/types/keypress.d.ts +0 -52
  180. package/dist-npm/types/keypress.js +0 -37
  181. package/dist-npm/types/readline.d.ts +0 -30
  182. package/dist-npm/types/readline.js +0 -0
  183. package/dist-npm/types/relinka.d.ts +0 -30
  184. package/dist-npm/types/relinka.js +0 -0
  185. package/dist-npm/types/utils.d.ts +0 -17
  186. package/dist-npm/types/utils.js +0 -0
  187. package/dist-npm/utils/colorize.d.ts +0 -2
  188. package/dist-npm/utils/colorize.js +0 -130
  189. package/dist-npm/utils/component.d.ts +0 -101
  190. package/dist-npm/utils/component.js +0 -630
  191. package/dist-npm/utils/core.d.ts +0 -2
  192. package/dist-npm/utils/core.js +0 -2
  193. package/dist-npm/utils/decoder.d.ts +0 -14
  194. package/dist-npm/utils/decoder.js +0 -281
  195. package/dist-npm/utils/errors.d.ts +0 -1
  196. package/dist-npm/utils/errors.js +0 -15
  197. package/dist-npm/utils/keypress.d.ts +0 -7
  198. package/dist-npm/utils/keypress.js +0 -327
  199. package/dist-npm/utils/mapping.d.ts +0 -4
  200. package/dist-npm/utils/mapping.js +0 -49
  201. package/dist-npm/utils/messages.d.ts +0 -17
  202. package/dist-npm/utils/messages.js +0 -213
  203. package/dist-npm/utils/platforms.d.ts +0 -1
  204. package/dist-npm/utils/platforms.js +0 -22
  205. package/dist-npm/utils/prompt-tmp.d.ts +0 -13
  206. package/dist-npm/utils/prompt-tmp.js +0 -254
  207. package/dist-npm/utils/prompt-two.d.ts +0 -13
  208. package/dist-npm/utils/prompt-two.js +0 -254
  209. package/dist-npm/utils/readline.d.ts +0 -2
  210. package/dist-npm/utils/readline.js +0 -9
  211. package/dist-npm/utils/skeleton.d.ts +0 -7
  212. package/dist-npm/utils/skeleton.js +0 -145
  213. package/dist-npm/utils/terminal.d.ts +0 -5
  214. package/dist-npm/utils/terminal.js +0 -33
  215. package/dist-npm/utils/types.d.ts +0 -0
  216. package/dist-npm/utils/types.js +0 -0
  217. package/dist-npm/utils/utils.d.ts +0 -3
  218. package/dist-npm/utils/utils.js +0 -9
  219. package/dist-npm/utils/variants.d.ts +0 -9
  220. package/dist-npm/utils/variants.js +0 -49
  221. /package/dist-npm/{utils/constants.js → components/levels/levels.js} +0 -0
  222. /package/dist-npm/components/{instance/reporter → reporters}/browser.js +0 -0
  223. /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>;