Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/prompt-message-newline-spacing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/prompts": patch
---

Keep the guide on wrapped message lines in `text`, `password`, `date`, `multiline`, `selectKey`, and `groupMultiselect` prompts.
11 changes: 8 additions & 3 deletions packages/prompts/src/date.ts
Original file line number Diff line number Diff line change
@@ -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 };
Expand Down Expand Up @@ -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';

Expand Down
8 changes: 7 additions & 1 deletion packages/prompts/src/group-multi-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,13 @@ export const groupMultiselect = <Value>(opts: GroupMultiSelectOptions<Value>) =>
},
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 = (
Expand Down
9 changes: 7 additions & 2 deletions packages/prompts/src/multi-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions packages/prompts/src/password.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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;

Expand Down
8 changes: 7 additions & 1 deletion packages/prompts/src/select-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ export const selectKey = <Value extends string>(opts: SelectKeyOptions<Value>) =
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': {
Expand Down
11 changes: 8 additions & 3 deletions packages/prompts/src/text.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions packages/prompts/test/message.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
Loading