@inquirer/select 1.2.10 → 1.2.12

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/cjs/index.js CHANGED
@@ -1,78 +1,75 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.Separator = void 0;
4
7
  const core_1 = require("@inquirer/core");
5
8
  Object.defineProperty(exports, "Separator", { enumerable: true, get: function () { return core_1.Separator; } });
6
- const chalk_1 = require("chalk");
7
- const figures_1 = require("figures");
8
- const ansi_escapes_1 = require("ansi-escapes");
9
- function isSelectableChoice(choice) {
10
- return choice != null && !core_1.Separator.isSeparator(choice) && !choice.disabled;
9
+ const chalk_1 = __importDefault(require("chalk"));
10
+ const figures_1 = __importDefault(require("figures"));
11
+ const ansi_escapes_1 = __importDefault(require("ansi-escapes"));
12
+ function isSelectable(item) {
13
+ return !core_1.Separator.isSeparator(item) && !item.disabled;
14
+ }
15
+ function renderItem({ item, isActive }) {
16
+ if (core_1.Separator.isSeparator(item)) {
17
+ return ` ${item.separator}`;
18
+ }
19
+ const line = item.name || item.value;
20
+ if (item.disabled) {
21
+ const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)';
22
+ return chalk_1.default.dim(`- ${line} ${disabledLabel}`);
23
+ }
24
+ const color = isActive ? chalk_1.default.cyan : (x) => x;
25
+ const prefix = isActive ? figures_1.default.pointer : ` `;
26
+ return color(`${prefix} ${line}`);
11
27
  }
12
28
  exports.default = (0, core_1.createPrompt)((config, done) => {
13
- const { choices } = config;
29
+ const { choices: items, pageSize } = config;
14
30
  const firstRender = (0, core_1.useRef)(true);
15
31
  const prefix = (0, core_1.usePrefix)();
16
32
  const [status, setStatus] = (0, core_1.useState)('pending');
17
- const [cursorPosition, setCursorPos] = (0, core_1.useState)(() => {
18
- const startIndex = choices.findIndex(isSelectableChoice);
19
- if (startIndex < 0) {
33
+ const [active, setActive] = (0, core_1.useState)(() => {
34
+ const selected = items.findIndex(isSelectable);
35
+ if (selected < 0)
20
36
  throw new Error('[select prompt] No selectable choices. All choices are disabled.');
21
- }
22
- return startIndex;
37
+ return selected;
23
38
  });
24
39
  // Safe to assume the cursor position always point to a Choice.
25
- const selectedChoice = choices[cursorPosition];
40
+ const selectedChoice = items[active];
26
41
  (0, core_1.useKeypress)((key) => {
27
42
  if ((0, core_1.isEnterKey)(key)) {
28
43
  setStatus('done');
29
44
  done(selectedChoice.value);
30
45
  }
31
46
  else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) {
32
- let newCursorPosition = cursorPosition;
33
47
  const offset = (0, core_1.isUpKey)(key) ? -1 : 1;
34
- let selectedOption;
35
- while (!isSelectableChoice(selectedOption)) {
36
- newCursorPosition =
37
- (newCursorPosition + offset + choices.length) % choices.length;
38
- selectedOption = choices[newCursorPosition];
39
- }
40
- setCursorPos(newCursorPosition);
48
+ let next = active;
49
+ do {
50
+ next = (next + offset + items.length) % items.length;
51
+ } while (!isSelectable(items[next]));
52
+ setActive(next);
41
53
  }
42
54
  else if ((0, core_1.isNumberKey)(key)) {
43
- // Adjust index to start at 1
44
- const newCursorPosition = Number(key.name) - 1;
45
- // Abort if the choice doesn't exists or if disabled
46
- if (!isSelectableChoice(choices[newCursorPosition])) {
55
+ const position = Number(key.name) - 1;
56
+ const item = items[position];
57
+ if (item == null || !isSelectable(item))
47
58
  return;
48
- }
49
- setCursorPos(newCursorPosition);
59
+ setActive(position);
50
60
  }
51
61
  });
52
62
  let message = chalk_1.default.bold(config.message);
53
63
  if (firstRender.current) {
54
- message += chalk_1.default.dim(' (Use arrow keys)');
55
64
  firstRender.current = false;
65
+ message += chalk_1.default.dim(' (Use arrow keys)');
56
66
  }
57
- const allChoices = choices
58
- .map((choice, index) => {
59
- if (core_1.Separator.isSeparator(choice)) {
60
- return ` ${choice.separator}`;
61
- }
62
- const line = choice.name || choice.value;
63
- if (choice.disabled) {
64
- const disabledLabel = typeof choice.disabled === 'string' ? choice.disabled : '(disabled)';
65
- return chalk_1.default.dim(`- ${line} ${disabledLabel}`);
66
- }
67
- if (index === cursorPosition) {
68
- return chalk_1.default.cyan(`${figures_1.default.pointer} ${line}`);
69
- }
70
- return ` ${line}`;
71
- })
67
+ const lines = items
68
+ .map((item, index) => renderItem({ item, isActive: index === active }))
72
69
  .join('\n');
73
- const windowedChoices = (0, core_1.usePagination)(allChoices, {
74
- active: cursorPosition,
75
- pageSize: config.pageSize,
70
+ const page = (0, core_1.usePagination)(lines, {
71
+ active,
72
+ pageSize,
76
73
  });
77
74
  if (status === 'done') {
78
75
  return `${prefix} ${message} ${chalk_1.default.cyan(selectedChoice.name || selectedChoice.value)}`;
@@ -80,5 +77,5 @@ exports.default = (0, core_1.createPrompt)((config, done) => {
80
77
  const choiceDescription = selectedChoice.description
81
78
  ? `\n${selectedChoice.description}`
82
79
  : ``;
83
- return `${prefix} ${message}\n${windowedChoices}${choiceDescription}${ansi_escapes_1.default.cursorHide}`;
80
+ return `${prefix} ${message}\n${page}${choiceDescription}${ansi_escapes_1.default.cursorHide}`;
84
81
  });
@@ -1,4 +1,4 @@
1
- import { Separator, AsyncPromptConfig } from '@inquirer/core';
1
+ import { Separator } from '@inquirer/core';
2
2
  type Choice<Value> = {
3
3
  value: Value;
4
4
  name?: string;
@@ -6,7 +6,8 @@ type Choice<Value> = {
6
6
  disabled?: boolean | string;
7
7
  type?: never;
8
8
  };
9
- declare const _default: <Value extends unknown>(config: AsyncPromptConfig & {
9
+ declare const _default: <Value extends unknown>(config: {
10
+ message: string | Promise<string> | (() => Promise<string>);
10
11
  choices: readonly (Separator | Choice<Value>)[];
11
12
  pageSize?: number | undefined;
12
13
  }, context?: import("@inquirer/type").Context | undefined) => import("@inquirer/type").CancelablePromise<Value>;
@@ -2,73 +2,67 @@ import { createPrompt, useState, useKeypress, usePrefix, usePagination, useRef,
2
2
  import chalk from 'chalk';
3
3
  import figures from 'figures';
4
4
  import ansiEscapes from 'ansi-escapes';
5
- function isSelectableChoice(choice) {
6
- return choice != null && !Separator.isSeparator(choice) && !choice.disabled;
5
+ function isSelectable(item) {
6
+ return !Separator.isSeparator(item) && !item.disabled;
7
+ }
8
+ function renderItem({ item, isActive }) {
9
+ if (Separator.isSeparator(item)) {
10
+ return ` ${item.separator}`;
11
+ }
12
+ const line = item.name || item.value;
13
+ if (item.disabled) {
14
+ const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)';
15
+ return chalk.dim(`- ${line} ${disabledLabel}`);
16
+ }
17
+ const color = isActive ? chalk.cyan : (x) => x;
18
+ const prefix = isActive ? figures.pointer : ` `;
19
+ return color(`${prefix} ${line}`);
7
20
  }
8
21
  export default createPrompt((config, done) => {
9
- const { choices } = config;
22
+ const { choices: items, pageSize } = config;
10
23
  const firstRender = useRef(true);
11
24
  const prefix = usePrefix();
12
25
  const [status, setStatus] = useState('pending');
13
- const [cursorPosition, setCursorPos] = useState(() => {
14
- const startIndex = choices.findIndex(isSelectableChoice);
15
- if (startIndex < 0) {
26
+ const [active, setActive] = useState(() => {
27
+ const selected = items.findIndex(isSelectable);
28
+ if (selected < 0)
16
29
  throw new Error('[select prompt] No selectable choices. All choices are disabled.');
17
- }
18
- return startIndex;
30
+ return selected;
19
31
  });
20
32
  // Safe to assume the cursor position always point to a Choice.
21
- const selectedChoice = choices[cursorPosition];
33
+ const selectedChoice = items[active];
22
34
  useKeypress((key) => {
23
35
  if (isEnterKey(key)) {
24
36
  setStatus('done');
25
37
  done(selectedChoice.value);
26
38
  }
27
39
  else if (isUpKey(key) || isDownKey(key)) {
28
- let newCursorPosition = cursorPosition;
29
40
  const offset = isUpKey(key) ? -1 : 1;
30
- let selectedOption;
31
- while (!isSelectableChoice(selectedOption)) {
32
- newCursorPosition =
33
- (newCursorPosition + offset + choices.length) % choices.length;
34
- selectedOption = choices[newCursorPosition];
35
- }
36
- setCursorPos(newCursorPosition);
41
+ let next = active;
42
+ do {
43
+ next = (next + offset + items.length) % items.length;
44
+ } while (!isSelectable(items[next]));
45
+ setActive(next);
37
46
  }
38
47
  else if (isNumberKey(key)) {
39
- // Adjust index to start at 1
40
- const newCursorPosition = Number(key.name) - 1;
41
- // Abort if the choice doesn't exists or if disabled
42
- if (!isSelectableChoice(choices[newCursorPosition])) {
48
+ const position = Number(key.name) - 1;
49
+ const item = items[position];
50
+ if (item == null || !isSelectable(item))
43
51
  return;
44
- }
45
- setCursorPos(newCursorPosition);
52
+ setActive(position);
46
53
  }
47
54
  });
48
55
  let message = chalk.bold(config.message);
49
56
  if (firstRender.current) {
50
- message += chalk.dim(' (Use arrow keys)');
51
57
  firstRender.current = false;
58
+ message += chalk.dim(' (Use arrow keys)');
52
59
  }
53
- const allChoices = choices
54
- .map((choice, index) => {
55
- if (Separator.isSeparator(choice)) {
56
- return ` ${choice.separator}`;
57
- }
58
- const line = choice.name || choice.value;
59
- if (choice.disabled) {
60
- const disabledLabel = typeof choice.disabled === 'string' ? choice.disabled : '(disabled)';
61
- return chalk.dim(`- ${line} ${disabledLabel}`);
62
- }
63
- if (index === cursorPosition) {
64
- return chalk.cyan(`${figures.pointer} ${line}`);
65
- }
66
- return ` ${line}`;
67
- })
60
+ const lines = items
61
+ .map((item, index) => renderItem({ item, isActive: index === active }))
68
62
  .join('\n');
69
- const windowedChoices = usePagination(allChoices, {
70
- active: cursorPosition,
71
- pageSize: config.pageSize,
63
+ const page = usePagination(lines, {
64
+ active,
65
+ pageSize,
72
66
  });
73
67
  if (status === 'done') {
74
68
  return `${prefix} ${message} ${chalk.cyan(selectedChoice.name || selectedChoice.value)}`;
@@ -76,6 +70,6 @@ export default createPrompt((config, done) => {
76
70
  const choiceDescription = selectedChoice.description
77
71
  ? `\n${selectedChoice.description}`
78
72
  : ``;
79
- return `${prefix} ${message}\n${windowedChoices}${choiceDescription}${ansiEscapes.cursorHide}`;
73
+ return `${prefix} ${message}\n${page}${choiceDescription}${ansiEscapes.cursorHide}`;
80
74
  });
81
75
  export { Separator };
@@ -1,4 +1,4 @@
1
- import { Separator, AsyncPromptConfig } from '@inquirer/core';
1
+ import { Separator } from '@inquirer/core';
2
2
  type Choice<Value> = {
3
3
  value: Value;
4
4
  name?: string;
@@ -6,7 +6,8 @@ type Choice<Value> = {
6
6
  disabled?: boolean | string;
7
7
  type?: never;
8
8
  };
9
- declare const _default: <Value extends unknown>(config: AsyncPromptConfig & {
9
+ declare const _default: <Value extends unknown>(config: {
10
+ message: string | Promise<string> | (() => Promise<string>);
10
11
  choices: readonly (Separator | Choice<Value>)[];
11
12
  pageSize?: number | undefined;
12
13
  }, context?: import("@inquirer/type").Context | undefined) => import("@inquirer/type").CancelablePromise<Value>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/select",
3
- "version": "1.2.10",
3
+ "version": "1.2.12",
4
4
  "description": "Inquirer select/list prompt",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "typings": "./dist/cjs/types/index.d.ts",
@@ -54,14 +54,14 @@
54
54
  "license": "MIT",
55
55
  "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/select/README.md",
56
56
  "dependencies": {
57
- "@inquirer/core": "^4.1.0",
58
- "@inquirer/type": "^1.1.3",
57
+ "@inquirer/core": "^5.0.1",
58
+ "@inquirer/type": "^1.1.5",
59
59
  "ansi-escapes": "^4.3.2",
60
60
  "chalk": "^4.1.2",
61
61
  "figures": "^3.2.0"
62
62
  },
63
63
  "devDependencies": {
64
- "@inquirer/testing": "^2.1.5"
64
+ "@inquirer/testing": "^2.1.7"
65
65
  },
66
66
  "scripts": {
67
67
  "tsc": "yarn run tsc:esm && yarn run tsc:cjs",
@@ -86,5 +86,5 @@
86
86
  }
87
87
  }
88
88
  },
89
- "gitHead": "f5c544a8ded1c7dfb6ac8364759955e8f2ecfb0c"
89
+ "gitHead": "85784061d702778bc9dd48ca08f09ee9976b06ee"
90
90
  }