Skip to content

Commit 5a9c60e

Browse files
committed
fix: correctly wrap multi-line messages in confirm prompt
Fixes #101
1 parent 8a96e2d commit 5a9c60e

4 files changed

Lines changed: 75 additions & 2 deletions

File tree

.changeset/sharp-lands-unite.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+
Apply guide to wrapped multi-line messages in confirm prompt.

packages/prompts/src/confirm.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { styleText } from 'node:util';
2-
import { ConfirmPrompt, settings } from '@clack/core';
2+
import { ConfirmPrompt, settings, wrapTextWithPrefix } from '@clack/core';
33
import {
44
type CommonOptions,
55
S_BAR,
@@ -28,7 +28,15 @@ export const confirm = (opts: ConfirmOptions) => {
2828
initialValue: opts.initialValue ?? true,
2929
render() {
3030
const hasGuide = opts.withGuide ?? settings.withGuide;
31-
const title = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${symbol(this.state)} ${opts.message}\n`;
31+
const titlePrefix = `${symbol(this.state)} `;
32+
const titlePrefixBar = hasGuide ? `${styleText('gray', S_BAR)} ` : '';
33+
const messageLines = wrapTextWithPrefix(
34+
opts.output,
35+
opts.message,
36+
titlePrefixBar,
37+
titlePrefix
38+
);
39+
const title = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${messageLines}\n`;
3240
const value = this.value ? active : inactive;
3341

3442
switch (this.state) {

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,29 @@ exports[`confirm (isCI = false) > renders message with choices 1`] = `
156156
]
157157
`;
158158
159+
exports[`confirm (isCI = false) > renders multi-line messages correctly 1`] = `
160+
[
161+
"<cursor.hide>",
162+
"│
163+
◆ foo
164+
│ bar
165+
│ baz
166+
│ ● Yes / ○ No
167+
└
168+
",
169+
"<cursor.backward count=999><cursor.up count=6>",
170+
"<cursor.down count=1>",
171+
"<erase.down>",
172+
"◇ foo
173+
│ bar
174+
│ baz
175+
│ Yes",
176+
"
177+
",
178+
"<cursor.show>",
179+
]
180+
`;
181+
159182
exports[`confirm (isCI = false) > renders options in vertical alignment 1`] = `
160183
[
161184
"<cursor.hide>",
@@ -373,6 +396,29 @@ exports[`confirm (isCI = true) > renders message with choices 1`] = `
373396
]
374397
`;
375398
399+
exports[`confirm (isCI = true) > renders multi-line messages correctly 1`] = `
400+
[
401+
"<cursor.hide>",
402+
"│
403+
◆ foo
404+
│ bar
405+
│ baz
406+
│ ● Yes / ○ No
407+
└
408+
",
409+
"<cursor.backward count=999><cursor.up count=6>",
410+
"<cursor.down count=1>",
411+
"<erase.down>",
412+
"◇ foo
413+
│ bar
414+
│ baz
415+
│ Yes",
416+
"
417+
",
418+
"<cursor.show>",
419+
]
420+
`;
421+
376422
exports[`confirm (isCI = true) > renders options in vertical alignment 1`] = `
377423
[
378424
"<cursor.hide>",

packages/prompts/test/confirm.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,18 @@ describe.each(['true', 'false'])('confirm (isCI = %s)', (isCI) => {
199199

200200
expect(output.buffer).toMatchSnapshot();
201201
});
202+
203+
test('renders multi-line messages correctly', async () => {
204+
const result = prompts.confirm({
205+
message: 'foo\nbar\nbaz',
206+
input,
207+
output,
208+
});
209+
210+
input.emit('keypress', '', { name: 'return' });
211+
212+
await result;
213+
214+
expect(output.buffer).toMatchSnapshot();
215+
});
202216
});

0 commit comments

Comments
 (0)