Skip to content

Commit 0e93ccb

Browse files
authored
feat: add vertical option to confirm prompt (#455)
1 parent 4e9ae13 commit 0e93ccb

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

.changeset/small-toys-move.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 `vertical` arrangement option to `confirm` prompt

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ dist
124124
# Stores VSCode versions used for testing VSCode extensions
125125
.vscode-test
126126

127+
# JetBrains IDEA
128+
.idea
129+
127130
# yarn v2
128131
.yarn/cache
129132
.yarn/unplugged

packages/prompts/src/confirm.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface ConfirmOptions extends CommonOptions {
1414
active?: string;
1515
inactive?: string;
1616
initialValue?: boolean;
17+
vertical?: boolean;
1718
}
1819
export const confirm = (opts: ConfirmOptions) => {
1920
const active = opts.active ?? 'Yes';
@@ -26,7 +27,7 @@ export const confirm = (opts: ConfirmOptions) => {
2627
output: opts.output,
2728
initialValue: opts.initialValue ?? true,
2829
render() {
29-
const hasGuide = (opts.withGuide ?? settings.withGuide) !== false;
30+
const hasGuide = opts.withGuide ?? settings.withGuide;
3031
const title = `${hasGuide ? `${color.gray(S_BAR)}\n` : ''}${symbol(this.state)} ${opts.message}\n`;
3132
const value = this.value ? active : inactive;
3233

@@ -48,7 +49,7 @@ export const confirm = (opts: ConfirmOptions) => {
4849
this.value
4950
? `${color.green(S_RADIO_ACTIVE)} ${active}`
5051
: `${color.dim(S_RADIO_INACTIVE)} ${color.dim(active)}`
51-
} ${color.dim('/')} ${
52+
}${opts.vertical ? (hasGuide ? `\n${color.cyan(S_BAR)} ` : '\n') : ` ${color.dim('/')} `}${
5253
!this.value
5354
? `${color.green(S_RADIO_ACTIVE)} ${inactive}`
5455
: `${color.dim(S_RADIO_INACTIVE)} ${color.dim(inactive)}`

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,26 @@ exports[`confirm (isCI = false) > renders message with choices 1`] = `
156156
]
157157
`;
158158
159+
exports[`confirm (isCI = false) > renders options in vertical alignment 1`] = `
160+
[
161+
"<cursor.hide>",
162+
"│
163+
◆ foo
164+
│ ● Yes
165+
│ ○ No
166+
└
167+
",
168+
"<cursor.backward count=999><cursor.up count=5>",
169+
"<cursor.down count=1>",
170+
"<erase.down>",
171+
"◇ foo
172+
│ Yes",
173+
"
174+
",
175+
"<cursor.show>",
176+
]
177+
`;
178+
159179
exports[`confirm (isCI = false) > right arrow moves to next choice 1`] = `
160180
[
161181
"<cursor.hide>",
@@ -353,6 +373,26 @@ exports[`confirm (isCI = true) > renders message with choices 1`] = `
353373
]
354374
`;
355375
376+
exports[`confirm (isCI = true) > renders options in vertical alignment 1`] = `
377+
[
378+
"<cursor.hide>",
379+
"│
380+
◆ foo
381+
│ ● Yes
382+
│ ○ No
383+
└
384+
",
385+
"<cursor.backward count=999><cursor.up count=5>",
386+
"<cursor.down count=1>",
387+
"<erase.down>",
388+
"◇ foo
389+
│ Yes",
390+
"
391+
",
392+
"<cursor.show>",
393+
]
394+
`;
395+
356396
exports[`confirm (isCI = true) > right arrow moves to next choice 1`] = `
357397
[
358398
"<cursor.hide>",

packages/prompts/test/confirm.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ describe.each(['true', 'false'])('confirm (isCI = %s)', (isCI) => {
7474
expect(output.buffer).toMatchSnapshot();
7575
});
7676

77+
test('renders options in vertical alignment', async () => {
78+
const result = prompts.confirm({
79+
message: 'foo',
80+
vertical: true,
81+
input,
82+
output,
83+
});
84+
85+
input.emit('keypress', '', { name: 'return' });
86+
87+
const value = await result;
88+
89+
expect(value).toBe(true);
90+
expect(output.buffer).toMatchSnapshot();
91+
});
92+
7793
test('right arrow moves to next choice', async () => {
7894
const result = prompts.confirm({
7995
message: 'foo',

0 commit comments

Comments
 (0)