Skip to content

Commit e9c11b1

Browse files
authored
Add grouping (group + groupMultiselect) + spinner + some description (#9)
* Add grouping (group + groupMultiselect) + spinner + some description * Code review (rephrasing + example teaking) * Reordering log example to match description order * Add cancel function + fix js line ending
1 parent 1d6f77c commit e9c11b1

File tree

1 file changed

+166
-17
lines changed

1 file changed

+166
-17
lines changed

src/content/docs/clack/packages/prompts.mdx

+166-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: Prompts
33
description: Learn about the prompts package and its capabilities
44
---
55

6+
import { Aside } from "@astrojs/starlight/components"
7+
68
The `@clack/prompts` package provides a collection of pre-built, high-level prompts that make it easy to create interactive command-line interfaces. It builds on top of the core package to provide a more developer-friendly experience.
79

810
## Key Features
@@ -139,31 +141,167 @@ const shouldProceed = await confirm({
139141
<font color="#06989A">│</font> <font color="#4E9A06">●</font> Yes / ○ No
140142
<font color="#06989A">└</font></pre>
141143

144+
## Grouping
145+
146+
### Group Multiselect
147+
148+
```ts twoslash
149+
import { groupMultiselect } from '@clack/prompts';
150+
151+
const projectOptions = await groupMultiselect({
152+
message: 'Define your project',
153+
options: {
154+
'Testing': [
155+
{ value: 'Jest' },
156+
{ value: 'Playwright' },
157+
{ value: 'Vitest' },
158+
],
159+
'Language': [{
160+
label: "Javascript",
161+
value: 'js',
162+
}, {
163+
label: 'TypeScript',
164+
value: 'ts',
165+
}, {
166+
label: "CoffeeScript",
167+
value: 'coffee',
168+
}],
169+
'Code quality': [
170+
{ value: 'Prettier' },
171+
{ value: 'ESLint' },
172+
{ value: 'Biome.js' },
173+
],
174+
},
175+
});
176+
```
177+
178+
<pre class="cli-preview"><font color="#555753">│</font>
179+
<font color="#06989A">◆</font> Define your project
180+
<font color="#06989A">│</font> <font color="#4E9A06">◼</font> Testing
181+
<font color="#06989A">│</font> │ <font color="#4E9A06">◼</font> Jest
182+
<font color="#06989A">│</font> │ <font color="#4E9A06">◼</font> Playwright
183+
<font color="#06989A">│</font> └ <font color="#4E9A06">◼</font> Vitest
184+
<font color="#06989A">│</font> <font color="#06989A">◻</font> Language
185+
<font color="#06989A">│</font> │ <font color="#4E9A06">◼</font> Javascript
186+
<font color="#06989A">│</font> │ <font color="#06989A">◻</font> TypeScript
187+
<font color="#06989A">│</font> └ <font color="#06989A">◻</font> CoffeeScript
188+
<font color="#06989A">│</font> ◻ Code quality
189+
<font color="#06989A">│</font> │ ◻ Prettier
190+
<font color="#06989A">│</font> │ ◻ ESLint
191+
<font color="#06989A">│</font> └ <font color="#4E9A06">◼</font> Biome.js
192+
<font color="#06989A">└</font></pre>
193+
194+
### Group
195+
196+
The `group` function provides a convenient API for combining a series of questions.
197+
Each question receives the `results` of previous answers.
198+
The `group` function returns an object containing the result of every question.
199+
200+
```ts twoslash
201+
import { group, text, password } from '@clack/prompts';
202+
203+
const account = await group({
204+
email: () => text({
205+
message: 'What is your email address?',
206+
validate: (value) => {
207+
if (!/^[a-z0-9_.-]+@[a-z0-9_.-]+\.[a-z]{2,}$/i.test(value)) return 'Please enter a valid email'
208+
}
209+
}),
210+
username: ({results}) => text({
211+
message: 'What is your username?',
212+
placeholder: results.email?.replace(/@.+$/, '').toLowerCase() ?? '',
213+
validate: (value) => {
214+
// FOR DEMO PURPOSES ONLY! Use a robust validation library in production
215+
if (value.length < 2) return 'Please enter at least 2 characters'
216+
}
217+
}),
218+
password: () => password({
219+
message: 'Define your password'
220+
}),
221+
});
222+
```
223+
224+
<pre class="cli-preview"><font color="#555753">│</font>
225+
<font color="#4E9A06">◇</font> What is your email address?
226+
<font color="#555753">│</font> [email protected]
227+
<font color="#555753">│</font>
228+
<font color="#4E9A06">◇</font> What is your username?
229+
<font color="#555753">│</font> bomb_sh
230+
<font color="#555753">│</font>
231+
<font color="#06989A">◆</font> Define your password
232+
<font color="#06989A">│</font> ▪▪▪▪▪▪▪▪▪▪▪▪<span style="background-color:#FFFFFF"><font color="#FFFFFF">_</font></span>
233+
<font color="#06989A">└</font></pre>
234+
142235
## Support functions
143236

144237
### Intro
145238

239+
The `intro` function defines the beginning of an interaction.
240+
It accepts an optional string parameter which is displayed as a title for the interaction.
241+
242+
<Aside type="tip">Feel free to use ANSI escape sequences to add colors and a more branded feel to your intro message!</Aside>
243+
146244
```ts twoslash
147245
import { intro } from '@clack/prompts';
148246

149-
intro('Welcome to clack')
247+
intro('Welcome to clack');
150248
```
151249

152250
<pre class="cli-preview"><font color="#555753">┌</font> Welcome to clack</pre>
153251

154252
### Outro
155253

254+
The `outro` function defines the end of an interaction.
255+
It accepts an optional string parameter which is displayed as a concluding message.
256+
156257
```ts twoslash
157258
import { outro } from '@clack/prompts';
158259

159-
outro('All operations are finished')
260+
outro('All operations are finished');
160261
```
161262

162263
<pre class="cli-preview"><font color="#555753">│</font>
163-
<font color="#555753">└</font> All operations are finished</pre>
264+
<font color="#555753">└</font> All operations are finished
265+
&nbsp;</pre>
266+
267+
### Cancel
268+
269+
The `cancel` function defines an interruption of an interaction and therefore its end.
270+
It accepts an optional string parameter which is displayed as a cancellation message.
271+
272+
```ts twoslash
273+
import { cancel } from '@clack/prompts';
274+
275+
cancel('Installation canceled');
276+
```
277+
278+
<pre class="cli-preview"><font color="#555753">└</font> <font color="#CC0000">Installation canceled</font>
279+
&nbsp;</pre>
280+
281+
### Spinner
282+
```ts twoslash
283+
import { spinner } from '@clack/prompts';
284+
285+
const spin = spinner();
286+
spin.start('Loading');
287+
// Do something
288+
spin.message('Finishing');
289+
// Do more things
290+
spin.stop('Done');
291+
```
292+
<pre class="cli-preview"><font color="#555753">│</font>
293+
<font color="#AD7FA8">◒</font> Loading{`...`}</pre>
294+
295+
The second parameter of `spinner.stop` (`code`) allow you to indicate the ending status of the spinner:
296+
- `0` (or no value) indicate a success and the symbol for a finished task will be used
297+
- `1` indicate a cancellation and the red square symbol will be used
298+
- Any other code indicate an error and the yellow triangle symbol will be used
164299

165300
### Note
166301

302+
The `note` function renders a box around a message to draw a user's attention.
303+
This is useful for displaying next steps and linking to your documentation towards the end of an interaction.
304+
167305
```ts twoslash
168306
import { note } from '@clack/prompts';
169307

@@ -198,32 +336,43 @@ note(
198336

199337
### Logs
200338

339+
The `log` utilities allow you to add semantic contextual information during an interaction.
340+
Each function renders with specific styling to communicate status.
341+
342+
`log` is an object containing the following methods:
343+
- `log.message` displays a message without any symbols to communicate state
344+
- `log.info` displays a message with a neutral state
345+
- `log.warn` (alias `log.warning`) displays a message with a caution state
346+
- `log.error` displays a message with a danger state
347+
- `log.success` displays a message with a success state
348+
- `log.step` displays a message with a neutral, completed state
349+
201350
```ts twoslash
202351
import { log } from '@clack/prompts';
203352

204-
log.step('Check files')
205-
log.info('No files to update')
206-
log.message('Entering directory "src"')
207-
log.error('Permission denied on file src/secret.js')
208-
log.success('Installation complete')
209-
log.warn('Directory is empty, skipping')
210-
log.warning('Directory is empty, skipping')
353+
log.message('Entering directory "src"');
354+
log.info('No files to update');
355+
log.warn('Directory is empty, skipping');
356+
log.warning('Directory is empty, skipping');
357+
log.error('Permission denied on file src/secret.js');
358+
log.success('Installation complete');
359+
log.step('Check files');
211360
```
212361

213362
<pre class="cli-preview"><font color="#555753">│</font>
214-
<font color="#4E9A06">◇</font> Check files
363+
<font color="#555753">│</font> Entering directory &quot;src&quot;
215364
<font color="#555753">│</font>
216365
<font color="#3465A4">●</font> No files to update
217366
<font color="#555753">│</font>
218-
<font color="#555753">│</font> Entering directory &quot;src&quot;
219-
<font color="#555753">│</font>
367+
<font color="#C4A000">▲</font> Directory is empty, skipping
368+
<font color="#555753">│</font>
369+
<font color="#C4A000">▲</font> Directory is empty, skipping
370+
<font color="#555753">│</font>
220371
<font color="#CC0000">■</font> Permission denied on file src/secret.js
221372
<font color="#555753">│</font>
222373
<font color="#4E9A06">◆</font> Installation complete
223374
<font color="#555753">│</font>
224-
<font color="#C4A000">▲</font> Directory is empty, skipping
225-
<font color="#555753">│</font>
226-
<font color="#C4A000">▲</font> Directory is empty, skipping</pre>
375+
<font color="#4E9A06">◇</font> Check files</pre>
227376

228377
## Installation
229378

@@ -235,4 +384,4 @@ npm install @clack/prompts
235384

236385
The prompts package is designed to be intuitive and easy to use. Each prompt function returns a Promise that resolves to the user's input.
237386

238-
For more detailed examples and advanced usage patterns, check out our [examples guide](/docs/guides/examples) and [best practices](/docs/guides/best-practices).
387+
For more detailed examples and advanced usage patterns, check out our [examples guide](/docs/guides/examples) and [best practices](/docs/guides/best-practices).

0 commit comments

Comments
 (0)