@inquirer/expand 2.2.0 → 3.0.0

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
@@ -94,6 +94,8 @@ const answer = await expand({
94
94
  | expanded | `boolean` | no | Expand the choices by default |
95
95
  | theme | [See Theming](#Theming) | no | Customize look of the prompt. |
96
96
 
97
+ `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.
98
+
97
99
  ### `Choice` object
98
100
 
99
101
  The `Choice` object is typed as
@@ -118,14 +120,14 @@ You can theme a prompt by passing a `theme` object option. The theme object only
118
120
 
119
121
  ```ts
120
122
  type Theme = {
121
- prefix: string;
123
+ prefix: string | { idle: string; done: string };
122
124
  spinner: {
123
125
  interval: number;
124
126
  frames: string[];
125
127
  };
126
128
  style: {
127
129
  answer: (text: string) => string;
128
- message: (text: string) => string;
130
+ message: (text: string, status: 'idle' | 'done' | 'loading') => string;
129
131
  error: (text: string) => string;
130
132
  defaultAnswer: (text: string) => string;
131
133
  highlight: (text: string) => string;
package/dist/cjs/index.js CHANGED
@@ -3,10 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Separator = void 0;
6
7
  const core_1 = require("@inquirer/core");
7
8
  const yoctocolors_cjs_1 = __importDefault(require("yoctocolors-cjs"));
8
9
  function normalizeChoices(choices) {
9
10
  return choices.map((choice) => {
11
+ if (core_1.Separator.isSeparator(choice)) {
12
+ return choice;
13
+ }
10
14
  const name = 'name' in choice ? choice.name : String(choice.value);
11
15
  const value = 'value' in choice ? choice.value : name;
12
16
  return {
@@ -25,12 +29,12 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
25
29
  var _a;
26
30
  const { default: defaultKey = 'h' } = config;
27
31
  const choices = (0, core_1.useMemo)(() => normalizeChoices(config.choices), [config.choices]);
28
- const [status, setStatus] = (0, core_1.useState)('pending');
32
+ const [status, setStatus] = (0, core_1.useState)('idle');
29
33
  const [value, setValue] = (0, core_1.useState)('');
30
34
  const [expanded, setExpanded] = (0, core_1.useState)((_a = config.expanded) !== null && _a !== void 0 ? _a : false);
31
35
  const [errorMsg, setError] = (0, core_1.useState)();
32
36
  const theme = (0, core_1.makeTheme)(config.theme);
33
- const prefix = (0, core_1.usePrefix)({ theme });
37
+ const prefix = (0, core_1.usePrefix)({ theme, status });
34
38
  (0, core_1.useKeypress)((event, rl) => {
35
39
  if ((0, core_1.isEnterKey)(event)) {
36
40
  const answer = (value || defaultKey).toLowerCase();
@@ -38,7 +42,7 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
38
42
  setExpanded(true);
39
43
  }
40
44
  else {
41
- const selectedChoice = choices.find(({ key }) => key === answer);
45
+ const selectedChoice = choices.find((choice) => !core_1.Separator.isSeparator(choice) && choice.key === answer);
42
46
  if (selectedChoice) {
43
47
  setStatus('done');
44
48
  // Set the value as we might've selected the default one.
@@ -58,10 +62,10 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
58
62
  setError(undefined);
59
63
  }
60
64
  });
61
- const message = theme.style.message(config.message);
65
+ const message = theme.style.message(config.message, status);
62
66
  if (status === 'done') {
63
67
  // If the prompt is done, it's safe to assume there is a selected value.
64
- const selectedChoice = choices.find(({ key }) => key === value);
68
+ const selectedChoice = choices.find((choice) => !core_1.Separator.isSeparator(choice) && choice.key === value.toLowerCase());
65
69
  return `${prefix} ${message} ${theme.style.answer(selectedChoice.name)}`;
66
70
  }
67
71
  const allChoices = expanded ? choices : [...choices, helpChoice];
@@ -69,6 +73,8 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
69
73
  let longChoices = '';
70
74
  let shortChoices = allChoices
71
75
  .map((choice) => {
76
+ if (core_1.Separator.isSeparator(choice))
77
+ return '';
72
78
  if (choice.key === defaultKey) {
73
79
  return choice.key.toUpperCase();
74
80
  }
@@ -81,6 +87,9 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
81
87
  shortChoices = '';
82
88
  longChoices = allChoices
83
89
  .map((choice) => {
90
+ if (core_1.Separator.isSeparator(choice)) {
91
+ return ` ${choice.separator}`;
92
+ }
84
93
  const line = ` ${choice.key}) ${choice.name}`;
85
94
  if (choice.key === value.toLowerCase()) {
86
95
  return theme.style.highlight(line);
@@ -90,7 +99,7 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
90
99
  .join('\n');
91
100
  }
92
101
  let helpTip = '';
93
- const currentOption = allChoices.find(({ key }) => key === value.toLowerCase());
102
+ const currentOption = choices.find((choice) => !core_1.Separator.isSeparator(choice) && choice.key === value.toLowerCase());
94
103
  if (currentOption) {
95
104
  helpTip = `${yoctocolors_cjs_1.default.cyan('>>')} ${currentOption.name}`;
96
105
  }
@@ -103,3 +112,5 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
103
112
  [longChoices, helpTip, error].filter(Boolean).join('\n'),
104
113
  ];
105
114
  });
115
+ var core_2 = require("@inquirer/core");
116
+ Object.defineProperty(exports, "Separator", { enumerable: true, get: function () { return core_2.Separator; } });
@@ -1,4 +1,4 @@
1
- import { type Theme } from '@inquirer/core';
1
+ import { Separator, type Theme } from '@inquirer/core';
2
2
  import type { PartialDeep } from '@inquirer/type';
3
3
  type Key = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
4
4
  type Choice<Value> = {
@@ -14,9 +14,12 @@ declare const _default: <Value>(config: {
14
14
  choices: readonly {
15
15
  key: Key;
16
16
  name: string;
17
- }[] | readonly Choice<Value>[];
17
+ }[] | readonly (Separator | Choice<Value>)[];
18
18
  default?: (Key | "h") | undefined;
19
19
  expanded?: boolean | undefined;
20
20
  theme?: PartialDeep<Theme> | undefined;
21
- }, context?: import("@inquirer/type").Context) => import("@inquirer/type").CancelablePromise<Value>;
21
+ }, context?: import("@inquirer/type").Context) => Promise<Value> & {
22
+ cancel: () => void;
23
+ };
22
24
  export default _default;
25
+ export { Separator } from '@inquirer/core';
@@ -1,7 +1,10 @@
1
- import { createPrompt, useMemo, useState, useKeypress, usePrefix, isEnterKey, makeTheme, } from '@inquirer/core';
1
+ import { createPrompt, useMemo, useState, useKeypress, usePrefix, isEnterKey, makeTheme, Separator, } from '@inquirer/core';
2
2
  import colors from 'yoctocolors-cjs';
3
3
  function normalizeChoices(choices) {
4
4
  return choices.map((choice) => {
5
+ if (Separator.isSeparator(choice)) {
6
+ return choice;
7
+ }
5
8
  const name = 'name' in choice ? choice.name : String(choice.value);
6
9
  const value = 'value' in choice ? choice.value : name;
7
10
  return {
@@ -19,12 +22,12 @@ const helpChoice = {
19
22
  export default createPrompt((config, done) => {
20
23
  const { default: defaultKey = 'h' } = config;
21
24
  const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
22
- const [status, setStatus] = useState('pending');
25
+ const [status, setStatus] = useState('idle');
23
26
  const [value, setValue] = useState('');
24
27
  const [expanded, setExpanded] = useState(config.expanded ?? false);
25
28
  const [errorMsg, setError] = useState();
26
29
  const theme = makeTheme(config.theme);
27
- const prefix = usePrefix({ theme });
30
+ const prefix = usePrefix({ theme, status });
28
31
  useKeypress((event, rl) => {
29
32
  if (isEnterKey(event)) {
30
33
  const answer = (value || defaultKey).toLowerCase();
@@ -32,7 +35,7 @@ export default createPrompt((config, done) => {
32
35
  setExpanded(true);
33
36
  }
34
37
  else {
35
- const selectedChoice = choices.find(({ key }) => key === answer);
38
+ const selectedChoice = choices.find((choice) => !Separator.isSeparator(choice) && choice.key === answer);
36
39
  if (selectedChoice) {
37
40
  setStatus('done');
38
41
  // Set the value as we might've selected the default one.
@@ -52,10 +55,10 @@ export default createPrompt((config, done) => {
52
55
  setError(undefined);
53
56
  }
54
57
  });
55
- const message = theme.style.message(config.message);
58
+ const message = theme.style.message(config.message, status);
56
59
  if (status === 'done') {
57
60
  // If the prompt is done, it's safe to assume there is a selected value.
58
- const selectedChoice = choices.find(({ key }) => key === value);
61
+ const selectedChoice = choices.find((choice) => !Separator.isSeparator(choice) && choice.key === value.toLowerCase());
59
62
  return `${prefix} ${message} ${theme.style.answer(selectedChoice.name)}`;
60
63
  }
61
64
  const allChoices = expanded ? choices : [...choices, helpChoice];
@@ -63,6 +66,8 @@ export default createPrompt((config, done) => {
63
66
  let longChoices = '';
64
67
  let shortChoices = allChoices
65
68
  .map((choice) => {
69
+ if (Separator.isSeparator(choice))
70
+ return '';
66
71
  if (choice.key === defaultKey) {
67
72
  return choice.key.toUpperCase();
68
73
  }
@@ -75,6 +80,9 @@ export default createPrompt((config, done) => {
75
80
  shortChoices = '';
76
81
  longChoices = allChoices
77
82
  .map((choice) => {
83
+ if (Separator.isSeparator(choice)) {
84
+ return ` ${choice.separator}`;
85
+ }
78
86
  const line = ` ${choice.key}) ${choice.name}`;
79
87
  if (choice.key === value.toLowerCase()) {
80
88
  return theme.style.highlight(line);
@@ -84,7 +92,7 @@ export default createPrompt((config, done) => {
84
92
  .join('\n');
85
93
  }
86
94
  let helpTip = '';
87
- const currentOption = allChoices.find(({ key }) => key === value.toLowerCase());
95
+ const currentOption = choices.find((choice) => !Separator.isSeparator(choice) && choice.key === value.toLowerCase());
88
96
  if (currentOption) {
89
97
  helpTip = `${colors.cyan('>>')} ${currentOption.name}`;
90
98
  }
@@ -97,3 +105,4 @@ export default createPrompt((config, done) => {
97
105
  [longChoices, helpTip, error].filter(Boolean).join('\n'),
98
106
  ];
99
107
  });
108
+ export { Separator } from '@inquirer/core';
@@ -1,4 +1,4 @@
1
- import { type Theme } from '@inquirer/core';
1
+ import { Separator, type Theme } from '@inquirer/core';
2
2
  import type { PartialDeep } from '@inquirer/type';
3
3
  type Key = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
4
4
  type Choice<Value> = {
@@ -14,9 +14,12 @@ declare const _default: <Value>(config: {
14
14
  choices: readonly {
15
15
  key: Key;
16
16
  name: string;
17
- }[] | readonly Choice<Value>[];
17
+ }[] | readonly (Separator | Choice<Value>)[];
18
18
  default?: (Key | "h") | undefined;
19
19
  expanded?: boolean | undefined;
20
20
  theme?: PartialDeep<Theme> | undefined;
21
- }, context?: import("@inquirer/type").Context) => import("@inquirer/type").CancelablePromise<Value>;
21
+ }, context?: import("@inquirer/type").Context) => Promise<Value> & {
22
+ cancel: () => void;
23
+ };
22
24
  export default _default;
25
+ export { Separator } from '@inquirer/core';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/expand",
3
- "version": "2.2.0",
3
+ "version": "3.0.0",
4
4
  "description": "Inquirer checkbox prompt",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "typings": "./dist/cjs/types/index.d.ts",
@@ -54,12 +54,12 @@
54
54
  "license": "MIT",
55
55
  "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/main/packages/expand/README.md",
56
56
  "dependencies": {
57
- "@inquirer/core": "^9.1.0",
58
- "@inquirer/type": "^1.5.3",
57
+ "@inquirer/core": "^9.2.0",
58
+ "@inquirer/type": "^1.5.4",
59
59
  "yoctocolors-cjs": "^2.1.2"
60
60
  },
61
61
  "devDependencies": {
62
- "@inquirer/testing": "^2.1.32"
62
+ "@inquirer/testing": "^2.1.33"
63
63
  },
64
64
  "scripts": {
65
65
  "tsc": "yarn run tsc:esm && yarn run tsc:cjs",
@@ -86,5 +86,5 @@
86
86
  }
87
87
  },
88
88
  "sideEffects": false,
89
- "gitHead": "0c039599ef88fe9eb804fe083ee386ec906a856f"
89
+ "gitHead": "5fe03a4686d349d0d6cf281f86f12a4f7c992ad4"
90
90
  }