@inquirer/rawlist 4.0.12 → 4.1.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.
package/README.md CHANGED
@@ -77,11 +77,12 @@ const answer = await rawlist({
77
77
 
78
78
  ## Options
79
79
 
80
- | Property | Type | Required | Description |
81
- | -------- | ----------------------- | -------- | ------------------------------ |
82
- | message | `string` | yes | The question to ask |
83
- | choices | `Choice[]` | yes | List of the available choices. |
84
- | theme | [See Theming](#Theming) | no | Customize look of the prompt. |
80
+ | Property | Type | Required | Description |
81
+ | -------- | ----------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------- |
82
+ | message | `string` | yes | The question to ask |
83
+ | choices | `Choice[]` | yes | List of the available choices. |
84
+ | loop | `boolean` | no | Defaults to `true`. When set to `false`, the cursor will be constrained to the top and bottom of the choice list without looping. |
85
+ | theme | [See Theming](#Theming) | no | Customize look of the prompt. |
85
86
 
86
87
  `Separator` objects can be used in the `choices` array to render non-selectable lines in the choice list. By default it'll render a line, but you can provide the text as argument (`new Separator('-- Dependencies --')`). This option is often used to add labels to groups within long list of options.
87
88
 
@@ -9,6 +9,7 @@ type Choice<Value> = {
9
9
  declare const _default: <Value>(config: {
10
10
  message: string;
11
11
  choices: readonly (string | Separator)[] | readonly (Separator | Choice<Value>)[];
12
+ loop?: boolean | undefined;
12
13
  theme?: PartialDeep<Theme> | undefined;
13
14
  }, context?: import("@inquirer/type").Context) => Promise<Value> & {
14
15
  cancel: () => void;
@@ -33,23 +33,39 @@ function normalizeChoices(choices) {
33
33
  };
34
34
  });
35
35
  }
36
+ function getSelectedChoice(input, choices) {
37
+ let selectedChoice;
38
+ const selectableChoices = choices.filter(isSelectableChoice);
39
+ if (numberRegex.test(input)) {
40
+ const answer = Number.parseInt(input, 10) - 1;
41
+ selectedChoice = selectableChoices[answer];
42
+ }
43
+ else {
44
+ selectedChoice = selectableChoices.find((choice) => choice.key === input);
45
+ }
46
+ return selectedChoice
47
+ ? [selectedChoice, choices.indexOf(selectedChoice)]
48
+ : [undefined, undefined];
49
+ }
36
50
  exports.default = (0, core_1.createPrompt)((config, done) => {
51
+ const { loop = true } = config;
37
52
  const choices = (0, core_1.useMemo)(() => normalizeChoices(config.choices), [config.choices]);
38
53
  const [status, setStatus] = (0, core_1.useState)('idle');
39
54
  const [value, setValue] = (0, core_1.useState)('');
40
55
  const [errorMsg, setError] = (0, core_1.useState)();
41
56
  const theme = (0, core_1.makeTheme)(config.theme);
42
57
  const prefix = (0, core_1.usePrefix)({ status, theme });
58
+ const bounds = (0, core_1.useMemo)(() => {
59
+ const first = choices.findIndex(isSelectableChoice);
60
+ const last = choices.findLastIndex(isSelectableChoice);
61
+ if (first === -1) {
62
+ throw new core_1.ValidationError('[select prompt] No selectable choices. All choices are disabled.');
63
+ }
64
+ return { first, last };
65
+ }, [choices]);
43
66
  (0, core_1.useKeypress)((key, rl) => {
44
67
  if ((0, core_1.isEnterKey)(key)) {
45
- let selectedChoice;
46
- if (numberRegex.test(value)) {
47
- const answer = Number.parseInt(value, 10) - 1;
48
- selectedChoice = choices.filter(isSelectableChoice)[answer];
49
- }
50
- else {
51
- selectedChoice = choices.find((choice) => isSelectableChoice(choice) && choice.key === value);
52
- }
68
+ const [selectedChoice] = getSelectedChoice(value, choices);
53
69
  if (isSelectableChoice(selectedChoice)) {
54
70
  setValue(selectedChoice.short);
55
71
  setStatus('done');
@@ -62,6 +78,26 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
62
78
  setError(`"${yoctocolors_cjs_1.default.red(value)}" isn't an available option`);
63
79
  }
64
80
  }
81
+ else if (key.name === 'up' || key.name === 'down') {
82
+ rl.clearLine(0);
83
+ const [selectedChoice, active] = getSelectedChoice(value, choices);
84
+ if (!selectedChoice) {
85
+ const firstChoice = key.name === 'down'
86
+ ? choices.find(isSelectableChoice)
87
+ : choices.findLast(isSelectableChoice);
88
+ setValue(firstChoice.key);
89
+ }
90
+ else if (loop ||
91
+ (key.name === 'up' && active !== bounds.first) ||
92
+ (key.name === 'down' && active !== bounds.last)) {
93
+ const offset = key.name === 'up' ? -1 : 1;
94
+ let next = active;
95
+ do {
96
+ next = (next + offset + choices.length) % choices.length;
97
+ } while (!isSelectableChoice(choices[next]));
98
+ setValue(choices[next].key);
99
+ }
100
+ }
65
101
  else {
66
102
  setValue(rl.line);
67
103
  setError(undefined);
@@ -9,6 +9,7 @@ type Choice<Value> = {
9
9
  declare const _default: <Value>(config: {
10
10
  message: string;
11
11
  choices: readonly (string | Separator)[] | readonly (Separator | Choice<Value>)[];
12
+ loop?: boolean | undefined;
12
13
  theme?: PartialDeep<Theme> | undefined;
13
14
  }, context?: import("@inquirer/type").Context) => Promise<Value> & {
14
15
  cancel: () => void;
package/dist/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createPrompt, useMemo, useState, useKeypress, usePrefix, isEnterKey, Separator, makeTheme, } from '@inquirer/core';
1
+ import { createPrompt, useMemo, useState, useKeypress, usePrefix, isEnterKey, Separator, makeTheme, ValidationError, } from '@inquirer/core';
2
2
  import colors from 'yoctocolors-cjs';
3
3
  const numberRegex = /\d+/;
4
4
  function isSelectableChoice(choice) {
@@ -27,23 +27,39 @@ function normalizeChoices(choices) {
27
27
  };
28
28
  });
29
29
  }
30
+ function getSelectedChoice(input, choices) {
31
+ let selectedChoice;
32
+ const selectableChoices = choices.filter(isSelectableChoice);
33
+ if (numberRegex.test(input)) {
34
+ const answer = Number.parseInt(input, 10) - 1;
35
+ selectedChoice = selectableChoices[answer];
36
+ }
37
+ else {
38
+ selectedChoice = selectableChoices.find((choice) => choice.key === input);
39
+ }
40
+ return selectedChoice
41
+ ? [selectedChoice, choices.indexOf(selectedChoice)]
42
+ : [undefined, undefined];
43
+ }
30
44
  export default createPrompt((config, done) => {
45
+ const { loop = true } = config;
31
46
  const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
32
47
  const [status, setStatus] = useState('idle');
33
48
  const [value, setValue] = useState('');
34
49
  const [errorMsg, setError] = useState();
35
50
  const theme = makeTheme(config.theme);
36
51
  const prefix = usePrefix({ status, theme });
52
+ const bounds = useMemo(() => {
53
+ const first = choices.findIndex(isSelectableChoice);
54
+ const last = choices.findLastIndex(isSelectableChoice);
55
+ if (first === -1) {
56
+ throw new ValidationError('[select prompt] No selectable choices. All choices are disabled.');
57
+ }
58
+ return { first, last };
59
+ }, [choices]);
37
60
  useKeypress((key, rl) => {
38
61
  if (isEnterKey(key)) {
39
- let selectedChoice;
40
- if (numberRegex.test(value)) {
41
- const answer = Number.parseInt(value, 10) - 1;
42
- selectedChoice = choices.filter(isSelectableChoice)[answer];
43
- }
44
- else {
45
- selectedChoice = choices.find((choice) => isSelectableChoice(choice) && choice.key === value);
46
- }
62
+ const [selectedChoice] = getSelectedChoice(value, choices);
47
63
  if (isSelectableChoice(selectedChoice)) {
48
64
  setValue(selectedChoice.short);
49
65
  setStatus('done');
@@ -56,6 +72,26 @@ export default createPrompt((config, done) => {
56
72
  setError(`"${colors.red(value)}" isn't an available option`);
57
73
  }
58
74
  }
75
+ else if (key.name === 'up' || key.name === 'down') {
76
+ rl.clearLine(0);
77
+ const [selectedChoice, active] = getSelectedChoice(value, choices);
78
+ if (!selectedChoice) {
79
+ const firstChoice = key.name === 'down'
80
+ ? choices.find(isSelectableChoice)
81
+ : choices.findLast(isSelectableChoice);
82
+ setValue(firstChoice.key);
83
+ }
84
+ else if (loop ||
85
+ (key.name === 'up' && active !== bounds.first) ||
86
+ (key.name === 'down' && active !== bounds.last)) {
87
+ const offset = key.name === 'up' ? -1 : 1;
88
+ let next = active;
89
+ do {
90
+ next = (next + offset + choices.length) % choices.length;
91
+ } while (!isSelectableChoice(choices[next]));
92
+ setValue(choices[next].key);
93
+ }
94
+ }
59
95
  else {
60
96
  setValue(rl.line);
61
97
  setError(undefined);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/rawlist",
3
- "version": "4.0.12",
3
+ "version": "4.1.1",
4
4
  "description": "Inquirer rawlist prompt",
5
5
  "keywords": [
6
6
  "answer",
@@ -74,14 +74,13 @@
74
74
  "tsc": "tshy"
75
75
  },
76
76
  "dependencies": {
77
- "@inquirer/core": "^10.1.10",
77
+ "@inquirer/core": "^10.1.11",
78
78
  "@inquirer/type": "^3.0.6",
79
79
  "yoctocolors-cjs": "^2.1.2"
80
80
  },
81
81
  "devDependencies": {
82
82
  "@arethetypeswrong/cli": "^0.17.4",
83
83
  "@inquirer/testing": "^2.1.46",
84
- "@repo/tsconfig": "workspace:*",
85
84
  "tshy": "^3.0.2"
86
85
  },
87
86
  "engines": {
@@ -107,5 +106,5 @@
107
106
  "optional": true
108
107
  }
109
108
  },
110
- "gitHead": "d367155a8d64d8b3e93f9c763adccf708aedc8a8"
109
+ "gitHead": "50bac82683b9a8d8e06ec87972894d56b2883bcd"
111
110
  }