@reliverse/relinka 1.1.4 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) 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 -86
  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 -168
  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 -86
  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 -98
  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 -98
  49. package/dist-npm/components/multiselect/num-multiselect-main.js +35 -24
  50. package/dist-npm/components/next-steps/next-steps.js +25 -24
  51. package/dist-npm/components/number/index.js +112 -78
  52. package/dist-npm/components/number/number-main.js +2 -96
  53. package/dist-npm/components/password/index.js +73 -54
  54. package/dist-npm/components/password/password-main.js +2 -120
  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 -125
  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 -95
  75. package/dist-npm/components/spinner/index.js +136 -107
  76. package/dist-npm/components/st-end/end.d.ts +1 -1
  77. package/dist-npm/components/st-end/end.js +26 -35
  78. package/dist-npm/components/st-end/start.d.ts +1 -1
  79. package/dist-npm/components/st-end/start.js +15 -30
  80. package/dist-npm/components/toggle/index.js +137 -113
  81. package/dist-npm/components/visual/animate/animate.js +10 -53
  82. package/dist-npm/components/visual/ascii-art/ascii-art.js +1 -12
  83. package/dist-npm/mod.js +2 -1
  84. package/dist-npm/testing/index.js +83 -58
  85. package/dist-npm/types/general.d.ts +2 -1
  86. package/dist-npm/types/general.js +1 -0
  87. package/dist-npm/types/index.js +3 -2
  88. package/dist-npm/types/keypress.js +36 -35
  89. package/dist-npm/types/readline.js +1 -0
  90. package/dist-npm/types/relinka.js +1 -0
  91. package/dist-npm/types/utils.js +1 -0
  92. package/dist-npm/utils/box.js +137 -135
  93. package/dist-npm/utils/color.js +74 -65
  94. package/dist-npm/utils/colorize.js +156 -124
  95. package/dist-npm/utils/component.js +657 -532
  96. package/dist-npm/utils/constants.js +63 -64
  97. package/dist-npm/utils/core.js +3 -2
  98. package/dist-npm/utils/decoder.js +223 -244
  99. package/dist-npm/utils/error.js +9 -4
  100. package/dist-npm/utils/errors.js +4 -14
  101. package/dist-npm/utils/format.js +24 -19
  102. package/dist-npm/utils/keypress.js +414 -316
  103. package/dist-npm/utils/log.js +15 -11
  104. package/dist-npm/utils/mapping.js +52 -45
  105. package/dist-npm/utils/messages.js +183 -183
  106. package/dist-npm/utils/platforms.js +20 -16
  107. package/dist-npm/utils/prompt-tmp.js +286 -235
  108. package/dist-npm/utils/prompt-two.js +286 -235
  109. package/dist-npm/utils/readline.js +7 -5
  110. package/dist-npm/utils/skeleton.js +170 -130
  111. package/dist-npm/utils/stream.js +2 -2
  112. package/dist-npm/utils/string.js +58 -44
  113. package/dist-npm/utils/terminal.js +34 -23
  114. package/dist-npm/utils/tree.js +41 -30
  115. package/dist-npm/utils/types.js +1 -0
  116. package/dist-npm/utils/utils.js +8 -8
  117. package/dist-npm/utils/variants.js +44 -36
  118. package/package.json +20 -28
@@ -1,109 +1,138 @@
1
- import { AsyncLocalStorage, AsyncResource } from "node:async_hooks";
2
- import { HookError, ValidationError } from "./errors.js";
1
+ import {
2
+ AsyncLocalStorage,
3
+ AsyncResource,
4
+ } from 'node:async_hooks';
5
+ import {
6
+ HookError,
7
+ ValidationError,
8
+ } from './errors.js';
9
+
10
+ const isFn = (a) => typeof a === 'function';
3
11
  const hookStorage = new AsyncLocalStorage();
12
+
4
13
  function createStore(rl) {
5
- const store = {
6
- rl,
7
- hooks: [],
8
- hooksCleanup: [],
9
- hooksEffect: [],
10
- index: 0,
11
- handleChange() {
12
- }
13
- };
14
- return store;
14
+ const store = {
15
+ rl,
16
+ hooks: [],
17
+ hooksCleanup: [],
18
+ hooksEffect: [],
19
+ index: 0,
20
+ handleChange() {},
21
+ };
22
+
23
+ return store;
15
24
  }
25
+
16
26
  export function withHooks(rl, cb) {
17
- const store = createStore(rl);
18
- return hookStorage.run(store, () => {
19
- function cycle(render) {
20
- store.handleChange = () => {
21
- store.index = 0;
22
- render();
23
- };
24
- store.handleChange();
25
- }
26
- return cb(cycle);
27
- });
27
+ const store = createStore(rl);
28
+
29
+ return hookStorage.run(store, () => {
30
+ function cycle(render) {
31
+ store.handleChange = () => {
32
+ store.index = 0;
33
+ render();
34
+ };
35
+ store.handleChange();
36
+ }
37
+
38
+ return cb(cycle);
39
+ });
28
40
  }
41
+
29
42
  function getStore() {
30
- const store = hookStorage.getStore();
31
- if (!store) {
32
- throw new HookError(
33
- "[Relinka] Hook functions can only be called from within a prompt"
34
- );
35
- }
36
- return store;
43
+ const store = hookStorage.getStore();
44
+
45
+ if (!store) {
46
+ throw new HookError('[Relinka] Hook functions can only be called from within a prompt');
47
+ }
48
+
49
+ return store;
37
50
  }
51
+
38
52
  export function readline() {
39
- return getStore().rl;
53
+ return getStore().rl;
40
54
  }
55
+
41
56
  export function withUpdates(fn) {
42
- const wrapped = (...args) => {
43
- const store = getStore();
44
- let shouldUpdate = false;
45
- const oldHandleChange = store.handleChange;
46
- store.handleChange = () => {
47
- shouldUpdate = true;
57
+ const wrapped = (...args) => {
58
+ const store = getStore();
59
+ let shouldUpdate = false;
60
+ const oldHandleChange = store.handleChange;
61
+
62
+ store.handleChange = () => {
63
+ shouldUpdate = true;
64
+ };
65
+ const returnValue = fn(...args);
66
+
67
+ if (shouldUpdate) {
68
+ oldHandleChange();
69
+ }
70
+
71
+ store.handleChange = oldHandleChange;
72
+ return returnValue;
48
73
  };
49
- const returnValue = fn(...args);
50
- if (shouldUpdate) {
51
- oldHandleChange();
52
- }
53
- store.handleChange = oldHandleChange;
54
- return returnValue;
55
- };
56
- return AsyncResource.bind(wrapped);
74
+
75
+ return AsyncResource.bind(wrapped);
57
76
  }
77
+
58
78
  export function withPointer(cb) {
59
- const store = getStore();
60
- const { index } = store;
61
- const pointer = {
62
- get() {
63
- return store.hooks[index];
64
- },
65
- set(value) {
66
- store.hooks[index] = value;
67
- },
68
- initialized: index in store.hooks
69
- };
70
- const returnValue = cb(pointer);
71
- store.index++;
72
- return returnValue;
79
+ const store = getStore();
80
+ const {index} = store;
81
+ const pointer = {
82
+ get() {
83
+ return store.hooks[index];
84
+ },
85
+ set(value) {
86
+ store.hooks[index] = value;
87
+ },
88
+ initialized: index in store.hooks,
89
+ };
90
+
91
+ const returnValue = cb(pointer);
92
+
93
+ store.index++;
94
+ return returnValue;
73
95
  }
96
+
74
97
  export function handleChange() {
75
- getStore().handleChange();
98
+ getStore().handleChange();
76
99
  }
100
+
77
101
  export const effectScheduler = {
78
- queue(cb) {
79
- const store = getStore();
80
- const { index } = store;
81
- store.hooksEffect.push(() => {
82
- store.hooksCleanup[index]?.();
83
- const cleanFn = cb(readline());
84
- if (cleanFn != null && typeof cleanFn !== "function") {
85
- throw new ValidationError(
86
- "useEffect return value must be a cleanup function or nothing."
87
- );
88
- }
89
- store.hooksCleanup[index] = cleanFn;
90
- });
91
- },
92
- run() {
93
- const store = getStore();
94
- withUpdates(() => {
95
- store.hooksEffect.forEach((effect) => {
96
- effect();
97
- });
98
- store.hooksEffect.length = 0;
99
- })();
100
- },
101
- clearAll() {
102
- const store = getStore();
103
- store.hooksCleanup.forEach((cleanFn) => {
104
- cleanFn?.();
105
- });
106
- store.hooksEffect.length = 0;
107
- store.hooksCleanup.length = 0;
108
- }
102
+ queue(cb) {
103
+ const store = getStore();
104
+ const {index} = store;
105
+
106
+ store.hooksEffect.push(() => {
107
+ store.hooksCleanup[index]?.();
108
+ const cleanFn = cb(readline());
109
+
110
+ if (cleanFn != null && !isFn(cleanFn)) {
111
+ throw new ValidationError('useEffect return value must be a cleanup function or nothing.');
112
+ }
113
+
114
+ store.hooksCleanup[index] = cleanFn;
115
+ });
116
+ },
117
+ run() {
118
+ const store = getStore();
119
+
120
+ withUpdates(() => {
121
+ for (const effect of store.hooksEffect) {
122
+ effect();
123
+ }
124
+
125
+ store.hooksEffect.length = 0;
126
+ })();
127
+ },
128
+ clearAll() {
129
+ const store = getStore();
130
+
131
+ for (const cleanFn of store.hooksCleanup) {
132
+ cleanFn?.();
133
+ }
134
+
135
+ store.hooksEffect.length = 0;
136
+ store.hooksCleanup.length = 0;
137
+ },
109
138
  };
@@ -1,12 +1,12 @@
1
- export * from "./key.js";
2
- export * from "./errors.js";
3
- export { usePrefix } from "./use-prefix.js";
4
- export { useState } from "./use-state.js";
5
- export { useEffect } from "./use-effect.js";
6
- export { useMemo } from "./use-memo.js";
7
- export { useRef } from "./use-ref.js";
8
- export { useKeypress } from "./use-keypress.js";
9
- export { makeTheme } from "./make-theme.js";
10
- export { usePagination } from "./use-pagination.js";
11
- export { createPrompt } from "./create-prompt.js";
12
- export { Separator } from "./Separator.js";
1
+ export * from './key.js';
2
+ export * from './errors.js';
3
+ export {usePrefix} from './use-prefix.js';
4
+ export {useState} from './use-state.js';
5
+ export {useEffect} from './use-effect.js';
6
+ export {useMemo} from './use-memo.js';
7
+ export {useRef} from './use-ref.js';
8
+ export {useKeypress} from './use-keypress.js';
9
+ export {makeTheme} from './make-theme.js';
10
+ export {usePagination} from './use-pagination.js';
11
+ export {createPrompt} from './create-prompt.js';
12
+ export {Separator} from './Separator.js';
@@ -1,16 +1,6 @@
1
- export const isUpKey = (key) => (
2
-
3
- key.name === "up" ||
4
- key.name === "k" ||
5
- key.ctrl && key.name === "p"
6
- );
7
- export const isDownKey = (key) => (
8
-
9
- key.name === "down" ||
10
- key.name === "j" ||
11
- key.ctrl && key.name === "n"
12
- );
13
- export const isSpaceKey = (key) => key.name === "space";
14
- export const isBackspaceKey = (key) => key.name === "backspace";
15
- export const isNumberKey = (key) => "123456789".includes(key.name);
16
- export const isEnterKey = (key) => key.name === "enter" || key.name === "return";
1
+ export const isUpKey = (key) => (key.name === 'up' || key.name === 'k' || key.ctrl && key.name === 'p');
2
+ export const isDownKey = (key) => (key.name === 'down' || key.name === 'j' || key.ctrl && key.name === 'n');
3
+ export const isSpaceKey = (key) => key.name === 'space';
4
+ export const isBackspaceKey = (key) => key.name === 'backspace';
5
+ export const isNumberKey = (key) => '123456789'.includes(key.name);
6
+ export const isEnterKey = (key) => key.name === 'enter' || key.name === 'return';
@@ -1,52 +1,61 @@
1
- import { breakLines } from "./utils.js";
1
+ import {breakLines} from './utils.js';
2
+
2
3
  function split(content, width) {
3
- return breakLines(content, width).split("\n");
4
+ return breakLines(content, width).split('\n');
4
5
  }
6
+
5
7
  function rotate(count, items) {
6
- const max = items.length;
7
- const offset = (count % max + max) % max;
8
- return [...items.slice(offset), ...items.slice(0, offset)];
8
+ const max = items.length;
9
+ const offset = (count % max + max) % max;
10
+
11
+ return [
12
+ ...items.slice(offset),
13
+ ...items.slice(0, offset),
14
+ ];
9
15
  }
10
- export function lines({
11
- items,
12
- width,
13
- renderItem,
14
- active,
15
- position: requested,
16
- pageSize
17
- }) {
18
- const layouts = items.map((item, index) => ({
19
- item,
20
- index,
21
- isActive: index === active
22
- }));
23
- const layoutsInPage = rotate(active - requested, layouts).slice(0, pageSize);
24
- const renderItemAt = (index) => layoutsInPage[index] == null ? [] : split(renderItem(layoutsInPage[index]), width);
25
- const pageBuffer = Array.from({ length: pageSize });
26
- const activeItem = renderItemAt(requested).slice(0, pageSize);
27
- const position = requested + activeItem.length <= pageSize ? requested : pageSize - activeItem.length;
28
- pageBuffer.splice(position, activeItem.length, ...activeItem);
29
- let bufferPointer = position + activeItem.length;
30
- let layoutPointer = requested + 1;
31
- while (bufferPointer < pageSize && layoutPointer < layoutsInPage.length) {
32
- for (const line of renderItemAt(layoutPointer)) {
33
- pageBuffer[bufferPointer++] = line;
34
- if (bufferPointer >= pageSize) {
35
- break;
36
- }
16
+
17
+ export function lines({items, width, renderItem, active, position: requested, pageSize}) {
18
+ const layouts = items.map((item, index) => ({
19
+ item,
20
+ index,
21
+ isActive: index === active,
22
+ }));
23
+
24
+ const layoutsInPage = rotate(active - requested, layouts).slice(0, pageSize);
25
+ const renderItemAt = (index) => layoutsInPage[index] == null ? [] : split(renderItem(layoutsInPage[index]), width);
26
+ const pageBuffer = Array.from({
27
+ length: pageSize,
28
+ });
29
+ const activeItem = renderItemAt(requested).slice(0, pageSize);
30
+ const position = pageSize >= requested + activeItem.length ? requested : pageSize - activeItem.length;
31
+
32
+ pageBuffer.splice(position, activeItem.length, ...activeItem);
33
+ let bufferPointer = position + activeItem.length;
34
+ let layoutPointer = requested + 1;
35
+
36
+ while (bufferPointer < pageSize && layoutPointer < layoutsInPage.length) {
37
+ for (const line of renderItemAt(layoutPointer)) {
38
+ pageBuffer[bufferPointer++] = line;
39
+ if (bufferPointer >= pageSize) {
40
+ break;
41
+ }
42
+ }
43
+
44
+ layoutPointer++;
37
45
  }
38
- layoutPointer++;
39
- }
40
- bufferPointer = position - 1;
41
- layoutPointer = requested - 1;
42
- while (bufferPointer >= 0 && layoutPointer >= 0) {
43
- for (const line of renderItemAt(layoutPointer).reverse()) {
44
- pageBuffer[bufferPointer--] = line;
45
- if (bufferPointer < 0) {
46
- break;
47
- }
46
+
47
+ bufferPointer = position - 1;
48
+ layoutPointer = requested - 1;
49
+ while (bufferPointer >= 0 && layoutPointer >= 0) {
50
+ for (const line of renderItemAt(layoutPointer).reverse()) {
51
+ pageBuffer[bufferPointer--] = line;
52
+ if (bufferPointer < 0) {
53
+ break;
54
+ }
55
+ }
56
+
57
+ layoutPointer--;
48
58
  }
49
- layoutPointer--;
50
- }
51
- return pageBuffer.filter((line) => typeof line === "string");
59
+
60
+ return pageBuffer.filter((line) => typeof line === 'string');
52
61
  }
@@ -1,28 +1,37 @@
1
- import { defaultTheme } from "./theme.js";
1
+ import {defaultTheme} from './theme.js';
2
+
2
3
  function isPlainObject(value) {
3
- if (typeof value !== "object" || value === null) {
4
- return false;
5
- }
6
- let proto = value;
7
- while (Object.getPrototypeOf(proto) !== null) {
8
- proto = Object.getPrototypeOf(proto);
9
- }
10
- return Object.getPrototypeOf(value) === proto;
4
+ if (typeof value !== 'object' || value === null) {
5
+ return false;
6
+ }
7
+
8
+ let proto = value;
9
+
10
+ while (Object.getPrototypeOf(proto) !== null) {
11
+ proto = Object.getPrototypeOf(proto);
12
+ }
13
+
14
+ return Object.getPrototypeOf(value) === proto;
11
15
  }
16
+
12
17
  function deepMerge(...objects) {
13
- const output = {};
14
- for (const obj of objects) {
15
- for (const [key, value] of Object.entries(obj)) {
16
- const prevValue = output[key];
17
- output[key] = isPlainObject(prevValue) && isPlainObject(value) ? deepMerge(prevValue, value) : value;
18
+ const output = {};
19
+
20
+ for (const obj of objects) {
21
+ for (const [key, value] of Object.entries(obj)) {
22
+ const prevValue = output[key];
23
+ output[key] = isPlainObject(prevValue) && isPlainObject(value) ? deepMerge(prevValue, value) : value;
24
+ }
18
25
  }
19
- }
20
- return output;
26
+
27
+ return output;
21
28
  }
29
+
22
30
  export function makeTheme(...themes) {
23
- const themesToMerge = [
24
- defaultTheme,
25
- ...themes.filter((theme) => theme != null)
26
- ];
27
- return deepMerge(...themesToMerge);
31
+ const themesToMerge = [
32
+ defaultTheme,
33
+ ...themes.filter((theme) => theme != null),
34
+ ];
35
+
36
+ return deepMerge(...themesToMerge);
28
37
  }
@@ -1,29 +1,25 @@
1
- export function finite({
2
- active,
3
- pageSize,
4
- total
5
- }) {
6
- const middle = Math.floor(pageSize / 2);
7
- if (total <= pageSize || active < middle) {
8
- return active;
9
- }
10
- if (active >= total - middle) {
11
- return active + pageSize - total;
12
- }
13
- return middle;
1
+ export function finite({active, pageSize, total}) {
2
+ const middle = Math.floor(pageSize / 2);
3
+
4
+ if (total <= pageSize || active < middle) {
5
+ return active;
6
+ }
7
+
8
+ if (active >= total - middle) {
9
+ return active + pageSize - total;
10
+ }
11
+
12
+ return middle;
14
13
  }
15
- export function infinite({
16
- active,
17
- lastActive,
18
- total,
19
- pageSize,
20
- pointer
21
- }) {
22
- if (total <= pageSize) {
23
- return active;
24
- }
25
- if (lastActive < active && active - lastActive < pageSize) {
26
- return Math.min(Math.floor(pageSize / 2), pointer + active - lastActive);
27
- }
28
- return pointer;
14
+
15
+ export function infinite({active, lastActive, total, pageSize, pointer}) {
16
+ if (total <= pageSize) {
17
+ return active;
18
+ }
19
+
20
+ if (lastActive < active && pageSize > active - lastActive) {
21
+ return Math.min(Math.floor(pageSize / 2), pointer + active - lastActive);
22
+ }
23
+
24
+ return pointer;
29
25
  }
@@ -1,13 +1,16 @@
1
1
  export class PromisePolyfill extends Promise {
2
-
3
-
4
- static withResolver() {
5
- let resolve;
6
- let reject;
7
- const promise = new Promise((res, rej) => {
8
- resolve = res;
9
- reject = rej;
10
- });
11
- return { promise, resolve, reject };
12
- }
2
+ static withResolver() {
3
+ let resolve;
4
+ let reject;
5
+ const promise = new Promise((res, rej) => {
6
+ resolve = res;
7
+ reject = rej;
8
+ });
9
+
10
+ return {
11
+ promise,
12
+ resolve,
13
+ reject,
14
+ };
15
+ }
13
16
  }