@reliverse/relinka 1.1.5 → 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-npm/components/anykey/index.js +77 -57
- package/dist-npm/components/block/block.js +61 -48
- package/dist-npm/components/checkbox/index.js +211 -178
- package/dist-npm/components/confirm/confirm-main.js +122 -85
- package/dist-npm/components/confirm/confirm-three.js +27 -24
- package/dist-npm/components/confirm/index.js +45 -36
- package/dist-npm/components/core/Separator.js +17 -15
- package/dist-npm/components/core/create-prompt.js +124 -101
- package/dist-npm/components/core/errors.js +15 -19
- package/dist-npm/components/core/hook-engine.js +120 -91
- package/dist-npm/components/core/index.js +12 -12
- package/dist-npm/components/core/key.js +6 -16
- package/dist-npm/components/core/lines.js +54 -45
- package/dist-npm/components/core/make-theme.js +30 -21
- package/dist-npm/components/core/position.js +23 -27
- package/dist-npm/components/core/promise-polyfill.js +14 -11
- package/dist-npm/components/core/screen-manager.js +74 -59
- package/dist-npm/components/core/theme.js +32 -22
- package/dist-npm/components/core/use-effect.js +15 -9
- package/dist-npm/components/core/use-keypress.js +23 -19
- package/dist-npm/components/core/use-memo.js +16 -10
- package/dist-npm/components/core/use-pagination.js +32 -33
- package/dist-npm/components/core/use-prefix.js +38 -40
- package/dist-npm/components/core/use-ref.js +5 -2
- package/dist-npm/components/core/use-state.js +23 -15
- package/dist-npm/components/core/useKeyPress.js +17 -14
- package/dist-npm/components/core/usePromptState.js +14 -8
- package/dist-npm/components/core/utils.js +16 -7
- package/dist-npm/components/date/date.js +204 -167
- package/dist-npm/components/editor/index.js +92 -71
- package/dist-npm/components/expand/index.js +124 -96
- package/dist-npm/components/figures/index.js +294 -283
- package/dist-npm/components/input/index.js +87 -61
- package/dist-npm/components/input/text-main.js +124 -97
- package/dist-npm/components/input/text.js +28 -24
- package/dist-npm/components/instance/basic.js +25 -17
- package/dist-npm/components/instance/browser.js +18 -14
- package/dist-npm/components/instance/reporter/basic.js +65 -46
- package/dist-npm/components/instance/reporter/browser.js +47 -44
- package/dist-npm/components/instance/reporter/fancy.js +96 -83
- package/dist-npm/components/instance/shared.js +2 -2
- package/dist-npm/components/mono/mono.js +62 -52
- package/dist-npm/components/mono/monoTwo.js +49 -35
- package/dist-npm/components/multiselect/group-multiselect.js +71 -55
- package/dist-npm/components/multiselect/multi-select-two.js +130 -97
- package/dist-npm/components/multiselect/multi-select.js +49 -43
- package/dist-npm/components/multiselect/multiselect-main.js +146 -120
- package/dist-npm/components/multiselect/num-multi-select.js +130 -97
- package/dist-npm/components/multiselect/num-multiselect-main.js +35 -24
- package/dist-npm/components/next-steps/next-steps.js +25 -23
- package/dist-npm/components/number/index.js +112 -78
- package/dist-npm/components/number/number-main.js +2 -95
- package/dist-npm/components/password/index.js +73 -54
- package/dist-npm/components/password/password-main.js +2 -119
- package/dist-npm/components/password/password-three.js +30 -26
- package/dist-npm/components/progressbar/ProgressBar.js +64 -45
- package/dist-npm/components/progressbar/helper.js +40 -33
- package/dist-npm/components/progressbar/index.js +1 -1
- package/dist-npm/components/prompts/create.js +44 -29
- package/dist-npm/components/prompts/index.js +45 -45
- package/dist-npm/components/prompts/prompt.js +260 -211
- package/dist-npm/components/prompts/promptTwo.js +605 -561
- package/dist-npm/components/prompts/relinka.js +295 -237
- package/dist-npm/components/range/range.js +294 -247
- package/dist-npm/components/rawlist/index.js +107 -87
- package/dist-npm/components/results/results.js +37 -31
- package/dist-npm/components/search/index.js +193 -148
- package/dist-npm/components/select/index.js +186 -148
- package/dist-npm/components/select/num-select-main.js +27 -27
- package/dist-npm/components/select/num-select.js +5 -124
- package/dist-npm/components/select/select-key.js +25 -24
- package/dist-npm/components/select/select-main.js +133 -109
- package/dist-npm/components/select/select-three.js +36 -32
- package/dist-npm/components/select/select-two.js +87 -94
- package/dist-npm/components/spinner/index.js +136 -107
- package/dist-npm/components/st-end/end.js +26 -34
- package/dist-npm/components/st-end/start.js +15 -29
- package/dist-npm/components/toggle/index.js +137 -113
- package/dist-npm/components/visual/animate/animate.js +10 -53
- package/dist-npm/components/visual/ascii-art/ascii-art.js +1 -12
- package/dist-npm/mod.js +2 -1
- package/dist-npm/testing/index.js +83 -58
- package/dist-npm/types/general.js +1 -0
- package/dist-npm/types/index.js +3 -2
- package/dist-npm/types/keypress.js +36 -35
- package/dist-npm/types/readline.js +1 -0
- package/dist-npm/types/relinka.js +1 -0
- package/dist-npm/types/utils.js +1 -0
- package/dist-npm/utils/box.js +137 -135
- package/dist-npm/utils/color.js +74 -65
- package/dist-npm/utils/colorize.js +156 -124
- package/dist-npm/utils/component.js +657 -532
- package/dist-npm/utils/constants.js +63 -64
- package/dist-npm/utils/core.js +3 -2
- package/dist-npm/utils/decoder.js +223 -244
- package/dist-npm/utils/error.js +9 -4
- package/dist-npm/utils/errors.js +4 -14
- package/dist-npm/utils/format.js +24 -19
- package/dist-npm/utils/keypress.js +414 -316
- package/dist-npm/utils/log.js +15 -11
- package/dist-npm/utils/mapping.js +52 -45
- package/dist-npm/utils/messages.js +183 -183
- package/dist-npm/utils/platforms.js +20 -16
- package/dist-npm/utils/prompt-tmp.js +286 -235
- package/dist-npm/utils/prompt-two.js +286 -235
- package/dist-npm/utils/readline.js +7 -5
- package/dist-npm/utils/skeleton.js +170 -130
- package/dist-npm/utils/stream.js +2 -2
- package/dist-npm/utils/string.js +58 -44
- package/dist-npm/utils/terminal.js +34 -23
- package/dist-npm/utils/tree.js +41 -30
- package/dist-npm/utils/types.js +1 -0
- package/dist-npm/utils/utils.js +8 -8
- package/dist-npm/utils/variants.js +44 -36
- package/package.json +20 -28
|
@@ -1,104 +1,124 @@
|
|
|
1
|
-
import colors from
|
|
1
|
+
import colors from 'picocolors';
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from
|
|
3
|
+
createPrompt,
|
|
4
|
+
useMemo,
|
|
5
|
+
useState,
|
|
6
|
+
useKeypress,
|
|
7
|
+
usePrefix,
|
|
8
|
+
isEnterKey,
|
|
9
|
+
Separator,
|
|
10
|
+
makeTheme,
|
|
11
|
+
} from '../../components/core/index.js';
|
|
12
|
+
|
|
13
|
+
const isString = (a) => typeof a === 'string';
|
|
14
|
+
|
|
12
15
|
const numberRegex = /\d+/;
|
|
16
|
+
|
|
13
17
|
function isSelectableChoice(choice) {
|
|
14
|
-
|
|
18
|
+
return choice != null && !Separator.isSeparator(choice);
|
|
15
19
|
}
|
|
20
|
+
|
|
16
21
|
function normalizeChoices(choices) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
22
|
+
let index = 0;
|
|
23
|
+
|
|
24
|
+
return choices.map((choice) => {
|
|
25
|
+
if (Separator.isSeparator(choice)) {
|
|
26
|
+
return choice;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
++index;
|
|
30
|
+
if (isString(choice)) {
|
|
31
|
+
return {
|
|
32
|
+
value: choice,
|
|
33
|
+
name: choice,
|
|
34
|
+
short: choice,
|
|
35
|
+
key: String(index),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const name = choice.name ?? String(choice.value);
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
value: choice.value,
|
|
43
|
+
name,
|
|
44
|
+
|
|
45
|
+
short: choice.short ?? name,
|
|
46
|
+
|
|
47
|
+
key: choice.key ?? String(index),
|
|
48
|
+
};
|
|
49
|
+
});
|
|
42
50
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const choices = useMemo(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
);
|
|
49
|
-
const [status, setStatus] = useState("idle");
|
|
50
|
-
const [value, setValue] = useState("");
|
|
51
|
+
|
|
52
|
+
const rawlist = createPrompt((config, done) => {
|
|
53
|
+
const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
|
|
54
|
+
|
|
55
|
+
const [status, setStatus] = useState('idle');
|
|
56
|
+
const [value, setValue] = useState('');
|
|
51
57
|
const [errorMsg, setError] = useState();
|
|
52
58
|
const theme = makeTheme(config.theme);
|
|
53
|
-
const prefix = usePrefix({
|
|
59
|
+
const prefix = usePrefix({
|
|
60
|
+
status,
|
|
61
|
+
theme,
|
|
62
|
+
});
|
|
63
|
+
|
|
54
64
|
useKeypress((key, rl) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
65
|
+
if (isEnterKey(key)) {
|
|
66
|
+
let selectedChoice;
|
|
67
|
+
|
|
68
|
+
if (numberRegex.test(value)) {
|
|
69
|
+
const answer = Number.parseInt(value, 10) - 1;
|
|
70
|
+
selectedChoice = choices.filter(isSelectableChoice)[answer];
|
|
71
|
+
} else {
|
|
72
|
+
selectedChoice = choices.find((choice) => isSelectableChoice(choice) && choice.key === value);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (isSelectableChoice(selectedChoice)) {
|
|
76
|
+
setValue(selectedChoice.short);
|
|
77
|
+
setStatus('done');
|
|
78
|
+
done(selectedChoice.value);
|
|
79
|
+
} else if (value === '') {
|
|
80
|
+
setError('Please input a value');
|
|
81
|
+
} else {
|
|
82
|
+
setError(`"${colors.red(value)}" isn't an available option`);
|
|
83
|
+
}
|
|
71
84
|
} else {
|
|
72
|
-
|
|
85
|
+
setValue(rl.line);
|
|
86
|
+
setError(void 0);
|
|
73
87
|
}
|
|
74
|
-
} else {
|
|
75
|
-
setValue(rl.line);
|
|
76
|
-
setError(void 0);
|
|
77
|
-
}
|
|
78
88
|
});
|
|
79
89
|
const message = theme.style.message(config.message, status);
|
|
80
|
-
|
|
81
|
-
|
|
90
|
+
|
|
91
|
+
if (status === 'done') {
|
|
92
|
+
return `${prefix} ${message} ${theme.style.answer(value)}`;
|
|
82
93
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
+
|
|
95
|
+
const choicesStr = choices
|
|
96
|
+
.map((choice) => {
|
|
97
|
+
if (Separator.isSeparator(choice)) {
|
|
98
|
+
return ` ${choice.separator}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const line = ` ${choice.key}) ${choice.name}`;
|
|
102
|
+
|
|
103
|
+
if (choice.key === value.toLowerCase()) {
|
|
104
|
+
return theme.style.highlight(line);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return line;
|
|
108
|
+
})
|
|
109
|
+
.join('\n');
|
|
110
|
+
|
|
111
|
+
let error = '';
|
|
112
|
+
|
|
94
113
|
if (errorMsg) {
|
|
95
|
-
|
|
114
|
+
error = theme.style.error(errorMsg);
|
|
96
115
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
116
|
+
|
|
117
|
+
return [`${prefix} ${message} ${value}`, [choicesStr, error]
|
|
118
|
+
.filter(Boolean)
|
|
119
|
+
.join('\n')];
|
|
120
|
+
});
|
|
121
|
+
|
|
103
122
|
export default rawlist;
|
|
104
|
-
|
|
123
|
+
|
|
124
|
+
export {Separator} from '../../components/core/index.js';
|
|
@@ -1,34 +1,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import {Type} from '@sinclair/typebox';
|
|
2
|
+
import {Value} from '@sinclair/typebox/value';
|
|
3
|
+
import {msg} from '../../utils/messages.js';
|
|
4
|
+
|
|
4
5
|
const ResultsSchema = Type.Object({
|
|
5
|
-
|
|
6
|
+
username: Type.String(),
|
|
6
7
|
});
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
8
|
+
|
|
9
|
+
export async function promptsDisplayResults({results, inline = true}) {
|
|
10
|
+
if (!Value.Check(ResultsSchema, results)) {
|
|
11
|
+
throw Error('Invalid results structure.');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const title = 'Your input results:';
|
|
15
|
+
|
|
16
|
+
if (inline) {
|
|
17
|
+
const formattedResults = Object
|
|
18
|
+
.entries(results)
|
|
19
|
+
.map(([key, value]) => `${key}: ${value}`)
|
|
20
|
+
.join(', ');
|
|
21
|
+
|
|
22
|
+
msg({
|
|
23
|
+
type: 'M_INFO',
|
|
24
|
+
title,
|
|
25
|
+
content: formattedResults,
|
|
26
|
+
titleColor: 'cyan',
|
|
27
|
+
contentColor: 'dim',
|
|
28
|
+
});
|
|
29
|
+
} else {
|
|
30
|
+
const formattedResults = JSON.stringify(results, null, 2);
|
|
31
|
+
|
|
32
|
+
msg({
|
|
33
|
+
type: 'M_INFO',
|
|
34
|
+
title,
|
|
35
|
+
content: formattedResults,
|
|
36
|
+
titleColor: 'cyan',
|
|
37
|
+
contentColor: 'dim',
|
|
38
|
+
});
|
|
39
|
+
}
|
|
34
40
|
}
|
|
@@ -1,186 +1,231 @@
|
|
|
1
|
-
import colors from
|
|
1
|
+
import colors from 'picocolors';
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
} from
|
|
15
|
-
import figures from
|
|
3
|
+
createPrompt,
|
|
4
|
+
useState,
|
|
5
|
+
useKeypress,
|
|
6
|
+
usePrefix,
|
|
7
|
+
usePagination,
|
|
8
|
+
useRef,
|
|
9
|
+
useEffect,
|
|
10
|
+
useMemo,
|
|
11
|
+
isEnterKey,
|
|
12
|
+
Separator,
|
|
13
|
+
makeTheme,
|
|
14
|
+
} from '../../components/core/index.js';
|
|
15
|
+
import figures from '../../components/figures/index.js';
|
|
16
|
+
|
|
17
|
+
const isString = (a) => typeof a === 'string';
|
|
18
|
+
|
|
16
19
|
const searchTheme = {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
icon: {
|
|
21
|
+
cursor: figures.pointer,
|
|
22
|
+
},
|
|
23
|
+
style: {
|
|
24
|
+
disabled: (text) => colors.dim(`- ${text}`),
|
|
25
|
+
searchTerm: (text) => colors.cyan(text),
|
|
26
|
+
description: (text) => colors.cyan(text),
|
|
27
|
+
},
|
|
28
|
+
helpMode: 'auto',
|
|
24
29
|
};
|
|
30
|
+
|
|
25
31
|
function isSelectable(item) {
|
|
26
|
-
|
|
32
|
+
return !Separator.isSeparator(item) && !item.disabled;
|
|
27
33
|
}
|
|
34
|
+
|
|
28
35
|
function normalizeChoices(choices) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
36
|
+
return choices.map((choice) => {
|
|
37
|
+
if (Separator.isSeparator(choice)) {
|
|
38
|
+
return choice;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (isString(choice)) {
|
|
42
|
+
return {
|
|
43
|
+
value: choice,
|
|
44
|
+
name: choice,
|
|
45
|
+
short: choice,
|
|
46
|
+
disabled: false,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const name = choice.name ?? String(choice.value);
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
value: choice.value,
|
|
54
|
+
name,
|
|
55
|
+
|
|
56
|
+
description: choice.description,
|
|
57
|
+
|
|
58
|
+
short: choice.short ?? name,
|
|
59
|
+
|
|
60
|
+
disabled: choice.disabled ?? false,
|
|
61
|
+
};
|
|
62
|
+
});
|
|
54
63
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const {
|
|
64
|
+
|
|
65
|
+
const search = createPrompt((config, done) => {
|
|
66
|
+
const {pageSize = 7, validate = () => true} = config;
|
|
58
67
|
const theme = makeTheme(searchTheme, config.theme);
|
|
59
68
|
const firstRender = useRef(true);
|
|
60
|
-
const [status, setStatus] = useState(
|
|
61
|
-
const [searchTerm, setSearchTerm] = useState(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
69
|
+
const [status, setStatus] = useState('loading');
|
|
70
|
+
const [searchTerm, setSearchTerm] = useState('');
|
|
71
|
+
|
|
72
|
+
const [searchResults, setSearchResults] = useState([]);
|
|
73
|
+
|
|
65
74
|
const [searchError, setSearchError] = useState();
|
|
66
|
-
const prefix = usePrefix({
|
|
75
|
+
const prefix = usePrefix({
|
|
76
|
+
status,
|
|
77
|
+
theme,
|
|
78
|
+
});
|
|
79
|
+
|
|
67
80
|
const bounds = useMemo(() => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
81
|
+
const first = searchResults.findIndex(isSelectable);
|
|
82
|
+
const last = searchResults.findLastIndex(isSelectable);
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
first,
|
|
86
|
+
last,
|
|
87
|
+
};
|
|
71
88
|
}, [searchResults]);
|
|
89
|
+
|
|
72
90
|
const [active = bounds.first, setActive] = useState();
|
|
91
|
+
|
|
73
92
|
useEffect(() => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
93
|
+
const controller = new AbortController();
|
|
94
|
+
setStatus('loading');
|
|
95
|
+
setSearchError(void 0);
|
|
96
|
+
const fetchResults = async () => {
|
|
97
|
+
try {
|
|
98
|
+
const results = await config.source(searchTerm || void 0, {
|
|
99
|
+
signal: controller.signal,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
if (!controller.signal.aborted) {
|
|
103
|
+
setActive(void 0);
|
|
104
|
+
setSearchError(void 0);
|
|
105
|
+
setSearchResults(normalizeChoices(results));
|
|
106
|
+
setStatus('idle');
|
|
107
|
+
}
|
|
108
|
+
} catch(error2) {
|
|
109
|
+
if (!controller.signal.aborted && error2 instanceof Error) {
|
|
110
|
+
setSearchError(error2.message);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
void fetchResults();
|
|
116
|
+
return () => {
|
|
117
|
+
controller.abort();
|
|
118
|
+
};
|
|
98
119
|
}, [searchTerm]);
|
|
99
120
|
const selectedChoice = searchResults[active];
|
|
121
|
+
|
|
100
122
|
useKeypress(async (key, rl) => {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
123
|
+
if (isEnterKey(key)) {
|
|
124
|
+
if (selectedChoice) {
|
|
125
|
+
setStatus('loading');
|
|
126
|
+
const isValid = await validate(selectedChoice.value);
|
|
127
|
+
setStatus('idle');
|
|
128
|
+
if (isValid) {
|
|
129
|
+
setStatus('done');
|
|
130
|
+
done(selectedChoice.value);
|
|
131
|
+
} else if (selectedChoice.name === searchTerm) {
|
|
132
|
+
setSearchError(isValid || 'You must provide a valid value');
|
|
133
|
+
} else {
|
|
134
|
+
rl.write(selectedChoice.name);
|
|
135
|
+
setSearchTerm(selectedChoice.name);
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
rl.write(searchTerm);
|
|
139
|
+
}
|
|
140
|
+
} else if (key.name === 'tab' && selectedChoice) {
|
|
141
|
+
rl.clearLine(0);
|
|
112
142
|
rl.write(selectedChoice.name);
|
|
113
143
|
setSearchTerm(selectedChoice.name);
|
|
114
|
-
|
|
144
|
+
} else if (status !== 'loading' && (key.name === 'up' || key.name === 'down')) {
|
|
145
|
+
rl.clearLine(0);
|
|
146
|
+
if (key.name === 'up' && active !== bounds.first || key.name === 'down' && active !== bounds.last) {
|
|
147
|
+
const offset = key.name === 'up' ? -1 : 1;
|
|
148
|
+
let next = active;
|
|
149
|
+
|
|
150
|
+
do {
|
|
151
|
+
next = (next + offset + searchResults.length) % searchResults.length;
|
|
152
|
+
} while (!isSelectable(searchResults[next]))
|
|
153
|
+
setActive(next);
|
|
154
|
+
}
|
|
115
155
|
} else {
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
} else if (key.name === "tab" && selectedChoice) {
|
|
119
|
-
rl.clearLine(0);
|
|
120
|
-
rl.write(selectedChoice.name);
|
|
121
|
-
setSearchTerm(selectedChoice.name);
|
|
122
|
-
} else if (status !== "loading" && (key.name === "up" || key.name === "down")) {
|
|
123
|
-
rl.clearLine(0);
|
|
124
|
-
if (key.name === "up" && active !== bounds.first || key.name === "down" && active !== bounds.last) {
|
|
125
|
-
const offset = key.name === "up" ? -1 : 1;
|
|
126
|
-
let next = active;
|
|
127
|
-
do {
|
|
128
|
-
next = (next + offset + searchResults.length) % searchResults.length;
|
|
129
|
-
} while (!isSelectable(searchResults[next]));
|
|
130
|
-
setActive(next);
|
|
156
|
+
setSearchTerm(rl.line);
|
|
131
157
|
}
|
|
132
|
-
|
|
133
|
-
setSearchTerm(rl.line);
|
|
134
|
-
}
|
|
158
|
+
|
|
135
159
|
});
|
|
136
160
|
const message = theme.style.message(config.message, status);
|
|
161
|
+
|
|
137
162
|
if (active > 0) {
|
|
138
|
-
|
|
163
|
+
firstRender.current = false;
|
|
139
164
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
165
|
+
|
|
166
|
+
let helpTip = '';
|
|
167
|
+
|
|
168
|
+
if (searchResults.length > 1 && (theme.helpMode === 'always' || theme.helpMode === 'auto' && firstRender.current)) {
|
|
169
|
+
helpTip = searchResults.length > pageSize ? `
|
|
170
|
+
${theme.style.help('(Use arrow keys to reveal more choices)')}` : `
|
|
171
|
+
${theme.style.help('(Use arrow keys)')}`;
|
|
145
172
|
}
|
|
173
|
+
|
|
146
174
|
const page = usePagination({
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
175
|
+
items: searchResults,
|
|
176
|
+
active,
|
|
177
|
+
renderItem({item, isActive}) {
|
|
178
|
+
if (Separator.isSeparator(item)) {
|
|
179
|
+
return ` ${item.separator}`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (item.disabled) {
|
|
183
|
+
const disabledLabel = isString(item.disabled) ? item.disabled : '(disabled)';
|
|
184
|
+
return theme.style.disabled(`${item.name} ${disabledLabel}`);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const color = isActive ? theme.style.highlight : (x) => x;
|
|
188
|
+
const cursor = isActive ? theme.icon.cursor : ` `;
|
|
189
|
+
|
|
190
|
+
return color(`${cursor} ${item.name}`);
|
|
191
|
+
},
|
|
192
|
+
pageSize,
|
|
193
|
+
loop: false,
|
|
163
194
|
});
|
|
195
|
+
|
|
164
196
|
let error;
|
|
197
|
+
|
|
165
198
|
if (searchError) {
|
|
166
|
-
|
|
167
|
-
} else if (searchResults.length
|
|
168
|
-
|
|
199
|
+
error = theme.style.error(searchError);
|
|
200
|
+
} else if (!searchResults.length && searchTerm !== '' && status === 'idle') {
|
|
201
|
+
error = theme.style.error('No results found');
|
|
169
202
|
}
|
|
203
|
+
|
|
204
|
+
|
|
170
205
|
let searchStr;
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
searchStr = theme.style.searchTerm(searchTerm);
|
|
206
|
+
|
|
207
|
+
if (status === 'done' && selectedChoice) {
|
|
208
|
+
const answer = selectedChoice.short;
|
|
209
|
+
return `${prefix} ${message} ${theme.style.answer(answer)}`;
|
|
176
210
|
}
|
|
211
|
+
|
|
212
|
+
searchStr = theme.style.searchTerm(searchTerm);
|
|
213
|
+
|
|
177
214
|
const choiceDescription = selectedChoice?.description ? `
|
|
178
215
|
${theme.style.description(selectedChoice.description)}` : ``;
|
|
216
|
+
|
|
179
217
|
return [
|
|
180
|
-
|
|
181
|
-
|
|
218
|
+
[
|
|
219
|
+
prefix,
|
|
220
|
+
message,
|
|
221
|
+
searchStr,
|
|
222
|
+
]
|
|
223
|
+
.filter(Boolean)
|
|
224
|
+
.join(' '),
|
|
225
|
+
`${error ?? page}${helpTip}${choiceDescription}`,
|
|
182
226
|
];
|
|
183
|
-
|
|
184
|
-
|
|
227
|
+
});
|
|
228
|
+
|
|
185
229
|
export default search;
|
|
186
|
-
|
|
230
|
+
|
|
231
|
+
export {Separator} from '../../components/core/index.js';
|