Skip to content

Commit 4e9ae13

Browse files
authored
feat: add withGuide support to confirm prompt (#450)
1 parent f9b9953 commit 4e9ae13

File tree

4 files changed

+122
-9
lines changed

4 files changed

+122
-9
lines changed

‎.changeset/tasty-horses-lay.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/prompts": patch
3+
---
4+
5+
Add support for `withGuide` to confirm prompt

‎packages/prompts/src/confirm.ts‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ConfirmPrompt } from '@clack/core';
1+
import { ConfirmPrompt, settings } from '@clack/core';
22
import color from 'picocolors';
33
import {
44
type CommonOptions,
@@ -26,26 +26,33 @@ export const confirm = (opts: ConfirmOptions) => {
2626
output: opts.output,
2727
initialValue: opts.initialValue ?? true,
2828
render() {
29-
const title = `${color.gray(S_BAR)}\n${symbol(this.state)} ${opts.message}\n`;
29+
const hasGuide = (opts.withGuide ?? settings.withGuide) !== false;
30+
const title = `${hasGuide ? `${color.gray(S_BAR)}\n` : ''}${symbol(this.state)} ${opts.message}\n`;
3031
const value = this.value ? active : inactive;
3132

3233
switch (this.state) {
33-
case 'submit':
34-
return `${title}${color.gray(S_BAR)} ${color.dim(value)}`;
35-
case 'cancel':
36-
return `${title}${color.gray(S_BAR)} ${color.strikethrough(
34+
case 'submit': {
35+
const submitPrefix = hasGuide ? `${color.gray(S_BAR)} ` : '';
36+
return `${title}${submitPrefix}${color.dim(value)}`;
37+
}
38+
case 'cancel': {
39+
const cancelPrefix = hasGuide ? `${color.gray(S_BAR)} ` : '';
40+
return `${title}${cancelPrefix}${color.strikethrough(
3741
color.dim(value)
38-
)}\n${color.gray(S_BAR)}`;
42+
)}${hasGuide ? `\n${color.gray(S_BAR)}` : ''}`;
43+
}
3944
default: {
40-
return `${title}${color.cyan(S_BAR)} ${
45+
const defaultPrefix = hasGuide ? `${color.cyan(S_BAR)} ` : '';
46+
const defaultPrefixEnd = hasGuide ? color.cyan(S_BAR_END) : '';
47+
return `${title}${defaultPrefix}${
4148
this.value
4249
? `${color.green(S_RADIO_ACTIVE)} ${active}`
4350
: `${color.dim(S_RADIO_INACTIVE)} ${color.dim(active)}`
4451
} ${color.dim('/')} ${
4552
!this.value
4653
? `${color.green(S_RADIO_ACTIVE)} ${inactive}`
4754
: `${color.dim(S_RADIO_INACTIVE)} ${color.dim(inactive)}`
48-
}\n${color.cyan(S_BAR_END)}\n`;
55+
}\n${defaultPrefixEnd}\n`;
4956
}
5057
}
5158
},

‎packages/prompts/test/__snapshots__/confirm.test.ts.snap‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ exports[`confirm (isCI = false) > can set initialValue 1`] = `
5353
]
5454
`;
5555
56+
exports[`confirm (isCI = false) > global withGuide: false removes guide 1`] = `
57+
[
58+
"<cursor.hide>",
59+
"◆ foo
60+
● Yes / ○ No
61+
62+
",
63+
"<cursor.backward count=999><cursor.up count=3>",
64+
"<erase.down>",
65+
"◇ foo
66+
Yes",
67+
"
68+
",
69+
"<cursor.show>",
70+
]
71+
`;
72+
5673
exports[`confirm (isCI = false) > left arrow moves to previous choice 1`] = `
5774
[
5875
"<cursor.hide>",
@@ -163,6 +180,23 @@ exports[`confirm (isCI = false) > right arrow moves to next choice 1`] = `
163180
]
164181
`;
165182
183+
exports[`confirm (isCI = false) > withGuide: false removes guide 1`] = `
184+
[
185+
"<cursor.hide>",
186+
"◆ foo
187+
● Yes / ○ No
188+
189+
",
190+
"<cursor.backward count=999><cursor.up count=3>",
191+
"<erase.down>",
192+
"◇ foo
193+
Yes",
194+
"
195+
",
196+
"<cursor.show>",
197+
]
198+
`;
199+
166200
exports[`confirm (isCI = true) > can be aborted by a signal 1`] = `
167201
[
168202
"<cursor.hide>",
@@ -216,6 +250,23 @@ exports[`confirm (isCI = true) > can set initialValue 1`] = `
216250
]
217251
`;
218252
253+
exports[`confirm (isCI = true) > global withGuide: false removes guide 1`] = `
254+
[
255+
"<cursor.hide>",
256+
"◆ foo
257+
● Yes / ○ No
258+
259+
",
260+
"<cursor.backward count=999><cursor.up count=3>",
261+
"<erase.down>",
262+
"◇ foo
263+
Yes",
264+
"
265+
",
266+
"<cursor.show>",
267+
]
268+
`;
269+
219270
exports[`confirm (isCI = true) > left arrow moves to previous choice 1`] = `
220271
[
221272
"<cursor.hide>",
@@ -325,3 +376,20 @@ exports[`confirm (isCI = true) > right arrow moves to next choice 1`] = `
325376
"<cursor.show>",
326377
]
327378
`;
379+
380+
exports[`confirm (isCI = true) > withGuide: false removes guide 1`] = `
381+
[
382+
"<cursor.hide>",
383+
"◆ foo
384+
● Yes / ○ No
385+
386+
",
387+
"<cursor.backward count=999><cursor.up count=3>",
388+
"<erase.down>",
389+
"◇ foo
390+
Yes",
391+
"
392+
",
393+
"<cursor.show>",
394+
]
395+
`;

‎packages/prompts/test/confirm.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { updateSettings } from '@clack/core';
12
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
23
import * as prompts from '../src/index.js';
34
import { MockReadable, MockWritable } from './test-utils.js';
@@ -23,6 +24,7 @@ describe.each(['true', 'false'])('confirm (isCI = %s)', (isCI) => {
2324

2425
afterEach(() => {
2526
vi.restoreAllMocks();
27+
updateSettings({ withGuide: true });
2628
});
2729

2830
test('renders message with choices', async () => {
@@ -150,4 +152,35 @@ describe.each(['true', 'false'])('confirm (isCI = %s)', (isCI) => {
150152
expect(prompts.isCancel(value)).toBe(true);
151153
expect(output.buffer).toMatchSnapshot();
152154
});
155+
156+
test('withGuide: false removes guide', async () => {
157+
const result = prompts.confirm({
158+
message: 'foo',
159+
withGuide: false,
160+
input,
161+
output,
162+
});
163+
164+
input.emit('keypress', '', { name: 'return' });
165+
166+
await result;
167+
168+
expect(output.buffer).toMatchSnapshot();
169+
});
170+
171+
test('global withGuide: false removes guide', async () => {
172+
updateSettings({ withGuide: false });
173+
174+
const result = prompts.confirm({
175+
message: 'foo',
176+
input,
177+
output,
178+
});
179+
180+
input.emit('keypress', '', { name: 'return' });
181+
182+
await result;
183+
184+
expect(output.buffer).toMatchSnapshot();
185+
});
153186
});

0 commit comments

Comments
 (0)