@inquirer/rawlist 5.0.2 → 5.2.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
@@ -72,6 +72,7 @@ const answer = await rawlist({
72
72
  | message | `string` | yes | The question to ask |
73
73
  | choices | `Choice[]` | yes | List of the available choices. |
74
74
  | 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. |
75
+ | default | `Value` | no | The value of the choice to preselect. If the value is not found, no choice is preselected. |
75
76
  | theme | [See Theming](#Theming) | no | Customize look of the prompt. |
76
77
 
77
78
  `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.
@@ -86,6 +87,7 @@ type Choice<Value> = {
86
87
  name?: string;
87
88
  short?: string;
88
89
  key?: string;
90
+ description?: string;
89
91
  };
90
92
  ```
91
93
 
@@ -95,6 +97,7 @@ Here's each property:
95
97
  - `name`: This is the string displayed in the choice list.
96
98
  - `short`: Once the prompt is done (press enter), we'll use `short` if defined to render next to the question. By default we'll use `name`.
97
99
  - `key`: The key of the choice. Displayed as `key) name`.
100
+ - `description`: Option description which appears below the list when the choice is selected.
98
101
 
99
102
  `choices` can also be an array of string, in which case the string will be used both as the `value` and the `name`.
100
103
 
@@ -114,6 +117,7 @@ type Theme = {
114
117
  message: (text: string, status: 'idle' | 'done' | 'loading') => string;
115
118
  error: (text: string) => string;
116
119
  highlight: (text: string) => string;
120
+ description: (text: string) => string;
117
121
  };
118
122
  };
119
123
  ```
package/dist/index.d.ts CHANGED
@@ -1,16 +1,23 @@
1
1
  import { Separator, type Theme } from '@inquirer/core';
2
2
  import type { PartialDeep } from '@inquirer/type';
3
+ type RawlistTheme = {
4
+ style: {
5
+ description: (text: string) => string;
6
+ };
7
+ };
3
8
  type Choice<Value> = {
4
9
  value: Value;
5
10
  name?: string;
6
11
  short?: string;
7
12
  key?: string;
13
+ description?: string;
8
14
  };
9
15
  declare const _default: <Value>(config: {
10
16
  message: string;
11
- choices: readonly (string | Separator)[] | readonly (Separator | Choice<Value>)[];
17
+ choices: readonly (Separator | Value | Choice<Value>)[];
12
18
  loop?: boolean | undefined;
13
- theme?: PartialDeep<Theme> | undefined;
19
+ theme?: PartialDeep<Theme<RawlistTheme>> | undefined;
20
+ default?: NoInfer<Value> | undefined;
14
21
  }, context?: import("@inquirer/type").Context) => Promise<Value>;
15
22
  export default _default;
16
23
  export { Separator } from '@inquirer/core';
package/dist/index.js CHANGED
@@ -1,6 +1,11 @@
1
1
  import { createPrompt, useMemo, useState, useKeypress, usePrefix, isDownKey, isEnterKey, isUpKey, Separator, makeTheme, ValidationError, } from '@inquirer/core';
2
2
  import { styleText } from 'node:util';
3
3
  const numberRegex = /\d+/;
4
+ const rawlistTheme = {
5
+ style: {
6
+ description: (text) => styleText('cyan', text),
7
+ },
8
+ };
4
9
  function isSelectableChoice(choice) {
5
10
  return choice != null && !Separator.isSeparator(choice);
6
11
  }
@@ -10,11 +15,12 @@ function normalizeChoices(choices) {
10
15
  if (Separator.isSeparator(choice))
11
16
  return choice;
12
17
  index += 1;
13
- if (typeof choice === 'string') {
18
+ if (typeof choice !== 'object' || choice === null || !('value' in choice)) {
19
+ const name = String(choice);
14
20
  return {
15
21
  value: choice,
16
- name: choice,
17
- short: choice,
22
+ name,
23
+ short: name,
18
24
  key: String(index),
19
25
  };
20
26
  }
@@ -24,6 +30,7 @@ function normalizeChoices(choices) {
24
30
  name,
25
31
  short: choice.short ?? name,
26
32
  key: choice.key ?? String(index),
33
+ description: choice.description,
27
34
  };
28
35
  });
29
36
  }
@@ -45,9 +52,14 @@ export default createPrompt((config, done) => {
45
52
  const { loop = true } = config;
46
53
  const choices = useMemo(() => normalizeChoices(config.choices), [config.choices]);
47
54
  const [status, setStatus] = useState('idle');
48
- const [value, setValue] = useState('');
55
+ const [value, setValue] = useState(() => {
56
+ const defaultChoice = config.default == null
57
+ ? undefined
58
+ : choices.find((choice) => isSelectableChoice(choice) && choice.value === config.default);
59
+ return defaultChoice?.key ?? '';
60
+ });
49
61
  const [errorMsg, setError] = useState();
50
- const theme = makeTheme(config.theme);
62
+ const theme = makeTheme(rawlistTheme, config.theme);
51
63
  const prefix = usePrefix({ status, theme });
52
64
  const bounds = useMemo(() => {
53
65
  const first = choices.findIndex(isSelectableChoice);
@@ -107,7 +119,7 @@ export default createPrompt((config, done) => {
107
119
  return ` ${choice.separator}`;
108
120
  }
109
121
  const line = ` ${choice.key}) ${choice.name}`;
110
- if (choice.key === value.toLowerCase()) {
122
+ if (choice.key === value) {
111
123
  return theme.style.highlight(line);
112
124
  }
113
125
  return line;
@@ -117,9 +129,14 @@ export default createPrompt((config, done) => {
117
129
  if (errorMsg) {
118
130
  error = theme.style.error(errorMsg);
119
131
  }
132
+ const [selectedChoice] = getSelectedChoice(value, choices);
133
+ let description = '';
134
+ if (!errorMsg && selectedChoice?.description) {
135
+ description = theme.style.description(selectedChoice.description);
136
+ }
120
137
  return [
121
138
  `${prefix} ${message} ${value}`,
122
- [choicesStr, error].filter(Boolean).join('\n'),
139
+ [choicesStr, error, description].filter(Boolean).join('\n'),
123
140
  ];
124
141
  });
125
142
  export { Separator } from '@inquirer/core';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/rawlist",
3
- "version": "5.0.2",
3
+ "version": "5.2.0",
4
4
  "description": "Inquirer rawlist prompt",
5
5
  "keywords": [
6
6
  "answer",
@@ -42,41 +42,38 @@
42
42
  "zsh"
43
43
  ],
44
44
  "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/main/packages/rawlist/README.md",
45
+ "license": "MIT",
46
+ "author": "Simon Boudrias <admin@simonboudrias.com>",
45
47
  "repository": {
46
48
  "type": "git",
47
49
  "url": "https://github.com/SBoudrias/Inquirer.js.git"
48
50
  },
49
- "license": "MIT",
50
- "author": "Simon Boudrias <admin@simonboudrias.com>",
51
- "sideEffects": false,
51
+ "files": [
52
+ "dist"
53
+ ],
52
54
  "type": "module",
55
+ "sideEffects": false,
53
56
  "exports": {
54
- "./package.json": "./package.json",
55
57
  ".": {
56
58
  "types": "./dist/index.d.ts",
57
59
  "default": "./dist/index.js"
58
- }
60
+ },
61
+ "./package.json": "./package.json"
62
+ },
63
+ "publishConfig": {
64
+ "access": "public"
59
65
  },
60
- "files": [
61
- "dist"
62
- ],
63
66
  "scripts": {
64
67
  "tsc": "tsc"
65
68
  },
66
69
  "dependencies": {
67
- "@inquirer/core": "^11.0.2",
68
- "@inquirer/type": "^4.0.2"
70
+ "@inquirer/core": "^11.1.1",
71
+ "@inquirer/type": "^4.0.3"
69
72
  },
70
73
  "devDependencies": {
71
- "@inquirer/testing": "^3.0.2",
74
+ "@inquirer/testing": "^3.0.4",
72
75
  "typescript": "^5.9.3"
73
76
  },
74
- "engines": {
75
- "node": ">=23.5.0 || ^22.13.0 || ^21.7.0 || ^20.12.0"
76
- },
77
- "publishConfig": {
78
- "access": "public"
79
- },
80
77
  "peerDependencies": {
81
78
  "@types/node": ">=18"
82
79
  },
@@ -85,7 +82,10 @@
85
82
  "optional": true
86
83
  }
87
84
  },
85
+ "engines": {
86
+ "node": ">=23.5.0 || ^22.13.0 || ^21.7.0 || ^20.12.0"
87
+ },
88
88
  "main": "./dist/index.js",
89
89
  "types": "./dist/index.d.ts",
90
- "gitHead": "9c724296daad3d740b09173f9aae035dc62cb106"
90
+ "gitHead": "99d00a9adc53be8b7edf5926b2ec4ba0b792f68f"
91
91
  }