From 364f1bbb3ab7e4ad5e03d142fa20a2ee105be5ee Mon Sep 17 00:00:00 2001 From: clean99 <53188310+clean99@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:56:17 +0800 Subject: [PATCH] fix(prompts): preserve the guide on multiline messages --- .changeset/prompt-message-newline-spacing.md | 5 ++ packages/prompts/src/date.ts | 11 ++-- packages/prompts/src/group-multi-select.ts | 8 ++- packages/prompts/src/multi-line.ts | 9 +++- packages/prompts/src/password.ts | 10 +++- packages/prompts/src/select-key.ts | 8 ++- packages/prompts/src/text.ts | 11 ++-- packages/prompts/test/message.test.ts | 56 ++++++++++++++++++++ 8 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 .changeset/prompt-message-newline-spacing.md create mode 100644 packages/prompts/test/message.test.ts diff --git a/.changeset/prompt-message-newline-spacing.md b/.changeset/prompt-message-newline-spacing.md new file mode 100644 index 00000000..2dd0c506 --- /dev/null +++ b/.changeset/prompt-message-newline-spacing.md @@ -0,0 +1,5 @@ +--- +"@clack/prompts": patch +--- + +Keep the guide on wrapped message lines in `text`, `password`, `date`, `multiline`, `selectKey`, and `groupMultiselect` prompts. diff --git a/packages/prompts/src/date.ts b/packages/prompts/src/date.ts index 6214a519..76f00bc5 100644 --- a/packages/prompts/src/date.ts +++ b/packages/prompts/src/date.ts @@ -1,6 +1,6 @@ import { styleText } from 'node:util'; import type { CANCEL_SYMBOL, DateFormat, State, Validate } from '@clack/core'; -import { DatePrompt, runValidation, settings } from '@clack/core'; +import { DatePrompt, runValidation, settings, wrapTextWithPrefix } from '@clack/core'; import { type CommonOptions, S_BAR, S_BAR_END, symbol } from './common.js'; export type { DateFormat }; @@ -97,8 +97,13 @@ export const date = (opts: DateOptions) => { }, render() { const hasGuide = (opts?.withGuide ?? settings.withGuide) !== false; - const titlePrefix = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${symbol(this.state)} `; - const title = `${titlePrefix}${opts.message}\n`; + const wrappedMessage = wrapTextWithPrefix( + opts.output, + opts.message, + hasGuide ? `${styleText('gray', S_BAR)} ` : '', + `${symbol(this.state)} ` + ); + const title = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${wrappedMessage}\n`; const state = this.state !== 'initial' ? this.state : 'active'; diff --git a/packages/prompts/src/group-multi-select.ts b/packages/prompts/src/group-multi-select.ts index 2e5cbeb5..72dc4501 100644 --- a/packages/prompts/src/group-multi-select.ts +++ b/packages/prompts/src/group-multi-select.ts @@ -224,7 +224,13 @@ export const groupMultiselect = (opts: GroupMultiSelectOptions) => }, render() { const hasGuide = opts.withGuide ?? settings.withGuide; - const title = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${symbol(this.state)} ${opts.message}\n`; + const wrappedMessage = wrapTextWithPrefix( + opts.output, + opts.message, + hasGuide ? `${styleText('gray', S_BAR)} ` : '', + `${symbol(this.state)} ` + ); + const title = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${wrappedMessage}\n`; const value = this.value ?? []; const styleOption = ( diff --git a/packages/prompts/src/multi-line.ts b/packages/prompts/src/multi-line.ts index 26d0e778..88d9b4e2 100644 --- a/packages/prompts/src/multi-line.ts +++ b/packages/prompts/src/multi-line.ts @@ -46,8 +46,13 @@ export const multiline = (opts: MultiLineOptions) => { input: opts.input, render() { const hasGuide = opts?.withGuide ?? settings.withGuide; - const titlePrefix = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${symbol(this.state)} `; - const title = `${titlePrefix}${opts.message}\n`; + const wrappedMessage = wrapTextWithPrefix( + opts.output, + opts.message, + hasGuide ? `${styleText('gray', S_BAR)} ` : '', + `${symbol(this.state)} ` + ); + const title = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${wrappedMessage}\n`; const placeholder = opts.placeholder && opts.placeholder.length > 0 ? // biome-ignore lint/style/noNonNullAssertion: guarded by placeholder.length > 0 diff --git a/packages/prompts/src/password.ts b/packages/prompts/src/password.ts index df9a18f9..b7a91b4e 100644 --- a/packages/prompts/src/password.ts +++ b/packages/prompts/src/password.ts @@ -1,6 +1,6 @@ import { styleText } from 'node:util'; import type { CANCEL_SYMBOL, Validate } from '@clack/core'; -import { PasswordPrompt, settings } from '@clack/core'; +import { PasswordPrompt, settings, wrapTextWithPrefix } from '@clack/core'; import { type CommonOptions, S_BAR, S_BAR_END, S_PASSWORD_MASK, symbol } from './common.js'; /** @@ -55,7 +55,13 @@ export const password = (opts: PasswordOptions) => { output: opts.output, render() { const hasGuide = opts.withGuide ?? settings.withGuide; - const title = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${symbol(this.state)} ${opts.message}\n`; + const wrappedMessage = wrapTextWithPrefix( + opts.output, + opts.message, + hasGuide ? `${styleText('gray', S_BAR)} ` : '', + `${symbol(this.state)} ` + ); + const title = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${wrappedMessage}\n`; const userInput = this.userInputWithCursor; const masked = this.masked; diff --git a/packages/prompts/src/select-key.ts b/packages/prompts/src/select-key.ts index 979b3d29..af5a10da 100644 --- a/packages/prompts/src/select-key.ts +++ b/packages/prompts/src/select-key.ts @@ -50,7 +50,13 @@ export const selectKey = (opts: SelectKeyOptions) = caseSensitive: opts.caseSensitive, render() { const hasGuide = opts.withGuide ?? settings.withGuide; - const title = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${symbol(this.state)} ${opts.message}\n`; + const wrappedMessage = wrapTextWithPrefix( + opts.output, + opts.message, + hasGuide ? `${styleText('gray', S_BAR)} ` : '', + `${symbol(this.state)} ` + ); + const title = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${wrappedMessage}\n`; switch (this.state) { case 'submit': { diff --git a/packages/prompts/src/text.ts b/packages/prompts/src/text.ts index 270d9592..cf73d09f 100644 --- a/packages/prompts/src/text.ts +++ b/packages/prompts/src/text.ts @@ -1,6 +1,6 @@ import { styleText } from 'node:util'; import type { CANCEL_SYMBOL, Validate } from '@clack/core'; -import { settings, TextPrompt } from '@clack/core'; +import { settings, TextPrompt, wrapTextWithPrefix } from '@clack/core'; import { type CommonOptions, S_BAR, S_BAR_END, symbol } from './common.js'; /** @@ -66,8 +66,13 @@ export const text = (opts: TextOptions) => { input: opts.input, render() { const hasGuide = opts?.withGuide ?? settings.withGuide; - const titlePrefix = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${symbol(this.state)} `; - const title = `${titlePrefix}${opts.message}\n`; + const wrappedMessage = wrapTextWithPrefix( + opts.output, + opts.message, + hasGuide ? `${styleText('gray', S_BAR)} ` : '', + `${symbol(this.state)} ` + ); + const title = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${wrappedMessage}\n`; const placeholder = opts.placeholder && opts.placeholder.length > 0 ? // biome-ignore lint/style/noNonNullAssertion: guarded by placeholder.length > 0 diff --git a/packages/prompts/test/message.test.ts b/packages/prompts/test/message.test.ts new file mode 100644 index 00000000..da8f370c --- /dev/null +++ b/packages/prompts/test/message.test.ts @@ -0,0 +1,56 @@ +import { stripVTControlCharacters } from 'node:util'; +import { describe, expect, test } from 'vitest'; +import type { CommonOptions } from '../src/common.js'; +import { S_BAR, S_STEP_ACTIVE, S_STEP_CANCEL, S_STEP_SUBMIT } from '../src/common.js'; +import * as prompts from '../src/index.js'; +import { MockReadable, MockWritable } from './test-utils.js'; + +type MessageOptions = CommonOptions & { message: string }; + +const promptCases = [ + ['text', prompts.text], + ['password', prompts.password], + ['multiline', prompts.multiline], + [ + 'date', + (opts: MessageOptions) => + prompts.date({ ...opts, initialValue: new Date(Date.UTC(2025, 0, 15)) }), + ], + [ + 'selectKey', + (opts: MessageOptions) => + prompts.selectKey({ ...opts, options: [{ value: 'a' }], initialValue: 'a' }), + ], + [ + 'groupMultiselect', + (opts: MessageOptions) => + prompts.groupMultiselect({ ...opts, options: { group: [{ value: 'a' }] }, required: false }), + ], +] as const; + +describe.each(promptCases)('%s message', (name, prompt) => { + describe.each([true, false])('withGuide = %s', (withGuide) => { + test.each(['return', 'escape'])('preserves newline spacing on %s', async (key) => { + const input = new MockReadable(); + const output = new MockWritable(); + const result = prompt({ message: 'foo\nbar\nbaz', withGuide, input, output }); + const initial = stripVTControlCharacters(output.buffer.join('')); + output.buffer.length = 0; + + input.emit('keypress', '', { name: key }); + if (name === 'multiline' && key === 'return') { + input.emit('keypress', '', { name: 'return' }); + } + const value = await result; + + const continuation = withGuide ? `${S_BAR} ` : ''; + const message = `foo\n${continuation}bar\n${continuation}baz\n`; + const finalSymbol = key === 'return' ? S_STEP_SUBMIT : S_STEP_CANCEL; + expect(initial).toContain(`${S_STEP_ACTIVE} ${message}`); + expect(stripVTControlCharacters(output.buffer.join(''))).toContain( + `${finalSymbol} ${message}` + ); + expect(prompts.isCancel(value)).toBe(key === 'escape'); + }); + }); +});